Skip to content

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

refactor(web): Make SpendAlertsTable use design system (#19305)

Ben Bachem5dc7959c9d1d
diff --git a/web/src/ee/features/billing/components/SpendAlerts/ConnectedDeleteSpendAlertDialogController.tsx b/web/src/ee/features/billing/components/SpendAlerts/ConnectedDeleteSpendAlertDialogController.tsx
new file mode 100644
index 0000000000..c457d15b61
--- /dev/null
+++ b/web/src/ee/features/billing/components/SpendAlerts/ConnectedDeleteSpendAlertDialogController.tsx
@@ -0,0 +1,64 @@
+import { type ComponentProps } from "react";
+import { ConfirmationDialogController } from "@/src/components/design-system/ConfirmationDialogController/ConfirmationDialogController";
+import { api, reportNonTrpcError } from "@/src/utils/api";
+import { usePostHogClientCapture } from "@/src/features/posthog-analytics";
+import { showSuccessToast, showToast } from "@/src/features/notifications";
+import { classifyTrpcToastError } from "@/src/utils/trpcErrorClassification";
+
+export function ConnectedDeleteSpendAlertDialogController({
+ orgId,
+ onSuccess,
+ children,
+}: {
+ orgId: string;
+ onSuccess: () => void;
+ children: ComponentProps<
+ typeof ConfirmationDialogController<string>
+ >["children"];
+}) {
+ const capture = usePostHogClientCapture();
+
+ const deleteMutation = api.spendAlerts.deleteSpendAlert.useMutation();
+
+ const handleDelete = async (alertId: string) => {
+ try {
+ await deleteMutation.mutateAsync({
+ orgId,
+ id: alertId,
+ });
+ capture("spend_alert:deleted", {
+ orgId,
+ alertId,
+ });
+ showSuccessToast({
+ operation: "spend_alert.delete",
+ title: "Spend alert deleted successfully",
+ description: "",
+ });
+ onSuccess();
+ } catch (error) {
+ reportNonTrpcError(error, "billing");
+ showToast({
+ type: "ERROR",
+ title: "Failed to delete spend alert. Please try again.",
+ analytics: classifyTrpcToastError(error, "spend_alert.delete"),
+ });
+ throw error;
+ }
+ };
+
+ return (
+ <ConfirmationDialogController<string>
+ title="Delete Spend Alert"
+ text="Are you sure you want to delete this spend alert? This action cannot be undone and you will no longer receive notifications for this threshold."
+ confirmLabel="Delete Alert"
+ variant="destructive"
+ loading={deleteMutation.isPending}
+ error={deleteMutation.error?.message}
+ onAfterDismiss={deleteMutation.reset}
+ onConfirm={handleDelete}
+ >
+ {children}
+ </ConfirmationDialogController>
+ );
+}
diff --git a/web/src/ee/features/billing/components/SpendAlerts/SpendAlertDialog.tsx b/web/src/ee/features/billing/components/SpendAlerts/ConnectedSpendAlertDialogController.tsx
similarity index 65%
rename from web/src/ee/features/billing/components/SpendAlerts/SpendAlertDialog.tsx
rename to web/src/ee/features/billing/components/SpendAlerts/ConnectedSpendAlertDialogController.tsx
index 77ebf9dbfc..258be20eb7 100644
--- a/web/src/ee/features/billing/components/SpendAlerts/SpendAlertDialog.tsx
+++ b/web/src/ee/features/billing/components/SpendAlerts/ConnectedSpendAlertDialogController.tsx
@@ -1,14 +1,10 @@
-/* eslint-disable no-nested-ternary, @typescript-eslint/no-deprecated -- Legacy UI usage retained during migration to design-system components. */
-import { useState } from "react";
+/* eslint-disable @typescript-eslint/no-use-before-define */
+import { useId, type ComponentProps } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
-import {
- Dialog,
- DialogContent,
- DialogDescription,
- DialogTitle,
-} from "@/src/components/ui/dialog";
+import { Dialog } from "@/src/components/design-system/Dialog/Dialog";
+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";
import {
Form,
FormControl,
@@ -18,8 +14,7 @@ import {
FormMessage,
} from "@/src/components/ui/form";
import { Input } from "@/src/components/ui/input";
-import { Button } from "@/src/components/ui/button";
-import { api, reportNonTrpcError } from "@/src/utils/api";
+import { api, reportNonTrpcError, type RouterOutputs } from "@/src/utils/api";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics";
import { showSuccessToast, showToast } from "@/src/features/notifications";
import { classifyTrpcToastError } from "@/src/utils/trpcErrorClassification";
@@ -39,53 +34,48 @@ const spendAlertSchema = z.object({
type SpendAlertFormInput = z.input<typeof spendAlertSchema>;
type SpendAlertFormOutput = z.output<typeof spendAlertSchema>;
-interface SpendAlertDialogProps {
- orgId: string;
- alert?: {
- id: string;
- title: string;
- threshold: { toString(): string };
- };
- open: boolean;
- onOpenChange: (open: boolean) => void;
- onSuccess: () => void;
-}
+type SpendAlertDialogState =
+ | { mode: "create" }
+ | {
+ mode: "edit";
+ alert: Pick<
+ RouterOutputs["spendAlerts"]["getSpendAlerts"][number],
+ "id" | "title" | "threshold"
+ >;
+ };
-export function SpendAlertDialog({
+export function ConnectedSpendAlertDialogController({
orgId,
- alert,
- open,
- onOpenChange,
onSuccess,
-}: SpendAlertDialogProps) {
- const [isSubmitting, setIsSubmitting] = useState(false);
+ children,
+}: {
+ orgId: string;
+ onSuccess: () => void;
+ children: ComponentProps<
+ typeof DialogController<SpendAlertDialogState>
+ >["children"];
+}) {
const capture = usePostHogClientCapture();
-
- const form = useForm<SpendAlertFormInput, undefined, SpendAlertFormOutput>({
- resolver: zodResolver(spendAlertSchema),
- defaultValues: {
- title: alert?.title ?? "",
- limit: alert ? parseFloat(alert.threshold.toString()) : undefined,
- },
- });
-
const createMutation = api.spendAlerts.createSpendAlert.useMutation();
const updateMutation = api.spendAlerts.updateSpendAlert.useMutation();
+ const isSubmitting = createMutation.isPending || updateMutation.isPending;
- const onSubmit = async (data: SpendAlertFormOutput) => {
- setIsSubmitting(true);
+ const onSubmit = async (
+ state: SpendAlertDialogState,
+ data: SpendAlertFormOutput,
+ closeDialog: () => void,
+ ) => {
try {
- if (alert) {
- // Update existing alert
+ if (state.mode === "edit") {
await updateMutation.mutateAsync({
orgId,
- id: alert.id,
+ id: state.alert.id,
title: data.title,
threshold: data.limit,
});
capture("spend_alert:updated", {
orgId,
- alertId: alert.id,
+ alertId: state.alert.id,
limit: data.limit,
});
showSuccessToast({
@@ -94,7 +84,6 @@ export function SpendAlertDialog({
description: "",
});
} else {
- // Create new alert
await createMutation.mutateAsync({
orgId,
title: data.title,
@@ -110,33 +99,81 @@ export function SpendAlertDialog({
description: "",
});
}
+ closeDialog();
onSuccess();
} catch (error) {
reportNonTrpcError(error, "billing");
showToast({
type: "ERROR",
- title: `Failed to ${alert ? "update" : "create"} spend alert. Please try again.`,
+ title: `Failed to ${state.mode === "edit" ? "update" : "create"} spend alert. Please try again.`,
analytics: classifyTrpcToastError(
error,
- alert ? "spend_alert.update" : "spend_alert.create",
+ state.mode === "edit" ? "spend_alert.update" : "spend_alert.create",
),
});
- } finally {
- setIsSubmitting(false);
}
};
return (
- <Dialog open={open} onOpenChange={onOpenChange}>
- <DialogContent className="p-4 sm:max-w-[425px]">
- <DialogTitle>
- {alert ? "Edit Spend Alert" : "Create Spend Alert"}
- </DialogTitle>
- <DialogDescription className="text-muted-foreground pt-1 pb-2 text-sm">
+ <DialogController<SpendAlertDialogState>
+ onBeforeClose={() => !isSubmitting}
+ renderDialog={({ state, closeDialog }) => (
+ <SpendAlertDialog
+ state={state}
+ isSubmitting={isSubmitting}
+ onSubmit={(data) => onSubmit(state, data, closeDialog)}
+ />
+ )}
+ >
+ {children}
+ </DialogController>
+ );
+}
+
+function SpendAlertDialog({
+ state,
+ isSubmitting,
+ onSubmit,
+}: {
+ state: SpendAlertDialogState;
+ isSubmitting: boolean;
+ onSubmit: (data: SpendAlertFormOutput) => Promise<void>;
+}) {
+ const formId = useId();
+ const form = useForm<SpendAlertFormInput, undefined, SpendAlertFormOutput>({
+ resolver: zodResolver(spendAlertSchema),
+ defaultValues: {
+ title: state.mode === "edit" ? state.alert.title : "",
+ limit:
+ state.mode === "edit"
+ ? parseFloat(state.alert.threshold.toString())
+ : undefined,
+ },
+ });
+
+ return (
+ <Dialog
+ size="sm"
+ title={state.mode === "edit" ? "Edit Spend Alert" : "Create Spend Alert"}
+ actions={[
+ {
+ label: state.mode === "edit" ? "Update Alert" : "Create Alert",
+ loading: isSubmitting,
+ type: "submit",
+ form: formId,
+ },
+ ]}
+ >
+ <Dialog.Body>
+ <p className="text-muted-foreground">
Get notified when your organization&apos;s spending exceeds a limit.
- </DialogDescription>
+ </p>
<Form {...form}>
- <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
+ <form
+ id={formId}
+ onSubmit={form.handleSubmit(onSubmit)}
+ className="space-y-4"
+ >
<FormField
control={form.control}
name="title"
@@ -194,28 +231,9 @@ export function SpendAlertDialog({
<li>Alerts are evaluated with a 90 minute delay.</li>
</ul>
</div>
- <div className="flex flex-row items-center justify-end gap-2">
- <Button
- type="button"
- variant="outline"
- onClick={() => onOpenChange(false)}
- disabled={isSubmitting}
- >
- Cancel
- </Button>
- <Button type="submit" disabled={isSubmitting}>
- {isSubmitting
- ? alert
- ? "Updating..."
- : "Creating..."
- : alert
- ? "Update Alert"
- : "Create Alert"}
- </Button>
- </div>
</form>
</Form>
- </DialogContent>
+ </Dialog.Body>
</Dialog>
);
}
diff --git a/web/src/ee/features/billing/components/SpendAlerts/ConnectedSpendAlertsTable.tsx b/web/src/ee/features/billing/components/SpendAlerts/ConnectedSpendAlertsTable.tsx
new file mode 100644
index 0000000000..b33981cfbf
--- /dev/null
+++ b/web/src/ee/features/billing/components/SpendAlerts/ConnectedSpendAlertsTable.tsx
@@ -0,0 +1,67 @@
+import { useMemo, type ComponentProps } from "react";
+import { api } from "@/src/utils/api";
+import { useHasOrganizationAccess } from "@/src/features/rbac";
+import { SpendAlertsTable } from "./SpendAlertsTable";
+import { ConnectedSpendAlertDialogController } from "./ConnectedSpendAlertDialogController";
+import { ConnectedDeleteSpendAlertDialogController } from "./ConnectedDeleteSpendAlertDialogController";
+
+export function ConnectedSpendAlertsTable({ orgId }: { orgId: string }) {
+ const hasAccess = useHasOrganizationAccess({
+ organizationId: orgId,
+ scope: "langfuseCloudBilling:CRUD",
+ });
+ const {
+ data: spendAlerts,
+ isLoading,
+ isError,
+ error,
+ refetch,
+ } = api.spendAlerts.getSpendAlerts.useQuery(
+ { orgId },
+ { enabled: hasAccess },
+ );
+
+ const data = useMemo<ComponentProps<typeof SpendAlertsTable>["data"]>(() => {
+ if (isLoading) return { status: "loading" };
+ if (isError) return { status: "error", error: error.message };
+ return {
+ status: "success",
+ data: (spendAlerts ?? []).map((alert) => ({
+ id: alert.id,
+ title: alert.title,
+ threshold: parseFloat(alert.threshold.toString()),
+ triggeredAt: alert.triggeredAt,
+ })),
+ };
+ }, [isLoading, isError, error, spendAlerts]);
+
+ return (
+ <ConnectedDeleteSpendAlertDialogController
+ orgId={orgId}
+ onSuccess={() => {
+ refetch();
+ }}
+ >
+ {({ openDialog: openDeleteDialog }) => (
+ <ConnectedSpendAlertDialogController
+ orgId={orgId}
+ onSuccess={() => {
+ refetch();
+ }}
+ >
+ {({ openDialog }) => (
+ <SpendAlertsTable
+ data={data}
+ onEdit={(id) => {
+ const alert = spendAlerts?.find((alert) => alert.id === id);
+ if (!alert) return;
+ openDialog({ mode: "edit", alert });
+ }}
+ onDelete={openDeleteDialog}
+ />
+ )}
+ </ConnectedSpendAlertDialogController>
+ )}
+ </ConnectedDeleteSpendAlertDialogController>
+ );
+}
diff --git a/web/src/ee/features/billing/components/SpendAlerts/DeleteSpendAlertDialog.tsx b/web/src/ee/features/billing/components/SpendAlerts/DeleteSpendAlertDialog.tsx
deleted file mode 100644
index 259f7b392c..0000000000
--- a/web/src/ee/features/billing/components/SpendAlerts/DeleteSpendAlertDialog.tsx
+++ /dev/null
@@ -1,96 +0,0 @@
-/* eslint-disable @typescript-eslint/no-deprecated -- Legacy UI usage retained during migration to design-system components. */
-import { useState } from "react";
-import {
- Dialog,
- DialogContent,
- DialogDescription,
- DialogFooter,
- DialogHeader,
- DialogTitle,
-} from "@/src/components/ui/dialog";
-import { Button } from "@/src/components/ui/button";
-import { api, reportNonTrpcError } from "@/src/utils/api";
-import { usePostHogClientCapture } from "@/src/features/posthog-analytics";
-import { showSuccessToast, showToast } from "@/src/features/notifications";
-import { classifyTrpcToastError } from "@/src/utils/trpcErrorClassification";
-
-interface DeleteSpendAlertDialogProps {
- orgId: string;
- alertId: string;
- open: boolean;
- onOpenChange: (open: boolean) => void;
- onSuccess: () => void;
-}
-
-export function DeleteSpendAlertDialog({
- orgId,
- alertId,
- open,
- onOpenChange,
- onSuccess,
-}: DeleteSpendAlertDialogProps) {
- const [isDeleting, setIsDeleting] = useState(false);
- const capture = usePostHogClientCapture();
-
- const deleteMutation = api.spendAlerts.deleteSpendAlert.useMutation();
-
- const handleDelete = async () => {
- setIsDeleting(true);
- try {
- await deleteMutation.mutateAsync({
- orgId,
- id: alertId,
- });
- capture("spend_alert:deleted", {
- orgId,
- alertId,
- });
- showSuccessToast({
- operation: "spend_alert.delete",
- title: "Spend alert deleted successfully",
- description: "",
- });
- onSuccess();
- } catch (error) {
- reportNonTrpcError(error, "billing");
- showToast({
- type: "ERROR",
- title: "Failed to delete spend alert. Please try again.",
- analytics: classifyTrpcToastError(error, "spend_alert.delete"),
- });
- } finally {
- setIsDeleting(false);
- }
- };
-
- return (
- <Dialog open={open} onOpenChange={onOpenChange}>
- <DialogContent>
- <DialogHeader>
- <DialogTitle>Delete Spend Alert</DialogTitle>
- <DialogDescription>
- Are you sure you want to delete this spend alert? This action cannot
- be undone and you will no longer receive notifications for this
- threshold.
- </DialogDescription>
- </DialogHeader>
- <DialogFooter>
- <Button
- variant="outline"
- disabled={isDeleting}
- onClick={() => onOpenChange(false)}
- >
- Cancel
- </Button>
- <Button
- variant="destructive"
- onClick={handleDelete}
- disabled={isDeleting}
- >
- {isDeleting ? "Deleting..." : "Delete Alert"}
- </Button>
- </DialogFooter>
- </DialogContent>
- </Dialog>
- );
-}
diff --git a/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsSection.tsx b/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsSection.tsx
index e34363f426..e340b5b27e 100644
--- a/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsSection.tsx
+++ b/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsSection.tsx
@@ -1,48 +1,43 @@
import { useState } from "react";
import { Button } from "@/src/components/ui/button";
import { Plus } from "@langfuse/icons";
-import { SpendAlertsTable } from "./SpendAlertsTable";
-import { SpendAlertDialog } from "./SpendAlertDialog";
+import { ConnectedSpendAlertsTable } from "./ConnectedSpendAlertsTable";
+import { ConnectedSpendAlertDialogController } from "./ConnectedSpendAlertDialogController";
interface SpendAlertsSectionProps {
orgId: string;
}
export function SpendAlertsSection({ orgId }: SpendAlertsSectionProps) {
- const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false);
const [refetchTrigger, setRefetchTrigger] = useState(0);
return (
- <>
- <div className="space-y-2">
- <div className="flex items-center justify-between pt-4">
- <div>
- <h3 className="font-bold">Spend Alerts</h3>
- <p className="text-muted-foreground max-w-prose text-sm">
- Get notified when your organization&apos;s spending exceeds
- configured thresholds. Alerts may be delayed by up to 90 minutes.
- </p>
- <p className="text-muted-foreground max-w-prose text-sm"></p>
+ <ConnectedSpendAlertDialogController
+ orgId={orgId}
+ onSuccess={() => setRefetchTrigger((prev) => prev + 1)}
+ >
+ {({ openDialog }) => (
+ <div className="space-y-2">
+ <div className="flex items-center justify-between pt-4">
+ <div>
+ <h3 className="font-bold">Spend Alerts</h3>
+ <p className="text-muted-foreground max-w-prose text-sm">
+ Get notified when your organization&apos;s spending exceeds
+ configured thresholds. Alerts may be delayed by up to 90
+ minutes.
+ </p>
+ <p className="text-muted-foreground max-w-prose text-sm"></p>
+ </div>
+
+ <Button onClick={() => openDialog({ mode: "create" })}>
+ <Plus size="base" __rawClassName="mr-2" />
+ Create Alert
+ </Button>
</div>
- <Button onClick={() => setIsCreateDialogOpen(true)}>
- <Plus size="base" __rawClassName="mr-2" />
- Create Alert
- </Button>
+ <ConnectedSpendAlertsTable orgId={orgId} key={refetchTrigger} />
</div>
-
- <SpendAlertsTable orgId={orgId} key={refetchTrigger} />
- </div>
-
- <SpendAlertDialog
- orgId={orgId}
- open={isCreateDialogOpen}
- onOpenChange={setIsCreateDialogOpen}
- onSuccess={() => {
- setIsCreateDialogOpen(false);
- setRefetchTrigger((prev) => prev + 1); // Trigger refetch
- }}
- />
- </>
+ )}
+ </ConnectedSpendAlertDialogController>
);
}
diff --git a/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsTable.stories.tsx b/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsTable.stories.tsx
new file mode 100644
index 0000000000..42b4aa227a
--- /dev/null
+++ b/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsTable.stories.tsx
@@ -0,0 +1,44 @@
+import { fn } from "storybook/test";
+import preview from "../../../../../../.storybook/preview";
+import { SpendAlertsTable } from "./SpendAlertsTable";
+
+const meta = preview.meta({ component: SpendAlertsTable });
+
+export const Default = meta.story({
+ args: {
+ data: {
+ status: "success",
+ data: [
+ {
+ id: "alert-1",
+ title: "Monthly budget",
+ threshold: 100,
+ triggeredAt: null,
+ },
+ {
+ id: "alert-2",
+ title: "Budget warning",
+ threshold: 50,
+ triggeredAt: new Date("2026-09-01T12:00:00Z"),
+ },
+ ],
+ },
+ onEdit: fn(),
+ onDelete: fn(),
+ },
+});
+
+export const Empty = meta.story({
+ args: { ...Default.input.args, data: { status: "success", data: [] } },
+});
+
+export const Loading = meta.story({
+ args: { ...Default.input.args, data: { status: "loading" } },
+});
+
+export const Error = meta.story({
+ args: {
+ ...Default.input.args,
+ data: { status: "error", error: "Unable to load spend alerts." },
+ },
+});
diff --git a/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsTable.tsx b/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsTable.tsx
index e2c9fe20d1..61b29e3bc7 100644
--- a/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsTable.tsx
+++ b/web/src/ee/features/billing/components/SpendAlerts/SpendAlertsTable.tsx
@@ -1,86 +1,44 @@
-import { useMemo, useState } from "react";
-import { DropdownMenuItem } from "@/src/components/ui/dropdown-menu";
import { Edit, Trash2 } from "@langfuse/icons";
-import { api } from "@/src/utils/api";
-import { useHasOrganizationAccess } from "@/src/features/rbac";
+import { type RouterOutputs } from "@/src/utils/api";
import { formatDistanceToNow } from "date-fns";
-import { SpendAlertDialog } from "./SpendAlertDialog";
-import { DeleteSpendAlertDialog } from "./DeleteSpendAlertDialog";
-import { DataTable } from "@/src/components/table/data-table";
-import { DataTableToolbar } from "@/src/components/table/data-table-toolbar";
+import { SettingsTable } from "@/src/components/SettingsTable/SettingsTable";
+import { type AsyncTableData } from "@/src/components/design-system/table/Table";
import { type LangfuseColumnDef } from "@/src/components/table/types";
-import { createDropdownTableColumn } from "@/src/components/design-system/table/columns/createDropdownTableColumn";
import { createNumberTableColumn } from "@/src/components/design-system/table/columns/createNumberTableColumn";
import { createStatusTableColumn } from "@/src/components/design-system/table/columns/createStatusTableColumn";
import { createTextTableColumn } from "@/src/components/design-system/table/columns/createTextTableColumn";
import { costFormatter } from "@/src/utils/numbers";
-interface SpendAlertsTableProps {
- orgId: string;
-}
-
-type AlertRow = {
- id: string;
- title: string;
+export type SpendAlertRow = Pick<
+ RouterOutputs["spendAlerts"]["getSpendAlerts"][number],
+ "id" | "title" | "triggeredAt"
+> & {
threshold: number; // USD
- triggeredAt: Date | null;
- createdAt: Date;
};
-export function SpendAlertsTable({ orgId }: SpendAlertsTableProps) {
- const [editingAlert, setEditingAlert] = useState<string | null>(null);
- const [deletingAlert, setDeletingAlert] = useState<string | null>(null);
-
- const hasAccess = useHasOrganizationAccess({
- organizationId: orgId,
- scope: "langfuseCloudBilling:CRUD",
- });
-
- const {
- data: spendAlerts,
- isLoading,
- isError,
- refetch,
- } = api.spendAlerts.getSpendAlerts.useQuery(
- { orgId },
- { enabled: hasAccess },
- );
-
- const rows = useMemo<AlertRow[]>(() => {
- return (spendAlerts ?? []).map((a: any) => ({
- id: a.id,
- title: a.title,
- threshold: parseFloat(a.threshold?.toString?.() ?? "0"),
- triggeredAt: a.triggeredAt ? new Date(a.triggeredAt) : null,
- createdAt: new Date(a.createdAt),
- }));
- }, [spendAlerts]);
-
- const data = useMemo(() => {
- if (isLoading) return { isLoading: true, isError: false } as const;
- if (isError)
- return {
- isLoading: false,
- isError: false,
- data: [] as AlertRow[],
- } as const;
- return { isLoading: false, isError: false, data: rows } as const;
- }, [isLoading, isError, rows]);
-
- const columns: LangfuseColumnDef<AlertRow>[] = [
- createTextTableColumn<AlertRow>({
+export function SpendAlertsTable({
+ data,
+ onEdit,
+ onDelete,
+}: {
+ data: AsyncTableData<SpendAlertRow[]>;
+ onEdit: (id: string) => void;
+ onDelete: (id: string) => void;
+}) {
+ const columns: LangfuseColumnDef<SpendAlertRow>[] = [
+ createTextTableColumn<SpendAlertRow>({
accessorKey: "title",
header: "Title",
size: 160,
}),
- createNumberTableColumn<AlertRow>({
+ createNumberTableColumn<SpendAlertRow>({
accessorFn: (row) => row.threshold,
id: "limit",
header: "Limit (USD)",
size: 140,
formatter: costFormatter,
}),
- createStatusTableColumn<AlertRow, Date>({
+ createStatusTableColumn<SpendAlertRow, Date>({
id: "status",
accessorFn: (row) => row.triggeredAt,
header: "Status",
@@ -100,62 +58,31 @@ export function SpendAlertsTable({ orgId }: SpendAlertsTableProps) {
})
: "Never",
},
- createDropdownTableColumn<AlertRow, string>({
- id: "actions",
- accessorFn: (row) => row.id,
- header: "Actions",
- size: 120,
- renderMenu: (id) =>
- id ? (
- <>
- <DropdownMenuItem onClick={() => setEditingAlert(id)}>
- <Edit size="base" color="default" __rawClassName="mr-2" />
- Edit
- </DropdownMenuItem>
- <DropdownMenuItem
- onClick={() => setDeletingAlert(id)}
- className="text-destructive"
- >
- <Trash2 size="base" __rawClassName="mr-2" />
- Delete
- </DropdownMenuItem>
- </>
- ) : null,
- }),
];
- const editingAlertData = spendAlerts?.find((a) => a.id === editingAlert);
-
return (
- <>
- <DataTableToolbar columns={columns} tableName="spend-alerts" />
- <DataTable tableName="spend-alerts" columns={columns} data={data} />
-
- {editingAlert && editingAlertData && (
- <SpendAlertDialog
- orgId={orgId}
- alert={editingAlertData}
- open={!!editingAlert}
- onOpenChange={(open) => !open && setEditingAlert(null)}
- onSuccess={() => {
- setEditingAlert(null);
- refetch();
- }}
- />
- )}
-
- {deletingAlert && (
- <DeleteSpendAlertDialog
- orgId={orgId}
- alertId={deletingAlert}
- open={!!deletingAlert}
- onOpenChange={(open) => !open && setDeletingAlert(null)}
- onSuccess={() => {
- setDeletingAlert(null);
- refetch();
- }}
- />
- )}
- </>
+ <SettingsTable
+ tableName="spend-alerts"
+ columnVisibilityKey="spend-alertsColumnVisibility"
+ columns={columns}
+ data={data}
+ actions={(row) => [
+ {
+ id: "edit",
+ type: "item",
+ title: "Edit",
+ icon: Edit,
+ onClick: () => onEdit(row.id),
+ },
+ {
+ id: "delete",
+ type: "item",
+ title: "Delete",
+ icon: Trash2,
+ variant: "destructive",
+ onClick: () => onDelete(row.id),
+ },
+ ]}
+ />
);
}