293 lines · 12.4 KBRaw
| 1 | import { Button, LinkButton } from "../ui/button"; |
| 2 | import { Badge, labelInk } from "../ui/feedback"; |
| 3 | import { Checkbox, Input } from "../ui/form"; |
| 4 | import { Tabs } from "../ui/navigation"; |
| 5 | import { Popover } from "../ui/overlay"; |
| 6 | import { Avatar } from "./brand"; |
| 7 | import { Empty } from "./empty"; |
| 8 | import { LayerCard } from "./surface"; |
| 9 | import { ChatCircleIcon, CheckCircleIcon, CheckSquareIcon, GearIcon, PlusIcon, RecordIcon, TagIcon, XIcon } from "../icons"; |
| 10 | import { LABEL_COLORS, taskLink, type Label, type LabelColor, type Page, type TaskCounts, type TaskStatus, type TaskSummary } from "@refs/backend/contract"; |
| 11 | import { useEffect, useState } from "react"; |
| 12 | import { Ago } from "./ago"; |
| 13 | import { Link, useNavigate } from "../provider"; |
| 14 | |
| 15 | export function StatusIcon({ status, size = 16 }: { status: TaskStatus; size?: number }) { |
| 16 | return status === "open" ? ( |
| 17 | <RecordIcon size={size} className="shrink-0 text-success" aria-label="Open" /> |
| 18 | ) : ( |
| 19 | <CheckCircleIcon size={size} className="shrink-0 text-muted" aria-label="Closed" /> |
| 20 | ); |
| 21 | } |
| 22 | |
| 23 | export function StatusBadge({ status }: { status: TaskStatus }) { |
| 24 | return ( |
| 25 | <span className={`inline-flex items-center gap-1 font-medium ${status === "open" ? "text-success" : "text-muted"}`}> |
| 26 | <StatusIcon status={status} size={14} /> |
| 27 | {status === "open" ? "Open" : "Closed"} |
| 28 | </span> |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | export function LabelBadge({ label, href }: { label: Pick<Label, "name" | "color">; href?: string }) { |
| 33 | const badge = <Badge variant={label.color}>{label.name}</Badge>; |
| 34 | return href ? ( |
| 35 | <Link href={href} className="inline-flex"> |
| 36 | {badge} |
| 37 | </Link> |
| 38 | ) : ( |
| 39 | badge |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | /** Creates a label from the picker. Present only for people who may manage labels. */ |
| 44 | export interface LabelCreation { |
| 45 | readonly create: (name: string, color: LabelColor) => void; |
| 46 | readonly pending: boolean; |
| 47 | readonly error?: string; |
| 48 | } |
| 49 | |
| 50 | function defaultColor(name: string): LabelColor { |
| 51 | const hash = [...name].reduce((total, char) => (total * 31 + char.charCodeAt(0)) >>> 0, 7); |
| 52 | return LABEL_COLORS[hash % (LABEL_COLORS.length - 1)]; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Picks the labels of a task. Type to filter the list; a name that no label has can be created in place and is |
| 57 | * applied at once, so the reader never leaves the task to make a label. |
| 58 | */ |
| 59 | export function LabelPicker({ labels, selected, onToggle, onClose, creation, trigger }: { labels: readonly Label[]; selected: readonly string[]; onToggle: (id: string, checked: boolean) => void; onClose?: () => void; creation?: LabelCreation; trigger: React.ReactElement }) { |
| 60 | const [query, setQuery] = useState(""); |
| 61 | const [color, setColor] = useState<LabelColor | null>(null); |
| 62 | const [created, setCreated] = useState<string | null>(null); |
| 63 | const name = query.trim(); |
| 64 | const same = (label: Label, text: string) => label.name.toLowerCase() === text.toLowerCase(); |
| 65 | const shown = labels.filter((label) => label.name.toLowerCase().includes(name.toLowerCase())); |
| 66 | const canCreate = creation !== undefined && name.length > 0 && !labels.some((label) => same(label, name)); |
| 67 | const chosenColor = color ?? defaultColor(name); |
| 68 | |
| 69 | // The new label arrives with the next page data. Apply it then, and clear the field for the next one. |
| 70 | useEffect(() => { |
| 71 | const label = created === null ? undefined : labels.find((item) => same(item, created)); |
| 72 | if (!label) return; |
| 73 | onToggle(label.id, true); |
| 74 | setCreated(null); |
| 75 | setQuery(""); |
| 76 | setColor(null); |
| 77 | }, [labels, created]); |
| 78 | |
| 79 | const create = () => { |
| 80 | if (!canCreate || creation.pending) return; |
| 81 | setCreated(name); |
| 82 | creation.create(name, chosenColor); |
| 83 | }; |
| 84 | const onEnter = () => { |
| 85 | if (canCreate) create(); |
| 86 | else if (shown.length === 1) onToggle(shown[0].id, !selected.includes(shown[0].id)); |
| 87 | }; |
| 88 | |
| 89 | return ( |
| 90 | <Popover |
| 91 | onOpenChange={(open) => { |
| 92 | if (open) return; |
| 93 | setQuery(""); |
| 94 | setColor(null); |
| 95 | onClose?.(); |
| 96 | }} |
| 97 | > |
| 98 | <Popover.Trigger render={trigger} /> |
| 99 | <Popover.Content align="start" className="flex w-72 max-w-[calc(100vw-2rem)] flex-col p-0"> |
| 100 | <div className="p-2 shadow-[inset_0_-1px_0_var(--hairline)]"> |
| 101 | <Input |
| 102 | aria-label={creation ? "Filter or create a label" : "Filter labels"} |
| 103 | placeholder={creation ? "Filter or create a label" : "Filter labels"} |
| 104 | value={query} |
| 105 | maxLength={50} |
| 106 | autoComplete="off" |
| 107 | onChange={(event) => setQuery(event.target.value)} |
| 108 | onKeyDown={(event) => { |
| 109 | if (event.key !== "Enter") return; |
| 110 | event.preventDefault(); |
| 111 | onEnter(); |
| 112 | }} |
| 113 | /> |
| 114 | </div> |
| 115 | <div className="flex max-h-64 flex-col overflow-y-auto p-1"> |
| 116 | {shown.map((label) => ( |
| 117 | <Checkbox |
| 118 | key={label.id} |
| 119 | checked={selected.includes(label.id)} |
| 120 | onCheckedChange={(checked) => onToggle(label.id, checked)} |
| 121 | className="w-full rounded-control px-2 py-1.5 transition-colors duration-150 hover:bg-hover pointer-coarse:py-2.5" |
| 122 | label={ |
| 123 | <span className="flex min-w-0 flex-col gap-0.5"> |
| 124 | <LabelBadge label={label} /> |
| 125 | {label.description && <span className="text-xs text-faint">{label.description}</span>} |
| 126 | </span> |
| 127 | } |
| 128 | /> |
| 129 | ))} |
| 130 | {shown.length === 0 && !canCreate && <p className="m-0 px-2 py-2 text-compact text-muted">{labels.length === 0 ? (creation ? "No labels yet. Type a name to create the first one." : "No labels yet.") : `No label matches "${name}".`}</p>} |
| 131 | {canCreate && ( |
| 132 | <div className="flex flex-col gap-2 px-2 py-2"> |
| 133 | <div role="radiogroup" aria-label="Label color" className="flex items-center gap-1.5"> |
| 134 | {LABEL_COLORS.map((option) => ( |
| 135 | <button |
| 136 | key={option} |
| 137 | type="button" |
| 138 | role="radio" |
| 139 | aria-checked={option === chosenColor} |
| 140 | aria-label={option} |
| 141 | onClick={() => setColor(option)} |
| 142 | className="hit-area flex size-5 cursor-pointer items-center justify-center rounded-full transition-shadow duration-150 aria-checked:shadow-[0_0_0_1.5px_var(--ink)]" |
| 143 | > |
| 144 | <span className="size-3 rounded-full" style={{ background: labelInk(option) }} /> |
| 145 | </button> |
| 146 | ))} |
| 147 | </div> |
| 148 | <Button type="button" variant="secondary" size="sm" icon={<PlusIcon />} loading={creation.pending} onClick={create} className="justify-start"> |
| 149 | <span className="truncate">Create label "{name}"</span> |
| 150 | </Button> |
| 151 | {creation.error && !creation.pending && ( |
| 152 | <p role="alert" className="m-0 text-xs text-danger"> |
| 153 | {creation.error} |
| 154 | </p> |
| 155 | )} |
| 156 | </div> |
| 157 | )} |
| 158 | </div> |
| 159 | <div className="p-1 shadow-[inset_0_1px_0_var(--hairline)]"> |
| 160 | <Link href="/tasks/labels" className="flex items-center gap-2 rounded-control px-2 py-1.5 text-compact text-muted transition-colors duration-150 hover:bg-hover hover:text-ink"> |
| 161 | <GearIcon size={16} /> |
| 162 | Manage labels |
| 163 | </Link> |
| 164 | </div> |
| 165 | </Popover.Content> |
| 166 | </Popover> |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | function listHref(base: string, params: Record<string, string | undefined>): string { |
| 171 | const search = new URLSearchParams(Object.entries(params).filter((entry): entry is [string, string] => Boolean(entry[1]))); |
| 172 | const query = search.toString(); |
| 173 | return query ? `${base}?${query}` : base; |
| 174 | } |
| 175 | |
| 176 | export function TaskList({ |
| 177 | page, |
| 178 | counts, |
| 179 | status, |
| 180 | label, |
| 181 | base, |
| 182 | showRepo, |
| 183 | newHref, |
| 184 | now, |
| 185 | }: { |
| 186 | page: Page<TaskSummary>; |
| 187 | counts: TaskCounts; |
| 188 | status: TaskStatus; |
| 189 | label: string | null; |
| 190 | base: string; |
| 191 | showRepo: boolean; |
| 192 | newHref: string; |
| 193 | now: number; |
| 194 | }) { |
| 195 | const navigate = useNavigate(); |
| 196 | const tasks = page.items; |
| 197 | const stateParam = (value: TaskStatus) => (value === "closed" ? "closed" : undefined); |
| 198 | return ( |
| 199 | <div className="flex flex-col gap-4"> |
| 200 | <div className="flex flex-wrap items-center justify-between gap-3"> |
| 201 | <div className="flex flex-wrap items-center gap-3"> |
| 202 | <Tabs |
| 203 | variant="segmented" |
| 204 | value={status} |
| 205 | onValueChange={(value) => navigate(listHref(base, { state: stateParam(value as TaskStatus), label: label ?? undefined }))} |
| 206 | tabs={[ |
| 207 | { value: "open", label: `${counts.open} Open` }, |
| 208 | { value: "closed", label: `${counts.closed} Closed` }, |
| 209 | ]} |
| 210 | /> |
| 211 | {label && ( |
| 212 | <span className="flex items-center gap-1.5 text-compact text-muted"> |
| 213 | Label |
| 214 | <Badge variant="secondary">{label}</Badge> |
| 215 | <Link href={listHref(base, { state: stateParam(status) })} aria-label="Clear label filter" className="hit-area flex hover:text-accent-text"> |
| 216 | <XIcon size={14} /> |
| 217 | </Link> |
| 218 | </span> |
| 219 | )} |
| 220 | </div> |
| 221 | <div className="flex items-center gap-2"> |
| 222 | <LinkButton variant="secondary" size="sm" href="/tasks/labels" icon={<TagIcon />}> |
| 223 | Labels |
| 224 | </LinkButton> |
| 225 | <LinkButton variant="primary" size="sm" href={newHref} icon={<PlusIcon />}> |
| 226 | New task |
| 227 | </LinkButton> |
| 228 | </div> |
| 229 | </div> |
| 230 | {tasks.length === 0 ? ( |
| 231 | <Empty |
| 232 | icon={<CheckSquareIcon size={40} />} |
| 233 | title={status === "open" ? "No open tasks" : "No closed tasks"} |
| 234 | description={label ? `Nothing ${status} is labelled "${label}".` : status === "open" ? "Create a task to track work." : "Closed tasks show up here."} |
| 235 | contents={ |
| 236 | status === "open" && !label && ( |
| 237 | <LinkButton variant="secondary" href={newHref} icon={<PlusIcon />}> |
| 238 | New task |
| 239 | </LinkButton> |
| 240 | ) |
| 241 | } |
| 242 | /> |
| 243 | ) : ( |
| 244 | <LayerCard> |
| 245 | <ul className="m-0 list-none p-0"> |
| 246 | {tasks.map((task) => ( |
| 247 | <li key={`${task.repo}#${task.number}`} className="flex items-start gap-3 px-4 py-2.5 shadow-[inset_0_-1px_0_var(--hairline)] transition-colors duration-150 last:shadow-none hover:bg-hover/50"> |
| 248 | <span className="mt-[3px]"> |
| 249 | <StatusIcon status={task.status} /> |
| 250 | </span> |
| 251 | <div className="flex min-w-0 flex-1 flex-col gap-0.5"> |
| 252 | <div className="flex flex-wrap items-center gap-x-2 gap-y-1"> |
| 253 | <Link href={taskLink(task.repo, task.number)} className="text-base font-medium text-ink hover:text-accent-text"> |
| 254 | {task.title} |
| 255 | </Link> |
| 256 | {task.labels.map((item) => ( |
| 257 | <LabelBadge key={item.id} label={item} href={listHref(base, { state: stateParam(status), label: item.name })} /> |
| 258 | ))} |
| 259 | </div> |
| 260 | <span className="text-xs text-muted"> |
| 261 | {showRepo && ( |
| 262 | <> |
| 263 | <Link href={`/${task.repo}/tasks`} className="hover:text-accent-text"> |
| 264 | {task.repo} |
| 265 | </Link>{" "} |
| 266 | </> |
| 267 | )} |
| 268 | <span className="tabular-nums">#{task.number}</span> {task.status === "open" ? "opened" : "closed"} <Ago at={task.status === "open" ? task.createdAt : (task.closedAt ?? task.createdAt)} now={now} /> by {task.author.name} |
| 269 | </span> |
| 270 | </div> |
| 271 | <div className="flex shrink-0 items-center gap-3 text-compact text-muted"> |
| 272 | {task.assignee && ( |
| 273 | <span className="flex items-center gap-1.5"> |
| 274 | <Avatar name={task.assignee.name} size="xs" /> |
| 275 | <span className="hidden sm:inline">{task.assignee.name}</span> |
| 276 | </span> |
| 277 | )} |
| 278 | {task.comments > 0 && ( |
| 279 | <Link href={taskLink(task.repo, task.number)} className="flex items-center gap-1 tabular-nums hover:text-accent-text" aria-label={`${task.comments} comments`}> |
| 280 | <ChatCircleIcon size={14} /> {task.comments} |
| 281 | </Link> |
| 282 | )} |
| 283 | </div> |
| 284 | </li> |
| 285 | ))} |
| 286 | </ul> |
| 287 | </LayerCard> |
| 288 | )} |
| 289 | {page.next && <p className="m-0 text-compact text-muted">Showing the {tasks.length} most recent tasks.</p>} |
| 290 | </div> |
| 291 | ); |
| 292 | } |
| 293 |