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