Skip to content

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

feat(annotation-queues): create queues from trace table (#16355)

Ben Bachemb08727aea52c
diff --git a/web/src/components/table/use-cases/traces.tsx b/web/src/components/table/use-cases/traces.tsx
index 13ad5e0533..719e0890cc 100644
--- a/web/src/components/table/use-cases/traces.tsx
+++ b/web/src/components/table/use-cases/traces.tsx
@@ -105,6 +105,7 @@ import { usePeekTableState } from "@/src/components/table/peek/contexts/PeekTabl
import { useScoreColumns } from "@/src/features/scores/hooks/useScoreColumns";
import { scoreFilters } from "@/src/features/scores/lib/scoreColumns";
import TagList from "@/src/features/tag/components/TagList";
+import { AddTracesToAnnotationQueueDialogController } from "@/src/features/annotation-queues/components/AddTracesToAnnotationQueueDialogController";
export type TracesTableRow = {
// Shown by default
@@ -616,13 +617,11 @@ export default function TracesTable({
setSelectedRows({});
};
- const displayCount = totalCountQuery.isPending ? (
- <span className="inline-block font-mono">...</span>
- ) : selectAll ? (
- compactNumberFormatter(totalCountQuery.data?.totalCount)
- ) : (
- compactNumberFormatter(Object.keys(selectedRows).length)
- );
+ const displayCount = totalCountQuery.isPending
+ ? "..."
+ : selectAll
+ ? compactNumberFormatter(totalCountQuery.data?.totalCount)
+ : compactNumberFormatter(Object.keys(selectedRows).length);
// Select-all deletes persist the raw filterState into the batch action, but
// comment filters resolve via Postgres at read time and the server rejects
@@ -656,9 +655,8 @@ export default function TracesTable({
id: ActionId.TraceAddToAnnotationQueue,
type: BatchActionType.Create,
label: "Add to Annotation Queue",
- description: "Add selected traces to an annotation queue.",
- targetLabel: "Annotation Queue",
- execute: handleAddToAnnotationQueue,
+ description: `Add ${displayCount} selected traces to an annotation queue.`,
+ customDialog: true,
accessCheck: {
scope: "annotationQueues:CUD",
},
@@ -1476,17 +1474,34 @@ export default function TracesTable({
columnsWithCustomSelect={["traceName", "traceTags"]}
actionButtons={[
selectedTraceIds.length > 0 || selectAll ? (
- <TableActionMenu
+ <AddTracesToAnnotationQueueDialogController
key="traces-multi-select-actions"
projectId={projectId}
- actions={tableActions}
- tableName={BatchExportTableName.Traces}
- selectedCount={selectedTraceCount}
- onClearSelection={() => {
+ onSuccess={() => {
setSelectedRows({});
setSelectAll(false);
}}
- />
+ description={`Add ${displayCount} selected traces to an annotation queue.`}
+ onAddToQueue={handleAddToAnnotationQueue}
+ >
+ {({ openDialog }) => (
+ <TableActionMenu
+ projectId={projectId}
+ actions={tableActions}
+ tableName={BatchExportTableName.Traces}
+ selectedCount={selectedTraceCount}
+ onClearSelection={() => {
+ setSelectedRows({});
+ setSelectAll(false);
+ }}
+ onCustomAction={(actionType) => {
+ if (actionType === ActionId.TraceAddToAnnotationQueue) {
+ openDialog();
+ }
+ }}
+ />
+ )}
+ </AddTracesToAnnotationQueueDialogController>
) : null,
<BatchExportTableButton
{...{
diff --git a/web/src/features/annotation-queues/components/AddTracesToAnnotationQueueDialogController.tsx b/web/src/features/annotation-queues/components/AddTracesToAnnotationQueueDialogController.tsx
new file mode 100644
index 0000000000..74e27c5cc5
--- /dev/null
+++ b/web/src/features/annotation-queues/components/AddTracesToAnnotationQueueDialogController.tsx
@@ -0,0 +1,144 @@
+import { type ReactNode, useState } from "react";
+import { ActionId, BatchExportTableName } from "@langfuse/shared";
+import { api } from "@/src/utils/api";
+import { Dialog, DialogContent } from "@/src/components/ui/dialog";
+import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
+import { useEntitlementLimit } from "@/src/features/entitlements/hooks";
+import { AddTracesToAnnotationQueueSelectDialogContent } from "@/src/features/annotation-queues/components/AddTracesToAnnotationQueueSelectDialogContent";
+import { AnnotationQueueFormDialogController } from "@/src/features/annotation-queues/components/AnnotationQueueFormDialogController";
+
+type AddTracesToAnnotationQueueDialogControllerProps = {
+ projectId: string;
+ onSuccess: () => void;
+ description: string;
+ onAddToQueue: (params: {
+ projectId: string;
+ targetId: string;
+ }) => Promise<void>;
+ children: (control: {
+ disabled: { reason: string } | undefined;
+ openDialog: () => void;
+ }) => ReactNode;
+};
+
+export function AddTracesToAnnotationQueueDialogController({
+ projectId,
+ onSuccess,
+ description,
+ onAddToQueue,
+ children,
+}: AddTracesToAnnotationQueueDialogControllerProps) {
+ const [open, setOpen] = useState(false);
+ const [newQueueId, setNewQueueId] = useState<string>();
+
+ const hasQueueAccess = useHasProjectAccess({
+ projectId,
+ scope: "annotationQueues:CUD",
+ });
+ const queueLimit = useEntitlementLimit("annotation-queue-count");
+ const disabled = hasQueueAccess
+ ? undefined
+ : {
+ reason: "You don't have permission to add traces to annotation queues.",
+ };
+ const openDialog = () => {
+ if (!hasQueueAccess) return;
+ setNewQueueId(undefined);
+ setOpen(true);
+ };
+
+ const queueOptionsQuery = api.annotationQueues.allNamesAndIds.useQuery(
+ { projectId },
+ { enabled: open && hasQueueAccess },
+ );
+
+ const queueCountQuery = api.annotationQueues.count.useQuery(
+ { projectId },
+ { enabled: open && hasQueueAccess },
+ );
+
+ const isInProgress = api.table.getIsBatchActionInProgress.useQuery(
+ {
+ projectId,
+ tableName: BatchExportTableName.Traces,
+ actionId: ActionId.TraceAddToAnnotationQueue,
+ },
+ {
+ enabled: open,
+ refetchInterval: 2 * 60 * 1000,
+ },
+ );
+
+ const atQueueLimit =
+ typeof queueLimit === "number" &&
+ typeof queueCountQuery.data === "number" &&
+ queueCountQuery.data >= queueLimit;
+
+ const createQueueState = !hasQueueAccess
+ ? ({
+ status: "disabled",
+ reason: "You don't have permission to create annotation queues.",
+ } as const)
+ : atQueueLimit
+ ? ({
+ status: "disabled",
+ reason: "Maximum number of annotation queues reached for your plan.",
+ } as const)
+ : ({ status: "enabled" } as const);
+
+ const handleSelectSubmit = async (targetId: string) => {
+ await onAddToQueue({ projectId, targetId });
+ onSuccess();
+ setOpen(false);
+ };
+
+ const queueOptions = queueOptionsQuery.data ?? [];
+ const initialTargetId =
+ newQueueId ?? (queueOptions.length === 1 ? queueOptions[0].id : "");
+
+ return (
+ <AnnotationQueueFormDialogController
+ mode="create"
+ projectId={projectId}
+ onSuccess={setNewQueueId}
+ >
+ {({ openDialog: openCreateDialog }) => (
+ <Dialog open={hasQueueAccess && open} onOpenChange={setOpen}>
+ {children({ disabled, openDialog })}
+ <DialogContent className="sm:max-w-md">
+ {open ? (
+ <AddTracesToAnnotationQueueSelectDialogContent
+ key={
+ queueOptionsQuery.isLoading
+ ? "loading"
+ : (newQueueId ?? "ready")
+ }
+ description={description}
+ initialTargetId={initialTargetId}
+ queueOptionsState={
+ queueOptionsQuery.isLoading
+ ? { status: "loading" }
+ : {
+ status: "ready",
+ options: queueOptions,
+ }
+ }
+ onSubmit={handleSelectSubmit}
+ onCreateNewQueue={openCreateDialog}
+ createQueueState={createQueueState}
+ hasAccess={hasQueueAccess}
+ batchActionState={
+ isInProgress.isLoading
+ ? { status: "checking" }
+ : isInProgress.data
+ ? { status: "inProgress" }
+ : { status: "ready" }
+ }
+ />
+ ) : null}
+ </DialogContent>
+ </Dialog>
+ )}
+ </AnnotationQueueFormDialogController>
+ );
+}
diff --git a/web/src/features/annotation-queues/components/AddTracesToAnnotationQueueSelectDialogContent.stories.tsx b/web/src/features/annotation-queues/components/AddTracesToAnnotationQueueSelectDialogContent.stories.tsx
new file mode 100644
index 0000000000..66cb4a5b5f
--- /dev/null
+++ b/web/src/features/annotation-queues/components/AddTracesToAnnotationQueueSelectDialogContent.stories.tsx
@@ -0,0 +1,207 @@
+import preview from "../../../../.storybook/preview";
+import { Dialog, DialogContent } from "@/src/components/ui/dialog";
+import { AnnotationQueueFormDialogContent } from "@/src/features/annotation-queues/components/AnnotationQueueFormDialogContent";
+import { type CreateQueueWithAssignments } from "@langfuse/shared";
+import { useState } from "react";
+import { flushSync } from "react-dom";
+import { expect, fn, userEvent, waitFor, within } from "storybook/test";
+
+import { AddTracesToAnnotationQueueSelectDialogContent } from "./AddTracesToAnnotationQueueSelectDialogContent";
+
+const meta = preview.meta({
+ component: AddTracesToAnnotationQueueSelectDialogContent,
+ parameters: {
+ layout: "fullscreen",
+ },
+});
+
+export const CreateQueueInStackedDialog = meta.story({
+ name: "(Test) Create Queue In Stacked Dialog",
+ render: () => {
+ const [createOpen, setCreateOpen] = useState(false);
+ const [selectedQueueId, setSelectedQueueId] = useState("queue-1");
+ const [queueOptions, setQueueOptions] = useState([
+ { id: "queue-1", name: "Support review" },
+ { id: "queue-2", name: "Quality audit" },
+ ]);
+ const handleCreate = (data: CreateQueueWithAssignments) => {
+ const queue = { id: `queue-${queueOptions.length + 1}`, name: data.name };
+ flushSync(() => setQueueOptions((current) => current.concat(queue)));
+ setSelectedQueueId(queue.id);
+ setCreateOpen(false);
+ };
+
+ return (
+ <>
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="sm:max-w-md">
+ <AddTracesToAnnotationQueueSelectDialogContent
+ key={selectedQueueId}
+ description="Add 12 selected traces to an annotation queue."
+ initialTargetId={selectedQueueId}
+ queueOptionsState={{ status: "ready", options: queueOptions }}
+ onSubmit={fn()}
+ onCreateNewQueue={() => setCreateOpen(true)}
+ createQueueState={{ status: "enabled" }}
+ hasAccess={true}
+ batchActionState={{ status: "ready" }}
+ />
+ </DialogContent>
+ </Dialog>
+ <Dialog open={createOpen} onOpenChange={setCreateOpen}>
+ <DialogContent className="max-h-[90vh] overflow-y-auto sm:max-w-lg">
+ {createOpen ? (
+ <AnnotationQueueFormDialogContent
+ mode="create"
+ initialValues={{
+ name: "",
+ description: "",
+ scoreConfigIds: ["config-1"],
+ newAssignmentUserIds: [],
+ }}
+ scoreConfigs={[
+ {
+ id: "config-1",
+ name: "Helpfulness",
+ dataType: "NUMERIC",
+ isArchived: false,
+ },
+ {
+ id: "config-2",
+ name: "Correctness",
+ dataType: "BOOLEAN",
+ isArchived: false,
+ },
+ ]}
+ projectId="project-1"
+ queueNames={queueOptions.map((queue) => queue.name)}
+ onManageScoreConfigsClick={fn()}
+ hasQueueAssignmentsReadAccess={false}
+ isSubmitting={false}
+ onSubmit={handleCreate}
+ submitLabel="Create queue"
+ />
+ ) : null}
+ </DialogContent>
+ </Dialog>
+ </>
+ );
+ },
+ play: async ({ canvasElement }) => {
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.click(
+ await body.findByRole("button", { name: "Create new queue" }),
+ );
+ await waitFor(() =>
+ expect(body.getAllByRole("dialog", { hidden: true })).toHaveLength(2),
+ );
+
+ await userEvent.type(
+ body.getByRole("textbox", { name: "Name" }),
+ "Escalation review",
+ );
+ await userEvent.click(body.getByRole("button", { name: "Create queue" }));
+
+ await waitFor(() =>
+ expect(body.getAllByRole("dialog", { hidden: true })).toHaveLength(1),
+ );
+ await expect(body.getByRole("combobox")).toHaveTextContent(
+ "Escalation review",
+ );
+ },
+});
+
+export const Empty = meta.story({
+ render: () => {
+ return (
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="sm:max-w-md">
+ <AddTracesToAnnotationQueueSelectDialogContent
+ description="Add 3 selected traces to an annotation queue."
+ initialTargetId=""
+ queueOptionsState={{ status: "ready", options: [] }}
+ onSubmit={fn()}
+ onCreateNewQueue={fn()}
+ createQueueState={{ status: "enabled" }}
+ hasAccess={true}
+ batchActionState={{ status: "ready" }}
+ />
+ </DialogContent>
+ </Dialog>
+ );
+ },
+});
+
+export const Loading = meta.story({
+ render: () => {
+ return (
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="sm:max-w-md">
+ <AddTracesToAnnotationQueueSelectDialogContent
+ description="Add 3 selected traces to an annotation queue."
+ initialTargetId=""
+ queueOptionsState={{ status: "loading" }}
+ onSubmit={fn()}
+ onCreateNewQueue={fn()}
+ createQueueState={{ status: "enabled" }}
+ hasAccess={true}
+ batchActionState={{ status: "checking" }}
+ />
+ </DialogContent>
+ </Dialog>
+ );
+ },
+});
+
+export const CreateDisabled = meta.story({
+ render: () => {
+ return (
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="sm:max-w-md">
+ <AddTracesToAnnotationQueueSelectDialogContent
+ description="Add 8 selected traces to an annotation queue."
+ initialTargetId="queue-1"
+ queueOptionsState={{
+ status: "ready",
+ options: [{ id: "queue-1", name: "Only queue" }],
+ }}
+ onSubmit={fn()}
+ onCreateNewQueue={fn()}
+ createQueueState={{
+ status: "disabled",
+ reason:
+ "Maximum number of annotation queues reached for your plan.",
+ }}
+ hasAccess={true}
+ batchActionState={{ status: "ready" }}
+ />
+ </DialogContent>
+ </Dialog>
+ );
+ },
+});
+
+export const BatchActionInProgress = meta.story({
+ render: () => {
+ return (
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="sm:max-w-md">
+ <AddTracesToAnnotationQueueSelectDialogContent
+ description="Add 50 selected traces to an annotation queue."
+ initialTargetId="queue-1"
+ queueOptionsState={{
+ status: "ready",
+ options: [{ id: "queue-1", name: "Support review" }],
+ }}
+ onSubmit={fn()}
+ onCreateNewQueue={fn()}
+ createQueueState={{ status: "enabled" }}
+ hasAccess={true}
+ batchActionState={{ status: "inProgress" }}
+ />
+ </DialogContent>
+ </Dialog>
+ );
+ },
+});
diff --git a/web/src/features/annotation-queues/components/AddTracesToAnnotationQueueSelectDialogContent.tsx b/web/src/features/annotation-queues/components/AddTracesToAnnotationQueueSelectDialogContent.tsx
new file mode 100644
index 0000000000..6e26db5fb7
--- /dev/null
+++ b/web/src/features/annotation-queues/components/AddTracesToAnnotationQueueSelectDialogContent.tsx
@@ -0,0 +1,167 @@
+import { ActionButton } from "@/src/components/ActionButton";
+import Spinner from "@/src/components/design-system/Spinner/Spinner";
+import { Button } from "@/src/components/ui/button";
+import {
+ DialogBody,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/src/components/ui/dialog";
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormMessage,
+} from "@/src/components/ui/form";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/src/components/ui/select";
+import { useState } from "react";
+import { useForm } from "react-hook-form";
+
+type AnnotationQueueSelectOption = {
+ id: string;
+ name: string;
+};
+
+type SelectFormValues = {
+ targetId: string;
+};
+
+type AddTracesToAnnotationQueueSelectDialogContentProps = {
+ description: string;
+ initialTargetId: string;
+ queueOptionsState:
+ | { status: "loading" }
+ | { status: "ready"; options: AnnotationQueueSelectOption[] };
+ onSubmit: (targetId: string) => Promise<void>;
+ onCreateNewQueue: () => void;
+ createQueueState:
+ | { status: "enabled" }
+ | { status: "disabled"; reason: string };
+ hasAccess: boolean;
+ batchActionState:
+ | { status: "checking" }
+ | { status: "inProgress" }
+ | { status: "ready" };
+};
+
+export function AddTracesToAnnotationQueueSelectDialogContent({
+ description,
+ initialTargetId,
+ queueOptionsState,
+ onSubmit,
+ onCreateNewQueue,
+ createQueueState,
+ hasAccess,
+ batchActionState,
+}: AddTracesToAnnotationQueueSelectDialogContentProps) {
+ const [isSubmitting, setIsSubmitting] = useState(false);
+ const form = useForm<SelectFormValues>({
+ defaultValues: { targetId: initialTargetId },
+ });
+ const isQueueOptionsLoading = queueOptionsState.status === "loading";
+ const queueOptions =
+ queueOptionsState.status === "ready" ? queueOptionsState.options : [];
+
+ const handleSubmit = async ({ targetId }: SelectFormValues) => {
+ if (!targetId) return;
+
+ setIsSubmitting(true);
+ try {
+ await onSubmit(targetId);
+ } finally {
+ setIsSubmitting(false);
+ }
+ };
+
+ return (
+ <>
+ <DialogHeader variant="action">
+ <DialogTitle>Add to Annotation Queue</DialogTitle>
+ </DialogHeader>
+ <Form {...form}>
+ <form className="space-y-6" onSubmit={form.handleSubmit(handleSubmit)}>
+ <DialogBody>
+ <div className="flex items-center justify-between gap-4">
+ <DialogDescription>{description}</DialogDescription>
+ <Button
+ type="button"
+ variant="link"
+ className="h-auto shrink-0 p-0 text-sm"
+ disabled={createQueueState.status === "disabled"}
+ title={
+ createQueueState.status === "disabled"
+ ? createQueueState.reason
+ : undefined
+ }
+ onClick={onCreateNewQueue}
+ >
+ Create new queue
+ </Button>
+ </div>
+ <FormField
+ control={form.control}
+ name="targetId"
+ render={({ field }) => (
+ <FormItem>
+ <Select
+ onValueChange={field.onChange}
+ value={field.value}
+ disabled={isQueueOptionsLoading}
+ >
+ <FormControl>
+ <SelectTrigger>
+ <SelectValue
+ placeholder={
+ isQueueOptionsLoading ? "Loading..." : "Select..."
+ }
+ />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ {queueOptions.map((option) => (
+ <SelectItem key={option.id} value={option.id}>
+ {option.name}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </DialogBody>
+ <DialogFooter variant="action">
+ {batchActionState.status === "inProgress" ? (
+ <div className="flex items-center gap-1">
+ <Spinner size="xxs" />
+ <p className="text-muted-foreground text-sm">
+ Batch action is in progress, please wait.
+ </p>
+ </div>
+ ) : null}
+ <ActionButton
+ type="submit"
+ hasAccess={hasAccess}
+ loading={batchActionState.status === "checking" || isSubmitting}
+ disabled={
+ isSubmitting ||
+ batchActionState.status !== "ready" ||
+ !form.watch("targetId")
+ }
+ >
+ Confirm
+ </ActionButton>
+ </DialogFooter>
+ </form>
+ </Form>
+ </>
+ );
+}
diff --git a/web/src/features/annotation-queues/components/AnnotationQueueFormDialogContent.stories.tsx b/web/src/features/annotation-queues/components/AnnotationQueueFormDialogContent.stories.tsx
new file mode 100644
index 0000000000..a641437dba
--- /dev/null
+++ b/web/src/features/annotation-queues/components/AnnotationQueueFormDialogContent.stories.tsx
@@ -0,0 +1,159 @@
+import preview from "../../../../.storybook/preview";
+import { Dialog, DialogContent } from "@/src/components/ui/dialog";
+import { type ScoreConfigDomain } from "@langfuse/shared";
+import { expect, fn, userEvent, waitFor, within } from "storybook/test";
+
+import { AnnotationQueueFormDialogContent } from "./AnnotationQueueFormDialogContent";
+
+const scoreConfigs = [
+ {
+ id: "config-1",
+ name: "Helpfulness",
+ dataType: "NUMERIC",
+ isArchived: false,
+ },
+ {
+ id: "config-2",
+ name: "Correctness",
+ dataType: "BOOLEAN",
+ isArchived: false,
+ },
+ {
+ id: "config-3",
+ name: "Legacy quality",
+ dataType: "CATEGORICAL",
+ isArchived: true,
+ },
+] satisfies Pick<
+ ScoreConfigDomain,
+ "id" | "name" | "dataType" | "isArchived"
+>[];
+
+const meta = preview.meta({
+ component: AnnotationQueueFormDialogContent,
+ parameters: {
+ layout: "fullscreen",
+ },
+});
+
+export const Create = meta.story({
+ name: "(Test) Create",
+ render: () => {
+ return (
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="max-h-[90vh] overflow-y-auto">
+ <AnnotationQueueFormDialogContent
+ mode="create"
+ initialValues={{
+ name: "",
+ scoreConfigIds: ["config-1"],
+ newAssignmentUserIds: [],
+ }}
+ scoreConfigs={scoreConfigs}
+ projectId="project-1"
+ queueNames={[]}
+ onManageScoreConfigsClick={fn()}
+ hasQueueAssignmentsReadAccess={false}
+ isSubmitting={false}
+ onSubmit={fn()}
+ submitLabel="Create queue"
+ />
+ </DialogContent>
+ </Dialog>
+ );
+ },
+ play: async ({ canvasElement }) => {
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.click(
+ await body.findByRole("button", { name: /Select.*Helpfulness/ }),
+ );
+
+ await waitFor(() =>
+ expect(body.getByPlaceholderText("Value")).toBeVisible(),
+ );
+ },
+});
+
+export const Edit = meta.story({
+ render: () => {
+ return (
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="max-h-[90vh] overflow-y-auto">
+ <AnnotationQueueFormDialogContent
+ mode="edit"
+ initialValues={{
+ name: "Support review queue",
+ description: "Weekly support trace review",
+ scoreConfigIds: ["config-1", "config-2"],
+ newAssignmentUserIds: [],
+ }}
+ scoreConfigs={scoreConfigs}
+ projectId="project-1"
+ queueId="queue-1"
+ queueNames={[]}
+ onManageScoreConfigsClick={fn()}
+ hasQueueAssignmentsReadAccess={false}
+ isSubmitting={false}
+ onSubmit={fn()}
+ submitLabel="Save queue"
+ />
+ </DialogContent>
+ </Dialog>
+ );
+ },
+});
+
+export const Submitting = meta.story({
+ render: () => {
+ return (
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="max-h-[90vh] overflow-y-auto">
+ <AnnotationQueueFormDialogContent
+ mode="create"
+ initialValues={{
+ name: "New queue",
+ scoreConfigIds: ["config-1"],
+ newAssignmentUserIds: [],
+ }}
+ scoreConfigs={scoreConfigs}
+ projectId="project-1"
+ queueNames={[]}
+ onManageScoreConfigsClick={fn()}
+ hasQueueAssignmentsReadAccess={false}
+ isSubmitting={true}
+ onSubmit={fn()}
+ submitLabel="Create queue"
+ />
+ </DialogContent>
+ </Dialog>
+ );
+ },
+});
+
+export const WithAdvancedOpen = meta.story({
+ render: () => {
+ return (
+ <Dialog open onOpenChange={fn()}>
+ <DialogContent className="max-h-[90vh] overflow-y-auto">
+ <AnnotationQueueFormDialogContent
+ mode="create"
+ initialValues={{
+ name: "Team queue",
+ scoreConfigIds: ["config-1"],
+ newAssignmentUserIds: ["user-1"],
+ }}
+ scoreConfigs={scoreConfigs}
+ projectId="project-1"
+ queueNames={[]}
+ onManageScoreConfigsClick={fn()}
+ hasQueueAssignmentsReadAccess={true}
+ isSubmitting={false}
+ onSubmit={fn()}
+ submitLabel="Create queue"
+ />
+ </DialogContent>
+ </Dialog>
+ );
+ },
+});
diff --git a/web/src/features/annotation-queues/components/AnnotationQueueFormDialogContent.tsx b/web/src/features/annotation-queues/components/AnnotationQueueFormDialogContent.tsx
new file mode 100644
index 0000000000..60db454cc4
--- /dev/null
+++ b/web/src/features/annotation-queues/components/AnnotationQueueFormDialogContent.tsx
@@ -0,0 +1,278 @@
+import { Button } from "@/src/components/ui/button";
+import {
+ DialogBody,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/src/components/ui/dialog";
+import {
+ FormControl,
+ FormDescription,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/src/components/ui/form";
+import { Input } from "@/src/components/ui/input";
+import { Textarea } from "@/src/components/ui/textarea";
+import { Form } from "@/src/components/ui/form";
+import {
+ Collapsible,
+ CollapsibleContent,
+ CollapsibleTrigger,
+} from "@/src/components/ui/collapsible";
+import { ChevronDown, ChevronRight } from "lucide-react";
+import { MultiSelectKeyValues } from "@/src/features/scores/components/multi-select-key-values";
+import { DropdownMenuItemWithSecondaryAction } from "@/src/components/ui/dropdown-menu";
+import { getScoreDataTypeIcon } from "@/src/features/scores/lib/scoreColumns";
+import {
+ CreateQueueWithAssignmentsData,
+ type CreateQueueWithAssignments,
+ type ScoreConfigDomain,
+} from "@langfuse/shared";
+import { zodResolver } from "@hookform/resolvers/zod";
+import { useMemo, useState } from "react";
+import { useForm } from "react-hook-form";
+import { UserAssignmentSection } from "@/src/features/annotation-queues/components/UserAssignmentSection";
+import { useUniqueNameValidation } from "@/src/hooks/useUniqueNameValidation";
+
+type AnnotationQueueScoreConfigOption = Pick<
+ ScoreConfigDomain,
+ "id" | "name" | "dataType" | "isArchived"
+>;
+
+type AnnotationQueueFormDialogContentProps = {
+ mode: "create" | "edit";
+ initialValues: CreateQueueWithAssignments;
+ scoreConfigs: AnnotationQueueScoreConfigOption[];
+ projectId: string;
+ queueId?: string;
+ queueNames: string[];
+ onManageScoreConfigsClick: () => void;
+ hasQueueAssignmentsReadAccess: boolean;
+ isSubmitting: boolean;
+ onSubmit: (data: CreateQueueWithAssignments) => void;
+ submitLabel: string;
+};
+
+export function AnnotationQueueFormDialogContent({
+ mode,
+ initialValues,
+ scoreConfigs,
+ projectId,
+ queueId,
+ queueNames,
+ onManageScoreConfigsClick,
+ hasQueueAssignmentsReadAccess,
+ isSubmitting,
+ onSubmit,
+ submitLabel,
+}: AnnotationQueueFormDialogContentProps) {
+ const [isAdvancedOpen, setIsAdvancedOpen] = useState(false);
+ const form = useForm<CreateQueueWithAssignments>({
+ resolver: zodResolver(CreateQueueWithAssignmentsData),
+ defaultValues: initialValues,
+ });
+ const queueNameOptions = useMemo(
+ () => queueNames.map((name) => ({ value: name })),
+ [queueNames],
+ );
+
+ useUniqueNameValidation({
+ currentName: form.watch("name"),
+ allNames: queueNameOptions,
+ form,
+ errorMessage: "Queue name already exists.",
+ });
+
+ const handleScoreConfigValueChange = (values: Record<string, string>[]) => {
+ form.setValue(
+ "scoreConfigIds",
+ values.map((value) => value.key),
+ );
+
+ if (values.length === 0) {
+ form.setError("scoreConfigIds", {
+ type: "manual",
+ message: "At least 1 score config must be selected",
+ });
+ } else {
+ form.clearErrors("scoreConfigIds");
+ }
+ };
+
+ const activeScoreConfigs = scoreConfigs.filter(
+ (config) => !config.isArchived,
+ );
+
+ return (
+ <>
+ <DialogHeader>
+ <DialogTitle>
+ {mode === "edit" ? "Edit" : "New"} annotation queue
+ </DialogTitle>
+ <DialogDescription>
+ {mode === "edit" ? "Edit" : "Create a new"} queue to manage your
+ annotation workflows.
+ </DialogDescription>
+ </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"
+ className="text-xs"
+ onBlur={(e) => field.onChange(e.target.value.trimEnd())}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="description"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Description (optional)</FormLabel>
+ <FormControl>
+ <Textarea
+ {...field}
+ placeholder="Add description..."
+ className="text-xs focus:ring-0 focus:outline-hidden focus-visible:ring-0 focus-visible:ring-offset-0 active:ring-0"
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="scoreConfigIds"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Score Configs</FormLabel>
+ <FormDescription>
+ Define which dimensions annotators should score for the
+ given queue.
+ </FormDescription>
+ <FormControl>
+ <MultiSelectKeyValues
+ placeholder="Value"
+ align="end"
+ variant="outline"
+ className="grid grid-cols-[auto_1fr_auto_auto] gap-2"
+ onValueChange={handleScoreConfigValueChange}
+ options={activeScoreConfigs.map((config) => ({
+ key: config.id,
+ value: `${getScoreDataTypeIcon(config.dataType)} ${config.name}`,
+ isArchived: config.isArchived,
+ }))}
+ values={field.value.map((configId) => {
+ const config = scoreConfigs.find(
+ (scoreConfig) => scoreConfig.id === configId,
+ );
+ return {
+ value: config
+ ? `${getScoreDataTypeIcon(config.dataType)} ${config.name}`
+ : `${configId}`,
+ key: configId,
+ };
+ })}
+ controlButtons={
+ <DropdownMenuItemWithSecondaryAction
+ onBeforeAction={onManageScoreConfigsClick}
+ href={`/project/${projectId}/settings/scores`}
+ title="Manage score configs"
+ />
+ }
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ <FormField
+ control={form.control}
+ name="newAssignmentUserIds"
+ render={() => (
+ <FormItem>
+ <FormLabel>Advanced Settings</FormLabel>
+ <div className="mt-1 rounded-md border">
+ <Collapsible
+ open={isAdvancedOpen && hasQueueAssignmentsReadAccess}
+ onOpenChange={(open) => {
+ if (!hasQueueAssignmentsReadAccess) {
+ setIsAdvancedOpen(false);
+ } else {
+ setIsAdvancedOpen(open);
+ }
+ }}
+ >
+ <CollapsibleTrigger asChild>
+ <Button
+ type="button"
+ variant="ghost"
+ className="group flex w-full items-center justify-between px-3 py-2.5 text-left hover:bg-transparent"
+ >
+ <div className="flex items-center gap-2">
+ {isAdvancedOpen ? (
+ <ChevronDown className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <ChevronRight className="text-muted-foreground h-4 w-4" />
+ )}
+ <span className="text-sm font-bold">
+ User Assignment
+ </span>
+ </div>
+ </Button>
+ </CollapsibleTrigger>
+ <CollapsibleContent className="border-border/20 border-t px-3 pt-1 pb-3">
+ {hasQueueAssignmentsReadAccess ? (
+ <>
+ <FormControl>
+ <UserAssignmentSection
+ projectId={projectId}
+ queueId={queueId}
+ selectedUserIds={form.watch(
+ "newAssignmentUserIds",
+ )}
+ onChange={(userIds) =>
+ form.setValue("newAssignmentUserIds", userIds)
+ }
+ />
+ </FormControl>
+ <FormMessage />
+ </>
+ ) : null}
+ </CollapsibleContent>
+ </Collapsible>
+ </div>
+ </FormItem>
+ )}
+ />
+ </DialogBody>
+ <DialogFooter>
+ <Button
+ type="submit"
+ className="text-xs"
+ disabled={!!form.formState.errors.name || isSubmitting}
+ >
+ {isSubmitting ? "Processing..." : submitLabel}
+ </Button>
+ </DialogFooter>
+ </form>
+ </Form>
+ </>
+ );
+}
diff --git a/web/src/features/annotation-queues/components/AnnotationQueueFormDialogController.tsx b/web/src/features/annotation-queues/components/AnnotationQueueFormDialogController.tsx
new file mode 100644
index 0000000000..f4aecbc5ad
--- /dev/null
+++ b/web/src/features/annotation-queues/components/AnnotationQueueFormDialogController.tsx
@@ -0,0 +1,191 @@
+import {
+ type CreateQueueWithAssignments,
+ type ScoreConfigDomain,
+} from "@langfuse/shared";
+import { type ReactNode, useRef, useState } from "react";
+
+import { Dialog, DialogContent } from "@/src/components/ui/dialog";
+import { AnnotationQueueFormDialogContent } from "@/src/features/annotation-queues/components/AnnotationQueueFormDialogContent";
+import { showErrorToast } from "@/src/features/notifications/showErrorToast";
+import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
+import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
+import { useWatchedPromiseCallback } from "@/src/hooks/useWatchedPromiseCallback";
+import { api } from "@/src/utils/api";
+
+type AnnotationQueueFormDialogControllerProps = {
+ projectId: string;
+ onSuccess: (queueId: string) => void;
+ children: (control: {
+ disabled: { reason: string } | undefined;
+ openDialog: () => void;
+ }) => ReactNode;
+} & (
+ | { mode: "create" }
+ | {
+ mode: "edit";
+ queueId: string;
+ }
+);
+
+export function AnnotationQueueFormDialogController(
+ props: AnnotationQueueFormDialogControllerProps,
+) {
+ const { projectId, onSuccess, mode, children } = props;
+ const queueId = mode === "edit" ? props.queueId : undefined;
+
+ const [open, setOpen] = useState(false);
+ const createdQueueIdRef = useRef<string | undefined>(undefined);
+
+ const hasQueueAccess = useHasProjectAccess({
+ projectId,
+ scope: "annotationQueues:CUD",
+ });
+
+ const disabled = hasQueueAccess
+ ? undefined
+ : {
+ reason: `You don't have permission to ${mode} annotation queues.`,
+ };
+
+ const openDialog = () => {
+ if (!hasQueueAccess) return;
+ createdQueueIdRef.current = undefined;
+ setOpen(true);
+ };
+
+ const hasQueueAssignmentsReadAccess = useHasProjectAccess({
+ projectId,
+ scope: "annotationQueueAssignments:read",
+ });
+
+ const capture = usePostHogClientCapture();
+
+ const queueQuery = api.annotationQueues.byId.useQuery(
+ { projectId, queueId: queueId ?? "" },
+ { enabled: mode === "edit" && hasQueueAccess && open },
+ );
+ const configsQuery = api.scoreConfigs.all.useQuery(
+ { projectId },
+ { enabled: hasQueueAccess && open },
+ );
+ const allQueueNamesAndIds = api.annotationQueues.allNamesAndIds.useQuery(
+ { projectId },
+ { enabled: hasQueueAccess && mode === "create" && open },
+ );
+
+ const utils = api.useUtils();
+ const createQueueMutation = api.annotationQueues.create.useMutation();
+ const editQueueMutation = api.annotationQueues.update.useMutation();
+ const createQueueAssignmentsMutation =
+ api.annotationQueueAssignments.createMany.useMutation();
+
+ const [handleSubmit, isSubmitting] = useWatchedPromiseCallback(
+ async (data: CreateQueueWithAssignments) => {
+ try {
+ let targetQueueId: string;
+ if (mode === "edit") {
+ await editQueueMutation.mutateAsync({
+ name: data.name,
+ description: data.description,
+ scoreConfigIds: data.scoreConfigIds,
+ projectId,
+ queueId: props.queueId,
+ });
+ targetQueueId = props.queueId;
+ } else if (createdQueueIdRef.current) {
+ targetQueueId = createdQueueIdRef.current;
+ } else {
+ const queueResponse = await createQueueMutation.mutateAsync({
+ name: data.name,
+ description: data.description,
+ scoreConfigIds: data.scoreConfigIds,
+ projectId,
+ });
+ targetQueueId = queueResponse.id;
+ createdQueueIdRef.current = targetQueueId;
+ }
+
+ if (data.newAssignmentUserIds.length > 0) {
+ await createQueueAssignmentsMutation.mutateAsync({
+ projectId,
+ queueId: targetQueueId,
+ userIds: data.newAssignmentUserIds,
+ });
+ }
+
+ await Promise.all([
+ utils.annotationQueues.invalidate(),
+ utils.annotationQueueAssignments.invalidate(),
+ ]);
+ createdQueueIdRef.current = undefined;
+ onSuccess(targetQueueId);
+ setOpen(false);
+ } catch {
+ showErrorToast(
+ "Operation failed",
+ "Failed to create or update queue or assign users. Please try again.",
+ );
+ }
+ },
+ [
+ createQueueAssignmentsMutation,
+ createQueueMutation,
+ editQueueMutation,
+ mode,
+ onSuccess,
+ projectId,
+ props,
+ utils.annotationQueueAssignments,
+ utils.annotationQueues,
+ ],
+ );
+
+ const initialValues: CreateQueueWithAssignments | undefined =
+ mode === "edit"
+ ? queueQuery.data
+ ? {
+ name: queueQuery.data.name ?? "",
+ description: queueQuery.data.description || undefined,
+ scoreConfigIds: queueQuery.data.scoreConfigs.map(
+ (config: ScoreConfigDomain) => config.id,
+ ),
+ newAssignmentUserIds: [],
+ }
+ : undefined
+ : {
+ name: "",
+ scoreConfigIds: [],
+ newAssignmentUserIds: [],
+ };
+
+ return (
+ <Dialog open={hasQueueAccess && open} onOpenChange={setOpen}>
+ {children({ disabled, openDialog })}
+ {open && configsQuery.data && initialValues ? (
+ <DialogContent className="max-h-[90vh] overflow-y-auto">
+ <AnnotationQueueFormDialogContent
+ mode={mode}
+ initialValues={initialValues}
+ scoreConfigs={configsQuery.data.configs}
+ projectId={projectId}
+ queueId={queueId}
+ queueNames={
+ mode === "create"
+ ? (allQueueNamesAndIds.data?.map((queue) => queue.name) ?? [])
+ : []
+ }
+ onManageScoreConfigsClick={() => {
+ capture("score_configs:manage_configs_item_click", {
+ source: "AnnotationQueue",
+ });
+ }}
+ hasQueueAssignmentsReadAccess={hasQueueAssignmentsReadAccess}
+ isSubmitting={isSubmitting}
+ onSubmit={handleSubmit}
+ submitLabel={mode === "edit" ? "Save queue" : "Create queue"}
+ />
+ </DialogContent>
+ ) : null}
+ </Dialog>
+ );
+}
diff --git a/web/src/features/annotation-queues/components/CreateOrEditAnnotationQueueButton.tsx b/web/src/features/annotation-queues/components/CreateOrEditAnnotationQueueButton.tsx
index c8bb6da632..47d3e8f5bc 100644
--- a/web/src/features/annotation-queues/components/CreateOrEditAnnotationQueueButton.tsx
+++ b/web/src/features/annotation-queues/components/CreateOrEditAnnotationQueueButton.tsx
@@ -1,53 +1,11 @@
-/* eslint-disable @repo/no-abstracted-overlay-trigger */
-import { Button, type ButtonProps } from "@/src/components/ui/button";
-import React, { useEffect, useMemo, useState } from "react";
-import {
- Dialog,
- DialogBody,
- DialogContent,
- DialogDescription,
- DialogFooter,
- DialogHeader,
- DialogTitle,
- DialogTrigger,
-} from "@/src/components/ui/dialog";
-import {
- FormControl,
- FormDescription,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from "@/src/components/ui/form";
-import { Input } from "@/src/components/ui/input";
+import { type ButtonProps } from "@/src/components/ui/button";
import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
-import { zodResolver } from "@hookform/resolvers/zod";
import { Edit, Pen, PlusIcon } from "lucide-react";
-import { useForm } from "react-hook-form";
-import { Form } from "@/src/components/ui/form";
-import { Textarea } from "@/src/components/ui/textarea";
-import {
- type CreateQueueWithAssignments,
- CreateQueueWithAssignmentsData,
- type ScoreConfigDomain,
-} from "@langfuse/shared";
import { api } from "@/src/utils/api";
-import { MultiSelectKeyValues } from "@/src/features/scores/components/multi-select-key-values";
-import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { useEntitlementLimit } from "@/src/features/entitlements/hooks";
import { ActionButton } from "@/src/components/ActionButton";
import { IconOnlyButton } from "@/src/components/IconOnlyButton";
-import { DropdownMenuItemWithSecondaryAction } from "@/src/components/ui/dropdown-menu";
-import { useUniqueNameValidation } from "@/src/hooks/useUniqueNameValidation";
-import {
- Collapsible,
- CollapsibleContent,
- CollapsibleTrigger,
-} from "@/src/components/ui/collapsible";
-import { ChevronDown, ChevronRight } from "lucide-react";
-import { UserAssignmentSection } from "@/src/features/annotation-queues/components/UserAssignmentSection";
-import { showErrorToast } from "@/src/features/notifications/showErrorToast";
-import { getScoreDataTypeIcon } from "@/src/features/scores/lib/scoreColumns";
+import { AnnotationQueueFormDialogController } from "@/src/features/annotation-queues/components/AnnotationQueueFormDialogController";
export const CreateOrEditAnnotationQueueButton = ({
projectId,
@@ -62,59 +20,11 @@ export const CreateOrEditAnnotationQueueButton = ({
size?: ButtonProps["size"];
isTableAction?: boolean;
}) => {
- const [isOpen, setIsOpen] = useState(false);
- const [isAdvancedOpen, setIsAdvancedOpen] = useState(false);
const hasQueueAccess = useHasProjectAccess({
projectId: projectId,
scope: "annotationQueues:CUD",
});
- const hasQueueAssignmentsReadAccess = useHasProjectAccess({
- projectId: projectId,
- scope: "annotationQueueAssignments:read",
- });
const queueLimit = useEntitlementLimit("annotation-queue-count");
- const capture = usePostHogClientCapture();
-
- const queueQuery = api.annotationQueues.byId.useQuery(
- { projectId, queueId: queueId as string },
- // Only needed once the edit dialog is open; otherwise this fires for every
- // row that eagerly mounts the inline table-action button.
- { enabled: !!queueId && hasQueueAccess && isOpen },
- );
-
- const form = useForm({
- resolver: zodResolver(CreateQueueWithAssignmentsData),
- });
-
- useEffect(() => {
- // Re-seed when the dialog opens so reopening discards any unsaved edits;
- // the component now stays mounted per table row instead of remounting.
- if (!isOpen) return;
- if (queueId && queueQuery.data) {
- form.reset({
- name: queueQuery.data.name,
- description: queueQuery.data.description || undefined,
- scoreConfigIds: queueQuery.data.scoreConfigs.map(
- (config: ScoreConfigDomain) => config.id,
- ),
- newAssignmentUserIds: [],
- });
- } else {
- form.reset({
- name: "",
- scoreConfigIds: [],
- newAssignmentUserIds: [],
- });
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [queueId, queueQuery.data, isOpen]);
-
- const utils = api.useUtils();
-
- const createQueueMutation = api.annotationQueues.create.useMutation();
- const editQueueMutation = api.annotationQueues.update.useMutation();
- const createQueueAssignmentsMutation =
- api.annotationQueueAssignments.createMany.useMutation();
const queueCountData = api.annotationQueues.count.useQuery(
{ projectId },
@@ -123,338 +33,53 @@ export const CreateOrEditAnnotationQueueButton = ({
{ enabled: hasQueueAccess && !queueId },
);
- const configsData = api.scoreConfigs.all.useQuery(
- {
- projectId,
- },
- {
- enabled: hasQueueAccess && isOpen,
- },
- );
-
- const allQueueNamesAndIds = api.annotationQueues.allNamesAndIds.useQuery(
- { projectId },
- { enabled: hasQueueAccess && !queueId },
- );
-
- const allQueueNames = useMemo(() => {
- return !queueId && allQueueNamesAndIds.data
- ? allQueueNamesAndIds.data.map((queue) => ({ value: queue.name }))
- : [];
- }, [allQueueNamesAndIds.data, queueId]);
-
- useUniqueNameValidation({
- currentName: form.watch("name"),
- allNames: allQueueNames,
- form,
- errorMessage: "Queue name already exists.",
- });
-
- const configs = configsData.data?.configs ?? [];
+ const controllerProps = queueId
+ ? ({ mode: "edit", queueId } as const)
+ : ({ mode: "create" } as const);
- const onSubmit = async (data: CreateQueueWithAssignments) => {
- try {
- // Step 1: Create or update the queue
- let queueResponse;
- if (queueId) {
- // Update existing queue
- queueResponse = await editQueueMutation.mutateAsync({
- name: data.name,
- description: data.description,
- scoreConfigIds: data.scoreConfigIds,
- projectId,
- queueId,
- });
- } else {
- // Create new queue
- queueResponse = await createQueueMutation.mutateAsync({
- name: data.name,
- description: data.description,
- scoreConfigIds: data.scoreConfigIds,
- projectId,
- });
- }
-
- // Step 2: Handle assignment if provided
- if (data.newAssignmentUserIds && data.newAssignmentUserIds.length > 0) {
- const targetQueueId = queueId || queueResponse.id;
-
- await createQueueAssignmentsMutation.mutateAsync({
- projectId,
- queueId: targetQueueId,
- userIds: data.newAssignmentUserIds,
- });
- }
-
- // Step 3: Success handling
- await Promise.all([
- utils.annotationQueues.invalidate(),
- utils.annotationQueueAssignments.invalidate(),
- ]);
- form.reset();
- setIsOpen(false);
-
- // capture posthog event
- } catch {
- showErrorToast(
- "Operation failed",
- "Failed to create or update queue or assign users. Please try again.",
- );
- }
- };
-
- const handleOnValueChange = (values: Record<string, string>[]) => {
- form.setValue(
- "scoreConfigIds",
- values.map((value) => value.key),
- );
-
- if (values.length === 0) {
- form.setError("scoreConfigIds", {
- type: "manual",
- message: "At least 1 score config must be selected",
- });
- } else {
- form.clearErrors("scoreConfigIds");
- }
- };
-
- // Table rows render a compact icon button and rely on the disabled state plus
- // a tooltip; everywhere else uses the labeled ActionButton with its built-in
- // access/limit messaging.
- const triggerButton = isTableAction ? (
- <IconOnlyButton
- icon={<Pen className="h-4 w-4" />}
- label="Edit"
- aria-label="edit"
- disabledReason={
- hasQueueAccess
- ? undefined
- : "You don't have permission to edit this queue."
- }
- onClick={(event) => {
- event.stopPropagation();
- setIsOpen(true);
- }}
- />
- ) : (
- <ActionButton
- variant={variant}
- onClick={() => setIsOpen(true)}
- icon={
- queueId ? (
- <Edit className="h-4 w-4" aria-hidden="true" />
+ return (
+ <AnnotationQueueFormDialogController
+ {...controllerProps}
+ projectId={projectId}
+ onSuccess={() => undefined}
+ >
+ {({ disabled, openDialog }) =>
+ isTableAction ? (
+ <IconOnlyButton
+ icon={<Pen className="h-4 w-4" />}
+ label="Edit"
+ aria-label="edit"
+ disabledReason={disabled?.reason}
+ onClick={(event) => {
+ event.stopPropagation();
+ openDialog();
+ }}
+ />
) : (
- <PlusIcon className="h-4 w-4" aria-hidden="true" />
+ <ActionButton
+ variant={variant}
+ onClick={openDialog}
+ icon={
+ queueId ? (
+ <Edit className="h-4 w-4" aria-hidden="true" />
+ ) : (
+ <PlusIcon className="h-4 w-4" aria-hidden="true" />
+ )
+ }
+ hasAccess={!disabled}
+ usageLimit={
+ typeof queueLimit === "number"
+ ? { current: queueCountData.data, max: queueLimit }
+ : undefined
+ }
+ size={size}
+ >
+ <span className="ml-1 text-sm font-normal">
+ {queueId ? "Edit" : "New queue"}
+ </span>
+ </ActionButton>
)
}
- hasAccess={hasQueueAccess}
- usageLimit={
- typeof queueLimit === "number"
- ? { current: queueCountData.data, max: queueLimit }
- : undefined
- }
- size={size}
- >
- <span className="ml-1 text-sm font-normal">
- {queueId ? "Edit" : "New queue"}
- </span>
- </ActionButton>
- );
-
- return (
- <Dialog open={isOpen} onOpenChange={setIsOpen}>
- {isTableAction ? (
- triggerButton
- ) : (
- <DialogTrigger asChild>{triggerButton}</DialogTrigger>
- )}
- {/* For an edit, also wait for the queue data so the form opens populated
- rather than briefly showing empty fields while byId loads. */}
- {configsData.data && (!queueId || queueQuery.data) && (
- <DialogContent className="max-h-[90vh] overflow-y-auto">
- <DialogHeader>
- <DialogTitle>
- {queueId ? "Edit" : "New"} annotation queue
- </DialogTitle>
- <DialogDescription>
- {queueId ? "Edit" : "Create a new"} queue to manage your
- annotation workflows.
- </DialogDescription>
- </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"
- className="text-xs"
- onBlur={(e) =>
- field.onChange(e.target.value.trimEnd())
- }
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="description"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Description (optional)</FormLabel>
- <FormControl>
- <Textarea
- {...field}
- placeholder="Add description..."
- className="text-xs focus:ring-0 focus:outline-hidden focus-visible:ring-0 focus-visible:ring-offset-0 active:ring-0"
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="scoreConfigIds"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Score Configs</FormLabel>
- <FormDescription>
- Define which dimensions annotators should score for the
- given queue.
- </FormDescription>
- <FormControl>
- <MultiSelectKeyValues
- placeholder="Value"
- align="end"
- variant="outline"
- className="grid grid-cols-[auto_1fr_auto_auto] gap-2"
- onValueChange={handleOnValueChange}
- options={configs
- .filter((config) => !config.isArchived)
- .map((config) => ({
- key: config.id,
- value: `${getScoreDataTypeIcon(config.dataType)} ${config.name}`,
- isArchived: config.isArchived,
- }))}
- values={field.value.map((configId) => {
- const config = configs.find(
- (config) => config.id === configId,
- );
- return {
- value: config
- ? `${getScoreDataTypeIcon(config.dataType)} ${config.name}`
- : `${configId}`,
- key: configId,
- };
- })}
- controlButtons={
- <DropdownMenuItemWithSecondaryAction
- onBeforeAction={() => {
- capture(
- "score_configs:manage_configs_item_click",
- { source: "AnnotationQueue" },
- );
- }}
- href={`/project/${projectId}/settings/scores`}
- title="Manage score configs"
- />
- }
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
-
- {/* Advanced Section */}
- <FormField
- control={form.control}
- name="newAssignmentUserIds"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Advanced Settings</FormLabel>
- <div className="mt-1 rounded-md border">
- <Collapsible
- open={isAdvancedOpen && hasQueueAssignmentsReadAccess}
- onOpenChange={(open) => {
- if (!hasQueueAssignmentsReadAccess) {
- setIsAdvancedOpen(false);
- } else {
- setIsAdvancedOpen(open);
- }
- }}
- >
- <CollapsibleTrigger asChild>
- <Button
- type="button"
- variant="ghost"
- className="group flex w-full items-center justify-between px-3 py-2.5 text-left hover:bg-transparent"
- >
- <div className="flex items-center gap-2">
- {isAdvancedOpen ? (
- <ChevronDown className="text-muted-foreground h-4 w-4" />
- ) : (
- <ChevronRight className="text-muted-foreground h-4 w-4" />
- )}
- <span className="text-sm font-bold">
- User Assignment
- </span>
- </div>
- </Button>
- </CollapsibleTrigger>
- <CollapsibleContent className="border-border/20 border-t px-3 pt-1 pb-3">
- {hasQueueAssignmentsReadAccess && (
- <>
- <FormControl>
- <UserAssignmentSection
- projectId={projectId}
- queueId={queueId}
- selectedUserIds={field.value}
- onChange={field.onChange}
- />
- </FormControl>
- <FormMessage />
- </>
- )}
- </CollapsibleContent>
- </Collapsible>
- </div>
- </FormItem>
- )}
- />
- </DialogBody>
- <DialogFooter>
- <Button
- type="submit"
- className="text-xs"
- disabled={
- !!form.formState.errors.name ||
- createQueueMutation.isPending ||
- editQueueMutation.isPending ||
- createQueueAssignmentsMutation.isPending
- }
- >
- {createQueueMutation.isPending ||
- editQueueMutation.isPending ||
- createQueueAssignmentsMutation.isPending
- ? "Processing..."
- : `${queueId ? "Save" : "Create"} queue`}
- </Button>
- </DialogFooter>
- </form>
- </Form>
- </DialogContent>
- )}
- </Dialog>
+ </AnnotationQueueFormDialogController>
);
};