469 lines · 18.2 KBRaw
| 1 | import { MAX_BODY_LENGTH, NONE } from "@refs/backend/contract"; |
| 2 | import { Ago, Avatar, Banner, Button, Dialog, DropdownMenu, InputArea, LayerCard } from "@refs/design-system"; |
| 3 | import { |
| 4 | ArrowsLeftRightIcon, |
| 5 | CheckCircleIcon, |
| 6 | DotsThreeIcon, |
| 7 | PencilSimpleIcon, |
| 8 | RecordIcon, |
| 9 | TagIcon, |
| 10 | TextAaIcon, |
| 11 | TrashIcon, |
| 12 | UserCircleIcon, |
| 13 | } from "@refs/design-system/icons"; |
| 14 | import { useEffect, useState } from "react"; |
| 15 | import { Link, data, redirect, useFetcher } from "react-router"; |
| 16 | import type { Route } from "./+types/task"; |
| 17 | import { pullLink, taskLink, type Attribution, type Choice, type Label, type Outcome, type TaskStatus, type TimelineItem } from "@refs/backend/contract"; |
| 18 | import { LabelBadge, LabelPicker, StatusBadge, StatusIcon } from "@refs/design-system"; |
| 19 | import { useLive } from "../components/live"; |
| 20 | import { failed, formList, formText, found, signedIn, fullName, nowSeconds } from "../lib/session"; |
| 21 | import { TitleEditor } from "../components/title-editor"; |
| 22 | import { useOnSuccess } from "../components/use-on-success"; |
| 23 | import { useLabelCreation } from "../components/label-creation"; |
| 24 | import { ActorMark, ActorName, EventLine, Field, GearButton, Timeline, TimelineRow, human, toneOf } from "../components/timeline"; |
| 25 | |
| 26 | export const meta: Route.MetaFunction = ({ loaderData }) => [{ title: loaderData ? `${loaderData.task.title} · ${loaderData.task.repo}#${loaderData.task.number} · refs` : "Task · refs" }]; |
| 27 | |
| 28 | function opposite(status: TaskStatus): TaskStatus { |
| 29 | return status === "open" ? "closed" : "open"; |
| 30 | } |
| 31 | |
| 32 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 33 | const refs = signedIn(context); |
| 34 | const task = await found(refs.task(fullName(params), Number(params.number))); |
| 35 | const { detail } = task; |
| 36 | if (detail.repo !== fullName(params) || String(detail.number) !== params.number) throw redirect(taskLink(detail.repo, detail.number)); |
| 37 | const [timeline, choices] = await Promise.all([task.timeline(), refs.taskChoices()]); |
| 38 | return { task: detail, timeline, choices, now: nowSeconds() }; |
| 39 | } |
| 40 | |
| 41 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 42 | const refs = signedIn(context); |
| 43 | const task = await found(refs.task(fullName(params), Number(params.number))); |
| 44 | const form = await request.formData(); |
| 45 | const intent = formText(form, "intent"); |
| 46 | const respond = (outcome: Outcome<unknown>) => (outcome.ok ? { intent, error: null } : failed(outcome.problem, { intent })); |
| 47 | |
| 48 | switch (intent) { |
| 49 | case "comment": { |
| 50 | const body = formText(form, "body"); |
| 51 | const then = formText(form, "toggle") === "1" ? opposite(task.detail.status) : undefined; |
| 52 | return respond(await (!body && then ? task.setStatus(then) : task.comment(body, then))); |
| 53 | } |
| 54 | case "title": |
| 55 | return respond(await task.rename(formText(form, "title"))); |
| 56 | case "description": |
| 57 | return respond(await task.describe(formText(form, "body"))); |
| 58 | case "assignee": { |
| 59 | const assignee = formText(form, "assignee"); |
| 60 | return respond(await task.assign(assignee && assignee !== NONE ? assignee : null)); |
| 61 | } |
| 62 | case "repo": { |
| 63 | const outcome = await task.transfer(formText(form, "repo")); |
| 64 | if (outcome.ok) throw redirect(taskLink(outcome.value.repo, outcome.value.number)); |
| 65 | return respond(outcome); |
| 66 | } |
| 67 | case "labels": |
| 68 | return respond(await task.setLabels(formList(form, "label"))); |
| 69 | case "edit-comment": |
| 70 | return respond(await found(task.editComment(formText(form, "comment"), formText(form, "body")))); |
| 71 | case "delete-comment": |
| 72 | return respond(await found(task.deleteComment(formText(form, "comment")))); |
| 73 | default: |
| 74 | return data({ intent, error: "Unknown action." }, { status: 400 }); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | type Event = Exclude<TimelineItem, { kind: "task.commented" | "task.opened" }>; |
| 79 | |
| 80 | function MarkdownEditor({ |
| 81 | intent, |
| 82 | body: initial, |
| 83 | commentId, |
| 84 | onDone, |
| 85 | submit, |
| 86 | }: { |
| 87 | intent: string; |
| 88 | body: string; |
| 89 | commentId?: string; |
| 90 | onDone: () => void; |
| 91 | submit: string; |
| 92 | }) { |
| 93 | const fetcher = useFetcher<typeof action>(); |
| 94 | const [body, setBody] = useState(initial); |
| 95 | useOnSuccess(fetcher, onDone); |
| 96 | return ( |
| 97 | <fetcher.Form method="post" className="flex flex-col gap-3"> |
| 98 | <input type="hidden" name="intent" value={intent} /> |
| 99 | {commentId !== undefined && <input type="hidden" name="comment" value={commentId} />} |
| 100 | <InputArea aria-label="Markdown" name="body" value={body} onValueChange={setBody} maxLength={MAX_BODY_LENGTH} rows={8} autoFocus /> |
| 101 | {fetcher.data?.error && <Banner variant="error" description={fetcher.data.error} />} |
| 102 | <div className="flex justify-end gap-2"> |
| 103 | <Button type="button" variant="secondary" onClick={onDone}> |
| 104 | Cancel |
| 105 | </Button> |
| 106 | <Button type="submit" variant="primary" loading={fetcher.state !== "idle"}> |
| 107 | {submit} |
| 108 | </Button> |
| 109 | </div> |
| 110 | </fetcher.Form> |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | function DeleteComment({ commentId, open, onOpenChange }: { commentId: string; open: boolean; onOpenChange: (open: boolean) => void }) { |
| 115 | const fetcher = useFetcher<typeof action>(); |
| 116 | return ( |
| 117 | <Dialog.Root open={open} onOpenChange={onOpenChange}> |
| 118 | <Dialog size="sm" className="p-6"> |
| 119 | <Dialog.Title className="m-0 text-lg font-semibold">Delete comment?</Dialog.Title> |
| 120 | <Dialog.Description className="mt-2 mb-5 text-muted">This can't be undone.</Dialog.Description> |
| 121 | <fetcher.Form method="post" className="flex justify-end gap-2"> |
| 122 | <input type="hidden" name="intent" value="delete-comment" /> |
| 123 | <input type="hidden" name="comment" value={commentId} /> |
| 124 | <Dialog.Close render={(props) => <Button {...props} variant="secondary">Cancel</Button>} /> |
| 125 | <Button type="submit" variant="primary" loading={fetcher.state !== "idle"}> |
| 126 | Delete |
| 127 | </Button> |
| 128 | </fetcher.Form> |
| 129 | </Dialog> |
| 130 | </Dialog.Root> |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | function CommentCard({ |
| 135 | by, |
| 136 | verb, |
| 137 | at, |
| 138 | edited, |
| 139 | html, |
| 140 | body, |
| 141 | now: current, |
| 142 | editIntent, |
| 143 | commentId, |
| 144 | canEdit, |
| 145 | canDelete, |
| 146 | emptyText, |
| 147 | }: { |
| 148 | by: Attribution; |
| 149 | verb: string; |
| 150 | at: number; |
| 151 | edited: boolean; |
| 152 | html: string | null; |
| 153 | body: string; |
| 154 | now: number; |
| 155 | editIntent: string; |
| 156 | commentId?: string; |
| 157 | canEdit: boolean; |
| 158 | canDelete: boolean; |
| 159 | emptyText?: string; |
| 160 | }) { |
| 161 | const [editing, setEditing] = useState(false); |
| 162 | const [deleting, setDeleting] = useState(false); |
| 163 | return ( |
| 164 | <TimelineRow tone={toneOf(by)}> |
| 165 | <LayerCard> |
| 166 | <LayerCard.Secondary className="flex items-center justify-between gap-3 py-1.5 font-normal"> |
| 167 | <span className="flex min-w-0 flex-wrap items-center gap-x-2 gap-y-0.5 py-1"> |
| 168 | <ActorMark by={by} /> |
| 169 | <ActorName by={by} /> |
| 170 | <span>{verb}</span> |
| 171 | <Ago at={at} now={current} /> |
| 172 | {edited && <span className="text-faint">· edited</span>} |
| 173 | </span> |
| 174 | {(canEdit || canDelete) && ( |
| 175 | <DropdownMenu> |
| 176 | <DropdownMenu.Trigger |
| 177 | render={ |
| 178 | <Button variant="ghost" size="sm" shape="square" className="hit-area" aria-label="Comment actions"> |
| 179 | <DotsThreeIcon size={18} weight="bold" /> |
| 180 | </Button> |
| 181 | } |
| 182 | /> |
| 183 | <DropdownMenu.Content align="end"> |
| 184 | {canEdit && ( |
| 185 | <DropdownMenu.Item icon={<PencilSimpleIcon />} onClick={() => setEditing(true)}> |
| 186 | Edit |
| 187 | </DropdownMenu.Item> |
| 188 | )} |
| 189 | {canDelete && ( |
| 190 | <DropdownMenu.Item variant="danger" icon={<TrashIcon />} onClick={() => setDeleting(true)}> |
| 191 | Delete |
| 192 | </DropdownMenu.Item> |
| 193 | )} |
| 194 | </DropdownMenu.Content> |
| 195 | </DropdownMenu> |
| 196 | )} |
| 197 | </LayerCard.Secondary> |
| 198 | <LayerCard.Primary className="px-4 py-3"> |
| 199 | {editing ? ( |
| 200 | <MarkdownEditor intent={editIntent} body={body} commentId={commentId} onDone={() => setEditing(false)} submit="Save" /> |
| 201 | ) : html ? ( |
| 202 | <article className="markdown" dangerouslySetInnerHTML={{ __html: html }} /> |
| 203 | ) : ( |
| 204 | <p className="m-0 text-muted italic">{emptyText}</p> |
| 205 | )} |
| 206 | </LayerCard.Primary> |
| 207 | </LayerCard> |
| 208 | {commentId !== undefined && canDelete && <DeleteComment commentId={commentId} open={deleting} onOpenChange={setDeleting} />} |
| 209 | </TimelineRow> |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | function eventIcon(kind: Event["kind"]) { |
| 214 | switch (kind) { |
| 215 | case "task.closed": |
| 216 | return <CheckCircleIcon size={14} weight="fill" />; |
| 217 | case "task.reopened": |
| 218 | return <RecordIcon size={14} className="text-success" />; |
| 219 | case "task.assigned": |
| 220 | case "task.unassigned": |
| 221 | return <UserCircleIcon size={14} />; |
| 222 | case "task.transferred": |
| 223 | return <ArrowsLeftRightIcon size={14} />; |
| 224 | case "task.labeled": |
| 225 | case "task.unlabeled": |
| 226 | return <TagIcon size={14} />; |
| 227 | default: |
| 228 | return <TextAaIcon size={14} />; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | function EventDescription({ event, repo }: { event: Event; repo: string }) { |
| 233 | switch (event.kind) { |
| 234 | case "task.closed": |
| 235 | return event.data.pull ? ( |
| 236 | <> |
| 237 | closed this by merging{" "} |
| 238 | <Link to={pullLink(repo, event.data.pull)} className="font-medium text-accent-text"> |
| 239 | #{event.data.pull} |
| 240 | </Link> |
| 241 | </> |
| 242 | ) : ( |
| 243 | <>closed this</> |
| 244 | ); |
| 245 | case "task.reopened": |
| 246 | return <>reopened this</>; |
| 247 | case "task.assigned": |
| 248 | return ( |
| 249 | <> |
| 250 | assigned <span className="font-medium text-ink">{event.data.assignee.name}</span> |
| 251 | </> |
| 252 | ); |
| 253 | case "task.unassigned": |
| 254 | return ( |
| 255 | <> |
| 256 | unassigned <span className="font-medium text-ink">{event.data.assignee.name}</span> |
| 257 | </> |
| 258 | ); |
| 259 | case "task.transferred": |
| 260 | return ( |
| 261 | <> |
| 262 | transferred this from <span className="font-medium text-ink">{event.data.from.repo}</span> to{" "} |
| 263 | <span className="font-medium text-ink">{event.data.to.repo}</span> |
| 264 | </> |
| 265 | ); |
| 266 | case "task.labeled": |
| 267 | return ( |
| 268 | <> |
| 269 | added <LabelBadge label={event.data.label} /> |
| 270 | </> |
| 271 | ); |
| 272 | case "task.unlabeled": |
| 273 | return ( |
| 274 | <> |
| 275 | removed <LabelBadge label={event.data.label} /> |
| 276 | </> |
| 277 | ); |
| 278 | case "task.renamed": |
| 279 | return ( |
| 280 | <> |
| 281 | changed the title <span className="line-through">{event.data.from}</span> <span className="font-medium text-ink">{event.data.to}</span> |
| 282 | </> |
| 283 | ); |
| 284 | default: |
| 285 | return null; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | function TaskEvent({ event, repo, now: current }: { event: Event; repo: string; now: number }) { |
| 290 | return ( |
| 291 | <EventLine by={event.by} icon={eventIcon(event.kind)}> |
| 292 | <span className="min-w-0"> |
| 293 | <EventDescription event={event} repo={repo} /> |
| 294 | </span> |
| 295 | <Ago at={event.at} now={current} /> |
| 296 | </EventLine> |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | function Composer({ status }: { status: "open" | "closed" }) { |
| 301 | const fetcher = useFetcher<typeof action>(); |
| 302 | const [body, setBody] = useState(""); |
| 303 | const [round, setRound] = useState(0); |
| 304 | const busy = fetcher.state !== "idle"; |
| 305 | useOnSuccess(fetcher, (answer) => { |
| 306 | if (answer.intent !== "comment") return; |
| 307 | setBody(""); |
| 308 | setRound((value) => value + 1); |
| 309 | }); |
| 310 | const toggleLabel = status === "open" ? (body ? "Close with comment" : "Close task") : body ? "Reopen with comment" : "Reopen task"; |
| 311 | |
| 312 | return ( |
| 313 | <TimelineRow tone="event"> |
| 314 | <LayerCard className="p-4"> |
| 315 | <fetcher.Form key={round} method="post" className="flex flex-col gap-3"> |
| 316 | <input type="hidden" name="intent" value="comment" /> |
| 317 | <InputArea label="Add a comment" name="body" value={body} onValueChange={setBody} maxLength={MAX_BODY_LENGTH} rows={4} description="Markdown is supported." /> |
| 318 | {fetcher.data?.intent === "comment" && fetcher.data.error && <Banner variant="error" description={fetcher.data.error} />} |
| 319 | <div className="flex flex-wrap justify-end gap-2"> |
| 320 | <Button type="submit" name="toggle" value="1" variant="secondary" icon={<StatusIcon status={status === "open" ? "closed" : "open"} size={16} />} loading={busy && fetcher.formData?.get("toggle") === "1"}> |
| 321 | {toggleLabel} |
| 322 | </Button> |
| 323 | <Button type="submit" variant="primary" disabled={!body.trim()} loading={busy && fetcher.formData?.get("toggle") !== "1"}> |
| 324 | Comment |
| 325 | </Button> |
| 326 | </div> |
| 327 | </fetcher.Form> |
| 328 | </LayerCard> |
| 329 | </TimelineRow> |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | function settingsTrigger(label: string) { |
| 334 | return <GearButton label={label} />; |
| 335 | } |
| 336 | |
| 337 | function SinglePicker({ intent, field, label, options, current, empty }: { intent: string; field: string; label: string; options: readonly Choice[]; current: string | null; empty?: string }) { |
| 338 | const fetcher = useFetcher<typeof action>(); |
| 339 | const pending = fetcher.formData?.get(field); |
| 340 | const value = typeof pending === "string" ? pending : (current ?? NONE); |
| 341 | return ( |
| 342 | <DropdownMenu> |
| 343 | <DropdownMenu.Trigger render={settingsTrigger(label)} /> |
| 344 | <DropdownMenu.Content align="end" className="max-h-96 min-w-56 overflow-y-auto"> |
| 345 | <div className="caption px-2 py-1.5 text-faint">{label}</div> |
| 346 | <DropdownMenu.RadioGroup value={value} onValueChange={(next) => fetcher.submit({ intent, [field]: String(next) }, { method: "post" })}> |
| 347 | {empty && <DropdownMenu.RadioItem value={NONE}>{empty}</DropdownMenu.RadioItem>} |
| 348 | {options.map((option) => ( |
| 349 | <DropdownMenu.RadioItem key={option.id} value={option.id}> |
| 350 | {option.name} |
| 351 | </DropdownMenu.RadioItem> |
| 352 | ))} |
| 353 | </DropdownMenu.RadioGroup> |
| 354 | </DropdownMenu.Content> |
| 355 | </DropdownMenu> |
| 356 | ); |
| 357 | } |
| 358 | |
| 359 | function LabelsSection({ repo, all, current }: { repo: string; all: readonly Label[]; current: readonly Label[] }) { |
| 360 | const fetcher = useFetcher<typeof action>(); |
| 361 | const [selected, setSelected] = useState(current.map((label) => label.id)); |
| 362 | const creation = useLabelCreation(); |
| 363 | useEffect(() => setSelected(current.map((label) => label.id)), [current.map((label) => label.id).join()]); |
| 364 | const save = () => { |
| 365 | const before = current.map((label) => label.id).sort().join(); |
| 366 | if (before === [...selected].sort().join()) return; |
| 367 | const form = new FormData(); |
| 368 | form.set("intent", "labels"); |
| 369 | for (const id of selected) form.append("label", id); |
| 370 | fetcher.submit(form, { method: "post" }); |
| 371 | }; |
| 372 | const shown = all.filter((label) => selected.includes(label.id)); |
| 373 | return ( |
| 374 | <Field label="Labels" action={<LabelPicker labels={all} selected={selected} creation={creation} onToggle={(id, checked) => setSelected((previous) => (checked ? [...new Set([...previous, id])] : previous.filter((value) => value !== id)))} onClose={save} trigger={settingsTrigger("Edit labels")} />}> |
| 375 | {shown.length ? ( |
| 376 | <div className="flex flex-wrap gap-1.5"> |
| 377 | {shown.map((label) => ( |
| 378 | <LabelBadge key={label.id} label={label} href={`/${repo}/tasks?label=${encodeURIComponent(label.name)}`} /> |
| 379 | ))} |
| 380 | </div> |
| 381 | ) : ( |
| 382 | <span className="text-muted">None yet</span> |
| 383 | )} |
| 384 | </Field> |
| 385 | ); |
| 386 | } |
| 387 | |
| 388 | export default function TaskPage({ loaderData }: Route.ComponentProps) { |
| 389 | const { task, timeline, choices, now: current } = loaderData; |
| 390 | useLive(task.repo, task.number); |
| 391 | |
| 392 | return ( |
| 393 | <> |
| 394 | <div className="mb-5 pb-5 shadow-[inset_0_-1px_0_var(--hairline)]"> |
| 395 | <TitleEditor |
| 396 | title={task.title} |
| 397 | number={task.number} |
| 398 | meta={(edit) => ( |
| 399 | <div className="flex flex-wrap items-center gap-x-2.5 gap-y-2 text-compact text-muted"> |
| 400 | <StatusBadge status={task.status} /> |
| 401 | <span aria-hidden className="text-faint">·</span> |
| 402 | <span className="flex flex-wrap items-center gap-x-1.5 gap-y-1"> |
| 403 | <Avatar name={task.author.name} size="xs" /> |
| 404 | <span className="font-medium text-ink">{task.author.name}</span> opened this <Ago at={task.createdAt} now={current} /> |
| 405 | {task.comments > 0 && <span className="tabular-nums">· {task.comments} {task.comments === 1 ? "comment" : "comments"}</span>} |
| 406 | </span> |
| 407 | {edit && <span className="sm:ml-auto">{edit}</span>} |
| 408 | </div> |
| 409 | )} |
| 410 | /> |
| 411 | </div> |
| 412 | <div className="grid items-start gap-8 lg:grid-cols-[minmax(0,1fr)_17rem]"> |
| 413 | <Timeline> |
| 414 | <CommentCard |
| 415 | by={human(task.author.name)} |
| 416 | verb="opened this" |
| 417 | at={task.createdAt} |
| 418 | edited={task.updatedAt > task.createdAt && task.body !== ""} |
| 419 | html={task.bodyHtml} |
| 420 | body={task.body} |
| 421 | now={current} |
| 422 | editIntent="description" |
| 423 | canEdit |
| 424 | canDelete={false} |
| 425 | emptyText="No description provided." |
| 426 | /> |
| 427 | {timeline.map((event) => |
| 428 | event.kind === "task.opened" ? null : event.kind === "task.commented" ? ( |
| 429 | <CommentCard |
| 430 | key={event.id} |
| 431 | by={event.by} |
| 432 | verb="commented" |
| 433 | at={event.at} |
| 434 | edited={event.data.editedAt !== null} |
| 435 | html={event.data.html} |
| 436 | body={event.data.body} |
| 437 | now={current} |
| 438 | editIntent="edit-comment" |
| 439 | commentId={event.id} |
| 440 | canEdit={event.can?.edit ?? false} |
| 441 | canDelete={event.can?.delete ?? false} |
| 442 | /> |
| 443 | ) : ( |
| 444 | <TaskEvent key={event.id} event={event} repo={task.repo} now={current} /> |
| 445 | ), |
| 446 | )} |
| 447 | <Composer status={task.status} /> |
| 448 | </Timeline> |
| 449 | <aside className="flex flex-col"> |
| 450 | <Field label="Assignee" action={<SinglePicker intent="assignee" field="assignee" label="Assign to" options={choices.people} current={task.assignee?.id ?? null} empty="Unassigned" />}> |
| 451 | {task.assignee ? ( |
| 452 | <span className="flex items-center gap-2"> |
| 453 | <Avatar name={task.assignee.name} size="xs" /> {task.assignee.name} |
| 454 | </span> |
| 455 | ) : ( |
| 456 | <span className="text-muted">No one assigned</span> |
| 457 | )} |
| 458 | </Field> |
| 459 | <LabelsSection repo={task.repo} all={choices.labels} current={task.labels} /> |
| 460 | <Field label="Repository" action={<SinglePicker intent="repo" field="repo" label="Transfer to repository" options={choices.repos} current={task.repo} />}> |
| 461 | <Link to={`/${task.repo}`} className="text-accent-text hover:underline"> |
| 462 | {task.repo} |
| 463 | </Link> |
| 464 | </Field> |
| 465 | </aside> |
| 466 | </div> |
| 467 | </> |
| 468 | ); |
| 469 | } |