saleor-dashboard/src/hooks/useListSettings.ts
Michał Droń 66bab6feab
🏛️ Implement new column picker (#3514)
* (wip) basic column picker implementation

* (wip) fix stories error

* (wip) fix custom column cells & sorting

* (wip) Group attributes query values in props

* (wip) Add docs

* (wip) fix story error

* Implement new column picker in order list

* Fix search input padding

* Fix empty settings crash

* Fix empty settings crash

* Portalize popover

* Preselect category when there is only one

* Remove legacy code

* Move product column adapters to proper directory

* Improve file structure

* Rename custom columns to dynamic columns

* Improve strings

* (wip) Implement pagination logic

* Beautify pagination

* Update macaw-ui

* Add unit test for useColumns

* Improve naming

* Code review style fixes

* Update docs

* Remove empty column

* Remove unexpected padding from attribute columns

* Add min height to prevent jumping

* Fix typo in column picker files

* CSS fixes to popover content

* Implement auto-scroll to new column

* Improve docs

* Improve type in useColumns reducer

* Extract dynamic columns from NewColumnPicker as a smaller component

* Disable movement of empty column

* Fix circular loader

* Do not persist expanded state when closing picker

* Fix dynamic columns with long names line break

* Prevent user from disabling all static columns

* Simplify dynamic column reducer into flatMap

* Optimistic UIbasic implementation

* Make available attributes query lazy

* Fix displaying arrow next to sorted column

* Add explainatory comment

* Fix product list stories

* Fix dynamic columns and query handling for empty lists

* Remove jump on first attribute selection

* Remove jump on first attribute selection

* Fix search

* Add debounce to search

* CR - base state change on previous in popover

* Extract search into separate component

* Rename useDynamicColumnSettings to useColumnPickerSettings

* Use difference from lodash

* Improve static column rendering

* Improve order list

* Fix stories error

* Improve search for edge cases

* Extract messages

* Calculate more complex dynamic column adapter parameters in separate functions

* Fix url pointing to CardMenu substitute

* Extract nodes to separate component

* Move files, remove New prefix

* Extract to static columns

* Refactor utility functions

* Render column picker categories only when provided in props

* Replace switch with if statements

* Refactor to hooks

* Remove comments with legacy stacing

* Add spaces for better readibility

* Tighten types

* Add space

* Add extra comments to useColumns optimistic update logic

* Fix paddings

* Add arbitrary min/max height for handling various resolutions

* Add full-width hover & click effect on candidate columns labels

* Handle long attribute names

* Make column picker popover a modal

* Bump macaw-ui

* Fix macaw-ui type mismatch

* Update types

* Add data-test-id

* Fix closing picker with trigger button

* Replace list settings deep merging with shallow merging

* Fix list settings deep merging behaviour for arrays

* Replace conditional data-test-id- with close-search

* Rename targetArray to columnsToCheck

* Rename filterColumns to filterSelectedColumns

* Rename gridAttributes query objects to more descriptive

* Add explainatory comment to customizer function in useListSettings

* Update strictness

* Fix rendering dynamic columns
2023-06-26 11:36:01 +02:00

66 lines
1.9 KiB
TypeScript

// @ts-strict-ignore
import useLocalStorage from "@dashboard/hooks/useLocalStorage";
import mergeWith from "lodash/mergeWith";
import { AppListViewSettings, defaultListSettings } from "./../config";
import { ListSettings, ListViews } from "./../types";
export const listSettingsStorageKey = "listConfig";
export interface UseListSettings<TColumns extends string = string> {
settings: ListSettings<TColumns>;
updateListSettings: <T extends keyof ListSettings<TColumns>>(
key: T,
value: ListSettings<TColumns>[T],
) => void;
}
/**
* 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;
}
};
export default function useListSettings<TColumns extends string = string>(
listName: ListViews,
): UseListSettings<TColumns> {
const [settings, setListSettings] = useLocalStorage<AppListViewSettings>(
listSettingsStorageKey,
storedListSettings => {
if (typeof storedListSettings !== "object") {
return defaultListSettings;
}
return mergeWith(
{},
defaultListSettings,
storedListSettings,
mergeCustomizer,
);
},
);
const updateListSettings = <T extends keyof ListSettings>(
key: T,
value: ListSettings[T],
) =>
setListSettings(settings => ({
...settings,
[listName]: {
...settings[listName],
[key]: value,
},
}));
return {
settings: settings[listName] as ListSettings<TColumns>,
updateListSettings,
};
}