|
| 1 | +import { Button, CloseTrigger, Input, Modal, Select, type SelectOption, Text, useToast } from "@repo/ui" |
| 2 | +import { useNavigate } from "@tanstack/react-router" |
| 3 | +import { Plus } from "lucide-react" |
| 4 | +import { useCallback, useMemo, useState } from "react" |
| 5 | +import { useDatasetsCollection } from "../../domains/datasets/datasets.collection.ts" |
| 6 | +import { |
| 7 | + addTracesToDatasetMutation, |
| 8 | + createDatasetFromTracesMutation, |
| 9 | +} from "../../domains/datasets/datasets.functions.ts" |
| 10 | +import { getQueryClient } from "../../lib/data/query-client.tsx" |
| 11 | + |
| 12 | +interface AddToDatasetModalProps { |
| 13 | + open: boolean |
| 14 | + onOpenChange: (open: boolean) => void |
| 15 | + projectId: string |
| 16 | + traceIds: string[] |
| 17 | + onSuccess: () => void |
| 18 | +} |
| 19 | + |
| 20 | +export function AddToDatasetModal({ open, onOpenChange, projectId, traceIds, onSuccess }: AddToDatasetModalProps) { |
| 21 | + const [selectedDatasetId, setSelectedDatasetId] = useState<string | null>(null) |
| 22 | + const [creatingNew, setCreatingNew] = useState(false) |
| 23 | + const [newDatasetName, setNewDatasetName] = useState("") |
| 24 | + const [submitting, setSubmitting] = useState(false) |
| 25 | + const { toast } = useToast() |
| 26 | + const navigate = useNavigate() |
| 27 | + const { data: datasets } = useDatasetsCollection(projectId) |
| 28 | + |
| 29 | + const datasetOptions = useMemo<SelectOption<string>[]>( |
| 30 | + () => (datasets ?? []).map((ds) => ({ label: ds.name, value: ds.id })), |
| 31 | + [datasets], |
| 32 | + ) |
| 33 | + |
| 34 | + const handleSelectChange = useCallback((value: string) => { |
| 35 | + setSelectedDatasetId(value) |
| 36 | + setCreatingNew(false) |
| 37 | + }, []) |
| 38 | + |
| 39 | + const handleCreateNew = useCallback(() => { |
| 40 | + setCreatingNew(true) |
| 41 | + setSelectedDatasetId(null) |
| 42 | + }, []) |
| 43 | + |
| 44 | + const handleSubmit = useCallback(async () => { |
| 45 | + if (traceIds.length === 0) return |
| 46 | + setSubmitting(true) |
| 47 | + try { |
| 48 | + if (creatingNew) { |
| 49 | + if (!newDatasetName.trim()) return |
| 50 | + const result = await createDatasetFromTracesMutation({ |
| 51 | + data: { projectId, name: newDatasetName.trim(), traceIds }, |
| 52 | + }) |
| 53 | + toast({ |
| 54 | + title: "Dataset created", |
| 55 | + description: `"${newDatasetName.trim()}" created with ${result.rowCount} row${result.rowCount === 1 ? "" : "s"}.`, |
| 56 | + }) |
| 57 | + getQueryClient().invalidateQueries({ queryKey: ["datasets", projectId] }) |
| 58 | + onSuccess() |
| 59 | + onOpenChange(false) |
| 60 | + navigate({ |
| 61 | + to: "/projects/$projectId/datasets/$datasetId", |
| 62 | + params: { projectId, datasetId: result.datasetId }, |
| 63 | + }) |
| 64 | + } else { |
| 65 | + if (!selectedDatasetId) return |
| 66 | + const result = await addTracesToDatasetMutation({ |
| 67 | + data: { projectId, datasetId: selectedDatasetId, traceIds }, |
| 68 | + }) |
| 69 | + toast({ |
| 70 | + title: "Traces added to dataset", |
| 71 | + description: `${result.rowCount} row${result.rowCount === 1 ? "" : "s"} added (version ${result.version}).`, |
| 72 | + }) |
| 73 | + getQueryClient().invalidateQueries({ queryKey: ["datasets", projectId] }) |
| 74 | + getQueryClient().invalidateQueries({ queryKey: ["datasetRows", selectedDatasetId] }) |
| 75 | + onSuccess() |
| 76 | + onOpenChange(false) |
| 77 | + } |
| 78 | + } catch (error) { |
| 79 | + toast({ |
| 80 | + variant: "destructive", |
| 81 | + title: "Error", |
| 82 | + description: error instanceof Error ? error.message : "Failed to add traces to dataset.", |
| 83 | + }) |
| 84 | + } finally { |
| 85 | + setSubmitting(false) |
| 86 | + } |
| 87 | + }, [creatingNew, selectedDatasetId, newDatasetName, projectId, traceIds, toast, navigate, onSuccess, onOpenChange]) |
| 88 | + |
| 89 | + const canSubmit = |
| 90 | + traceIds.length > 0 && !submitting && (creatingNew ? newDatasetName.trim().length > 0 : !!selectedDatasetId) |
| 91 | + |
| 92 | + return ( |
| 93 | + <Modal |
| 94 | + open={open} |
| 95 | + onOpenChange={onOpenChange} |
| 96 | + title="Add traces to dataset" |
| 97 | + description={`${traceIds.length} trace${traceIds.length === 1 ? "" : "s"} selected`} |
| 98 | + dismissible |
| 99 | + footer={ |
| 100 | + <div className="flex flex-row items-center gap-2"> |
| 101 | + <CloseTrigger /> |
| 102 | + <Button onClick={handleSubmit} disabled={!canSubmit} isLoading={submitting}> |
| 103 | + {!submitting && <Plus className="h-4 w-4" />} |
| 104 | + <Text.H5 color="white">{creatingNew ? "Create & add" : "Add to dataset"}</Text.H5> |
| 105 | + </Button> |
| 106 | + </div> |
| 107 | + } |
| 108 | + > |
| 109 | + <div className="flex flex-col gap-4"> |
| 110 | + {creatingNew ? ( |
| 111 | + <div className="flex flex-col gap-2"> |
| 112 | + <Input |
| 113 | + label="New dataset name" |
| 114 | + placeholder="My dataset" |
| 115 | + value={newDatasetName} |
| 116 | + onChange={(e) => setNewDatasetName(e.target.value)} |
| 117 | + autoFocus |
| 118 | + /> |
| 119 | + <button type="button" onClick={() => setCreatingNew(false)} className="self-start"> |
| 120 | + <Text.H6 color="primary">Back to existing datasets</Text.H6> |
| 121 | + </button> |
| 122 | + </div> |
| 123 | + ) : ( |
| 124 | + <Select<string> |
| 125 | + name="dataset" |
| 126 | + label="Dataset" |
| 127 | + placeholder="Select a dataset" |
| 128 | + options={datasetOptions} |
| 129 | + value={selectedDatasetId ?? undefined} |
| 130 | + onChange={handleSelectChange} |
| 131 | + searchable |
| 132 | + searchPlaceholder="Search datasets..." |
| 133 | + searchableEmptyMessage="No datasets found." |
| 134 | + side="bottom" |
| 135 | + footerAction={{ |
| 136 | + label: "Create new dataset", |
| 137 | + icon: <Plus className="h-4 w-4" />, |
| 138 | + onClick: handleCreateNew, |
| 139 | + }} |
| 140 | + /> |
| 141 | + )} |
| 142 | + </div> |
| 143 | + </Modal> |
| 144 | + ) |
| 145 | +} |
0 commit comments