mirror of
https://github.com/chartdb/chartdb.git
synced 2025-11-02 04:53:27 +00:00
* refactor(filters): refactor diagram filters * replace old filters * fix storage * fix * fix * fix * fix * fix * fix * fix * fix * fix
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import { ConfigContext } from './config-context';
|
|
|
|
import { useStorage } from '@/hooks/use-storage';
|
|
import type { ChartDBConfig } from '@/lib/domain/config';
|
|
|
|
export const ConfigProvider: React.FC<React.PropsWithChildren> = ({
|
|
children,
|
|
}) => {
|
|
const { getConfig, updateConfig: updateDataConfig } = useStorage();
|
|
const [config, setConfig] = useState<ChartDBConfig | undefined>();
|
|
|
|
useEffect(() => {
|
|
const loadConfig = async () => {
|
|
const config = await getConfig();
|
|
setConfig(config);
|
|
};
|
|
|
|
loadConfig();
|
|
}, [getConfig]);
|
|
|
|
const updateConfig: ConfigContext['updateConfig'] = async ({
|
|
config,
|
|
updateFn,
|
|
}) => {
|
|
const promise = new Promise<void>((resolve) => {
|
|
setConfig((prevConfig) => {
|
|
let baseConfig: ChartDBConfig = { defaultDiagramId: '' };
|
|
if (prevConfig) {
|
|
baseConfig = prevConfig;
|
|
}
|
|
|
|
const updatedConfig = updateFn
|
|
? updateFn(baseConfig)
|
|
: { ...baseConfig, ...config };
|
|
|
|
updateDataConfig(updatedConfig).then(() => {
|
|
resolve();
|
|
});
|
|
return updatedConfig;
|
|
});
|
|
});
|
|
|
|
return promise;
|
|
};
|
|
|
|
return (
|
|
<ConfigContext.Provider
|
|
value={{
|
|
config,
|
|
updateConfig,
|
|
}}
|
|
>
|
|
{children}
|
|
</ConfigContext.Provider>
|
|
);
|
|
};
|