diff --git a/src/Components/Modals/BZDBSettingsModal.tsx b/src/Components/Modals/BZDBSettingsModal.tsx index 35ef484..3f85db9 100644 --- a/src/Components/Modals/BZDBSettingsModal.tsx +++ b/src/Components/Modals/BZDBSettingsModal.tsx @@ -1,41 +1,114 @@ -import React, { useCallback, useMemo } from 'react'; +import produce from 'immer'; +import React, { useCallback, useMemo, useState } from 'react'; import { useDialogState } from 'reakit'; import { useRecoilState } from 'recoil'; +import { IOptions } from '../../Document/Obstacles/Option'; import { BZDBSettingsModalOpenEventName } from '../../Events/IBZDBSettingsModalOpenEvent'; import { documentState } from '../../atoms'; import { BZDBType } from '../../data/bzdb-types'; +import Button from '../Button'; +import NumberField from '../Form/NumberField'; import ListenerModal from '../ListenerModal'; +import Markdown from '../Markdown'; import { Tab, TabList } from '../TabList'; import BZDBDocs from '../../data/bzdb-documention.json'; +import generalStyles from '../../sass/general.module.scss'; type BZDBVariableType = typeof BZDBDocs.variables[number]; +interface SettingEditorProps { + onChange: (setting: string, value: any) => void; + variable: BZDBVariableType; +} + +const SettingEditor = ({ variable }: SettingEditorProps) => { + const [value, setValue] = useState(variable.default); + + const renderEditor = (type: string) => { + if (type === 'integer' || type === 'float') { + return ( + + ); + } + + return 'Unsupported'; + }; + + return ( +
+
+
{variable.name}
+
{renderEditor(variable.type ?? 'string')}
+
+
+ +
+
+ ); +}; + const BZDBSettingsModal = () => { const [world, setBZWDocument] = useRecoilState(documentState); + const [bzdbStore, setBZDBStore] = useState({}); const dialog = useDialogState(); - const { bzdbCategories, bzdbDefinitions } = useMemo(() => { - const groupedByCat: Record = {}; + const { bzdbCategories, bzdbDefinitionsByCategory } = useMemo(() => { + const groupedByCat: Record = {}; const mappedByVariable: Partial> = {}; BZDBDocs.variables.forEach((variable) => { - groupedByCat[variable.category ?? 'Miscellaneous'] = variable; + const cat = variable.category ?? 'Miscellaneous'; + + if (!groupedByCat.hasOwnProperty(cat)) { + groupedByCat[cat] = []; + } + + groupedByCat[cat].push(variable); mappedByVariable[variable.name as BZDBType] = variable; }); return { - bzdbDefinitions: groupedByCat, + bzdbDefinitionsByCategory: groupedByCat, bzdbCategories: Object.keys(groupedByCat).sort(), - bzdbVariables: mappedByVariable, + bzdbDefinitionsByVariable: mappedByVariable, + bzdbVariables: Object.keys(mappedByVariable).sort(), }; }, []); const syncStateToWorld = useCallback(() => { const sets = world?._options?.['-set'] ?? {}; + + setBZDBStore(sets); }, [world?._options]); + const handleOnChange = (variable: string, value: string) => { + const newStore: IOptions['-set'] = { ...bzdbStore }; + + newStore[variable] = value; + + setBZDBStore(newStore); + }; + + const handleOnSave = () => { + if (!world) { + return; + } + + const nextWorld = produce(world, (draftWorld) => { + draftWorld._options['-set'] = bzdbStore; + }); + + setBZWDocument(nextWorld); + dialog.hide(); + }; + return ( { {bzdbCategories.map((category) => ( - ... + {bzdbDefinitionsByCategory[category].map((variable) => ( + + ))} ))} +
+ +
); };