import { type UseMutationOptions, useMutation } from "@tanstack/react-query"; import type { RpcSchema } from "@yaakapp-internal/proxy-lib"; import { minPromiseMillis } from "@yaakapp-internal/ui"; import type { Req, Res } from "../lib/rpc"; import { rpc } from "../lib/rpc"; import { announceEngineChange } from "../lib/windowSync"; /** * React Query mutation wrapper for RPC commands. The app's other windows hear of each one once * it settles, and read again what it may have changed. */ export function useRpcMutation( cmd: K, opts?: Omit, Error, Req>, "mutationFn">, ) { return useMutation, Error, Req>({ mutationFn: (payload) => minPromiseMillis(rpc(cmd, payload)).finally(announceEngineChange), ...opts, }); }