🐛 Fix deprecated zustand dependencies

This commit is contained in:
Meier Lukas
2023-08-09 21:41:57 +02:00
parent d9eec612d8
commit db8d88affc
5 changed files with 94 additions and 130 deletions

View File

@@ -1,74 +1,77 @@
import { create } from 'zustand';
import { createWithEqualityFn } from 'zustand/traditional';
import { trcpProxyClient } from '~/utils/api';
import { ConfigType } from '../types/config';
export const useConfigStore = create<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));
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);
const updatedConfig = updateCallback(currentConfig.value);
set((old) => ({
...old,
configs: [
...old.configs.filter((x) => x.value.configProperties.name !== name),
{ value: updatedConfig, increaseVersion: currentConfig.increaseVersion },
],
}));
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 (
(typeof shouldRegenerateGridstack === 'boolean' && shouldRegenerateGridstack) ||
(typeof shouldRegenerateGridstack === 'function' &&
shouldRegenerateGridstack(previousConfig, updatedConfig))
) {
currentConfig.increaseVersion();
}
if (shouldSaveConfigToFileSystem) {
trcpProxyClient.config.save.mutate({
name,
config: updatedConfig,
});
}
},
}));
if (shouldSaveConfigToFileSystem) {
trcpProxyClient.config.save.mutate({
name,
config: updatedConfig,
});
}
},
}),
Object.is
);
interface UseConfigStoreType {
configs: { increaseVersion: () => void; value: ConfigType }[];