import { type QueryKey } from '@tanstack/react-query'; export interface UseOptimisticMutationOptions { /** Query whose cached value the mutation changes. */ queryKey: QueryKey; mutationFn: (variables: TVariables) => Promise; /** * Cache value to show while the request is in flight. Runs synchronously on * `mutate`, so the UI repaints on the same frame as the click. Return * `undefined` to leave the cache untouched. */ optimistic: (current: TCache | undefined, variables: TVariables) => TCache | undefined; /** * Cache value once the server confirms. Write/apply endpoints that return * the resulting resource should map it here, so nothing is refetched. * Default: keep the optimistic value. */ reconcile?: (data: TData, variables: TVariables, current: TCache | undefined) => TCache | undefined; /** * Coalesce bursts (drag and drop, sliders): the cache updates on every * call, and only the last variables are sent once `debounceMs` pass * without a new call. Pending work is flushed on unmount. */ debounceMs?: number; /** * A call equal to the one already in flight is ignored (double clicks). * Default: structural equality via JSON. */ isEqual?: (a: TVariables, b: TVariables) => boolean; /** Other queries to refresh in the background after a confirmed write. */ invalidate?: QueryKey[]; onSuccess?: (data: TData, variables: TVariables) => void; /** * Runs after the cache was rolled back to the last confirmed value. Only * the latest call reports: a superseded write that fails stays silent. */ onError?: (error: unknown, variables: TVariables) => void; } export interface UseOptimisticMutationResult { /** Apply optimistically and persist. Safe to call repeatedly. */ mutate: (variables: TVariables) => void; /** True from the first `mutate` until the last one settles. */ isPending: boolean; /** Variables of the latest unconfirmed call (e.g. which card is applying). */ pendingVariables: TVariables | undefined; error: unknown; /** Re-send the variables of the last call that failed (toast "Reintentar"). */ retry: () => void; } /** * Optimistic write over a TanStack Query cache entry, with rollback. * * ```tsx * const apply = useOptimisticMutation({ * queryKey: ['org-sidebar-layout'], * mutationFn: (key: string) => api.post('/apply', { key }).then((r) => r.data), * optimistic: (current, key) => ({ ...current, template_key: key }), * reconcile: (server) => server, * onError: (_e, key) => toast.error('No se aplicó', { * action: { label: 'Reintentar', onClick: () => apply.mutate(key) }, * }), * }) * apply.mutate(tpl.key)} /> * ``` * * - The cache is patched before the request, so the screen answers at once. * - Writes to the same `queryKey` run one at a time, in call order (mutation * `scope`), so the last click is the last write the server sees. * - Only the latest call decides what the cache shows: an older response * never overwrites a newer optimistic value. If the latest call fails, the * cache returns to the last value the server confirmed. */ export declare function useOptimisticMutation(options: UseOptimisticMutationOptions): UseOptimisticMutationResult; //# sourceMappingURL=use-optimistic-mutation.d.ts.map