Skip to content
Source
Sign inSign up

useWorkspaceMutationRefresh.ts

27 lines975 Bytes

1import { useEffect, useRef } from "react";
2​
3function workspaceMutationRefreshToken(
4 resourceKey: string,
5 mutationId: string | null,
6): string | null {
7 return mutationId === null ? null : `${resourceKey}\u0000${mutationId}`;
8}
9​
10export function useWorkspaceMutationRefresh(input: {
11 readonly enabled?: boolean;
12 readonly mutationId: string | null;
13 readonly refresh: () => void;
14 readonly resourceKey: string;
15}): void {
16 const { enabled = true, mutationId, refresh, resourceKey } = input;
17 const handledTokenRef = useRef<string | null>(null);
18​
19 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- refetches the resource once per new workspace mutation token
20 useEffect(() => {
21 if (!enabled) return;
22 const token = workspaceMutationRefreshToken(resourceKey, mutationId);
23 if (token === null || token === handledTokenRef.current) return;
24 handledTokenRef.current = token;
25 refresh();
26 }, [enabled, mutationId, refresh, resourceKey]);