mirror of
https://github.com/ajnart/homarr.git
synced 2026-01-26 17:29:18 +01:00
🗑️ Remove most of config context and store
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
import { useEffect } from 'react';
|
||||
import { ConfigType } from '~/types/config';
|
||||
|
||||
import { useConfigContext } from './provider';
|
||||
import { useConfigStore } from './store';
|
||||
|
||||
export const useInitConfig = (initialConfig: ConfigType) => {
|
||||
const { setConfigName, increaseVersion } = useConfigContext();
|
||||
const configName = initialConfig.configProperties?.name ?? 'default';
|
||||
const initConfig = useConfigStore((x) => x.initConfig);
|
||||
const removeConfig = useConfigStore((x) => x.removeConfig);
|
||||
|
||||
useEffect(() => {
|
||||
setConfigName(configName);
|
||||
initConfig(configName, initialConfig, increaseVersion);
|
||||
return () => {
|
||||
removeConfig(configName);
|
||||
};
|
||||
}, [configName]);
|
||||
};
|
||||
@@ -1,10 +1,6 @@
|
||||
import { ReactNode, createContext, useContext, useEffect, useState } from 'react';
|
||||
import { shallow } from 'zustand/shallow';
|
||||
import { useColorTheme } from '~/tools/color';
|
||||
import { createContext, useContext } from 'react';
|
||||
import { ConfigType } from '~/types/config';
|
||||
|
||||
import { useConfigStore } from './store';
|
||||
|
||||
export type ConfigContextType = {
|
||||
config: ConfigType | undefined;
|
||||
name: string | undefined;
|
||||
@@ -21,41 +17,4 @@ const ConfigContext = createContext<ConfigContextType>({
|
||||
setConfigName: () => {},
|
||||
});
|
||||
|
||||
export const ConfigProvider = ({
|
||||
children,
|
||||
config: fallbackConfig,
|
||||
}: {
|
||||
children: ReactNode;
|
||||
config?: ConfigType;
|
||||
}) => {
|
||||
const [configName, setConfigName] = useState<string>(
|
||||
fallbackConfig?.configProperties.name || 'unknown'
|
||||
);
|
||||
const [configVersion, setConfigVersion] = useState(0);
|
||||
const { configs } = useConfigStore((s) => ({ configs: s.configs }), shallow);
|
||||
|
||||
const currentConfig = configs.find((c) => c.value.configProperties.name === configName)?.value;
|
||||
const { setPrimaryColor, setSecondaryColor, setPrimaryShade } = useColorTheme();
|
||||
|
||||
useEffect(() => {
|
||||
setPrimaryColor(currentConfig?.settings.customization.colors.primary || 'red');
|
||||
setSecondaryColor(currentConfig?.settings.customization.colors.secondary || 'orange');
|
||||
setPrimaryShade(currentConfig?.settings.customization.colors.shade || 6);
|
||||
}, [currentConfig]);
|
||||
|
||||
return (
|
||||
<ConfigContext.Provider
|
||||
value={{
|
||||
name: configName,
|
||||
config: currentConfig ?? fallbackConfig,
|
||||
configVersion,
|
||||
increaseVersion: () => setConfigVersion((v) => v + 1),
|
||||
setConfigName: (name: string) => setConfigName(name),
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</ConfigContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useConfigContext = () => useContext(ConfigContext);
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
import { createWithEqualityFn } from 'zustand/traditional';
|
||||
import { ConfigType } from '~/types/config';
|
||||
import { trcpProxyClient } from '~/utils/api';
|
||||
|
||||
export const useConfigStore = createWithEqualityFn<UseConfigStoreType>(
|
||||
(set, get) => ({
|
||||
configs: [],
|
||||
initConfig: (name, config, increaseVersion) => {
|
||||
set((old) => ({
|
||||
...old,
|
||||
configs: [
|
||||
...old.configs.filter((x) => x.value.configProperties?.name !== name),
|
||||
{ increaseVersion, value: config },
|
||||
],
|
||||
}));
|
||||
},
|
||||
addConfig: async (name: string, config: ConfigType) => {
|
||||
set((old) => ({
|
||||
...old,
|
||||
configs: [
|
||||
...old.configs.filter((x) => x.value.configProperties.name !== name),
|
||||
{ value: config, increaseVersion: () => {} },
|
||||
],
|
||||
}));
|
||||
},
|
||||
removeConfig: (name: string) => {
|
||||
set((old) => ({
|
||||
...old,
|
||||
configs: old.configs.filter((x) => x.value.configProperties.name !== name),
|
||||
}));
|
||||
},
|
||||
updateConfig: async (
|
||||
name,
|
||||
updateCallback: (previous: ConfigType) => ConfigType,
|
||||
shouldRegenerateGridstack = false,
|
||||
shouldSaveConfigToFileSystem = false
|
||||
) => {
|
||||
const { configs } = get();
|
||||
const currentConfig = configs.find((x) => x.value.configProperties.name === name);
|
||||
if (!currentConfig) {
|
||||
return;
|
||||
}
|
||||
// copies the value of currentConfig and creates a non reference object named previousConfig
|
||||
const previousConfig: ConfigType = JSON.parse(JSON.stringify(currentConfig.value));
|
||||
|
||||
const updatedConfig = updateCallback(currentConfig.value);
|
||||
|
||||
set((old) => ({
|
||||
...old,
|
||||
configs: [
|
||||
...old.configs.filter((x) => x.value.configProperties.name !== name),
|
||||
{ value: updatedConfig, increaseVersion: currentConfig.increaseVersion },
|
||||
],
|
||||
}));
|
||||
|
||||
if (
|
||||
(typeof shouldRegenerateGridstack === 'boolean' && shouldRegenerateGridstack) ||
|
||||
(typeof shouldRegenerateGridstack === 'function' &&
|
||||
shouldRegenerateGridstack(previousConfig, updatedConfig))
|
||||
) {
|
||||
currentConfig.increaseVersion();
|
||||
}
|
||||
|
||||
if (shouldSaveConfigToFileSystem) {
|
||||
trcpProxyClient.config.save.mutate({
|
||||
name,
|
||||
config: updatedConfig,
|
||||
});
|
||||
}
|
||||
},
|
||||
}),
|
||||
Object.is
|
||||
);
|
||||
|
||||
interface UseConfigStoreType {
|
||||
configs: { increaseVersion: () => void; value: ConfigType }[];
|
||||
initConfig: (name: string, config: ConfigType, increaseVersion: () => void) => void;
|
||||
addConfig: (name: string, config: ConfigType) => Promise<void>;
|
||||
removeConfig: (name: string) => void;
|
||||
updateConfig: (
|
||||
name: string,
|
||||
updateCallback: (previous: ConfigType) => ConfigType,
|
||||
shouldRegenerateGridstack?:
|
||||
| boolean
|
||||
| ((previousConfig: ConfigType, currentConfig: ConfigType) => boolean),
|
||||
shouldSaveConfigToFileSystem?: boolean
|
||||
) => Promise<void>;
|
||||
}
|
||||
@@ -21,7 +21,6 @@ import Script from 'next/script';
|
||||
import { useEffect, useState } from 'react';
|
||||
import 'video.js/dist/video-js.css';
|
||||
import { CommonHead } from '~/components/layout/Meta/CommonHead';
|
||||
import { ConfigProvider } from '~/config/provider';
|
||||
import { env } from '~/env.js';
|
||||
import { ColorSchemeProvider } from '~/hooks/use-colorscheme';
|
||||
import { modals } from '~/modals';
|
||||
@@ -148,12 +147,10 @@ function App(
|
||||
withNormalizeCSS
|
||||
withCSSVariables
|
||||
>
|
||||
<ConfigProvider {...props.pageProps}>
|
||||
<Notifications limit={4} position="bottom-left" />
|
||||
<ModalsProvider modals={modals}>
|
||||
<Component {...pageProps} />
|
||||
</ModalsProvider>
|
||||
</ConfigProvider>
|
||||
<Notifications limit={4} position="bottom-left" />
|
||||
<ModalsProvider modals={modals}>
|
||||
<Component {...pageProps} />
|
||||
</ModalsProvider>
|
||||
</MantineProvider>
|
||||
</ColorTheme.Provider>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user