Skip to content

Commit 9f9d428

Browse files
authored
Merge pull request #84 from moda20/Improvement/udpating-the-config-logic-based-on-the-new-categorized-endpoint
2 parents a84c306 + 160a06c commit 9f9d428

4 files changed

Lines changed: 75 additions & 83 deletions

File tree

src/components/custom/configs/configInputCombo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ export default function ConfigInputCombo({
7575
<Checkbox
7676
id={`encrypted-${item.id}`}
7777
disabled={item.base}
78-
checked={item.encrypted}
78+
checked={item.is_encrypted}
7979
onCheckedChange={checked =>
80-
updateConfigItem("encrypted", item.id, checked as boolean)
80+
updateConfigItem("is_encrypted", item.id, checked as boolean)
8181
}
8282
/>
8383
) : (

src/features/system/configs.tsx

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,53 @@ import configService from "@/services/configs"
88
import ConfirmationDialogAction from "@/components/confirmationDialogAction"
99
import { genUID, isEqual } from "@/utils/generalUtils"
1010
import { cn } from "@/lib/utils"
11-
import { categorizeConfig } from "@/utils/configUtils"
1211
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
1312
import LoadingOverlay from "@/components/custom/LoadingOverlay"
1413

1514
import { ConfigAside } from "@/components/custom/configs/ConfigAside"
1615
import { ConfigCenter } from "@/components/custom/configs/ConfigCenter"
16+
import { categorizeConfigArray } from "@/utils/configUtils"
1717

1818
export 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

src/services/configs.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import axios from "@/lib/httpUtils"
22
import type { ConfigItem, ConfigType } from "@/models/configs"
33

44
const configService = {
5-
getAllConfigs(): Promise<ConfigType> {
5+
getAllConfigs(): Promise<{
6+
configArray: ConfigType
7+
categoriesMap: any
8+
}> {
69
return axios.get("system/config/getConfig")
710
},
11+
getCategorizedConfigs(): Promise<ConfigType> {
12+
return axios.get("system/config/getCategorizedConfig")
13+
},
814
updateConfig(newConfig: Array<ConfigItem>) {
915
return axios.post("system/config/updateConfig", newConfig)
1016
},

src/utils/configUtils.ts

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,18 @@
1-
import type {
2-
CategorizedConfigs,
3-
ConfigCategory,
4-
ConfigItem,
5-
} from "@/models/configs"
6-
7-
export function categorizeConfig(configs: ConfigItem[]): CategorizedConfigs {
8-
const categorized: CategorizedConfigs = {
9-
system: [],
10-
logging: [],
11-
notifications: [],
12-
custom: [],
13-
}
14-
15-
configs.forEach(config => {
16-
const category = getConfigCategory(config)
17-
categorized[category].push(config)
18-
})
19-
20-
return categorized
21-
}
22-
23-
function getConfigCategory(config: ConfigItem): ConfigCategory {
24-
// key or title base categorization
25-
const key = config.key || config.title
26-
if (!key) return "custom"
27-
if (
28-
key === "env" ||
29-
key === "appName" ||
30-
key === "swaggerServer" ||
31-
key.startsWith("server") ||
32-
key.startsWith("jobs") ||
33-
key.startsWith("DB") ||
34-
key.startsWith("baseDB")
35-
) {
36-
return "system"
37-
}
38-
if (key.startsWith("files")) {
39-
return "logging"
40-
}
41-
42-
if (key.startsWith("notifications") || key.startsWith("grafana")) {
43-
return "notifications"
44-
}
45-
46-
return "custom"
1+
export const categorizeConfigArray = (
2+
inputConfigArray: any,
3+
transposedConfigMap,
4+
) => {
5+
return inputConfigArray.reduce(
6+
(p: any, c: any) => {
7+
const targetKey = c.key ?? c.title
8+
const categoryName = transposedConfigMap[targetKey] ?? "custom"
9+
if (p[categoryName]) {
10+
p[categoryName].push(c)
11+
} else {
12+
p[categoryName] = [c]
13+
}
14+
return p
15+
},
16+
{} as { [key: string]: any[] },
17+
)
4718
}

0 commit comments

Comments
 (0)