mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-02-05 13:10:08 +00:00
feat: 修改用户名,简化代码
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
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);
|
||||
};
|
||||
@@ -1,16 +0,0 @@
|
||||
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),
|
||||
},
|
||||
),
|
||||
)
|
||||
11
src/hooks/useSetting.tsx
Normal file
11
src/hooks/useSetting.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
import { swrFetcher } from "@/api/api";
|
||||
import { ModelConfig } from "@/types";
|
||||
import useSWR from "swr";
|
||||
|
||||
export default function useSetting() {
|
||||
const { data } = useSWR<ModelConfig>(
|
||||
"/api/v1/setting",
|
||||
swrFetcher
|
||||
);
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user