Files
kandev/apps/web/app/office/components/new-task-dialog.tsx
T
João Salavisa f149bd1292 fix(office): disable create button until project + title set (#1243)
* fix(office): disable create button until project + title set

Office "New Task" dialog let user submit without picking a project, which
hit the backend isOfficeRequest() heuristic and failed validation with
"workflow_id is required for non-ephemeral tasks". Disable the button
until both title and project are set and surface the missing requirement
in a tooltip instead of a backend error toast.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(office): guard handleCreate on projectId + cover with tests

PR review feedback (claude, cubic): the handleCreate path only checked
title and workspace, so a keyboard shortcut or future programmatic
caller could still produce a request with no project and reproduce the
backend 'workflow_id is required' error. Guard it there too, and add
unit tests that pin the disabled-state + reason behavior of the
extracted CreateTaskButton. aria-label on the trigger wrapper surfaces
the reason to assistive tech even when the tooltip is closed.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(office): cover submitting state for CreateTaskButton

PR review feedback (claude): the disabled check had three branches
(missing title / missing project / submitting); only two were
tested. Add the submitting case so a regression that drops it from the
disabled expression is caught.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 13:37:39 +01:00

218 lines
6.5 KiB
TypeScript

"use client";
import { useState, useCallback } from "react";
import { Button } from "@kandev/ui/button";
import { Badge } from "@kandev/ui/badge";
import { Textarea } from "@kandev/ui/textarea";
import { Dialog, DialogContent, DialogHeader, DialogFooter, DialogTitle } from "@kandev/ui/dialog";
import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip";
import { toast } from "sonner";
import { useAppStore } from "@/components/state-provider";
import { createTask } from "@/lib/api/domains/kanban-api";
import { useIssueDraft, type IssueDraft } from "./new-task-draft";
import { NewTaskSelectorRow } from "./new-task-selector-row";
import { NewTaskBottomBar } from "./new-task-bottom-bar";
import {
NewTaskStages,
buildExecutionPolicy,
EMPTY_STAGES,
type StagesDraft,
} from "./new-task-stages";
function buildMetadata(draft: IssueDraft): Record<string, unknown> | undefined {
const meta: Record<string, unknown> = {};
if (draft.assigneeId) meta.assignee_agent_profile_id = draft.assigneeId;
if (draft.status && draft.status !== "todo") meta.initial_status = draft.status;
return Object.keys(meta).length > 0 ? meta : undefined;
}
type NewIssueDialogProps = {
open: boolean;
onOpenChange: (open: boolean) => void;
parentTaskId?: string;
defaultProjectId?: string;
defaultAssigneeId?: string;
};
export function NewTaskDialog({
open,
onOpenChange,
parentTaskId,
defaultProjectId,
defaultAssigneeId,
}: NewIssueDialogProps) {
const workspaceId = useAppStore((s) => s.workspaces.activeId);
const [submitting, setSubmitting] = useState(false);
const [stages, setStages] = useState<StagesDraft>(EMPTY_STAGES);
const { draft, updateDraft, clearDraft } = useIssueDraft(workspaceId, parentTaskId, {
projectId: defaultProjectId,
assigneeId: defaultAssigneeId,
});
const updateStages = useCallback(
(patch: Partial<StagesDraft>) => setStages((prev) => ({ ...prev, ...patch })),
[],
);
const handleCreate = useCallback(async () => {
if (!draft.title.trim() || !draft.projectId || !workspaceId) return;
setSubmitting(true);
try {
const executionPolicy = buildExecutionPolicy(stages);
const metadata = buildMetadata(draft);
await createTask({
workspace_id: workspaceId,
workflow_id: "",
title: draft.title.trim(),
description: draft.description.trim() || undefined,
parent_id: parentTaskId,
priority: draft.priority,
project_id: draft.projectId || undefined,
metadata: executionPolicy ? { ...metadata, execution_policy: executionPolicy } : metadata,
});
clearDraft();
setStages(EMPTY_STAGES);
onOpenChange(false);
toast.success("Task created");
} catch (err) {
toast.error(err instanceof Error ? err.message : "Failed to create issue");
} finally {
setSubmitting(false);
}
}, [draft, stages, workspaceId, parentTaskId, clearDraft, onOpenChange]);
const handleDiscard = useCallback(() => {
clearDraft();
onOpenChange(false);
}, [clearDraft, onOpenChange]);
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-3xl sm:max-w-3xl lg:max-w-4xl">
<NewIssueDialogBody
draft={draft}
updateDraft={updateDraft}
stages={stages}
updateStages={updateStages}
parentTaskId={parentTaskId}
submitting={submitting}
onDiscard={handleDiscard}
onCreate={handleCreate}
/>
</DialogContent>
</Dialog>
);
}
function NewIssueDialogBody({
draft,
updateDraft,
stages,
updateStages,
parentTaskId,
submitting,
onDiscard,
onCreate,
}: {
draft: IssueDraft;
updateDraft: (patch: Partial<IssueDraft>) => void;
stages: StagesDraft;
updateStages: (patch: Partial<StagesDraft>) => void;
parentTaskId?: string;
submitting: boolean;
onDiscard: () => void;
onCreate: () => void;
}) {
return (
<>
<DialogHeader>
<DialogTitle className="sr-only">New issue</DialogTitle>
<div className="flex items-center gap-2">
<Badge variant="outline" className="font-mono text-xs">
KAN
</Badge>
<span className="text-sm text-muted-foreground">New issue</span>
{parentTaskId && (
<Badge variant="secondary" className="text-xs">
Sub-issue of {parentTaskId}
</Badge>
)}
</div>
</DialogHeader>
<div className="space-y-4">
<Textarea
placeholder="Task title"
value={draft.title}
onChange={(e) => updateDraft({ title: e.target.value })}
className="text-lg font-medium border-0 resize-none focus-visible:ring-0 min-h-[40px]"
rows={1}
autoFocus
/>
<NewTaskSelectorRow draft={draft} onUpdate={updateDraft} />
<Textarea
placeholder="Add description..."
value={draft.description}
onChange={(e) => updateDraft({ description: e.target.value })}
className="min-h-[120px] text-sm"
/>
<NewTaskStages stages={stages} onUpdate={updateStages} />
<NewTaskBottomBar draft={draft} onUpdate={updateDraft} />
</div>
<DialogFooter className="flex justify-between sm:justify-between">
<Button
variant="ghost"
className="text-muted-foreground cursor-pointer"
onClick={onDiscard}
>
Discard Draft
</Button>
<CreateTaskButton draft={draft} submitting={submitting} onCreate={onCreate} />
</DialogFooter>
</>
);
}
export function CreateTaskButton({
draft,
submitting,
onCreate,
}: {
draft: IssueDraft;
submitting: boolean;
onCreate: () => void;
}) {
const missingTitle = !draft.title.trim();
const missingProject = !draft.projectId;
const disabled = missingTitle || missingProject || submitting;
let reason: string | null = null;
if (missingProject) reason = "Select a project to create a task";
else if (missingTitle) reason = "Add a title to create a task";
const button = (
<Button
onClick={onCreate}
disabled={disabled}
className="cursor-pointer"
data-testid="new-task-create-button"
>
{submitting ? "Creating..." : "Create Task"}
</Button>
);
if (!reason) return button;
return (
<Tooltip>
<TooltipTrigger asChild>
<span tabIndex={0} aria-label={reason}>
{button}
</span>
</TooltipTrigger>
<TooltipContent>{reason}</TooltipContent>
</Tooltip>
);
}