2023-06-21 09:28:00 +00:00
|
|
|
// @ts-strict-ignore
|
2023-01-16 09:45:12 +00:00
|
|
|
import useLocalStorage from "@dashboard/hooks/useLocalStorage";
|
2023-06-26 09:36:01 +00:00
|
|
|
import mergeWith from "lodash/mergeWith";
|
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
|
|
|
}
|
2023-06-26 09:36:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This customizer is used to keep state of the columns field
|
|
|
|
* consistent in the list settings. Deep merge is used to update
|
|
|
|
* settigns with defaults when they are missing in the LS, but
|
|
|
|
* we want to avoid updating columns array to default when
|
|
|
|
* they are explicitly set by a user to array which doesn't
|
|
|
|
* contain all default values.
|
|
|
|
*/
|
|
|
|
const mergeCustomizer = (objValue: unknown, srcValue: unknown) => {
|
|
|
|
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
|
|
|
|
return srcValue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2023-06-26 09:36:01 +00:00
|
|
|
return mergeWith(
|
|
|
|
{},
|
|
|
|
defaultListSettings,
|
|
|
|
storedListSettings,
|
|
|
|
mergeCustomizer,
|
|
|
|
);
|
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
|
|
|
};
|
|
|
|
}
|