import React from 'react' import dynamic from 'next/dynamic' import Dropdown from '../Dropdown' import { managePopout } from '../Popout' import ThemeIcon from '../svg/Theme' import RemoveIcon from '../svg/Remove' import { COLORS, DEFAULT_THEME } from '../../lib/constants' import { getRouteState } from '../../lib/routing' const ThemeCreate = dynamic(() => import('./ThemeCreate'), { loading: () => null }) const ThemeItem = ({ children, item, isSelected, remove }) => (
{children} {item.custom && !isSelected && (
{ e.stopPropagation() remove(item.id) }} >
)}
) const themeIcon = const getCustomName = themes => `Custom Theme ${themes.filter(({ name }) => name.startsWith('Custom Theme')).length + 1}` class Themes extends React.PureComponent { state = { highlights: {}, name: '' } componentDidMount() { const { queryState } = getRouteState(this.props.router) if (queryState.highlights) { this.updateHighlights(queryState.highlights) } } dropdown = React.createRef() static getDerivedStateFromProps(props) { if (!props.isVisible) { const themeConfig = (props.themes && props.themes.find(t => t.id === props.theme)) || DEFAULT_THEME return { highlights: themeConfig.highlights, name: getCustomName(props.themes) } } return null } handleThemeSelected = theme => { const { toggleVisibility, update } = this.props if (theme.id === 'create') { toggleVisibility() this.dropdown.current.closeMenu() } else { update(theme.id) } } updateHighlights = updates => this.setState(({ highlights }) => ({ highlights: { ...highlights, ...updates } })) create = theme => { this.props.toggleVisibility() this.props.create(theme) } itemWrapper = props => render() { const { themes, theme, isVisible, toggleVisibility } = this.props const { highlights } = this.state const themeConfig = themes.find(t => t.id === theme) || DEFAULT_THEME const dropdownValue = isVisible ? { name: this.state.name } : themeConfig const dropdownList = [ { id: 'create', name: 'Create +' }, ...themes ] return (
{isVisible && ( this.setState({ name: e.target.value })} /> )}
) } } export default managePopout(Themes)