| 1 | import { |
| 2 | CircleCheck, |
| 3 | CircleDot, |
| 4 | MessageSquare, |
| 5 | PlusIcon, |
| 6 | TagsIcon, |
| 7 | } from "lucide-react"; |
| 8 | import Link from "next/link |
| 9 | import { notFound } |
| 10 | |
| 11 | import { UserLink } |
| 12 | import { FromNowHoverCard } |
| 13 | import type { IssueFilter, |
| 14 | import { issueFilters, issueSorts |
| 15 | import { LabelBadge } |
| 16 | import { Button, buttonVariants |
| 17 | import { Input } |
| 18 | import { |
| 19 | Select, |
| 20 | SelectContent, |
| 21 | SelectItem, |
| 22 | SelectTrigger, |
| 23 | SelectValue, |
| 24 | } from "@/components/ui/select"; |
| 25 | import { createServerClient } |
| 26 | import { cn } |
| 27 | |
| 28 | const PAGE_SIZE = 20 |
| 29 | |
| 30 | function stringParam(value: |
| 31 | return typeof value |
| 32 | } |
| 33 | |
| 34 | export default async function |
| 35 | params, |
| 36 | searchParams, |
| 37 | }: PageProps< |
| 38 | const { username, |
| 39 | const query = |
| 40 | |
| 41 | const filter: |
| 42 | ? (query.state |
| 43 | : "open"; |
| 44 | const sort: |
| 45 | ? (query.sort |
| 46 | : "created"; |
| 47 | const direction = |
| 48 | const q = |
| 49 | const labels = |
| 50 | const assignee = |
| 51 | const author = |
| 52 | const cursor = |
| 53 | |
| 54 | const client = |
| 55 | const [issues, |
| 56 | client.GET("/api/repositories/{username}/{repo}/issues |
| 57 | params: { |
| 58 | path: { username, |
| 59 | query: { |
| 60 | state: filter, |
| 61 | limit: PAGE_SIZE, |
| 62 | cursor, |
| 63 | q, |
| 64 | labels, |
| 65 | assignee, |
| 66 | author, |
| 67 | sort, |
| 68 | direction, |
| 69 | }, |
| 70 | }, |
| 71 | }), |
| 72 | client.GET("/api/repositories/{username}/{repo}/labels |
| 73 | params: { path: { |
| 74 | }), |
| 75 | ]); |
| 76 | |
| 77 | if (issues.response |
| 78 | if (!issues.data |
| 79 | throw new Error( |
| 80 | } |
| 81 | |
| 82 | const base = |
| 83 | |
| 84 | function href(next |
| 85 | const merged: |
| 86 | const current: |
| 87 | state: filter === |
| 88 | q, |
| 89 | labels, |
| 90 | assignee, |
| 91 | author, |
| 92 | sort: sort === |
| 93 | direction: direction === |
| 94 | }; |
| 95 | for (const [key |
| 96 | if (value !== |
| 97 | } |
| 98 | const params = |
| 99 | const suffix = |
| 100 | return `${base}${ |
| 101 | } |
| 102 | |
| 103 | const selectedLabels = |
| 104 | .split(",") |
| 105 | .map((name) |
| 106 | .filter(Boolean); |
| 107 | |
| 108 | const repoLabels = |
| 109 | const filterChips = |
| 110 | ...repoLabels, |
| 111 | // A name left in the URL by a deleted label still needs a way out. |
| 112 | ...selectedLabels |
| 113 | .filter((name) |
| 114 | .map((name) |
| 115 | ]; |
| 116 | |
| 117 | function labelHref(name |
| 118 | const next = |
| 119 | ? selectedLabels.filter( |
| 120 | : [...selectedLabels, |
| 121 | return href({ |
| 122 | } |
| 123 | |
| 124 | return ( |
| 125 | <div className="flex flex-col gap-4 |
| 126 | <div className="flex flex-wrap items-center gap-2 |
| 127 | <div className="flex gap-1 |
| 128 | <Link |
| 129 | href={href({ |
| 130 | className={cn( |
| 131 | buttonVariants({ |
| 132 | variant: filter === |
| 133 | size: "sm", |
| 134 | }), |
| 135 | )} |
| 136 | > |
| 137 | <CircleDot data-icon=" |
| 138 | Open |
| 139 | <span className="ml-1 tabular-nums |
| 140 | </Link> |
| 141 | <Link |
| 142 | href={href({ |
| 143 | className={cn( |
| 144 | buttonVariants({ |
| 145 | variant: filter === |
| 146 | size: "sm", |
| 147 | }), |
| 148 | )} |
| 149 | > |
| 150 | <CircleCheck data-icon=" |
| 151 | Closed |
| 152 | <span className="ml-1 tabular-nums |
| 153 | </Link> |
| 154 | </div> |
| 155 | |
| 156 | <Button size=" |
| 157 | <Link href={ |
| 158 | <TagsIcon data-icon=" |
| 159 | Labels |
| 160 | </Link> |
| 161 | </Button> |
| 162 | <Button size=" |
| 163 | <Link href={ |
| 164 | <PlusIcon data-icon=" |
| 165 | New issue |
| 166 | </Link> |
| 167 | </Button> |
| 168 | </div> |
| 169 | |
| 170 | <form action={base |
| 171 | <Input |
| 172 | name="q" |
| 173 | defaultValue={q ?? |
| 174 | placeholder="Search title and body…" |
| 175 | autoComplete="off" |
| 176 | className="min-w-0 flex-1 sm:max-w-xs" |
| 177 | /> |
| 178 | {filter !== "open |
| 179 | <input type="hidden |
| 180 | )} |
| 181 | {assignee && <input |
| 182 | {author && <input |
| 183 | {labels && <input |
| 184 | <Select name=" |
| 185 | <SelectTrigger className=" |
| 186 | <SelectValue placeholder=" |
| 187 | </SelectTrigger> |
| 188 | <SelectContent> |
| 189 | {issueSorts.map(( |
| 190 | <SelectItem key={ |
| 191 | <span className="capitalize |
| 192 | </SelectItem> |
| 193 | ))} |
| 194 | </SelectContent> |
| 195 | </Select> |
| 196 | <Button type=" |
| 197 | Search |
| 198 | </Button> |
| 199 | {(q || labels |
| 200 | <Link |
| 201 | href={href({ |
| 202 | q: undefined, |
| 203 | labels: undefined, |
| 204 | assignee: undefined, |
| 205 | author: undefined, |
| 206 | })} |
| 207 | className={buttonVariants({ |
| 208 | > |
| 209 | Clear |
| 210 | </Link> |
| 211 | )} |
| 212 | </form> |
| 213 | |
| 214 | {filterChips.length > |
| 215 | <div className="flex flex-wrap items-center gap-1.5 |
| 216 | <span className="text-xs text-muted-foreground |
| 217 | Filter by label: |
| 218 | </span> |
| 219 | {filterChips.map(( |
| 220 | <Link key={ |
| 221 | <LabelBadge |
| 222 | label={label} |
| 223 | className={cn( |
| 224 | "cursor-pointer", |
| 225 | selectedLabels.includes(label |
| 226 | ? "ring-2 ring-primary ring-offset-1 ring-offset-background" |
| 227 | : "opacity-75 hover:opacity-100", |
| 228 | )} |
| 229 | /> |
| 230 | </Link> |
| 231 | ))} |
| 232 | </div> |
| 233 | )} |
| 234 | |
| 235 | <div className="overflow-hidden rounded-lg border |
| 236 | {issues.data.issues |
| 237 | <p className="py-16 text-center text-sm text-muted-foreground |
| 238 | {filter === "all |
| 239 | ? "This repository has no issues yet." |
| 240 | : `There are no ${filter} |
| 241 | </p> |
| 242 | ) : ( |
| 243 | <ul className="divide-y |
| 244 | {issues.data.issues |
| 245 | <li |
| 246 | key={issue.id |
| 247 | className="flex items-start gap-3 px-4 py-3 text-sm hover:bg-muted/40" |
| 248 | > |
| 249 | {issue.state === |
| 250 | <CircleDot className=" |
| 251 | ) : ( |
| 252 | <CircleCheck className=" |
| 253 | )} |
| 254 | |
| 255 | <div className="min-w-0 flex-1 |
| 256 | <div className="flex flex-wrap items-center gap-x-2 gap-y-1 |
| 257 | <Link |
| 258 | href={`${base |
| 259 | className="font-medium hover:underline" |
| 260 | > |
| 261 | {issue.title} |
| 262 | </Link> |
| 263 | {issue.labels.map |
| 264 | <Link key={ |
| 265 | <LabelBadge label={ |
| 266 | </Link> |
| 267 | ))} |
| 268 | </div> |
| 269 | <p className="truncate text-xs text-muted-foreground |
| 270 | #{issue.number} opened |
| 271 | <FromNowHoverCard date={ |
| 272 | <UserLink |
| 273 | username={issue.authorUsername |
| 274 | className="font-normal text-muted-foreground" |
| 275 | /> |
| 276 | {issue.assignees.length |
| 277 | ` · assigned to ${issue.assignees |
| 278 | </p> |
| 279 | </div> |
| 280 | |
| 281 | {issue.commentCount > |
| 282 | <span className="flex shrink-0 items-center gap-1 text-xs text-muted-foreground |
| 283 | <MessageSquare className=" |
| 284 | <span className="tabular-nums |
| 285 | </span> |
| 286 | )} |
| 287 | </li> |
| 288 | ))} |
| 289 | </ul> |
| 290 | )} |
| 291 | </div> |
| 292 | |
| 293 | {issues.data.nextCursor |
| 294 | <div className="flex justify-end |
| 295 | <Link |
| 296 | href={href({ |
| 297 | className={buttonVariants({ |
| 298 | > |
| 299 | Older |
| 300 | </Link> |
| 301 | </div> |
| 302 | )} |
| 303 | </div> |
| 304 | ); |
| 305 | } |