feat: 修改用户名,简化代码

This commit is contained in:
naiba
2024-11-30 15:02:42 +08:00
parent 7d672fa8c5
commit eb044d42bc
15 changed files with 395 additions and 447 deletions

View File

@@ -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);
};

View File

@@ -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
View 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;
}