1+ import { Document } from 'flexsearch' ;
12import produce from 'immer' ;
2- import React , {
3- useCallback ,
4- useEffect ,
5- useMemo ,
6- useReducer ,
7- useState ,
8- } from 'react' ;
3+ import React , { useCallback , useEffect , useReducer , useState } from 'react' ;
94import { useDialogState } from 'reakit' ;
105import { useRecoilState } from 'recoil' ;
116
127import { IOptions } from '../../Document/Obstacles/Option' ;
138import { BZDBSettingsModalOpenEventName } from '../../Events/IBZDBSettingsModalOpenEvent' ;
9+ import bzdbDocumentation , { BZDBDocType } from '../../Utilities/BZDBDocumentor' ;
1410import { documentState } from '../../atoms' ;
1511import { BZDBType } from '../../data/bzdb-types' ;
12+ import { useDocumentSearch } from '../../hooks/useFlexSearch' ;
1613import Button from '../Button' ;
1714import BZDBEquationField from '../Form/BZDBEquationField' ;
1815import CheckboxField from '../Form/CheckboxField' ;
16+ import TextField from '../Form/TextField' ;
1917import ListenerModal from '../ListenerModal' ;
2018import Markdown from '../Markdown' ;
2119import { Tab , TabList } from '../TabList' ;
2220
23- import BZDBDocs from '../../data/bzdb-documention.json' ;
2421import generalStyles from '../../sass/general.module.scss' ;
2522import styles from './BZDBSettingsModal.module.scss' ;
2623
27- type BZDBVariableType = typeof BZDBDocs . variables [ number ] ;
28-
2924interface SettingEditorProps {
3025 onChange : ( setting : string , value : any ) => void ;
31- variable : BZDBVariableType ;
26+ variable : BZDBDocType ;
3227}
3328
3429// https://github.com/BZFlag-Dev/bzflag/blob/a249151/src/common/StateDatabase.cxx#L252-L256
@@ -87,7 +82,7 @@ const SettingEditor = ({ onChange, variable }: SettingEditorProps) => {
8782 < div > { renderEditor ( variable . type ?? 'string' ) } </ div >
8883 </ div >
8984 < div className = { generalStyles . descriptionLike } >
90- < Markdown content = { variable . desc } inline />
85+ < Markdown content = { variable . description } inline />
9186 </ div >
9287 </ div >
9388 ) ;
@@ -123,37 +118,28 @@ function bzdbReducer(state: BZDBStore, action: ReducerAction) {
123118 } ) ;
124119}
125120
121+ const bzdbSearchIndex = new Document ( {
122+ id : 'name' ,
123+ index : [
124+ {
125+ field : 'name' ,
126+ tokenize : 'reverse' ,
127+ } ,
128+ 'description' ,
129+ ] ,
130+ store : [ 'name' , 'description' , 'default' , 'category' ] ,
131+ } ) ;
132+
133+ bzdbDocumentation . forEach ( ( variable ) => {
134+ bzdbSearchIndex . add ( { ...variable } ) ;
135+ } ) ;
136+
126137const BZDBSettingsModal = ( ) => {
127138 const [ world , setBZWDocument ] = useRecoilState ( documentState ) ;
128139 const [ bzdbStore , bzdbStoreDispatch ] = useReducer ( bzdbReducer , { } ) ;
129140 const dialog = useDialogState ( ) ;
130-
131- const {
132- bzdbCategories,
133- bzdbDefinitionsByCategory,
134- bzdbDefinitionsByVariable,
135- } = useMemo ( ( ) => {
136- const groupedByCat : Record < string , BZDBVariableType [ ] > = { } ;
137- const mappedByVariable : Partial < Record < BZDBType , BZDBVariableType > > = { } ;
138-
139- BZDBDocs . variables . forEach ( ( variable ) => {
140- const cat = variable . category ?? 'Miscellaneous' ;
141-
142- if ( ! groupedByCat . hasOwnProperty ( cat ) ) {
143- groupedByCat [ cat ] = [ ] ;
144- }
145-
146- groupedByCat [ cat ] . push ( variable ) ;
147- mappedByVariable [ variable . name as BZDBType ] = variable ;
148- } ) ;
149-
150- return {
151- bzdbDefinitionsByCategory : groupedByCat ,
152- bzdbCategories : Object . keys ( groupedByCat ) . sort ( ) ,
153- bzdbDefinitionsByVariable : mappedByVariable ,
154- bzdbVariables : Object . keys ( mappedByVariable ) . sort ( ) ,
155- } ;
156- } , [ ] ) ;
141+ const [ searchQuery , setSearchQuery ] = useState < string > ( '' ) ;
142+ const results = useDocumentSearch ( searchQuery , bzdbSearchIndex ) ;
157143
158144 const syncStateToWorld = useCallback ( ( ) => {
159145 bzdbStoreDispatch ( {
@@ -163,7 +149,7 @@ const BZDBSettingsModal = () => {
163149 } , [ world ?. _options ] ) ;
164150
165151 const handleOnChange = ( variable : string , value : string ) => {
166- const definition = bzdbDefinitionsByVariable [ variable as BZDBType ] ;
152+ const definition = bzdbDocumentation . store [ variable as BZDBType ] ;
167153
168154 if ( value === definition ?. default ) {
169155 bzdbStoreDispatch ( { type : 'delete' , variable } ) ;
@@ -184,6 +170,8 @@ const BZDBSettingsModal = () => {
184170 dialog . hide ( ) ;
185171 } ;
186172
173+ console . log ( { results } ) ;
174+
187175 return (
188176 < ListenerModal
189177 event = { BZDBSettingsModalOpenEventName }
@@ -200,10 +188,17 @@ const BZDBSettingsModal = () => {
200188 hideOnEsc = { false }
201189 hideOnClickOutside = { false }
202190 >
191+ < div >
192+ < TextField
193+ label = "Search"
194+ onChange = { setSearchQuery }
195+ value = { searchQuery }
196+ />
197+ </ div >
203198 < TabList aria-label = "BZDB Settings" className = { styles . tabList } vertical >
204- { bzdbCategories . map ( ( category ) => (
199+ { bzdbDocumentation . categories . map ( ( category ) => (
205200 < Tab title = { category } key = { category } >
206- { bzdbDefinitionsByCategory [ category ] . map ( ( variable ) => (
201+ { bzdbDocumentation . mapByCategory ( category , ( variable ) => (
207202 < SettingEditor
208203 key = { variable . name }
209204 onChange = { handleOnChange }
0 commit comments