Skip to content

Commit

Permalink
bring file over from other branch
Browse files Browse the repository at this point in the history
  • Loading branch information
rafpaf committed May 17, 2024
1 parent 7460cad commit 6628f19
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion frontend/src/metabase/common/hooks/use-setting/use-setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ export const useUserSetting = <T extends keyof UserSettings>(
key: T,
{
shouldRefresh = false,
shouldMemoize = false,
shouldDebounce = true,
debounceTimeout = 200,
debounceOnLeadingEdge,
}: {
shouldRefresh?: boolean;
/** If true, only the first value retrieved from the API will be used */
shouldMemoize?: boolean;
shouldDebounce?: boolean;
debounceTimeout?: number;
debounceOnLeadingEdge?: boolean;
} = {},
): [UserSettings[T], (value: UserSettings[T]) => void] => {
const currentValue = useSetting(key);
const memoizedValue = useMemo(
() => currentValue,
// eslint-disable-next-line react-hooks/exhaustive-deps -- Update only when currentValue first becomes defined
[currentValue === undefined],
);
const dispatch = useDispatch();
const setter = useCallback(
(value: UserSettings[T]) => {
Expand All @@ -38,5 +46,8 @@ export const useUserSetting = <T extends keyof UserSettings>(
() => _.debounce(setter, debounceTimeout, debounceOnLeadingEdge),
[setter, debounceTimeout, debounceOnLeadingEdge],
);
return [currentValue, shouldDebounce ? debouncedSetter : setter];
return [
shouldMemoize ? memoizedValue : currentValue,
shouldDebounce ? debouncedSetter : setter,
];
};

0 comments on commit 6628f19

Please sign in to comment.