mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-02-05 05:00:06 +00:00
implement install-commands button (#10)
* fix: type conversion * implement install-commands button
This commit is contained in:
48
src/hooks/useConfig.tsx
Normal file
48
src/hooks/useConfig.tsx
Normal file
@@ -0,0 +1,48 @@
|
||||
import { createContext, useContext, useEffect, useMemo } from "react"
|
||||
import { useConfigStore } from "./useConfigStore"
|
||||
import { getSettings } from "@/api/settings"
|
||||
import { ConfigContextProps } from "@/types"
|
||||
import { useLocation } from "react-router-dom"
|
||||
|
||||
const ConfigContext = createContext<ConfigContextProps>({});
|
||||
|
||||
interface ConfigProviderProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export const ConfigProvider: React.FC<ConfigProviderProps> = ({ children }) => {
|
||||
const config = useConfigStore(store => store.config);
|
||||
const setConfig = useConfigStore(store => store.setConfig);
|
||||
|
||||
const location = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
if (location.pathname !== "/dashboard/settings")
|
||||
try {
|
||||
const c = await getSettings();
|
||||
const { agent_secret_key, language, listen_port, install_host, site_name, tls } = c;
|
||||
const data = {
|
||||
agent_secret_key,
|
||||
language,
|
||||
listen_port,
|
||||
install_host,
|
||||
site_name,
|
||||
tls,
|
||||
};
|
||||
setConfig(data);
|
||||
} catch (error) {
|
||||
setConfig(undefined);
|
||||
}
|
||||
})();
|
||||
}, [location.pathname])
|
||||
|
||||
const value: ConfigContextProps = useMemo(() => ({
|
||||
config: config,
|
||||
}), [config]);
|
||||
return <ConfigContext.Provider value={value}>{children}</ConfigContext.Provider>;
|
||||
}
|
||||
|
||||
export const useConfig = () => {
|
||||
return useContext(ConfigContext);
|
||||
};
|
||||
16
src/hooks/useConfigStore.ts
Normal file
16
src/hooks/useConfigStore.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { ConfigStore } from '@/types'
|
||||
import { create } from 'zustand'
|
||||
import { persist, createJSONStorage } from 'zustand/middleware'
|
||||
|
||||
export const useConfigStore = create<ConfigStore, [['zustand/persist', ConfigStore]]>(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
config: get()?.config,
|
||||
setConfig: config => set({ config }),
|
||||
}),
|
||||
{
|
||||
name: 'configStore',
|
||||
storage: createJSONStorage(() => localStorage),
|
||||
},
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user