2020-08-13 13:45:18 +03:00
|
|
|
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) {
|
2020-12-29 12:00:41 +03:00
|
|
|
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'
|
2020-08-13 13:45:18 +03:00
|
|
|
return (
|
2020-12-29 12:00:41 +03:00
|
|
|
<a href={domain === this.props.site.domain ? null : `/${encodeURIComponent(domain)}`} key={domain} className={`block truncate px-4 py-2 md:text-sm leading-5 text-gray-700 dark:text-gray-300 ${extraClass}`}>
|
2020-10-13 10:31:32 +03:00
|
|
|
<img src={`https://icons.duckduckgo.com/ip3/${domain}.ico`} referrerPolicy="no-referrer" className="inline w-4 mr-2 align-middle" />
|
2020-08-13 13:45:18 +03:00
|
|
|
<span>{domain}</span>
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderDropdown() {
|
|
|
|
if (this.state.loading) {
|
|
|
|
return <div className="px-4 py-6"><div className="loading sm mx-auto"><div></div></div></div>
|
|
|
|
} else if (this.state.error) {
|
2020-12-16 12:57:28 +03:00
|
|
|
return <div className="mx-auto px-4 py-6 dark:text-gray-100">Something went wrong, try again</div>
|
2020-08-13 13:45:18 +03:00
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<div className="py-1">
|
2020-12-29 12:00:41 +03:00
|
|
|
<a href={`/${encodeURIComponent(this.props.site.domain)}/settings`} className="group flex items-center px-4 py-2 md:text-sm leading-5 text-gray-700 dark:text-gray-300 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" role="menuitem">
|
2020-12-16 12:57:28 +03:00
|
|
|
<svg viewBox="0 0 20 20" fill="currentColor" className="mr-2 h-4 w-4 text-gray-500 dark:text-gray-200 group-hover:text-gray-600 dark:group-hover:text-gray-400 group-focus:text-gray-500 dark:group-focus:text-gray-200"><path d="M5 4a1 1 0 00-2 0v7.268a2 2 0 000 3.464V16a1 1 0 102 0v-1.268a2 2 0 000-3.464V4zM11 4a1 1 0 10-2 0v1.268a2 2 0 000 3.464V16a1 1 0 102 0V8.732a2 2 0 000-3.464V4zM16 3a1 1 0 011 1v7.268a2 2 0 010 3.464V16a1 1 0 11-2 0v-1.268a2 2 0 010-3.464V4a1 1 0 011-1z" /></svg>
|
2020-08-13 13:45:18 +03:00
|
|
|
Site settings
|
|
|
|
</a>
|
|
|
|
</div>
|
2020-12-29 12:00:41 +03:00
|
|
|
<div className="border-t border-gray-200 dark:border-gray-500"></div>
|
2020-08-13 13:45:18 +03:00
|
|
|
<div className="py-1">
|
|
|
|
{ this.state.sites.map(this.renderSiteLink.bind(this)) }
|
|
|
|
</div>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderArrow() {
|
|
|
|
if (this.props.loggedIn) {
|
|
|
|
return (
|
|
|
|
<svg className="-mr-1 ml-2 h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
|
|
|
|
<path fillRule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clipRule="evenodd" />
|
|
|
|
</svg>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-12-16 12:57:28 +03:00
|
|
|
const hoverClass = this.props.loggedIn ? 'hover:text-gray-500 dark:hover:text-gray-200 focus:border-blue-300 focus:ring ' : 'cursor-default'
|
2020-08-13 13:45:18 +03:00
|
|
|
|
|
|
|
return (
|
2020-12-29 12:00:41 +03:00
|
|
|
<div className="relative inline-block text-left z-10 mr-4">
|
2020-12-16 12:57:28 +03:00
|
|
|
<button onClick={this.toggle.bind(this)} className={`inline-flex items-center text-lg w-full rounded-md py-2 leading-5 font-bold text-gray-700 dark:text-gray-300 focus:outline-none transition ease-in-out duration-150 ${hoverClass}`}>
|
2020-08-13 13:45:18 +03:00
|
|
|
|
2020-10-13 10:31:32 +03:00
|
|
|
<img src={`https://icons.duckduckgo.com/ip3/${this.props.site.domain}.ico`} referrerPolicy="no-referrer" className="inline w-4 mr-2 align-middle" />
|
2020-08-13 13:45:18 +03:00
|
|
|
{this.props.site.domain}
|
|
|
|
{this.renderArrow()}
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<Transition
|
|
|
|
show={this.state.open}
|
|
|
|
enter="transition ease-out duration-100 transform"
|
|
|
|
enterFrom="opacity-0 scale-95"
|
|
|
|
enterTo="opacity-100 scale-100"
|
|
|
|
leave="transition ease-in duration-75 transform"
|
|
|
|
leaveFrom="opacity-100 scale-100"
|
|
|
|
leaveTo="opacity-0 scale-95"
|
|
|
|
>
|
|
|
|
<div className="origin-top-left absolute left-0 mt-2 w-64 rounded-md shadow-lg" ref={node => this.dropDownNode = node} >
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="rounded-md bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5">
|
2020-08-13 13:45:18 +03:00
|
|
|
{ this.renderDropdown() }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Transition>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|