implement install-commands button (#10)

* fix: type conversion

* implement install-commands button
This commit is contained in:
UUBulb
2024-11-30 13:44:17 +08:00
committed by GitHub
parent 35436e69bb
commit 7d672fa8c5
8 changed files with 149 additions and 12 deletions

48
src/hooks/useConfig.tsx Normal file
View 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);
};

View 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),
},
),
)