1import { useEffect, useRef } from "react";
2
3export function workspaceMutationRefreshToken(
4 resourceKey: string,
5 mutationId: string | null,
6): string | null {
7 return mutationId === null ? null : `${resourceKey}\u0000${mutationId}`;
8}
9
10/**
11 * Refreshes once per mutation and resource. Disabled mutations stay pending,
12 * which lets an editable file catch up after its local save finishes.
13 */
14export function useWorkspaceMutationRefresh(input: {
15 readonly enabled?: boolean;
16 readonly mutationId: string | null;
17 readonly refresh: () => void;
18 readonly resourceKey: string;
19}): void {
20 const { enabled = true, mutationId, refresh, resourceKey } = input;
21 const handledTokenRef = useRef<string | null>(null);
22
23 useEffect(() => {
24 if (!enabled) return;
25 const token = workspaceMutationRefreshToken(resourceKey, mutationId);
26 if (token === null || token === handledTokenRef.current) return;
27 handledTokenRef.current = token;
28 refresh();
29 }, [enabled, mutationId, refresh, resourceKey]);
30}