gitscope
https://dev.kronoz.win/r/marzneshin.git
app/dashboard/src/stores/core-settings.store.ts · v0.2.1
Source
1 bf7c131 Mardin 2y ago import { fetch } from 'service/http';
2 bf7c131 Mardin 2y ago import { create } from 'zustand';
3 512d183 Mrdn 2y ago import { NodeType } from './nodes.store';
4 a47eb4c Dawsh 2y ago
5 a47eb4c Dawsh 2y ago type CoreSettingsStore = {
6 a47eb4c Dawsh 2y ago isLoading: boolean;
7 a47eb4c Dawsh 2y ago isPostLoading: boolean;
8 a47eb4c Dawsh 2y ago fetchCoreSettings: () => void;
9 a47eb4c Dawsh 2y ago updateConfig: (json: string) => Promise<void>;
10 a47eb4c Dawsh 2y ago restartCore: () => Promise<void>;
11 a47eb4c Dawsh 2y ago config: string;
12 512d183 Mrdn 2y ago selectedNode: NodeType | null;
13 512d183 Mrdn 2y ago isEditingCore: boolean;
14 a47eb4c Dawsh 2y ago };
15 a47eb4c Dawsh 2y ago
16 512d183 Mrdn 2y ago export const useCoreSettings = create<CoreSettingsStore>((set, get) => ({
17 a47eb4c Dawsh 2y ago isLoading: true,
18 a47eb4c Dawsh 2y ago isPostLoading: false,
19 512d183 Mrdn 2y ago isEditingCore: false,
20 bf7c131 Mardin 2y ago config: '',
21 512d183 Mrdn 2y ago selectedNode: null,
22 a47eb4c Dawsh 2y ago fetchCoreSettings: () => {
23 a47eb4c Dawsh 2y ago set({ isLoading: true });
24 a47eb4c Dawsh 2y ago Promise.all([
25 512d183 Mrdn 2y ago fetch(`/nodes/${get().selectedNode?.id}/xray_config`).then((config) => set({ config })),
26 a47eb4c Dawsh 2y ago ]).finally(() => set({ isLoading: false }));
27 a47eb4c Dawsh 2y ago },
28 512d183 Mrdn 2y ago updateConfig: async (body) => {
29 a47eb4c Dawsh 2y ago set({ isPostLoading: true });
30 512d183 Mrdn 2y ago return fetch(`/nodes/${get().selectedNode?.id}/xray_config`, { method: 'PUT', body }).finally(() => {
31 a47eb4c Dawsh 2y ago set({ isPostLoading: false });
32 a47eb4c Dawsh 2y ago });
33 a47eb4c Dawsh 2y ago },
34 a47eb4c Dawsh 2y ago restartCore: () => {
35 bf7c131 Mardin 2y ago return fetch('/core/restart', { method: 'POST' });
36 a47eb4c Dawsh 2y ago },
37 a47eb4c Dawsh 2y ago }));