langfuse
116 строк · 3.2 Кб
1import { useState } from "react";
2import { useRouter } from "next/router";
3import {
4Popover,
5PopoverContent,
6PopoverTrigger,
7} from "@/src/components/ui/popover";
8import { Button } from "@/src/components/ui/button";
9import { TrashIcon } from "lucide-react";
10import { usePostHog } from "posthog-js/react";
11import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
12import { type Scope } from "@/src/features/rbac/constants/roleAccessRights";
13import { api } from "@/src/utils/api";
14
15interface DeleteButtonProps {
16itemId: string;
17projectId: string;
18isTableAction?: boolean;
19scope: Scope;
20invalidateFunc: () => void;
21type: "trace" | "dataset";
22redirectUrl?: string;
23}
24
25export function DeleteButton({
26itemId,
27projectId,
28isTableAction = false,
29scope,
30invalidateFunc,
31type,
32redirectUrl,
33}: DeleteButtonProps) {
34const [isDeleted, setIsDeleted] = useState(false);
35const router = useRouter();
36const posthog = usePostHog();
37
38const hasAccess = useHasAccess({ projectId, scope: scope });
39const traceMutation = api.traces.deleteMany.useMutation({
40onSuccess: () => {
41setIsDeleted(true);
42!isTableAction && redirectUrl
43? void router.push(redirectUrl)
44: invalidateFunc();
45},
46});
47const datasetMutation = api.datasets.deleteDataset.useMutation({
48onSuccess: () => {
49setIsDeleted(true);
50!isTableAction && redirectUrl
51? void router.push(redirectUrl)
52: invalidateFunc();
53},
54});
55
56if (!hasAccess) {
57return null;
58}
59
60return (
61<Popover key={itemId}>
62<PopoverTrigger asChild>
63<Button
64variant={isTableAction ? "ghost" : "outline"}
65size={isTableAction ? "xs" : "icon"}
66>
67<TrashIcon className="h-4 w-4" />
68</Button>
69</PopoverTrigger>
70<PopoverContent>
71<h2 className="text-md mb-3 font-semibold">Please confirm</h2>
72<p className="mb-3 text-sm">
73This action cannot be undone and removes all the data associated with
74this {type}.
75</p>
76<div className="flex justify-end space-x-4">
77{type === "trace" ? (
78<Button
79type="button"
80variant="destructive"
81loading={traceMutation.isLoading || isDeleted}
82onClick={() => {
83void traceMutation.mutateAsync({
84traceIds: [itemId],
85projectId,
86});
87posthog.capture("trace:delete", {
88source: isTableAction ? "table-single-row" : "trace",
89});
90}}
91>
92Delete trace
93</Button>
94) : (
95<Button
96type="button"
97variant="destructive"
98loading={datasetMutation.isLoading || isDeleted}
99onClick={() => {
100void datasetMutation.mutateAsync({
101projectId,
102datasetId: itemId,
103});
104posthog.capture("dataset:delete", {
105source: isTableAction ? "table-single-row" : "dataset",
106});
107}}
108>
109Delete dataset
110</Button>
111)}
112</div>
113</PopoverContent>
114</Popover>
115);
116}
117