// @flow import React from 'react'; import * as SETTINGS from 'constants/settings'; import { FormField } from 'component/common/form'; type SetDaemonSettingArg = boolean | string | number; type DarkModeTimes = { from: { hour: string, min: string, formattedTime: string }, to: { hour: string, min: string, formattedTime: string }, }; type OptionTimes = { fromTo: string, time: string, }; type Props = { currentTheme: string, themes: Array, automaticDarkModeEnabled: boolean, darkModeTimes: DarkModeTimes, clock24h: boolean, setClientSetting: (string, SetDaemonSettingArg) => void, setDarkTime: (string, {}) => void, }; export default function ThemeSelector(props: Props) { const { currentTheme, themes, automaticDarkModeEnabled, darkModeTimes, clock24h, setClientSetting, setDarkTime, } = props; const startHours = ['18', '19', '20', '21']; const endHours = ['5', '6', '7', '8']; function onThemeChange(event: SyntheticInputEvent<*>) { const { value } = event.target; if (value === 'dark') { onAutomaticDarkModeChange(false); } setClientSetting(SETTINGS.THEME, value); } function onAutomaticDarkModeChange(value: boolean) { setClientSetting(SETTINGS.AUTOMATIC_DARK_MODE_ENABLED, value); } function onChangeTime(event: SyntheticInputEvent<*>, options: OptionTimes) { setDarkTime(event.target.value, options); } function formatHour(time: string, clock24h: boolean) { if (clock24h) { return `${time}:00`; } const now = new Date(0, 0, 0, Number(time)); return now.toLocaleTimeString('en-US', { hour12: true, hour: '2-digit' }); } return ( <> {themes.map((theme) => ( ))} onAutomaticDarkModeChange(!automaticDarkModeEnabled)} checked={automaticDarkModeEnabled} label={__('Automatic dark mode')} /> {automaticDarkModeEnabled && ( onChangeTime(value, { fromTo: 'from', time: 'hour' })} value={darkModeTimes.from.hour} label={__('From --[initial time]--')} > {startHours.map((time) => ( ))} onChangeTime(value, { fromTo: 'to', time: 'hour' })} value={darkModeTimes.to.hour} > {endHours.map((time) => ( ))} )} ); }