Files
sign/apps/remix/app/components/dialogs/token-delete-dialog.tsx

188 lines
5.2 KiB
TypeScript
Raw Normal View History

2023-11-28 12:37:01 +02:00
import { useEffect, useState } from 'react';
import { zodResolver } from '@hookform/resolvers/zod';
2025-01-02 15:33:37 +11:00
import { msg } from '@lingui/core/macro';
2024-08-27 20:34:39 +09:00
import { useLingui } from '@lingui/react';
2025-01-02 15:33:37 +11:00
import { Trans } from '@lingui/react/macro';
import type { ApiToken } from '@prisma/client';
2023-11-28 12:37:01 +02:00
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { trpc } from '@documenso/trpc/react';
import { Button } from '@documenso/ui/primitives/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '@documenso/ui/primitives/dialog';
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@documenso/ui/primitives/form/form';
import { Input } from '@documenso/ui/primitives/input';
import { useToast } from '@documenso/ui/primitives/use-toast';
2025-02-21 00:34:50 +11:00
import { useOptionalCurrentTeam } from '~/providers/team';
2025-01-02 15:33:37 +11:00
export type TokenDeleteDialogProps = {
token: Pick<ApiToken, 'id' | 'name'>;
onDelete?: () => void;
children?: React.ReactNode;
2023-11-28 12:37:01 +02:00
};
2025-02-21 00:34:50 +11:00
export default function TokenDeleteDialog({ token, onDelete, children }: TokenDeleteDialogProps) {
2024-08-27 20:34:39 +09:00
const { _ } = useLingui();
2023-11-28 12:37:01 +02:00
const { toast } = useToast();
2025-02-21 00:34:50 +11:00
const team = useOptionalCurrentTeam();
2023-11-28 12:37:01 +02:00
const [isOpen, setIsOpen] = useState(false);
const deleteMessage = _(msg`delete ${token.name}`);
2023-11-28 12:37:01 +02:00
2025-01-02 15:33:37 +11:00
const ZTokenDeleteDialogSchema = z.object({
2023-11-28 12:37:01 +02:00
tokenName: z.literal(deleteMessage, {
2024-08-27 20:34:39 +09:00
errorMap: () => ({ message: _(msg`You must enter '${deleteMessage}' to proceed`) }),
2023-11-28 12:37:01 +02:00
}),
});
2025-01-02 15:33:37 +11:00
type TDeleteTokenByIdMutationSchema = z.infer<typeof ZTokenDeleteDialogSchema>;
2023-11-28 12:37:01 +02:00
const { mutateAsync: deleteTokenMutation } = trpc.apiToken.deleteTokenById.useMutation({
onSuccess() {
onDelete?.();
},
});
2023-11-28 12:37:01 +02:00
const form = useForm<TDeleteTokenByIdMutationSchema>({
2025-01-02 15:33:37 +11:00
resolver: zodResolver(ZTokenDeleteDialogSchema),
2023-11-28 12:37:01 +02:00
values: {
tokenName: '',
},
});
const onSubmit = async () => {
try {
await deleteTokenMutation({
id: token.id,
2025-02-21 00:34:50 +11:00
teamId: team?.id,
2023-11-28 12:37:01 +02:00
});
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`Token deleted`),
description: _(msg`The token was deleted successfully.`),
2023-11-28 12:37:01 +02:00
duration: 5000,
});
setIsOpen(false);
} catch (error) {
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`An unknown error occurred`),
description: _(
msg`We encountered an unknown error while attempting to delete this token. Please try again later.`,
),
2023-11-28 12:37:01 +02:00
variant: 'destructive',
duration: 5000,
});
}
};
useEffect(() => {
2023-12-19 15:51:43 +02:00
if (!isOpen) {
2023-11-28 12:37:01 +02:00
form.reset();
}
2023-12-19 15:51:43 +02:00
}, [isOpen, form]);
2023-11-28 12:37:01 +02:00
return (
<Dialog
open={isOpen}
onOpenChange={(value) => !form.formState.isSubmitting && setIsOpen(value)}
>
<DialogTrigger asChild={true}>
{children ?? (
2023-11-28 12:37:01 +02:00
<Button className="mr-4" variant="destructive">
2024-08-27 20:34:39 +09:00
<Trans>Delete</Trans>
2023-11-28 12:37:01 +02:00
</Button>
)}
</DialogTrigger>
2023-11-28 12:37:01 +02:00
<DialogContent>
<DialogHeader>
2024-08-27 20:34:39 +09:00
<DialogTitle>
<Trans>Are you sure you want to delete this token?</Trans>
</DialogTitle>
2023-11-28 12:37:01 +02:00
<DialogDescription>
2024-08-27 20:34:39 +09:00
<Trans>
Please note that this action is irreversible. Once confirmed, your token will be
permanently deleted.
</Trans>
2023-11-28 12:37:01 +02:00
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<fieldset
className="flex h-full flex-col space-y-4"
disabled={form.formState.isSubmitting}
>
<FormField
control={form.control}
name="tokenName"
render={({ field }) => (
<FormItem>
<FormLabel>
2024-08-27 20:34:39 +09:00
<Trans>
Confirm by typing:{' '}
<span className="font-sm text-destructive font-semibold">
{deleteMessage}
</span>
</Trans>
2023-11-28 12:37:01 +02:00
</FormLabel>
2023-11-28 12:37:01 +02:00
<FormControl>
<Input className="bg-background" type="text" {...field} />
2023-11-28 12:37:01 +02:00
</FormControl>
<FormMessage />
</FormItem>
)}
/>
2023-11-28 12:37:01 +02:00
<DialogFooter>
<div className="flex w-full flex-nowrap gap-4">
2023-11-28 12:37:01 +02:00
<Button
type="button"
variant="secondary"
className="flex-1"
2023-12-18 12:24:42 +02:00
onClick={() => setIsOpen(false)}
2023-11-28 12:37:01 +02:00
>
2024-08-27 20:34:39 +09:00
<Trans>Cancel</Trans>
2023-11-28 12:37:01 +02:00
</Button>
<Button
type="submit"
variant="destructive"
className="flex-1"
disabled={!form.formState.isValid}
2023-11-28 12:37:01 +02:00
loading={form.formState.isSubmitting}
>
2024-08-27 20:34:39 +09:00
<Trans>I'm sure! Delete it</Trans>
2023-11-28 12:37:01 +02:00
</Button>
</div>
</DialogFooter>
</fieldset>
</form>
</Form>
</DialogContent>
</Dialog>
);
}