mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-16 14:06:49 +01:00
40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { InlineCode } from '../components/core/InlineCode';
|
|
import { trackEvent } from '../lib/analytics';
|
|
import type { Environment, Workspace } from '../lib/models';
|
|
import { invokeCmd } from '../lib/tauri';
|
|
import { useConfirm } from './useConfirm';
|
|
import { environmentsQueryKey } from './useEnvironments';
|
|
|
|
export function useDeleteEnvironment(environment: Environment | null) {
|
|
const queryClient = useQueryClient();
|
|
const confirm = useConfirm();
|
|
|
|
return useMutation<Environment | null, string>({
|
|
mutationKey: ['delete_environment', environment?.id],
|
|
mutationFn: async () => {
|
|
const confirmed = await confirm({
|
|
id: 'delete-environment',
|
|
title: 'Delete Environment',
|
|
variant: 'delete',
|
|
description: (
|
|
<>
|
|
Permanently delete <InlineCode>{environment?.name}</InlineCode>?
|
|
</>
|
|
),
|
|
});
|
|
if (!confirmed) return null;
|
|
return invokeCmd('cmd_delete_environment', { environmentId: environment?.id });
|
|
},
|
|
onSettled: () => trackEvent('environment', 'delete'),
|
|
onSuccess: async (environment) => {
|
|
if (environment === null) return;
|
|
|
|
const { id: environmentId, workspaceId } = environment;
|
|
queryClient.setQueryData<Workspace[]>(environmentsQueryKey({ workspaceId }), (environments) =>
|
|
environments?.filter((e) => e.id !== environmentId),
|
|
);
|
|
},
|
|
});
|
|
}
|