mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-17 06:26:58 +01:00
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { trackEvent } from '../lib/analytics';
|
|
import type { CookieJar } from '../lib/models';
|
|
import { invokeCmd } from '../lib/tauri';
|
|
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
|
import { cookieJarsQueryKey } from './useCookieJars';
|
|
import { usePrompt } from './usePrompt';
|
|
|
|
export function useCreateCookieJar() {
|
|
const workspaceId = useActiveWorkspaceId();
|
|
const queryClient = useQueryClient();
|
|
const prompt = usePrompt();
|
|
|
|
return useMutation<CookieJar>({
|
|
mutationKey: ['create_cookie_jar'],
|
|
mutationFn: async () => {
|
|
if (workspaceId === null) {
|
|
throw new Error("Cannot create cookie jar when there's no active workspace");
|
|
}
|
|
const name = await prompt({
|
|
id: 'new-cookie-jar',
|
|
name: 'name',
|
|
title: 'New CookieJar',
|
|
placeholder: 'My Jar',
|
|
label: 'Name',
|
|
defaultValue: 'My Jar',
|
|
});
|
|
return invokeCmd('cmd_create_cookie_jar', { workspaceId, name });
|
|
},
|
|
onSettled: () => trackEvent('cookie_jar', 'create'),
|
|
onSuccess: async (cookieJar) => {
|
|
queryClient.setQueryData<CookieJar[]>(
|
|
cookieJarsQueryKey({ workspaceId: cookieJar.workspaceId }),
|
|
(items) => [...(items ?? []), cookieJar],
|
|
);
|
|
},
|
|
});
|
|
}
|