Skip to content
refs
84 lines · 3.3 KBRaw
1import { Banner, Button, Input, InputArea, LayerCard, LinkButton, Select } from "@refs/design-system";
2import { TagIcon } from "@refs/design-system/icons";
3import { LabelBadge, LabelPicker } from "@refs/design-system";
4import { MAX_BODY_LENGTH, MAX_TITLE_LENGTH, NONE, type Choice, type Label } from "@refs/backend/contract";
5import { useState } from "react";
6import { Form, useNavigation } from "react-router";
7import { useLabelCreation } from "./label-creation";
8
9export interface TaskFormValues {
10 title: string;
11 body: string;
12 repo: string;
13 assignee: string;
14}
15
16function optionItems(options: readonly Choice[], empty?: string): Record<string, string> {
17 return Object.fromEntries([...(empty ? [[NONE, empty]] : []), ...options.map((option) => [option.id, option.name])]);
18}
19
20export function TaskForm({
21 values,
22 repos,
23 people,
24 labels,
25 initialLabels = [],
26 error,
27 cancelHref,
28}: {
29 values: TaskFormValues;
30 repos: readonly Choice[];
31 people: readonly Choice[];
32 labels: readonly Label[];
33 initialLabels?: string[];
34 error?: string;
35 cancelHref: string;
36}) {
37 const submitting = useNavigation().state === "submitting";
38 const creation = useLabelCreation();
39 const [selected, setSelected] = useState(initialLabels);
40 const chosen = labels.filter((label) => selected.includes(label.id));
41 return (
42 <Form key={JSON.stringify({ values, error })} method="post" className="flex max-w-3xl flex-col gap-4">
43 {error && <Banner variant="error" description={error} />}
44 <LayerCard className="flex flex-col gap-4 p-4">
45 <Input label="Title" name="title" defaultValue={values.title} maxLength={MAX_TITLE_LENGTH} required autoFocus />
46 <InputArea label="Description" name="body" defaultValue={values.body} maxLength={MAX_BODY_LENGTH} rows={8} description="Markdown is supported." />
47 <div className="grid gap-5 sm:grid-cols-2">
48 <Select label="Repository" name="repo" defaultValue={values.repo || repos[0]?.id} items={optionItems(repos)} />
49 <Select label="Assignee" name="assignee" defaultValue={values.assignee || NONE} items={optionItems(people, "Unassigned")} />
50 </div>
51 <div className="flex flex-col gap-1.5">
52 <span className="caption text-faint">Labels</span>
53 <div className="flex flex-wrap items-center gap-2">
54 {chosen.map((label) => (
55 <LabelBadge key={label.id} label={label} />
56 ))}
57 <LabelPicker
58 labels={labels}
59 selected={selected}
60 creation={creation}
61 onToggle={(id, checked) => setSelected((previous) => (checked ? [...new Set([...previous, id])] : previous.filter((value) => value !== id)))}
62 trigger={
63 <Button type="button" variant="secondary" size="sm" icon={<TagIcon />}>
64 {chosen.length ? "Edit labels" : "Add labels"}
65 </Button>
66 }
67 />
68 </div>
69 {selected.map((id) => (
70 <input key={id} type="hidden" name="label" value={id} />
71 ))}
72 </div>
73 </LayerCard>
74 <div className="flex gap-2">
75 <Button type="submit" variant="primary" loading={submitting}>
76 Create task
77 </Button>
78 <LinkButton variant="secondary" href={cancelHref}>
79 Cancel
80 </LinkButton>
81 </div>
82 </Form>
83 );
84}