Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(dashboard): add new widget to dashboard on save (#6854)

Steffen Schmitz5b62df91226a
diff --git a/web/src/features/dashboard/components/SelectDashboardDialog.tsx b/web/src/features/dashboard/components/SelectDashboardDialog.tsx
new file mode 100644
index 0000000000..63bd768677
--- /dev/null
+++ b/web/src/features/dashboard/components/SelectDashboardDialog.tsx
@@ -0,0 +1,125 @@
+import React, { useState } from "react";
+import { api } from "@/src/utils/api";
+import {
+ Dialog,
+ DialogContent,
+ DialogHeader,
+ DialogTitle,
+ DialogFooter,
+} from "@/src/components/ui/dialog";
+import { Button } from "@/src/components/ui/button";
+import {
+ Table,
+ TableHeader,
+ TableRow,
+ TableHead,
+ TableBody,
+ TableCell,
+} from "@/src/components/ui/table";
+
+export interface SelectDashboardDialogProps {
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+ projectId: string;
+ onSelectDashboard: (dashboardId: string) => void;
+ onSkip: () => void;
+}
+
+export function SelectDashboardDialog({
+ open,
+ onOpenChange,
+ projectId,
+ onSelectDashboard,
+ onSkip,
+}: SelectDashboardDialogProps) {
+ const [selectedDashboardId, setSelectedDashboardId] = useState<string | null>(
+ null,
+ );
+
+ const dashboards = api.dashboard.allDashboards.useQuery(
+ {
+ projectId,
+ orderBy: {
+ column: "updatedAt",
+ order: "DESC",
+ },
+ },
+ {
+ enabled: Boolean(projectId) && open,
+ },
+ );
+
+ const handleAdd = () => {
+ if (selectedDashboardId) {
+ onSelectDashboard(selectedDashboardId);
+ onOpenChange(false);
+ }
+ };
+
+ const handleSkip = () => {
+ onSkip();
+ onOpenChange(false);
+ };
+
+ return (
+ <Dialog open={open} onOpenChange={onOpenChange}>
+ <DialogContent className="sm:max-w-[800px]">
+ <DialogHeader>
+ <DialogTitle>Select dashboard to add widget to</DialogTitle>
+ </DialogHeader>
+
+ <div className="mt-4 max-h-[400px] overflow-y-auto">
+ {dashboards.isLoading ? (
+ <div className="py-8 text-center">Loading dashboards...</div>
+ ) : dashboards.isError ? (
+ <div className="py-8 text-center text-destructive">
+ Error: {dashboards.error.message}
+ </div>
+ ) : dashboards.data?.dashboards.length === 0 ? (
+ <div className="py-8 text-center text-muted-foreground">
+ No dashboards found.
+ </div>
+ ) : (
+ <Table>
+ <TableHeader>
+ <TableRow>
+ <TableHead>Name</TableHead>
+ <TableHead>Description</TableHead>
+ <TableHead>Updated</TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ {dashboards.data?.dashboards.map((d) => (
+ <TableRow
+ key={d.id}
+ onClick={() => setSelectedDashboardId(d.id)}
+ className={`cursor-pointer hover:bg-muted ${
+ selectedDashboardId === d.id ? "bg-muted" : ""
+ }`}
+ >
+ <TableCell className="font-medium">{d.name}</TableCell>
+ <TableCell className="max-w-[200px] truncate">
+ {d.description}
+ </TableCell>
+ <TableCell>
+ {new Date(d.updatedAt).toLocaleString()}
+ </TableCell>
+ </TableRow>
+ ))}
+ </TableBody>
+ </Table>
+ )}
+ </div>
+
+ <DialogFooter className="mt-4 flex justify-between">
+ <Button variant="outline" onClick={handleSkip}>
+ Skip
+ </Button>
+ <Button onClick={handleAdd} disabled={!selectedDashboardId}>
+ Add to Dashboard
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/web/src/pages/project/[projectId]/widgets/new.tsx b/web/src/pages/project/[projectId]/widgets/new.tsx
index 739b43208b..a413016834 100644
--- a/web/src/pages/project/[projectId]/widgets/new.tsx
+++ b/web/src/pages/project/[projectId]/widgets/new.tsx
@@ -10,6 +10,8 @@ import {
type metricAggregations,
} from "@/src/features/query/types";
import { type z } from "zod";
+import { SelectDashboardDialog } from "@/src/features/dashboard/components/SelectDashboardDialog";
+import { useState } from "react";
export default function NewWidget() {
const router = useRouter();
@@ -18,7 +20,6 @@ export default function NewWidget() {
dashboardId?: string;
};
- // Save widget mutation
const createWidgetMutation = api.dashboardWidgets.create.useMutation({
onSuccess: (data) => {
showSuccessToast({
@@ -26,14 +27,13 @@ export default function NewWidget() {
description: "Your widget has been created.",
});
- // If dashboardId is provided, redirect back to the dashboard with the new widget ID
if (dashboardId) {
void router.push(
`/project/${projectId}/dashboards/${dashboardId}?addWidgetId=${data.widget.id}`,
);
} else {
- // Otherwise, navigate to widgets list
- void router.push(`/project/${projectId}/widgets`);
+ setPendingWidgetId(data.widget.id); // store for dialog
+ setDashboardDialogOpen(true);
}
},
onError: (error) => {
@@ -41,7 +41,6 @@ export default function NewWidget() {
},
});
- // Handle save widget
const handleSaveWidget = (widgetData: {
name: string;
description: string;
@@ -74,6 +73,9 @@ export default function NewWidget() {
});
};
+ const [dashboardDialogOpen, setDashboardDialogOpen] = useState(false);
+ const [pendingWidgetId, setPendingWidgetId] = useState<string | null>(null);
+
return (
<Page
withPadding
@@ -98,6 +100,21 @@ export default function NewWidget() {
chartType: "LINE_TIME_SERIES",
}}
/>
+ {pendingWidgetId && (
+ <SelectDashboardDialog
+ open={dashboardDialogOpen}
+ onOpenChange={setDashboardDialogOpen}
+ projectId={projectId}
+ onSelectDashboard={(dashboardId) => {
+ router.push(
+ `/project/${projectId}/dashboards/${dashboardId}?addWidgetId=${pendingWidgetId}`,
+ );
+ }}
+ onSkip={() => {
+ router.push(`/project/${projectId}/widgets`);
+ }}
+ />
+ )}
</Page>
);
}