// @flow import * as React from 'react'; import autobind from 'autobind-decorator'; import Button from '../base/button'; import type { Theme as ThemeType } from '../../../plugins'; import { getThemes } from '../../../plugins'; const THEMES_PER_ROW = 5; type Props = { handleChangeTheme: string => void, activeTheme: string, }; type State = { themes: Array, }; @autobind class Theme extends React.PureComponent { constructor(props: Props) { super(props); this.state = { themes: [], }; } componentDidMount() { this._loadThemes(); } async _loadThemes() { const themes = await getThemes(); this.setState({ themes }); } renderTheme(theme: ThemeType) { const { handleChangeTheme, activeTheme } = this.props; const isActive = activeTheme === theme.theme.name; return (

{theme.theme.displayName}

); } renderThemeRows() { const { themes } = this.state; const rows = []; let row = []; for (const theme of themes) { row.push(theme); if (row.length === THEMES_PER_ROW) { rows.push(row); row = []; } } // Push the last row if it wasn't finished if (row.length) { rows.push(row); } return rows.map((row, i) => (
{row.map(this.renderTheme)}
)); } render() { return
{this.renderThemeRows()}
; } } export default Theme;