import { FC, useContext, useEffect, useReducer, useRef } from 'react'; import { useTranslation } from 'next-i18next'; import { useCreateReducer } from '@/hooks/useCreateReducer'; import { getSettings, saveSettings } from '@/utils/app/settings'; import { Settings } from '@/types/settings'; import HomeContext from '@/pages/api/home/home.context'; interface Props { open: boolean; onClose: () => void; } export const SettingDialog: FC = ({ open, onClose }) => { const { t } = useTranslation('settings'); const settings: Settings = getSettings(); const { state, dispatch } = useCreateReducer({ initialState: settings, }); const { dispatch: homeDispatch } = useContext(HomeContext); const modalRef = useRef(null); useEffect(() => { const handleMouseDown = (e: MouseEvent) => { if (modalRef.current && !modalRef.current.contains(e.target as Node)) { window.addEventListener('mouseup', handleMouseUp); } }; const handleMouseUp = (e: MouseEvent) => { window.removeEventListener('mouseup', handleMouseUp); onClose(); }; window.addEventListener('mousedown', handleMouseDown); return () => { window.removeEventListener('mousedown', handleMouseDown); }; }, [onClose]); const handleSave = () => { homeDispatch({ field: 'lightMode', value: state.theme }); saveSettings(state); }; // Render nothing if the dialog is not open. if (!open) { return <>; } // Render the dialog. return (
); };