Skip to content

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

refactor(web): Clean up `UpsertScoreConfigDialog` (#16615)

Ben Bachem24a77ddb8e9f
diff --git a/web/src/components/table/use-cases/score-configs.tsx b/web/src/components/table/use-cases/score-configs.tsx
index 7b469a66b0..1878f3386a 100644
--- a/web/src/components/table/use-cases/score-configs.tsx
+++ b/web/src/components/table/use-cases/score-configs.tsx
@@ -1,4 +1,4 @@
-import React, { useState } from "react";
+import React from "react";
import { useRowHeightLocalStorage } from "@/src/components/table/data-table-row-height-switch";
import useColumnVisibility from "@/src/features/column-visibility/hooks/useColumnVisibility";
import { api } from "@/src/utils/api";
@@ -17,7 +17,7 @@ import {
isCategoricalDataType,
isNumericDataType,
} from "@/src/features/scores/lib/helpers";
-import { Archive, Edit, MoreVertical } from "lucide-react";
+import { Archive, Edit, MoreVertical, PlusIcon } from "lucide-react";
import { Button } from "@/src/components/ui/button";
import useColumnOrder from "@/src/features/column-visibility/hooks/useColumnOrder";
import { SettingsTableCard } from "@/src/components/layouts/settings-table-card";
@@ -29,7 +29,7 @@ import {
DropdownMenuItem,
} from "@/src/components/ui/dropdown-menu";
import { ArchiveScoreConfigPopoverController } from "@/src/features/score-configs/components/ArchiveScoreConfigButton";
-import { UpsertScoreConfigDialog } from "@/src/features/score-configs/components/UpsertScoreConfigDialog";
+import { UpsertScoreConfigDialogController } from "@/src/features/score-configs/components/UpsertScoreConfigDialogController";
type ScoreConfigTableRow = {
id: string;
@@ -72,8 +72,6 @@ function getConfigRange(
}
export function ScoreConfigsTable({ projectId }: { projectId: string }) {
- const [editConfigId, setEditConfigId] = useState<string | null>(null);
- const [createConfigOpen, setCreateConfigOpen] = useState(false);
const [paginationState, setPaginationState] = usePaginationState(0, 50, {
page: "pageIndex",
limit: "pageSize",
@@ -98,11 +96,6 @@ export function ScoreConfigsTable({ projectId }: { projectId: string }) {
{ enabled: hasAccess },
);
- const configQuery = api.scoreConfigs.byId.useQuery(
- { projectId, id: editConfigId as string },
- { enabled: !!editConfigId && hasAccess },
- );
-
const totalCount = configs.data?.totalCount ?? null;
const columns: LangfuseColumnDef<ScoreConfigTableRow>[] = [
@@ -181,44 +174,64 @@ export function ScoreConfigsTable({ projectId }: { projectId: string }) {
const { id: configId, isArchived, name } = row.original;
return (
- <ArchiveScoreConfigPopoverController
- configId={configId}
+ <UpsertScoreConfigDialogController
+ mode="edit"
projectId={projectId}
- isArchived={isArchived}
- name={name}
+ defaultValues={{
+ id: configId,
+ name,
+ dataType: row.original.dataType,
+ minValue: row.original.range.minValue ?? undefined,
+ maxValue: row.original.range.maxValue ?? undefined,
+ description: row.original.description ?? undefined,
+ categories: row.original.range.categories?.length
+ ? row.original.range.categories
+ : undefined,
+ }}
>
- {({ Anchor, disabled, openPopover }) => (
- <DropdownMenu>
- <Anchor asChild>
- <DropdownMenuTrigger asChild>
- <Button variant="ghost">
- <MoreVertical className="h-4 w-4" />
- </Button>
- </DropdownMenuTrigger>
- </Anchor>
- <DropdownMenuContent>
- <DropdownMenuItem
- key={configId}
- aria-label="edit"
- onClick={() => setEditConfigId(configId)}
- >
- <Edit className="mr-2 h-4 w-4" />
- Edit
- </DropdownMenuItem>
- <DropdownMenuItem
- key="archive"
- disabled={disabled !== undefined}
- title={disabled?.reason}
- onClick={(event) => event.stopPropagation()}
- onSelect={openPopover}
- >
- <Archive className="mr-2 h-4 w-4" />
- Archive
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
+ {({ disabled: editDisabled, Trigger }) => (
+ <ArchiveScoreConfigPopoverController
+ configId={configId}
+ projectId={projectId}
+ isArchived={isArchived}
+ name={name}
+ >
+ {({ Anchor, disabled, openPopover }) => (
+ <DropdownMenu>
+ <Anchor asChild>
+ <DropdownMenuTrigger asChild>
+ <Button variant="ghost">
+ <MoreVertical className="h-4 w-4" />
+ </Button>
+ </DropdownMenuTrigger>
+ </Anchor>
+ <DropdownMenuContent>
+ <Trigger asChild>
+ <DropdownMenuItem
+ aria-label="edit"
+ disabled={editDisabled !== undefined}
+ title={editDisabled?.reason}
+ >
+ <Edit className="mr-2 h-4 w-4" />
+ Edit
+ </DropdownMenuItem>
+ </Trigger>
+ <DropdownMenuItem
+ key="archive"
+ disabled={disabled !== undefined}
+ title={disabled?.reason}
+ onClick={(event) => event.stopPropagation()}
+ onSelect={openPopover}
+ >
+ <Archive className="mr-2 h-4 w-4" />
+ Archive
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )}
+ </ArchiveScoreConfigPopoverController>
)}
- </ArchiveScoreConfigPopoverController>
+ </UpsertScoreConfigDialogController>
);
},
},
@@ -246,12 +259,28 @@ export function ScoreConfigsTable({ projectId }: { projectId: string }) {
rowHeight={rowHeight}
setRowHeight={setRowHeight}
actionButtons={
- <UpsertScoreConfigDialog
+ <UpsertScoreConfigDialogController
key="new-config-dialog"
+ mode="create"
projectId={projectId}
- open={createConfigOpen}
- onOpenChange={setCreateConfigOpen}
- />
+ >
+ {({ disabled, isSubmitting, Trigger }) => (
+ <Trigger asChild>
+ <Button
+ variant="secondary"
+ disabled={disabled !== undefined}
+ loading={isSubmitting}
+ title={disabled?.reason}
+ >
+ <PlusIcon
+ className="mr-1.5 -ml-0.5 h-4 w-4"
+ aria-hidden="true"
+ />
+ Add new score config
+ </Button>
+ </Trigger>
+ )}
+ </UpsertScoreConfigDialogController>
}
className="px-0"
/>
@@ -301,33 +330,6 @@ export function ScoreConfigsTable({ projectId }: { projectId: string }) {
className="gap-2"
/>
</SettingsTableCard>
-
- {!!editConfigId && configQuery.isSuccess && (
- <UpsertScoreConfigDialog
- key={editConfigId}
- id={editConfigId}
- projectId={projectId}
- open={!!editConfigId && configQuery.isSuccess}
- onOpenChange={(open) => {
- if (!open) setEditConfigId(null);
- }}
- defaultValues={
- configQuery.data
- ? {
- id: editConfigId,
- name: configQuery.data.name,
- dataType: configQuery.data.dataType,
- minValue: configQuery.data.minValue ?? undefined,
- maxValue: configQuery.data.maxValue ?? undefined,
- description: configQuery.data.description ?? undefined,
- categories: configQuery.data.categories?.length
- ? configQuery.data.categories
- : undefined,
- }
- : undefined
- }
- />
- )}
</>
);
}
diff --git a/web/src/features/score-configs/components/UpsertScoreConfigDialog.tsx b/web/src/features/score-configs/components/UpsertScoreConfigDialog.tsx
deleted file mode 100644
index 0e455dbd83..0000000000
--- a/web/src/features/score-configs/components/UpsertScoreConfigDialog.tsx
+++ /dev/null
@@ -1,454 +0,0 @@
-/* eslint-disable @repo/no-abstracted-overlay-trigger */
-import React, { useState } from "react";
-import { Button } from "@/src/components/ui/button";
-import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
-import {
- Dialog,
- DialogBody,
- DialogContent,
- DialogFooter,
- DialogHeader,
- DialogTitle,
- DialogTrigger,
-} from "@/src/components/ui/dialog";
-import { PlusIcon, Trash } from "lucide-react";
-import { type UseFormReturn, useFieldArray, useForm } from "react-hook-form";
-import { zodResolver } from "@hookform/resolvers/zod";
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from "@/src/components/ui/form";
-import { Input } from "@/src/components/ui/input";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/src/components/ui/select";
-import { api } from "@/src/utils/api";
-import { Textarea } from "@/src/components/ui/textarea";
-import {
- isBooleanDataType,
- isCategoricalDataType,
- isTextDataType,
- isNumericDataType,
-} from "@/src/features/scores/lib/helpers";
-import DocPopup from "@/src/components/layouts/doc-popup";
-import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
-import {
- createConfigSchema,
- updateConfigSchema,
- type CreateConfig,
- type UpdateConfig,
-} from "@/src/features/score-configs/lib/upsertFormTypes";
-import { validateScoreConfigUpsertFormInput } from "@/src/features/score-configs/lib/validateScoreConfigUpsertFormInput";
-import { ScoreConfigDataType } from "@langfuse/shared";
-
-export function UpsertScoreConfigDialog({
- projectId,
- id,
- open,
- onOpenChange,
- defaultValues,
-}: {
- projectId: string;
- id?: string | null;
- open: boolean;
- onOpenChange: (open: boolean) => void;
- defaultValues?: CreateConfig | UpdateConfig;
-}) {
- const [formError, setFormError] = useState<string | null>(null);
- const capture = usePostHogClientCapture();
-
- const hasAccess = useHasProjectAccess({
- projectId: projectId,
- scope: "scoreConfigs:CUD",
- });
-
- const utils = api.useUtils();
- const createScoreConfig = api.scoreConfigs.create.useMutation({
- onSuccess: () => utils.scoreConfigs.invalidate(),
- onError: (error) =>
- setFormError(error.message ?? "An error occurred while creating config."),
- });
-
- const updateScoreConfig = api.scoreConfigs.update.useMutation({
- onSuccess: () => utils.scoreConfigs.invalidate(),
- onError: (error) =>
- setFormError(error.message ?? "An error occurred while updating config."),
- });
-
- const form = useForm({
- resolver: zodResolver(id ? updateConfigSchema : createConfigSchema),
- defaultValues: defaultValues ?? {
- dataType: ScoreConfigDataType.NUMERIC,
- minValue: undefined,
- maxValue: undefined,
- name: "",
- },
- }) as UseFormReturn<CreateConfig | UpdateConfig>;
-
- const { fields, append, remove, replace } = useFieldArray({
- control: form.control,
- name: "categories",
- });
-
- if (!hasAccess) return null;
-
- async function onSubmit(values: CreateConfig | UpdateConfig) {
- const error = validateScoreConfigUpsertFormInput(values);
- setFormError(error);
- const isValid = await form.trigger();
- if (!isValid || error) return;
-
- if (!!id) {
- return updateScoreConfig
- .mutateAsync({
- ...values,
- projectId,
- id: id as string,
- description: values.description ?? null,
- categories: values.categories?.length ? values.categories : undefined,
- })
- .then(() => {
- capture("score_configs:update_form_submit", {
- dataType: values.dataType,
- });
- form.reset();
- onOpenChange(false);
- });
- }
- return createScoreConfig
- .mutateAsync({
- projectId,
- ...values,
- description: values.description ?? null,
- categories: values.categories?.length ? values.categories : undefined,
- })
- .then(() => {
- capture("score_configs:create_form_submit", {
- dataType: values.dataType,
- });
- form.reset();
- onOpenChange(false);
- });
- }
-
- return (
- <>
- <Dialog
- open={open}
- onOpenChange={(v) => {
- form.reset();
- setFormError(null);
- onOpenChange(v);
- }}
- >
- <DialogTrigger asChild>
- <Button variant="secondary" loading={createScoreConfig.isPending}>
- <PlusIcon className="mr-1.5 -ml-0.5 h-4 w-4" aria-hidden="true" />
- {id ? "Update score config" : "Add new score config"}
- </Button>
- </DialogTrigger>
- <DialogContent className="max-h-[90vh] overflow-y-auto">
- <DialogHeader>
- <DialogTitle>
- {id ? "Update score config" : "Add new score config"}
- </DialogTitle>
- </DialogHeader>
- <Form {...form}>
- <form className="space-y-6" onSubmit={form.handleSubmit(onSubmit)}>
- <DialogBody>
- <FormField
- control={form.control}
- name="name"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Name</FormLabel>
- <FormControl>
- <Input
- {...field}
- type="text"
- onBlur={(e) =>
- field.onChange(e.target.value.trimEnd())
- }
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="dataType"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Data type</FormLabel>
- <Select
- disabled={!!id}
- defaultValue={field.value}
- onValueChange={(value) => {
- const dt = value as ScoreConfigDataType;
- field.onChange(dt);
- form.clearErrors();
- if (isNumericDataType(dt)) {
- form.setValue("categories", undefined);
- } else if (isTextDataType(dt)) {
- form.setValue("categories", undefined);
- form.setValue("minValue", undefined);
- form.setValue("maxValue", undefined);
- } else {
- form.setValue("minValue", undefined);
- form.setValue("maxValue", undefined);
- if (isBooleanDataType(dt)) {
- replace([
- { label: "True", value: 1 },
- { label: "False", value: 0 },
- ]);
- } else {
- replace([{ label: "", value: 0 }]);
- }
- }
- }}
- >
- <FormControl>
- <SelectTrigger>
- <SelectValue placeholder="Select a data type" />
- </SelectTrigger>
- </FormControl>
- <SelectContent>
- {Object.values(ScoreConfigDataType).map(
- (dataType) => (
- <SelectItem value={dataType} key={dataType}>
- {dataType}
- </SelectItem>
- ),
- )}
- </SelectContent>
- </Select>
- <FormMessage />
- </FormItem>
- )}
- />
- {isNumericDataType(form.getValues("dataType")) ? (
- <>
- <FormField
- control={form.control}
- name="minValue"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Minimum (optional) </FormLabel>
- <FormControl>
- <Input
- {...field}
- value={field.value ?? ""}
- // manually manage controlled input state
- onChange={(e) => {
- const value = e.target.value;
- field.onChange(
- value === "" ? undefined : Number(value),
- );
- }}
- type="number"
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="maxValue"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Maximum (optional)</FormLabel>
- <FormControl>
- <Input
- {...field}
- value={field.value ?? ""}
- // manually manage controlled input state
- onChange={(e) => {
- const value = e.target.value;
- field.onChange(
- value === "" ? undefined : Number(value),
- );
- }}
- type="number"
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- </>
- ) : isTextDataType(form.getValues("dataType")) ? null : (
- <div className="grid grid-flow-row gap-2">
- <FormField
- control={form.control}
- name="categories"
- render={() => (
- <>
- {fields.length > 0 && (
- <div className="mb-2 grid grid-cols-[1fr_3fr] items-center gap-2 text-left sm:grid-cols-[1fr_7fr]">
- <FormLabel className="grid grid-flow-col">
- Value
- <DocPopup
- description={`This is how the ${
- isCategoricalDataType(
- form.getValues("dataType"),
- )
- ? "category"
- : "boolean"
- } label is mapped to an integer value internally.`}
- />
- </FormLabel>
- <FormLabel>Label</FormLabel>
- </div>
- )}
- {fields.map((category, index) => (
- <div
- key={`${category.id}-langfuseObject`}
- className="mb-2 grid grid-cols-[1fr_3fr] gap-2 text-left sm:grid-cols-[1fr_7fr]"
- >
- <FormField
- control={form.control}
- name={`categories.${index}.value`}
- render={({ field }) => (
- <FormItem>
- <FormControl>
- <Input
- {...field}
- readOnly
- disabled
- inputMode="numeric"
- className="text-center"
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <div className="grid grid-cols-[1fr_auto] gap-2">
- <FormField
- control={form.control}
- name={`categories.${index}.label`}
- render={({ field }) => (
- <FormItem>
- <FormControl>
- <Input
- {...field}
- type="text"
- onBlur={(e) =>
- field.onChange(
- e.target.value.trimEnd(),
- )
- }
- readOnly={isBooleanDataType(
- form.getValues("dataType"),
- )}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- {isCategoricalDataType(
- form.getValues("dataType"),
- ) && (
- <Button
- onClick={() => remove(index)}
- variant="outline"
- size="icon"
- disabled={
- index === 0 || index !== fields.length - 1
- }
- >
- <Trash className="h-4 w-4" />
- </Button>
- )}
- </div>
- </div>
- ))}
- {isCategoricalDataType(
- form.getValues("dataType"),
- ) && (
- <div className="grid">
- <Button
- type="button"
- variant="secondary"
- disabled={
- isBooleanDataType(
- form.getValues("dataType"),
- ) && fields.length === 2
- }
- onClick={() =>
- append({
- label: "",
- value:
- fields.length > 0
- ? fields.reduce(
- (max, f) => Math.max(max, f.value),
- 0,
- ) + 1
- : 0,
- })
- }
- >
- Add category
- </Button>
- </div>
- )}
- </>
- )}
- />
- </div>
- )}
- <FormField
- control={form.control}
- name="description"
- render={({ field }) => (
- <>
- <FormItem>
- <FormLabel>Description (optional)</FormLabel>
- <FormControl>
- <Textarea
- {...field}
- placeholder="Provide an optional description of the score config..."
- value={field.value ?? undefined}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- </>
- )}
- />
- </DialogBody>
- <DialogFooter>
- <div className="flex w-full flex-col items-end gap-4">
- {formError ? (
- <p className="w-full text-center">
- <span className="font-bold">Error:</span> {formError}
- </p>
- ) : null}
- <Button
- type="submit"
- loading={
- createScoreConfig.isPending || updateScoreConfig.isPending
- }
- >
- Submit
- </Button>
- </div>
- </DialogFooter>
- </form>
- </Form>
- </DialogContent>
- </Dialog>
- </>
- );
-}
diff --git a/web/src/features/score-configs/components/UpsertScoreConfigDialogContent.stories.tsx b/web/src/features/score-configs/components/UpsertScoreConfigDialogContent.stories.tsx
new file mode 100644
index 0000000000..a4fdcdd74f
--- /dev/null
+++ b/web/src/features/score-configs/components/UpsertScoreConfigDialogContent.stories.tsx
@@ -0,0 +1,107 @@
+import { type ComponentProps } from "react";
+import { expect, fn, userEvent, within } from "storybook/test";
+
+import preview from "../../../../.storybook/preview";
+import { Dialog, DialogContent } from "@/src/components/ui/dialog";
+
+import { UpsertScoreConfigDialogContent } from "./UpsertScoreConfigDialogContent";
+
+const defaultArgs = {
+ mode: "create",
+ defaultValues: {
+ dataType: "NUMERIC",
+ minValue: undefined,
+ maxValue: undefined,
+ name: "",
+ },
+ onSubmit: fn().mockResolvedValue(undefined),
+ onFormSuccess: fn(),
+ isSubmitting: false,
+} satisfies ComponentProps<typeof UpsertScoreConfigDialogContent>;
+
+const meta = preview.meta({
+ component: UpsertScoreConfigDialogContent,
+ parameters: {
+ layout: "fullscreen",
+ },
+ decorators: [
+ (Story) => (
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="max-h-[90vh] overflow-y-auto">
+ <Story />
+ </DialogContent>
+ </Dialog>
+ ),
+ ],
+});
+
+export const Create = meta.story({
+ args: defaultArgs,
+});
+
+export const Edit = meta.story({
+ args: {
+ ...defaultArgs,
+ mode: "edit",
+ defaultValues: {
+ id: "config-1",
+ name: "Answer quality",
+ dataType: "NUMERIC",
+ minValue: 0,
+ maxValue: 1,
+ description: "How well the answer addresses the question.",
+ },
+ },
+});
+
+export const Submitting = meta.story({
+ args: {
+ ...defaultArgs,
+ isSubmitting: true,
+ },
+});
+
+export const AddsCategories = meta.story({
+ name: "(Test) Adds categories",
+ args: defaultArgs,
+ play: async ({ canvasElement }) => {
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.click(body.getByRole("combobox"));
+ const categoricalOption = (await body.findAllByText("CATEGORICAL")).find(
+ (element) => element.tagName !== "OPTION",
+ );
+ if (!categoricalOption) {
+ throw new Error("Categorical data type option is not available.");
+ }
+ await userEvent.click(categoricalOption);
+ await userEvent.click(body.getByRole("button", { name: "Add category" }));
+
+ await expect(
+ body.getByRole("button", { name: "Add category" }),
+ ).toBeVisible();
+ await expect(body.getAllByRole("textbox")).toHaveLength(6);
+ },
+});
+
+export const SubmitsConfig = meta.story({
+ name: "(Test) Submits config",
+ args: defaultArgs,
+ play: async ({ args, canvasElement }) => {
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.type(
+ body.getByRole("textbox", { name: "Name" }),
+ "Answer quality",
+ );
+ await userEvent.click(body.getByRole("button", { name: "Submit" }));
+
+ await expect(args.onSubmit).toHaveBeenCalledWith(
+ expect.objectContaining({
+ name: "Answer quality",
+ dataType: "NUMERIC",
+ }),
+ );
+ await expect(args.onFormSuccess).toHaveBeenCalledOnce();
+ },
+});
diff --git a/web/src/features/score-configs/components/UpsertScoreConfigDialogContent.tsx b/web/src/features/score-configs/components/UpsertScoreConfigDialogContent.tsx
new file mode 100644
index 0000000000..3207ce576f
--- /dev/null
+++ b/web/src/features/score-configs/components/UpsertScoreConfigDialogContent.tsx
@@ -0,0 +1,375 @@
+import { zodResolver } from "@hookform/resolvers/zod";
+import { ScoreDataTypeEnum, type ScoreConfigDataType } from "@langfuse/shared";
+import { useState } from "react";
+import { type UseFormReturn, useFieldArray, useForm } from "react-hook-form";
+
+import { Button } from "@/src/components/ui/button";
+import {
+ DialogBody,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/src/components/ui/dialog";
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/src/components/ui/form";
+import { Input } from "@/src/components/ui/input";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/src/components/ui/select";
+import { Textarea } from "@/src/components/ui/textarea";
+import DocPopup from "@/src/components/layouts/doc-popup";
+import {
+ isBooleanDataType,
+ isCategoricalDataType,
+ isNumericDataType,
+ isTextDataType,
+} from "@/src/features/scores/lib/helpers";
+import {
+ createConfigSchema,
+ type CreateConfig,
+ type UpdateConfig,
+ updateConfigSchema,
+} from "@/src/features/score-configs/lib/upsertFormTypes";
+import { validateScoreConfigUpsertFormInput } from "@/src/features/score-configs/lib/validateScoreConfigUpsertFormInput";
+import { Trash } from "lucide-react";
+
+type SharedUpsertScoreConfigDialogContentProps = {
+ onSubmit: (values: CreateConfig | UpdateConfig) => Promise<void>;
+ onFormSuccess: () => void;
+ isSubmitting: boolean;
+};
+
+export type UpsertScoreConfigDialogContentProps =
+ | (SharedUpsertScoreConfigDialogContentProps & {
+ mode: "create";
+ defaultValues: CreateConfig;
+ })
+ | (SharedUpsertScoreConfigDialogContentProps & {
+ mode: "edit";
+ defaultValues: UpdateConfig;
+ });
+
+export function UpsertScoreConfigDialogContent({
+ mode,
+ defaultValues,
+ onSubmit,
+ onFormSuccess,
+ isSubmitting,
+}: UpsertScoreConfigDialogContentProps) {
+ const [formError, setFormError] = useState<string | null>(null);
+ const form = useForm({
+ resolver: zodResolver(
+ mode === "edit" ? updateConfigSchema : createConfigSchema,
+ ),
+ defaultValues,
+ }) as UseFormReturn<CreateConfig | UpdateConfig>;
+
+ const { fields, append, remove, replace } = useFieldArray({
+ control: form.control,
+ name: "categories",
+ });
+ const dataType = form.watch("dataType");
+
+ async function handleSubmit(values: CreateConfig | UpdateConfig) {
+ const error = validateScoreConfigUpsertFormInput(values);
+ setFormError(error);
+ if (error) return;
+
+ try {
+ await onSubmit(values);
+ form.reset();
+ onFormSuccess();
+ } catch (error) {
+ setFormError(
+ error instanceof Error
+ ? error.message
+ : "An error occurred while submitting config.",
+ );
+ }
+ }
+
+ return (
+ <>
+ <DialogHeader>
+ <DialogTitle>
+ {mode === "edit" ? "Update score config" : "Add new score config"}
+ </DialogTitle>
+ </DialogHeader>
+ <Form {...form}>
+ <form className="space-y-6" onSubmit={form.handleSubmit(handleSubmit)}>
+ <DialogBody>
+ <FormField
+ control={form.control}
+ name="name"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Name</FormLabel>
+ <FormControl>
+ <Input
+ {...field}
+ type="text"
+ onBlur={(e) => field.onChange(e.target.value.trimEnd())}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="dataType"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Data type</FormLabel>
+ <Select
+ disabled={mode === "edit"}
+ defaultValue={field.value}
+ onValueChange={(value) => {
+ const nextDataType = value as ScoreConfigDataType;
+ field.onChange(nextDataType);
+ form.clearErrors();
+ if (isNumericDataType(nextDataType)) {
+ form.setValue("categories", undefined);
+ } else if (isTextDataType(nextDataType)) {
+ form.setValue("categories", undefined);
+ form.setValue("minValue", undefined);
+ form.setValue("maxValue", undefined);
+ } else {
+ form.setValue("minValue", undefined);
+ form.setValue("maxValue", undefined);
+ if (isBooleanDataType(nextDataType)) {
+ replace([
+ { label: "True", value: 1 },
+ { label: "False", value: 0 },
+ ]);
+ } else {
+ replace([{ label: "", value: 0 }]);
+ }
+ }
+ }}
+ >
+ <FormControl>
+ <SelectTrigger>
+ <SelectValue placeholder="Select a data type" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ {Object.values(ScoreDataTypeEnum)
+ .filter((value) => value !== "CORRECTION")
+ .map((value) => (
+ <SelectItem value={value} key={value}>
+ {value}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ {isNumericDataType(dataType) ? (
+ <>
+ <FormField
+ control={form.control}
+ name="minValue"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Minimum (optional) </FormLabel>
+ <FormControl>
+ <Input
+ {...field}
+ value={field.value ?? ""}
+ onChange={(e) => {
+ const value = e.target.value;
+ field.onChange(
+ value === "" ? undefined : Number(value),
+ );
+ }}
+ type="number"
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="maxValue"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Maximum (optional)</FormLabel>
+ <FormControl>
+ <Input
+ {...field}
+ value={field.value ?? ""}
+ onChange={(e) => {
+ const value = e.target.value;
+ field.onChange(
+ value === "" ? undefined : Number(value),
+ );
+ }}
+ type="number"
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </>
+ ) : isTextDataType(dataType) ? null : (
+ <div className="grid grid-flow-row gap-2">
+ <FormField
+ control={form.control}
+ name="categories"
+ render={() => (
+ <>
+ {fields.length > 0 && (
+ <div className="mb-2 grid grid-cols-[1fr_3fr] items-center gap-2 text-left sm:grid-cols-[1fr_7fr]">
+ <FormLabel className="grid grid-flow-col">
+ Value
+ <DocPopup
+ description={`This is how the ${
+ isCategoricalDataType(dataType)
+ ? "category"
+ : "boolean"
+ } label is mapped to an integer value internally.`}
+ />
+ </FormLabel>
+ <FormLabel>Label</FormLabel>
+ </div>
+ )}
+ {fields.map((category, index) => (
+ <div
+ key={`${category.id}-langfuseObject`}
+ className="mb-2 grid grid-cols-[1fr_3fr] gap-2 text-left sm:grid-cols-[1fr_7fr]"
+ >
+ <FormField
+ control={form.control}
+ name={`categories.${index}.value`}
+ render={({ field }) => (
+ <FormItem>
+ <FormControl>
+ <Input
+ {...field}
+ readOnly
+ disabled
+ inputMode="numeric"
+ className="text-center"
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <div className="grid grid-cols-[1fr_auto] gap-2">
+ <FormField
+ control={form.control}
+ name={`categories.${index}.label`}
+ render={({ field }) => (
+ <FormItem>
+ <FormControl>
+ <Input
+ {...field}
+ type="text"
+ onBlur={(e) =>
+ field.onChange(e.target.value.trimEnd())
+ }
+ readOnly={isBooleanDataType(dataType)}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ {isCategoricalDataType(dataType) && (
+ <Button
+ type="button"
+ onClick={() => remove(index)}
+ variant="outline"
+ size="icon"
+ disabled={
+ index === 0 || index !== fields.length - 1
+ }
+ >
+ <Trash className="h-4 w-4" />
+ </Button>
+ )}
+ </div>
+ </div>
+ ))}
+ {isCategoricalDataType(dataType) && (
+ <div className="grid">
+ <Button
+ type="button"
+ variant="secondary"
+ disabled={
+ isBooleanDataType(dataType) && fields.length === 2
+ }
+ onClick={() =>
+ append({
+ label: "",
+ value:
+ fields.length > 0
+ ? fields.reduce(
+ (max, field) =>
+ Math.max(max, field.value),
+ 0,
+ ) + 1
+ : 0,
+ })
+ }
+ >
+ Add category
+ </Button>
+ </div>
+ )}
+ </>
+ )}
+ />
+ </div>
+ )}
+ <FormField
+ control={form.control}
+ name="description"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Description (optional)</FormLabel>
+ <FormControl>
+ <Textarea
+ {...field}
+ placeholder="Provide an optional description of the score config..."
+ value={field.value ?? undefined}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </DialogBody>
+ <DialogFooter>
+ <div className="flex w-full flex-col items-end gap-4">
+ {formError ? (
+ <p className="w-full text-center">
+ <span className="font-bold">Error:</span> {formError}
+ </p>
+ ) : null}
+ <Button type="submit" loading={isSubmitting}>
+ Submit
+ </Button>
+ </div>
+ </DialogFooter>
+ </form>
+ </Form>
+ </>
+ );
+}
diff --git a/web/src/features/score-configs/components/UpsertScoreConfigDialogController.tsx b/web/src/features/score-configs/components/UpsertScoreConfigDialogController.tsx
new file mode 100644
index 0000000000..964756e78b
--- /dev/null
+++ b/web/src/features/score-configs/components/UpsertScoreConfigDialogController.tsx
@@ -0,0 +1,111 @@
+import { ScoreDataTypeEnum } from "@langfuse/shared";
+import { type ReactNode, useState } from "react";
+
+import {
+ Dialog,
+ DialogContent,
+ DialogTrigger,
+} from "@/src/components/ui/dialog";
+import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
+import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
+import { UpsertScoreConfigDialogContent } from "@/src/features/score-configs/components/UpsertScoreConfigDialogContent";
+import {
+ type CreateConfig,
+ type UpdateConfig,
+} from "@/src/features/score-configs/lib/upsertFormTypes";
+import { api } from "@/src/utils/api";
+
+type UpsertScoreConfigDialogControllerProps = {
+ projectId: string;
+ children: (control: {
+ disabled: { reason: string } | undefined;
+ isSubmitting: boolean;
+ Trigger: typeof DialogTrigger;
+ }) => ReactNode;
+} & ({ mode: "create" } | { mode: "edit"; defaultValues: UpdateConfig });
+
+const createDefaultValues: CreateConfig = {
+ dataType: ScoreDataTypeEnum.NUMERIC,
+ minValue: undefined,
+ maxValue: undefined,
+ name: "",
+};
+
+export function UpsertScoreConfigDialogController(
+ props: UpsertScoreConfigDialogControllerProps,
+) {
+ const { children, projectId, mode } = props;
+ const [open, setOpen] = useState(false);
+ const capture = usePostHogClientCapture();
+ const hasAccess = useHasProjectAccess({
+ projectId,
+ scope: "scoreConfigs:CUD",
+ });
+ const utils = api.useUtils();
+ const createScoreConfig = api.scoreConfigs.create.useMutation({
+ onSuccess: () => utils.scoreConfigs.invalidate(),
+ });
+ const updateScoreConfig = api.scoreConfigs.update.useMutation({
+ onSuccess: () => utils.scoreConfigs.invalidate(),
+ });
+
+ const isSubmitting =
+ createScoreConfig.isPending || updateScoreConfig.isPending;
+ const disabled = hasAccess
+ ? undefined
+ : { reason: `You don't have permission to ${mode} score configs.` };
+
+ async function handleSubmit(values: CreateConfig | UpdateConfig) {
+ if (mode === "edit") {
+ await updateScoreConfig.mutateAsync({
+ ...values,
+ projectId,
+ id: props.defaultValues.id,
+ description: values.description ?? null,
+ categories: values.categories?.length ? values.categories : undefined,
+ });
+ capture("score_configs:update_form_submit", {
+ dataType: values.dataType,
+ });
+ return;
+ }
+
+ await createScoreConfig.mutateAsync({
+ projectId,
+ ...values,
+ description: values.description ?? null,
+ categories: values.categories?.length ? values.categories : undefined,
+ });
+ capture("score_configs:create_form_submit", {
+ dataType: values.dataType,
+ });
+ }
+
+ const content =
+ mode === "edit" ? (
+ <UpsertScoreConfigDialogContent
+ mode="edit"
+ defaultValues={props.defaultValues}
+ onSubmit={handleSubmit}
+ onFormSuccess={() => setOpen(false)}
+ isSubmitting={isSubmitting}
+ />
+ ) : (
+ <UpsertScoreConfigDialogContent
+ mode="create"
+ defaultValues={createDefaultValues}
+ onSubmit={handleSubmit}
+ onFormSuccess={() => setOpen(false)}
+ isSubmitting={isSubmitting}
+ />
+ );
+
+ return (
+ <Dialog open={hasAccess && open} onOpenChange={setOpen}>
+ {children({ disabled, isSubmitting, Trigger: DialogTrigger })}
+ <DialogContent className="max-h-[90vh] overflow-y-auto">
+ {hasAccess && open ? content : null}
+ </DialogContent>
+ </Dialog>
+ );
+}