Skip to content

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

feat(dashboard): add newly created widget to dashboard on save (#6819)

Steffen Schmitz76a090241e98
diff --git a/web/src/features/widgets/components/SelectWidgetDialog.tsx b/web/src/features/widgets/components/SelectWidgetDialog.tsx
index 1cf13bb840..ba3b53af29 100644
--- a/web/src/features/widgets/components/SelectWidgetDialog.tsx
+++ b/web/src/features/widgets/components/SelectWidgetDialog.tsx
@@ -35,6 +35,7 @@ interface SelectWidgetDialogProps {
onOpenChange: (open: boolean) => void;
projectId: string;
onSelectWidget: (widget: WidgetItem) => void;
+ dashboardId: string;
}
export function SelectWidgetDialog({
@@ -42,6 +43,7 @@ export function SelectWidgetDialog({
onOpenChange,
projectId,
onSelectWidget,
+ dashboardId,
}: SelectWidgetDialogProps) {
const router = useRouter();
const [selectedWidgetId, setSelectedWidgetId] = useState<string | null>(null);
@@ -61,7 +63,7 @@ export function SelectWidgetDialog({
);
const handleNavigateToNewWidget = () => {
- router.push(`/project/${projectId}/widgets/new`);
+ router.push(`/project/${projectId}/widgets/new?dashboardId=${dashboardId}`);
};
const handleAddWidget = () => {
diff --git a/web/src/pages/project/[projectId]/dashboards/[dashboardId]/index.tsx b/web/src/pages/project/[projectId]/dashboards/[dashboardId]/index.tsx
index 9743f93bba..7d840538d4 100644
--- a/web/src/pages/project/[projectId]/dashboards/[dashboardId]/index.tsx
+++ b/web/src/pages/project/[projectId]/dashboards/[dashboardId]/index.tsx
@@ -6,7 +6,7 @@ import { DashboardWidget } from "@/src/features/widgets";
import { DatePickerWithRange } from "@/src/components/date-picker";
import { PopoverFilterBuilder } from "@/src/features/filters/components/filter-builder";
import { useDashboardDateRange } from "@/src/hooks/useDashboardDateRange";
-import { useEffect, useState } from "react";
+import { useEffect, useState, useCallback } from "react";
import type { ColumnDefinition, FilterState } from "@langfuse/shared";
import { Button } from "@/src/components/ui/button";
import { PlusIcon } from "lucide-react";
@@ -31,9 +31,10 @@ interface WidgetPlacement {
export default function DashboardDetail() {
const router = useRouter();
- const { projectId, dashboardId } = router.query as {
+ const { projectId, dashboardId, addWidgetId } = router.query as {
projectId: string;
dashboardId: string;
+ addWidgetId?: string;
};
const hasCUDAccess = useHasProjectAccess({
@@ -72,6 +73,50 @@ export default function DashboardDetail() {
},
});
+ // Helper function to add a widget to the dashboard
+ const addWidgetToDashboard = useCallback(
+ (widget: WidgetItem) => {
+ if (!localDashboardDefinition) return;
+
+ // Find the maximum y position to place the new widget at the bottom
+ const maxY =
+ localDashboardDefinition.widgets.length > 0
+ ? Math.max(
+ ...localDashboardDefinition.widgets.map((w) => w.y + w.y_size),
+ )
+ : 0;
+
+ // Create a new widget placement
+ const newWidgetPlacement: WidgetPlacement = {
+ id: uuidv4(),
+ widgetId: widget.id,
+ x: 0, // Start at left
+ y: maxY, // Place below existing widgets
+ x_size: 6, // Default size (half of 12-column grid)
+ y_size: 2, // Default height of 2 rows
+ type: "widget",
+ };
+
+ // Add the widget to the local dashboard definition
+ setLocalDashboardDefinition({
+ ...localDashboardDefinition,
+ widgets: [...localDashboardDefinition.widgets, newWidgetPlacement],
+ });
+
+ setHasUnsavedChanges(true);
+
+ showSuccessToast({
+ title: "Widget added",
+ description: `"${widget.name}" has been added to the dashboard. Click Save to apply changes.`,
+ });
+ },
+ [
+ localDashboardDefinition,
+ setLocalDashboardDefinition,
+ setHasUnsavedChanges,
+ ],
+ );
+
const traceFilterOptions = api.traces.filterOptions.useQuery(
{
projectId,
@@ -186,12 +231,45 @@ export default function DashboardDetail() {
},
);
+ // Fetch widget data if addWidgetId is present
+ const widgetToAdd = api.dashboardWidgets.get.useQuery(
+ { projectId, widgetId: addWidgetId || "" },
+ {
+ enabled: Boolean(projectId) && Boolean(addWidgetId),
+ },
+ );
+
useEffect(() => {
if (dashboard.data && !localDashboardDefinition) {
setLocalDashboardDefinition(dashboard.data.definition);
}
}, [dashboard.data, localDashboardDefinition]);
+ useEffect(() => {
+ if (localDashboardDefinition && widgetToAdd.data && addWidgetId) {
+ if (
+ !localDashboardDefinition.widgets.some(
+ (w) => w.widgetId === addWidgetId,
+ )
+ ) {
+ addWidgetToDashboard(widgetToAdd.data);
+ }
+ // Remove the addWidgetId query parameter
+ router.replace({
+ pathname: router.pathname,
+ query: { projectId, dashboardId },
+ });
+ }
+ }, [
+ widgetToAdd.data,
+ addWidgetId,
+ addWidgetToDashboard,
+ localDashboardDefinition,
+ projectId,
+ dashboardId,
+ router,
+ ]);
+
// Handle deleting a widget
const handleDeleteWidget = (tileId: string) => {
if (localDashboardDefinition) {
@@ -215,39 +293,7 @@ export default function DashboardDetail() {
// Handle widget selection from dialog
const handleSelectWidget = (widget: WidgetItem) => {
- if (localDashboardDefinition) {
- // Find the maximum y position to place the new widget at the bottom
- const maxY =
- localDashboardDefinition.widgets.length > 0
- ? Math.max(
- ...localDashboardDefinition.widgets.map((w) => w.y + w.y_size),
- )
- : 0;
-
- // Create a new widget placement
- const newWidgetPlacement: WidgetPlacement = {
- id: uuidv4(),
- widgetId: widget.id,
- x: 0, // Start at left
- y: maxY, // Place below existing widgets
- x_size: 6, // Default size (half of 12-column grid)
- y_size: 2, // Default height of 2 rows
- type: "widget",
- };
-
- // Add the widget to the local dashboard definition
- setLocalDashboardDefinition({
- ...localDashboardDefinition,
- widgets: [...localDashboardDefinition.widgets, newWidgetPlacement],
- });
-
- setHasUnsavedChanges(true);
-
- showSuccessToast({
- title: "Widget added",
- description: `"${widget.name}" has been added to the dashboard. Click Save to apply changes.`,
- });
- }
+ addWidgetToDashboard(widget);
};
// Handle saving the dashboard
@@ -268,6 +314,7 @@ export default function DashboardDetail() {
onOpenChange={setIsWidgetDialogOpen}
projectId={projectId}
onSelectWidget={handleSelectWidget}
+ dashboardId={dashboardId}
/>
<Page
diff --git a/web/src/pages/project/[projectId]/widgets/new.tsx b/web/src/pages/project/[projectId]/widgets/new.tsx
index b2d904df1f..739b43208b 100644
--- a/web/src/pages/project/[projectId]/widgets/new.tsx
+++ b/web/src/pages/project/[projectId]/widgets/new.tsx
@@ -13,17 +13,28 @@ import { type z } from "zod";
export default function NewWidget() {
const router = useRouter();
- const { projectId } = router.query as { projectId: string };
+ const { projectId, dashboardId } = router.query as {
+ projectId: string;
+ dashboardId?: string;
+ };
// Save widget mutation
const createWidgetMutation = api.dashboardWidgets.create.useMutation({
- onSuccess: () => {
+ onSuccess: (data) => {
showSuccessToast({
title: "Widget created successfully",
description: "Your widget has been created.",
});
- // Navigate back to widgets list
- void router.push(`/project/${projectId}/widgets`);
+
+ // 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`);
+ }
},
onError: (error) => {
showErrorToast("Failed to save widget", error.message);