@@ -8,46 +8,53 @@ import configService from "@/services/configs"
88import ConfirmationDialogAction from "@/components/confirmationDialogAction"
99import { genUID , isEqual } from "@/utils/generalUtils"
1010import { cn } from "@/lib/utils"
11- import { categorizeConfig } from "@/utils/configUtils"
1211import { useMutation , useQuery , useQueryClient } from "@tanstack/react-query"
1312import LoadingOverlay from "@/components/custom/LoadingOverlay"
1413
1514import { ConfigAside } from "@/components/custom/configs/ConfigAside"
1615import { ConfigCenter } from "@/components/custom/configs/ConfigCenter"
16+ import { categorizeConfigArray } from "@/utils/configUtils"
1717
1818export default function ConfigsDashboard ( ) {
1919 const queryClient = useQueryClient ( )
2020 const [ activeView , setActiveView ] = useState < ConfigViewType > ( "system" )
2121 const [ editMode , setEditMode ] = useState ( false )
22+ const [ configMap , setConfigMap ] = useState < any > ( { } )
2223 const [ config , setConfig ] = useState < Array < ConfigItem > > ( [ ] )
2324 const [ shadowConfig , setShadowConfig ] = useState < Array < ConfigItem > > ( [ ] )
2425
25- const convertConfigStructure = useCallback ( ( config : ConfigType ) => {
26- const recParse = ( parentKey : string , input : ConfigType ) : any => {
27- if ( typeof input === "object" ) {
28- if ( "db_mirror" in input ) {
29- return {
30- id : genUID ( ) ,
31- title : input . doc || "" ,
32- key : parentKey ,
33- value : input . value ,
34- encrypted : input . is_encrypted ,
35- base : input . base ,
36- format : input . format ,
37- }
38- } else {
39- return {
40- id : genUID ( ) ,
41- title : input . doc || parentKey || "" ,
42- subGroups : Object . keys ( input ) . map ( e => recParse ( e , input [ e ] ) ) ,
26+ const convertConfigStructure = useCallback (
27+ ( config : ConfigType , transposedConfigMap : any ) => {
28+ const recParse = ( parentKey : string , input : ConfigType ) : any => {
29+ if ( typeof input === "object" ) {
30+ if ( "db_mirror" in input ) {
31+ return {
32+ id : genUID ( ) ,
33+ title : input . doc || "" ,
34+ key : parentKey ,
35+ value : input . value ,
36+ is_encrypted : input . is_encrypted ,
37+ base : input . base ,
38+ format : input . format ,
39+ }
40+ } else {
41+ return {
42+ id : genUID ( ) ,
43+ title : input . doc || parentKey || "" ,
44+ subGroups : Object . keys ( input ) . map ( e => recParse ( e , input [ e ] ) ) ,
45+ }
4346 }
4447 }
48+ return input
4549 }
46- return input
47- }
48- const parsedConfig = recParse ( "" , config )
49- return parsedConfig . subGroups
50- } , [ ] )
50+
51+ const basicConfig = recParse ( "" , config )
52+ return {
53+ configList : basicConfig . subGroups ,
54+ }
55+ } ,
56+ [ ] ,
57+ )
5158
5259 const { data : rawConfig , isLoading } = useQuery ( {
5360 queryKey : [ "configs" ] ,
@@ -56,12 +63,23 @@ export default function ConfigsDashboard() {
5663
5764 React . useEffect ( ( ) => {
5865 if ( rawConfig ) {
59- const convertedConfig = convertConfigStructure ( rawConfig )
60- setConfig ( convertedConfig )
61- setShadowConfig ( JSON . parse ( JSON . stringify ( convertedConfig ) ) )
66+ setConfigMap ( rawConfig . categoriesMap )
67+ const { configList } = convertConfigStructure (
68+ rawConfig . configArray ,
69+ rawConfig . categoriesMap ,
70+ )
71+ setConfig ( configList )
72+ setShadowConfig ( JSON . parse ( JSON . stringify ( configList ) ) )
6273 }
6374 } , [ rawConfig , convertConfigStructure ] )
6475
76+ const categorizedConfigs = useMemo ( ( ) => {
77+ if ( configMap ) {
78+ return categorizeConfigArray ( config , configMap )
79+ }
80+ return [ ]
81+ } , [ config , configMap ] )
82+
6583 const configChanged = useMemo ( ( ) => {
6684 return isEqual ( config , shadowConfig )
6785 } , [ config , shadowConfig ] )
@@ -130,9 +148,9 @@ export default function ConfigsDashboard() {
130148 targetConfig . subGroups ?. find ( e => e . id === itemId ) as ConfigItem
131149 ) . deleted = setValue
132150 } else {
133- targetConfigList = config
134- . find ( e => e . id === parentId )
135- ?. subGroups ?. filter ( e => e . id !== itemId )
151+ targetConfig . subGroups = targetConfig . subGroups ?. filter (
152+ e => e . id !== itemId ,
153+ )
136154 }
137155 }
138156 }
@@ -213,7 +231,7 @@ export default function ConfigsDashboard() {
213231 e =>
214232 e . key in mappedShadowConfig &&
215233 ( mappedShadowConfig [ e . key ] . value !== e . value ||
216- mappedShadowConfig [ e . key ] . encrypted !== e . encrypted ) ,
234+ mappedShadowConfig [ e . key ] . is_encrypted !== e . is_encrypted ) ,
217235 )
218236 const fullDiffs = [ ...newDiffs , ...deletedDiffs , ...updatedDiffs ]
219237 updateConfigMutation . mutate ( fullDiffs )
@@ -239,9 +257,6 @@ export default function ConfigsDashboard() {
239257 }
240258 return config . map ( e => recConvert ( "" , e ) . flat ( ) ) . flat ( )
241259 } , [ ] )
242-
243- const categorizedConfigs = categorizeConfig ( config )
244-
245260 return (
246261 < div className = "w-full h-full relative" >
247262 < div
0 commit comments