import React, { useMemo } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useDispatch } from 'react-redux'; import { changeSetting } from 'soapbox/actions/settings'; import { Icon } from 'soapbox/components/ui'; import { useSettings } from 'soapbox/hooks'; const messages = defineMessages({ switchToLight: { id: 'tabs_bar.theme_toggle_light', defaultMessage: 'Switch to light theme' }, switchToDark: { id: 'tabs_bar.theme_toggle_dark', defaultMessage: 'Switch to dark theme' }, }); interface IThemeToggle { showLabel?: boolean, } const ThemeToggle = ({ showLabel }: IThemeToggle) => { const intl = useIntl(); const dispatch = useDispatch(); const themeMode = useSettings().get('themeMode'); const label = intl.formatMessage(themeMode === 'light' ? messages.switchToDark : messages.switchToLight); const onToggle = (event: React.ChangeEvent) => { dispatch(changeSetting(['themeMode'], event.target.value)); }; const themeIconSrc = useMemo(() => { switch (themeMode) { case 'system': return require('@tabler/icons/icons/device-desktop.svg'); case 'light': return require('@tabler/icons/icons/sun.svg'); case 'dark': return require('@tabler/icons/icons/moon.svg'); default: return null; } }, [themeMode]); return ( ); }; export default ThemeToggle;