From 0bd70e7695696cefa4b23267363d86b850f76ad1 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Tue, 24 Sep 2024 07:20:27 -0500 Subject: [PATCH] global mutate hook --- web/src/hooks/use-global-mutate.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 web/src/hooks/use-global-mutate.ts diff --git a/web/src/hooks/use-global-mutate.ts b/web/src/hooks/use-global-mutate.ts new file mode 100644 index 000000000..b95d31f50 --- /dev/null +++ b/web/src/hooks/use-global-mutate.ts @@ -0,0 +1,16 @@ +// https://github.com/vercel/swr/issues/1670#issuecomment-1844114401 +import { useCallback } from "react"; +import { cache, mutate } from "swr/_internal"; + +const useGlobalMutation = () => { + return useCallback((swrKey: string | ((key: string) => boolean), ...args) => { + if (typeof swrKey === "function") { + const keys = Array.from(cache.keys()).filter(swrKey); + keys.forEach((key) => mutate(key, ...args)); + } else { + mutate(swrKey, ...args); + } + }, []) as typeof mutate; +}; + +export default useGlobalMutation;