2023-01-16 09:45:12 +00:00
|
|
|
import useLocalStorage from "@dashboard/hooks/useLocalStorage";
|
2021-09-17 09:59:28 +00:00
|
|
|
import merge from "lodash/merge";
|
2020-05-14 09:30:32 +00:00
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
import { AppListViewSettings, defaultListSettings } from "./../config";
|
|
|
|
import { ListSettings, ListViews } from "./../types";
|
|
|
|
|
2021-09-17 09:59:28 +00:00
|
|
|
export const listSettingsStorageKey = "listConfig";
|
2019-08-09 10:17:04 +00:00
|
|
|
export interface UseListSettings<TColumns extends string = string> {
|
|
|
|
settings: ListSettings<TColumns>;
|
2021-06-15 15:15:14 +00:00
|
|
|
updateListSettings: <T extends keyof ListSettings<TColumns>>(
|
|
|
|
key: T,
|
2022-06-21 09:36:55 +00:00
|
|
|
value: ListSettings<TColumns>[T],
|
2021-06-15 15:15:14 +00:00
|
|
|
) => void;
|
2019-08-09 10:17:04 +00:00
|
|
|
}
|
|
|
|
export default function useListSettings<TColumns extends string = string>(
|
2022-06-21 09:36:55 +00:00
|
|
|
listName: ListViews,
|
2019-08-09 10:17:04 +00:00
|
|
|
): UseListSettings<TColumns> {
|
|
|
|
const [settings, setListSettings] = useLocalStorage<AppListViewSettings>(
|
2021-09-17 09:59:28 +00:00
|
|
|
listSettingsStorageKey,
|
|
|
|
storedListSettings => {
|
|
|
|
if (typeof storedListSettings !== "object") {
|
|
|
|
return defaultListSettings;
|
|
|
|
}
|
2019-08-09 10:17:04 +00:00
|
|
|
|
2021-09-17 09:59:28 +00:00
|
|
|
return merge({}, defaultListSettings, storedListSettings);
|
2022-06-21 09:36:55 +00:00
|
|
|
},
|
2021-09-17 09:59:28 +00:00
|
|
|
);
|
2020-01-30 11:46:35 +00:00
|
|
|
|
2021-06-15 15:15:14 +00:00
|
|
|
const updateListSettings = <T extends keyof ListSettings>(
|
|
|
|
key: T,
|
2022-06-21 09:36:55 +00:00
|
|
|
value: ListSettings[T],
|
2021-06-15 15:15:14 +00:00
|
|
|
) =>
|
2019-08-09 10:17:04 +00:00
|
|
|
setListSettings(settings => ({
|
|
|
|
...settings,
|
|
|
|
[listName]: {
|
|
|
|
...settings[listName],
|
2022-06-21 09:36:55 +00:00
|
|
|
[key]: value,
|
|
|
|
},
|
2019-08-09 10:17:04 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
return {
|
|
|
|
settings: settings[listName] as ListSettings<TColumns>,
|
2022-06-21 09:36:55 +00:00
|
|
|
updateListSettings,
|
2019-08-09 10:17:04 +00:00
|
|
|
};
|
|
|
|
}
|