import React, { useState } from "react"; import { useRouter } from "next/router"; import { api } from "@/src/utils/api"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogBody, } from "@/src/components/ui/dialog"; import { Button } from "@/src/components/ui/button"; import { PlusIcon } from "lucide-react"; import { Table, TableHeader, TableRow, TableHead, TableBody, TableCell, } from "@/src/components/ui/table"; import startCase from "lodash/startCase"; import { getChartTypeDisplayName } from "@/src/features/widgets/chart-library/utils"; import { type DashboardWidgetChartType } from "@langfuse/shared/src/db"; export type WidgetItem = { id: string; name: string; description: string; view: string; chartType: string; createdAt: Date; updatedAt: Date; }; interface SelectWidgetDialogProps { open: boolean; onOpenChange: (open: boolean) => void; projectId: string; onSelectWidget: (widget: WidgetItem) => void; dashboardId: string; } export function SelectWidgetDialog({ open, onOpenChange, projectId, onSelectWidget, dashboardId, }: SelectWidgetDialogProps) { const router = useRouter(); const [selectedWidgetId, setSelectedWidgetId] = useState(null); // Fetch widgets const widgets = api.dashboardWidgets.all.useQuery( { projectId, orderBy: { column: "updatedAt", order: "DESC", }, }, { enabled: Boolean(projectId) && open, }, ); const handleNavigateToNewWidget = () => { router.push(`/project/${projectId}/widgets/new?dashboardId=${dashboardId}`); }; const handleAddWidget = () => { if (selectedWidgetId) { const selectedWidget = widgets.data?.widgets.find( (widget) => widget.id === selectedWidgetId, ); if (selectedWidget) { onSelectWidget(selectedWidget as WidgetItem); onOpenChange(false); } } }; return ( Select widget to add
{widgets.isPending ? (
Loading widgets...
) : widgets.isError ? (
Error: {widgets.error.message}
) : widgets.data?.widgets.length === 0 ? (
No widgets found. Create a new widget to get started.
) : ( Name Description View Type Chart Type {widgets.data?.widgets.map((widget) => ( setSelectedWidgetId(widget.id)} className={`cursor-pointer hover:bg-muted ${ selectedWidgetId === widget.id ? "bg-muted" : "" }`} > {widget.name} {widget.description} {startCase(widget.view.toLowerCase())} {getChartTypeDisplayName( widget.chartType as DashboardWidgetChartType, )} ))}
)}
); }