mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-02 11:29:02 -05:00
31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import type { Workspace } from '../lib/models';
|
|
import { getWorkspace } from '../lib/store';
|
|
import { invokeCmd } from '../lib/tauri';
|
|
import { workspacesQueryKey } from './useWorkspaces';
|
|
|
|
export function useUpdateWorkspace(id: string | null) {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<void, unknown, Partial<Workspace> | ((w: Workspace) => Workspace)>({
|
|
mutationKey: ['update_workspace', id],
|
|
mutationFn: async (v) => {
|
|
const workspace = await getWorkspace(id);
|
|
if (workspace == null) {
|
|
throw new Error("Can't update a null workspace");
|
|
}
|
|
|
|
const newWorkspace = typeof v === 'function' ? v(workspace) : { ...workspace, ...v };
|
|
await invokeCmd('cmd_update_workspace', { workspace: newWorkspace });
|
|
},
|
|
onMutate: async (v) => {
|
|
const workspace = await getWorkspace(id);
|
|
if (workspace === null) return;
|
|
|
|
const newWorkspace = typeof v === 'function' ? v(workspace) : { ...workspace, ...v };
|
|
queryClient.setQueryData<Workspace[]>(workspacesQueryKey(workspace), (workspaces) =>
|
|
(workspaces ?? []).map((w) => (w.id === newWorkspace.id ? newWorkspace : w)),
|
|
);
|
|
},
|
|
});
|
|
}
|