import React from 'react'; import Transition from "../transition.js"; export default class SiteSwitcher extends React.Component { constructor() { super() this.handleClick = this.handleClick.bind(this) this.state = { open: false, sites: null, error: null, loading: true } } componentDidMount() { document.addEventListener('mousedown', this.handleClick, false); } componentWillUnmount() { document.removeEventListener('mousedown', this.handleClick, false); } handleClick(e) { if (this.dropDownNode && this.dropDownNode.contains(e.target)) return; if (!this.state.open) return; this.setState({open: false}) } toggle() { if (!this.props.loggedIn) return; this.setState({open: !this.state.open}) if (!this.state.sites) { fetch('/api/sites') .then( response => { if (!response.ok) { throw response } return response.json() }) .then((sites) => this.setState({loading: false, sites: sites})) .catch((e) => this.setState({loading: false, error: e})) } } renderSiteLink(domain) { const extraClass = domain === this.props.site.domain ? 'font-medium text-gray-900 dark:text-gray-100 cursor-default font-bold' : 'hover:bg-gray-100 dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100 focus:outline-none focus:bg-gray-100 dark:focus:bg-gray-900 focus:text-gray-900 dark:focus:text-gray-100' return ( {domain} ) } renderDropdown() { if (this.state.loading) { return
} else if (this.state.error) { return
Something went wrong, try again
} else { return (
Site settings
{ this.state.sites.map(this.renderSiteLink.bind(this)) }
) } } renderArrow() { if (this.props.loggedIn) { return ( ) } } render() { const hoverClass = this.props.loggedIn ? 'hover:text-gray-500 dark:hover:text-gray-200 focus:border-blue-300 focus:ring ' : 'cursor-default' return (
this.dropDownNode = node} >
{ this.renderDropdown() }
) } }