diff --git a/dashboard/src/features/nodes/dialogs/index.ts b/dashboard/src/features/nodes/dialogs/index.ts index ad9aa79..a1f7b43 100644 --- a/dashboard/src/features/nodes/dialogs/index.ts +++ b/dashboard/src/features/nodes/dialogs/index.ts @@ -1,3 +1,3 @@ export * from './mutation.dialog' export * from './delete-confirmation.dialog' -export * from './settings.dialog' +export * from './settings' diff --git a/dashboard/src/features/nodes/dialogs/settings.dialog.tsx b/dashboard/src/features/nodes/dialogs/settings.dialog.tsx deleted file mode 100644 index c08f9cd..0000000 --- a/dashboard/src/features/nodes/dialogs/settings.dialog.tsx +++ /dev/null @@ -1,97 +0,0 @@ -import { - Button, - Card, - CardContent, - ScrollArea, - Sheet, - SheetContent, - SheetHeader, - SheetTitle, - Tabs, - TabsContent, - TabsList, - TabsTrigger, -} from "@marzneshin/components" -import { FC, useState } from "react" -import { useTranslation } from "react-i18next" -import { NodeType, useNodesSettingsMutation } from ".." -import { useNodesSettingsQuery } from "../services/settings.query"; -import { LogContainer } from "../log"; -import { NodesDetailTable } from "../tables/detail-table"; -import Editor from '@monaco-editor/react'; -import { useTheme } from "@marzneshin/features/theme-switch"; - -interface NodesSettingsDialogProps { - onOpenChange: (state: boolean) => void - open: boolean - entity: NodeType | null -} - -export const NodesSettingsDialog: FC = ({ onOpenChange, open, entity }) => { - - const { t } = useTranslation(); - const { data } = useNodesSettingsQuery(entity); - const [config, setConfig] = useState(data) - const mutate = useNodesSettingsMutation(); - const { theme } = useTheme() - - const handleConfigChange = (newConfig: string | undefined) => { - if (newConfig) { - try { - const parsedConfig = JSON.parse(newConfig); - setConfig(parsedConfig); - } catch (error) { - throw null; - } - } - }; - - if (entity) { - return ( - - - - - - {t('page.nodes.settings.title')} - - -
-

{t('page.nodes.settings.detail')}

- -
- - - {t('logs')} - {t('config')} - - - - - - - - - - - - - -
-
-
- ) - } -} diff --git a/dashboard/src/features/nodes/dialogs/settings/index.tsx b/dashboard/src/features/nodes/dialogs/settings/index.tsx new file mode 100644 index 0000000..74da188 --- /dev/null +++ b/dashboard/src/features/nodes/dialogs/settings/index.tsx @@ -0,0 +1,63 @@ +import { + Card, + CardContent, + ScrollArea, + Sheet, + SheetContent, + SheetHeader, + SheetTitle, + Tabs, + TabsContent, + TabsList, + TabsTrigger, +} from "@marzneshin/components" +import { FC } from "react" +import { useTranslation } from "react-i18next" +import { NodeType } from "../.." +import { LogContainer } from "../../log"; +import { NodesDetailTable } from "../../tables/detail-table"; +import { NodeConfigEditor } from "./node-config-editor"; + +interface NodesSettingsDialogProps { + onOpenChange: (state: boolean) => void + open: boolean + entity: NodeType +} + +export const NodesSettingsDialog: FC = ({ onOpenChange, open, entity }) => { + const { t } = useTranslation(); + + return ( + + + + + + {t('page.nodes.settings.title')} + + +
+

{t('page.nodes.settings.detail')}

+ +
+ + + {t('logs')} + {t('config')} + + + + + + + + + + + + +
+
+
+ ) +} diff --git a/dashboard/src/features/nodes/dialogs/settings/node-config-editor.tsx b/dashboard/src/features/nodes/dialogs/settings/node-config-editor.tsx new file mode 100644 index 0000000..29e743f --- /dev/null +++ b/dashboard/src/features/nodes/dialogs/settings/node-config-editor.tsx @@ -0,0 +1,41 @@ + +import Editor from '@monaco-editor/react'; +import { useTheme } from "@marzneshin/features/theme-switch"; +import { useNodesSettings } from "./use-nodes-settings"; +import { Button } from '@marzneshin/components'; +import { useTranslation } from 'react-i18next'; +import { NodeType } from '../..'; + +export const NodeConfigEditor = ({ entity }: { entity: NodeType }) => { + const { t } = useTranslation() + const { theme } = useTheme() + const { + payloadValidity, + data, + handleConfigSave, + handleConfigChange, + handleEditorValidation + } = useNodesSettings(entity) + + return (<> + + + + ) +} diff --git a/dashboard/src/features/nodes/dialogs/settings/use-nodes-settings.ts b/dashboard/src/features/nodes/dialogs/settings/use-nodes-settings.ts new file mode 100644 index 0000000..96d5994 --- /dev/null +++ b/dashboard/src/features/nodes/dialogs/settings/use-nodes-settings.ts @@ -0,0 +1,30 @@ +import { useState } from "react"; +import { NodeType, useNodesSettingsMutation, useNodesSettingsQuery } from "../.."; + +export const useNodesSettings = (entity: NodeType) => { + const { data } = useNodesSettingsQuery(entity); + const [config, setConfig] = useState(data) + const [payloadValidity, setPayloadValidity] = useState(true) + const mutate = useNodesSettingsMutation(); + + const handleEditorValidation = (markers: any[]) => { + setPayloadValidity(!markers.length) + } + + const handleConfigSave = () => { + mutate.mutate({ node: entity, config }) + } + + const handleConfigChange = (newConfig: string | undefined) => { + if (newConfig) { + try { + const parsedConfig = JSON.parse(newConfig); + setConfig(parsedConfig); + } catch (error) { + setPayloadValidity(false) + } + } + }; + + return { payloadValidity, data, handleConfigSave, handleConfigChange, handleEditorValidation } +}