mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-02 19:39:08 -05:00
25 lines
873 B
TypeScript
25 lines
873 B
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { invoke } from '@tauri-apps/api';
|
|
import type { Workspace } from '../lib/models';
|
|
import { useAppRoutes } from './useAppRoutes';
|
|
import { workspacesQueryKey } from './useWorkspaces';
|
|
|
|
export function useCreateWorkspace({ navigateAfter }: { navigateAfter: boolean }) {
|
|
const routes = useAppRoutes();
|
|
const queryClient = useQueryClient();
|
|
return useMutation<Workspace, unknown, Pick<Workspace, 'name'>>({
|
|
mutationFn: (patch) => {
|
|
return invoke('create_workspace', patch);
|
|
},
|
|
onSuccess: async (workspace) => {
|
|
queryClient.setQueryData<Workspace[]>(workspacesQueryKey({}), (workspaces) => [
|
|
...(workspaces ?? []),
|
|
workspace,
|
|
]);
|
|
if (navigateAfter) {
|
|
routes.navigate('workspace', { workspaceId: workspace.id });
|
|
}
|
|
},
|
|
});
|
|
}
|