import * as React from "react"; import { cn } from "@/shared/lib/cn"; import { Button } from "@/shared/ui/button"; import { ChooserDialogContent } from "@/shared/ui/chooser-dialog-content"; import { Dialog } from "@/shared/ui/dialog"; import { Input } from "@/shared/ui/input"; import { Textarea } from "@/shared/ui/textarea"; const FIELD_SHELL_CLASS = "rounded-xl border border-input bg-muted/40 transition-colors hover:border-muted-foreground/40 focus-within:border-muted-foreground/50"; const FIELD_CONTROL_CLASS = "border-0 bg-transparent shadow-none outline-none ring-0 placeholder:text-muted-foreground/55 focus-visible:ring-0"; export type CreateProjectWorkItemDialogInput = { title: string; body: string; }; export function CreateProjectWorkItemDialog({ bodyPlaceholder, children, description, isCreating, itemName, onCreate, onOpenChange, open, submitDisabled = false, title, titlePlaceholder, }: { bodyPlaceholder: string; children?: React.ReactNode; description: string; isCreating: boolean; itemName: "issue" | "pull-request"; onCreate: (input: CreateProjectWorkItemDialogInput) => Promise; onOpenChange: (open: boolean) => void; open: boolean; submitDisabled?: boolean; title: string; titlePlaceholder: string; }) { const [workItemTitle, setWorkItemTitle] = React.useState(""); const [body, setBody] = React.useState(""); const [errorMessage, setErrorMessage] = React.useState(null); const titleInputRef = React.useRef(null); const submitInFlightRef = React.useRef(false); const testIdPrefix = `create-${itemName}`; const itemLabel = itemName === "issue" ? "issue" : "pull request"; React.useEffect(() => { if (!open) return; setWorkItemTitle(""); setBody(""); setErrorMessage(null); const timerId = globalThis.setTimeout( () => titleInputRef.current?.focus(), 50, ); return () => globalThis.clearTimeout(timerId); }, [open]); async function handleSubmit(event: React.FormEvent) { event.preventDefault(); if (isCreating || submitDisabled || submitInFlightRef.current) return; const trimmedTitle = workItemTitle.trim(); if (!trimmedTitle) return; submitInFlightRef.current = true; setErrorMessage(null); try { await onCreate({ title: trimmedTitle, body: body.trim() }); onOpenChange(false); } catch (error) { setErrorMessage( error instanceof Error ? error.message : `Failed to create ${itemLabel}.`, ); } finally { submitInFlightRef.current = false; } } return ( { if (!nextOpen && isCreating) return; onOpenChange(nextOpen); }} open={open} > } footerClassName="border-t-0 pt-0" headerClassName="pb-2" title={title} >
void handleSubmit(event)} > {children}
{ setWorkItemTitle(event.target.value); setErrorMessage(null); }} placeholder={titlePlaceholder} ref={titleInputRef} value={workItemTitle} />