Skip to content
refs
45 lines · 2.0 KBRaw
1import { Button, Input } from "@refs/design-system";
2import { PencilSimpleIcon } from "@refs/design-system/icons";
3import { MAX_TITLE_LENGTH } from "@refs/backend/contract";
4import { useState } from "react";
5import { useFetcher } from "react-router";
6import { useOnSuccess } from "./use-on-success";
7
8/** The title of a task or pull request, with an editor that posts `intent=title` to the page's action. */
9export function TitleEditor({ title, number, meta }: { title: string; number: number; meta: (edit: React.ReactNode) => React.ReactNode }) {
10 const fetcher = useFetcher<{ error: string | null }>();
11 const [editing, setEditing] = useState(false);
12 const saving = fetcher.state !== "idle";
13 useOnSuccess(fetcher, () => setEditing(false));
14
15 if (!editing) {
16 return (
17 <div className="flex flex-col gap-2.5">
18 <h1 className="m-0 min-w-0 text-heading leading-tight font-semibold break-words sm:text-title">
19 {title} <span className="font-normal text-faint tabular-nums">#{number}</span>
20 </h1>
21 {meta(
22 <Button variant="ghost" size="sm" shape="square" icon={<PencilSimpleIcon />} aria-label="Edit title" title="Edit title" onClick={() => setEditing(true)} />,
23 )}
24 </div>
25 );
26 }
27 return (
28 <div className="flex flex-col gap-2.5">
29 <fetcher.Form method="post" className="flex flex-wrap items-end gap-2">
30 <input type="hidden" name="intent" value="title" />
31 <div className="min-w-60 flex-1">
32 <Input key={title} aria-label="Title" name="title" defaultValue={title} maxLength={MAX_TITLE_LENGTH} required autoFocus size="lg" />
33 </div>
34 <Button type="submit" variant="primary" size="lg" loading={saving}>
35 Save
36 </Button>
37 <Button type="button" variant="secondary" size="lg" onClick={() => setEditing(false)}>
38 Cancel
39 </Button>
40 {fetcher.data?.error && <p className="m-0 w-full text-compact text-danger">{fetcher.data.error}</p>}
41 </fetcher.Form>
42 {meta(null)}
43 </div>
44 );
45}