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;