import React, { useCallback } from 'react'; import { connect } from 'react-redux'; import { FormFieldContainer, Checkbox, Label, Description, css, spacing, RadioBoxGroup, RadioBox, palette, } from '@mongodb-js/compass-components'; import type { RootState } from '../../stores'; import { changeFieldValue } from '../../stores/settings'; import type { PreferenceStateInformation, THEMES, } from 'compass-preferences-model'; import { settingStateLabels } from './state-labels'; type ThemeSettingsProps = { onChange: (field: 'theme', value: THEMES) => void; preferenceStates: PreferenceStateInformation; themeValue: THEMES; }; const checkboxStyles = css({ marginTop: spacing[3], marginBottom: spacing[3], }); const radioBoxStyles = css({ div: { textAlign: 'left', padding: spacing[3], justifyContent: 'flex-start', }, }); const themePreviewStyles = css({ marginRight: spacing[2], maxWidth: '50%', }); const ThemeIcon: React.FunctionComponent<{ theme: 'DARK' | 'LIGHT' }> = ({ theme, }) => { const lightTheme = { fg: palette.gray.base, bg: palette.white, }; const darkTheme = { fg: palette.black, bg: palette.gray.dark1, }; const { fg, bg } = theme === 'DARK' ? darkTheme : lightTheme; return ( ); }; export const ThemeSettings: React.FunctionComponent = ({ themeValue, preferenceStates, onChange, }) => { const handleOSCheckboxChange = useCallback( (event: React.ChangeEvent) => { onChange('theme', event.target.checked ? 'OS_THEME' : 'LIGHT'); }, [onChange] ); const handleSelectorChange = useCallback( (event: React.ChangeEvent) => { onChange('theme', event.target.value as THEMES); }, [onChange] ); return (
Change the appearance of Compass.
Automatically switch between light and dark themes based on your OS settings } onChange={handleOSCheckboxChange} checked={themeValue === 'OS_THEME'} disabled={!!preferenceStates.theme} /> {settingStateLabels[preferenceStates.theme ?? '']} Light Theme Dark Theme
); }; const mapState = ({ settings: { settings, preferenceStates } }: RootState) => ({ themeValue: settings.theme ?? 'OS_THEME', preferenceStates, }); const mapDispatch = { onChange: changeFieldValue, }; export default connect(mapState, mapDispatch)(ThemeSettings);