Skip to content
Myr

project-header.tsx

This file

Path

apps/web/src/features/project/project-header.tsx

347 lines · 11.0 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 1–347why, one step back
1/**
2 * A project's one header (ADR 0041): its name, posture, description, facts3 * and three actions, then its eight tabs. Every page of the project draws it4 * the same; only the current tab moves.5 */6import { Brand } from "@repo/brand";7import type { Handle, ProjectName } from "@repo/protocol/ids";8import type { Posture } from "@repo/protocol/posture";9import type { DateTime } from "effect";10import { ChevronDown, Tag } from "lucide-react";11import type { ReactNode } from "react";1213import { buttonVariants } from "../../components/ui/button.tsx";14import {15 Popover,16 PopoverContent,17 PopoverTrigger,18} from "../../components/ui/popover.tsx";19import { plural } from "../front/facts.tsx";20import { LINK } from "../shared/classes.ts";21import { CommandLine } from "../shared/command.tsx";22import { HandleLink } from "../shared/handle.tsx";23import { AppLink } from "../shared/link.tsx";24import { PostureBadge } from "../shared/posture.tsx";25import { GUTTER } from "../shared/shell/gutter.ts";26import { projectSections } from "../shared/shell/sections.ts";27import type { ProjectTab } from "../shared/shell/sections.ts";28import { When } from "../shared/time.tsx";29import { postureWord } from "./posture-label.ts";3031export interface ProjectHeaderFacts {32 readonly project: ProjectName;33 readonly now: DateTime.Utc;34 readonly deciders?: readonly Handle[] | undefined;35 readonly landedAt?: DateTime.Utc | undefined;36 readonly latestTag?: string | undefined;37 readonly lede?: string | undefined;38 readonly open?:39 | { readonly asks: number; readonly takeable: number }40 | undefined;41 readonly posture?:42 | { readonly label: string; readonly posture: Posture }43 | undefined;44}4546/** A source path's owner is everything before its last slash (`github.com/pallets`). */47const ownerAndName = (project: ProjectName) => {48 const at = project.lastIndexOf("/");4950 return { name: project.slice(at + 1), owner: project.slice(0, at) };51};5253export const cloneCommand = (platformUrl: string, project: ProjectName) =>54 `git clone ${platformUrl}/${project}.git`;5556/**57 * Clone with git, or claim an ask to answer it: a claim brings its own fork.58 * The Clone popover and Overview's clone card say the same thing.59 */60export const CloneSteps = ({61 platformUrl,62 project,63}: {64 readonly platformUrl: string;65 readonly project: ProjectName;66}) => (67 <div className="flex min-w-0 flex-col gap-3" data-steps="clone">68 <div className="flex min-w-0 flex-col gap-1">69 <p className="text-ui">Clone it with git</p>70 <CommandLine command={cloneCommand(platformUrl, project)} />71 </div>72 <div className="flex min-w-0 flex-col gap-1">73 <p className="text-ui">Or claim an ask to answer it</p>74 <CommandLine command={`${Brand.cli} claim <ask> --project ${project}`} />75 </div>76 <p className="text-ui text-muted-foreground">77 A claim comes with a fork to push your answer to, so there is nothing to78 fork by hand.79 </p>80 </div>81);8283/**84 * The CLI has no command that follows a project, so Watch says what already85 * follows it (this page, and the events) and how to read your own part in it.86 */87export const WatchSteps = ({ project }: { readonly project: ProjectName }) => (88 <div className="flex min-w-0 flex-col gap-3" data-steps="watch">89 <p className="text-ui">90 This page follows the project as it happens: claims, pushes, verdicts and91 landings arrive without a reload.92 </p>93 <div className="flex min-w-0 flex-col gap-1">94 <p className="text-ui">Your own claims and answers here</p>95 <CommandLine command={`${Brand.cli} status --project ${project}`} />96 </div>97 <AppLink className={`text-ui ${LINK} self-start`} to="/docs/events">98 Follow its events from a program99 </AppLink>100 </div>101);102103const ACTION = buttonVariants({ variant: "outline" });104105const Action = ({106 children,107 label,108 primary = false,109 wide = false,110}: {111 readonly label: string;112 readonly primary?: boolean;113 readonly wide?: boolean;114 readonly children: ReactNode;115}) => (116 <Popover>117 <PopoverTrigger118 className={primary ? buttonVariants({ variant: "default" }) : ACTION}119 data-action={label.toLowerCase()}120 >121 {label}122 <ChevronDown aria-hidden="true" strokeWidth={1.75} />123 </PopoverTrigger>124 <PopoverContent125 align="end"126 className={`${wide ? "w-[min(28rem,calc(100vw-2rem))]" : "w-[min(20rem,calc(100vw-2rem))]"} gap-3 p-3`}127 >128 {children}129 </PopoverContent>130 </Popover>131);132133const MenuLink = ({134 children,135 detail,136 hash,137 to,138}: {139 readonly to: string;140 readonly hash?: string;141 readonly detail: string;142 readonly children: string;143}) => (144 <AppLink145 className="hover:bg-surface -mx-1 flex flex-col gap-0.5 px-1 py-1.5 no-underline"146 to={to}147 {...(hash === undefined ? {} : { hash })}148 >149 <span className="text-ui font-medium">{children}</span>150 <span className="text-label text-muted-foreground">{detail}</span>151 </AppLink>152);153154const Actions = ({155 open,156 platformUrl,157 project,158}: {159 readonly open: ProjectHeaderFacts["open"];160 readonly platformUrl: string;161 readonly project: ProjectName;162}) => (163 <div className="flex shrink-0 flex-wrap items-center gap-2">164 <Action label="Watch">165 <WatchSteps project={project} />166 </Action>167 <Action label="Clone" wide>168 <CloneSteps platformUrl={platformUrl} project={project} />169 </Action>170 <Action label="Contribute" primary>171 <nav aria-label="Contribute" className="flex flex-col">172 <MenuLink173 detail={174 open === undefined175 ? "Its open asks"176 : `${plural(open.takeable, "ask", "asks")} open to take now`177 }178 hash="open-asks"179 to={`/${project}`}180 >181 Take an open ask182 </MenuLink>183 <MenuLink184 detail="A change you want, with a check the agent can draft"185 to={`/${project}/proposals/new`}186 >187 Propose an ask188 </MenuLink>189 <MenuLink190 detail="What lands here and who decides"191 to={`/${project}/people`}192 >193 Read the policy194 </MenuLink>195 </nav>196 </Action>197 </div>198);199200const Facts = ({ facts }: { readonly facts: ProjectHeaderFacts }) => {201 const { deciders = [], landedAt, latestTag, now, open } = facts;202203 const parts = [204 open === undefined ? undefined : (205 <span className="tabular-nums" key="asks">206 {plural(open.asks, "open ask", "open asks")}207 </span>208 ),209 landedAt === undefined ? undefined : (210 <span key="landed">211 Landed <When at={landedAt} now={now} />212 </span>213 ),214 latestTag === undefined ? undefined : (215 <span className="inline-flex items-center gap-1.5" key="tag">216 <Tag aria-hidden="true" className="size-3.5" strokeWidth={1.75} />217 <code className="text-mono font-mono">{latestTag}</code>218 </span>219 ),220 deciders.length === 0 ? undefined : (221 <span key="deciders">222 Decided by{" "}223 {deciders.map((handle, at) => (224 <span key={handle}>225 {at === 0 ? null : ", "}226 <HandleLink handle={handle} />227 </span>228 ))}229 </span>230 ),231 ].filter((part) => part !== undefined);232233 return parts.length === 0 ? null : (234 <p235 className="text-ui text-muted-foreground flex flex-wrap items-center gap-x-4 gap-y-1"236 data-project-facts=""237 >238 {parts}239 </p>240 );241};242243const TAB =244 "text-ui text-muted-foreground hover:text-foreground data-current:border-foreground data-current:text-foreground -mb-px flex h-(--tabs) shrink-0 items-center gap-1.5 border-b-2 border-transparent px-3 whitespace-nowrap no-underline transition-colors duration-[120ms] first:pl-0 focus-visible:outline-offset-[-2px] data-current:font-medium";245246/**247 * The router marks a link to the page being read. A tab that opens a region248 * of Overview counts the fragment, so the router marks it only in the browser249 * once the reader has opened that region; every other tab counts the path.250 */251const activeFor = (hash: string | undefined) => ({252 exact: true,253 includeHash: hash !== undefined,254 includeSearch: false,255});256257/**258 * Text tabs, the current one ink with the 2 px underline. The current tab259 * follows the page's path alone, so the server and the browser draw the same260 * mark, and a region of Overview opened by its tab leaves Overview current.261 */262export const ProjectTabs = ({263 current,264 open,265 project,266}: {267 readonly current: ProjectTab | undefined;268 readonly open?: ProjectHeaderFacts["open"];269 readonly project: ProjectName;270}) => (271 <nav272 aria-label={`${project} tabs`}273 className={`border-rule bg-background border-b ${GUTTER}`}274 data-project-tabs=""275 >276 <div className="flex [scrollbar-width:none] overflow-x-auto overscroll-x-contain">277 {projectSections(project).map(({ hash, label, tab, to }) => (278 <AppLink279 activeOptions={activeFor(hash)}280 aria-current={tab === current ? "page" : undefined}281 className={TAB}282 data-current={tab === current ? "" : undefined}283 key={tab}284 to={to}285 {...(hash === undefined ? {} : { hash })}286 >287 {label}288 {tab === "asks" && open !== undefined ? (289 <span className="text-label text-muted-foreground tabular-nums">290 {open.asks}291 </span>292 ) : null}293 </AppLink>294 ))}295 </div>296 </nav>297);298299export const ProjectHeader = ({300 current,301 header,302 platformUrl,303}: {304 readonly header: ProjectHeaderFacts;305 readonly current: ProjectTab | undefined;306 readonly platformUrl: string;307}) => {308 const { lede, open, posture, project } = header;309 const { name, owner } = ownerAndName(project);310 // Overview's title is the project; every other page titles its own subject.311 const Name = current === "overview" ? "h1" : "p";312313 return (314 // The project's frame sticks the header and its tabs under the top bar together.315 <div data-project-header="">316 <div317 className={`nav:flex-row nav:items-start nav:justify-between flex flex-col gap-4 pt-6 pb-5 ${GUTTER}`}318 >319 <div className="flex min-w-0 flex-col gap-2">320 <div className="flex flex-wrap items-center gap-x-3 gap-y-1">321 <Name className="text-headline min-w-0 break-words">322 <AppLink className="no-underline" to={`/${project}`}>323 <span className="text-muted-foreground font-normal">324 {owner} /325 </span>{" "}326 {name}327 </AppLink>328 </Name>329 {posture === undefined ? null : (330 <PostureBadge331 label={postureWord(posture.posture)}332 posture={posture.posture}333 title={posture.label}334 />335 )}336 </div>337 {lede === undefined ? null : (338 <p className="text-want max-w-(--measure)">{lede}</p>339 )}340 <Facts facts={header} />341 </div>342 <Actions open={open} platformUrl={platformUrl} project={project} />343 </div>344 <ProjectTabs current={current} open={open} project={project} />345 </div>346 );347};