Read a project header's newest tag in the browser, so no page's server render waits on the refs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by oscabriel · committed by oscabriel · parent 5e2dcf6 · Browse files · 4 files·+97 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+97 added−28 removed
- tests:+45 added−3 removed
- other:+52 added−25 removed
Provenance
- Commit
481725e- Tree
638dd4d- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent
5e2dcf6
4 files changed+97 added−28 removed
modified:apps/web/src/features/project/project-header.tsx+23 added
2 unmodified lines3 * and three actions, then its nine tabs. Every page of the project draws it4 * the same; only the current tab moves.5 */6 import { Brand } from "@repo/brand";7+import type { RefsView, TagName } from "@repo/protocol/domain/code";8 import type { Handle, ProjectName } from "@repo/protocol/ids";9 import type { Posture } from "@repo/protocol/posture";10+import { Option } from "effect";11 import type { DateTime } from "effect";12 import { ChevronDown, Tag } from "lucide-react";13 import type { ReactNode } from "react";14 29 unmodified lines44 | { readonly label: string; readonly posture: Posture }45 | undefined;46 }47 48+/**49+ * Newest by name, not by date: a tag carries only its name and commit. Its50+ * numbers sort as numbers (`v0.10.0` is newer than `v0.9.0`), but a51+ * pre-release such as `v2.0.0-beta` reads as newer than `v2.0.0`, and a tag52+ * of no version shape wins or loses by its letters. Once a tag is read with53+ * its time, the header can say the newest in time.54+ */55+const byNewest = (a: TagName, b: TagName) =>56+ b.localeCompare(a, "en", { numeric: true });57+58+/** The header's newest tag once the refs are read; none while they are not, or when there is none. */59+export const newestTag = (refs: Option.Option<RefsView>) =>60+ Option.match(refs, {61+ onNone: () => ({}),62+ onSome: ({ tags }) => {63+ const [latestTag] = tags.map((tag) => tag.name).toSorted(byNewest);64+65+ return latestTag === undefined ? {} : { latestTag };66+ },67+ });68+69 /** A source path's owner is everything before its last slash (`github.com/pallets`). */70 const ownerAndName = (project: ProjectName) => {71 const at = project.lastIndexOf("/");72 305 unmodified linesmodified:apps/web/src/routes/$owner/$project/route.tsx+24 added-2 removed
123452 unmodified lines8910111213141516171418192021222324252677 unmodified lines1041051061071081091101111121131141151161171181191201211221231241251269 unmodified lines13613713813911814014114214314427 unmodified linesimport { useAtomValue } from "@effect/atom-react";import { createFileRoute, getRouteApi, Outlet,2 unmodified lines} from "@tanstack/react-router";import { createServerFn } from "@tanstack/react-start";import { env } from "cloudflare:workers";import { Effect, Option, Schema } from "effect";import { AsyncResult } from "effect/reactivity";import { useMemo, useState } from "react";import type { ComponentProps } from "react";
import { refsAtom } from "../../../client/code.ts";import { ProjectFrame } from "../../../features/project/project-frame.tsx";import { ProjectHeader } from "../../../features/project/project-header.tsx";import { newestTag, ProjectHeader,} from "../../../features/project/project-header.tsx";import { inProjectFrame, projectTabOf,} from "../../../features/shared/shell/sections.ts";import { useShellName } from "../../../features/shared/shell/shells.ts";77 unmodified lines [encoded] );};
/** The header with its newest tag, read in the browser once the page is drawn: no page waits on the refs. */const HeaderWithTag = ({ header, ...props}: ComponentProps<typeof ProjectHeader>) => { const refs = useAtomValue(refsAtom(header.project));
return ( <ProjectHeader {...props} header={{ ...header, ...newestTag(AsyncResult.value(refs)) }} /> );};
const ProjectLayout = () => { const { platformUrl } = root.useLoaderData(); const shell = useShellName(); const header = useHeaderRead();9 unmodified lines return ( <ProjectFrame header={ header === undefined ? undefined : ( <ProjectHeader <HeaderWithTag current={projectTabOf(pathname)} header={header} platformUrl={platformUrl} />27 unmodified linesmodified:apps/web/src/server/project-page.ts+5 added-23 removed
1 import { Client, PolicyOf } from "@repo/protocol/contracts";2 import { Ask, AskListing } from "@repo/protocol/domain/ask";3-import { FileView, TagName, TreeListing } from "@repo/protocol/domain/code";3+import { FileView, TreeListing } from "@repo/protocol/domain/code";4 import { ProjectEvent } from "@repo/protocol/domain/event";5 import { Ledger } from "@repo/protocol/domain/visibility";6 import { Handle, ProjectName } from "@repo/protocol/ids";7 import { Posture } from "@repo/protocol/posture";21 unmodified lines29 export const decodeName = Schema.decodeUnknownOption(ProjectName);30 31 /**32 * What a project's header says on every one of its pages: only the facts it33- * shows, so the header's read stays small in each page's HTML.33+ * shows, so the header's read stays small in each page's HTML. Its newest tag34+ * is read in the browser: the refs are the slowest read a page would wait on.35 */36 export const ProjectHeaderData = Schema.Struct({37 deciders: Schema.optionalKey(Schema.Array(Handle)),38 landedAt: Schema.optionalKey(Schema.DateTimeUtcFromString),38- latestTag: Schema.optionalKey(TagName),39 lede: Schema.optionalKey(Schema.String),40 now: Schema.DateTimeUtcFromString,41 open: Schema.optionalKey(42 Schema.Struct({ asks: Schema.Int, takeable: Schema.Int })49 unmodified lines92 ),93 ledger: optional(client.ledger({ project })),94 log: optional(client.log({ limit: 1, project })),95 policy: optional(client.projectPolicy({ project })),96- refs: optional(client.listRefs({ project })),96 });97 98 type HeaderReads = ReturnType<typeof headerReads>;99 100 type HeaderRead = {101 readonly [K in keyof HeaderReads]: Effect.Success<HeaderReads[K]>;102 };103 105-/**106- * Newest by name, not by date: a tag carries only its name and commit. Its107- * numbers sort as numbers (`v0.10.0` is newer than `v0.9.0`), but a108- * pre-release such as `v2.0.0-beta` reads as newer than `v2.0.0`, and a tag109- * of no version shape wins or loses by its letters. Once a tag is read with110- * its time, the header can say the newest in time.111- */112-const byNewest = (a: TagName, b: TagName) =>113- b.localeCompare(a, "en", { numeric: true });114-104 /** A field only when its read gave it; the header never says what it was not told. */105 const onlySome = <A, B extends object>(106 read: Option.Option<A>,107 field: (value: A) => B108 ) => Option.match(read, { onNone: () => ({}), onSome: field });109 110 /**122- * The asks it counts, the ledger's posture, who decides, the newest tag,123- * main's last landing and the README's first sentence.111+ * The asks it counts, the ledger's posture, who decides, main's last landing112+ * and the README's first sentence.113 */114 const headerOf = (115 project: ProjectName,116 now: DateTime.Utc,29 unmodified lines146 ({ commits }) => commits[0]?.landing?.landedAt147 ),148 (landedAt) => ({ landedAt })149 ),161- ...onlySome(162- Option.flatMapNullishOr(163- read.refs,164- ({ tags }) => tags.map((tag) => tag.name).toSorted(byNewest)[0]165- ),166- (latestTag) => ({ latestTag })167- ),150 ...onlySome(lede, (sentence) => ({ lede: sentence })),151 ...onlySome(read.ledger, ({ posture, postureLabel }) => ({152 posture: { label: postureLabel, posture },153 })),95 unmodified linesmodified:apps/web/test/project.test.tsx+45 added-3 removed
12 unmodified lines13 import type { Ledger } from "@repo/protocol/domain/visibility";14 import {15 AreaName,16 AskId,17+ CommitSha,18 Handle,19 KindName,20 LayerName,21 ProjectName,26 unmodified lines48 import { postureWord } from "../src/features/project/posture-label.ts";49 import { ProjectFrame } from "../src/features/project/project-frame.tsx";50 import {51 CloneSteps,52+ newestTag,53 ProjectHeader,54 WatchSteps,55 } from "../src/features/project/project-header.tsx";56+import type { ProjectHeaderFacts } from "../src/features/project/project-header.tsx";57 import {58 ProjectView,59 heldBackOf,60 homeListing,673 unmodified lines734 Effect.provide(world)735 );736 737 /** A project page as its route draws it: the project's frame, its header (its tab from the path alone), then the page. */735-const framed = (url: string, header: ProjectHeaderData, body: ReactNode) =>738+const framed = (url: string, header: ProjectHeaderFacts, body: ReactNode) =>739 renderAt(740 url,741 <ProjectFrame742 header={118 unmodified lines861 label: "Open with sealed evidence · sealed checks in export",862 posture: "openWithSealedEvidence",863 },864 project: ProjectName.make("oscabriel/datekit"),862-} satisfies ProjectHeaderData;865+} satisfies ProjectHeaderFacts;866 867 const headerOf = (markup: string) =>868 elementWith(markup, 'data-project-header=""');869 9 unmodified lines879 880 const HEADER_READS = [881 "ledger",882 "listAsks",880- "listRefs",883 "log",884 "projectPolicy",885 "readFile",886 "readTree",335 unmodified lines1222 }),1223 seededWorld1224 );1225 1226+ it.effect(1227+ "never waits on the refs, on Overview or on any other page",1228+ () =>1229+ Effect.gen(function* () {1230+ const seen = yield* Ref.make<readonly string[]>([]);1231+1232+ yield* Effect.gen(function* () {1233+ yield* readProjectPage("oscabriel/datekit");1234+ yield* readProjectHeader("oscabriel/datekit");1235+ }).pipe(Effect.provide(countingWorld(seen)));1236+1237+ expect(yield* Ref.get(seen)).not.toContain("listRefs");1238+ }),1239+ seededWorld1240+ );1241+1242+ it.effect(1243+ "says the newest tag by its numbers once the refs are read, and none before",1244+ () =>1245+ Effect.sync(() => {1246+ const project = ProjectName.make("oscabriel/datekit");1247+ const sha = CommitSha.make("a".repeat(40));1248+1249+ const refs = {1250+ forks: [],1251+ project,1252+ tags: ["v0.9.0", "v0.10.0", "v0.2.0"].map((name) => ({1253+ name: TagName.make(name),1254+ sha,1255+ })),1256+ };1257+1258+ expect(newestTag(Option.some(refs))).toStrictEqual({1259+ latestTag: "v0.10.0",1260+ });1261+ expect(newestTag(Option.some({ ...refs, tags: [] }))).toStrictEqual({});1262+ expect(newestTag(Option.none())).toStrictEqual({});1263+ })1264+ );1265+1266 it.live("makes every read at once, none waiting on another", () =>1267 Effect.gen(function* () {1268 const header = yield* readProjectHeader("oscabriel/datekit").pipe(1269 Effect.flatMap(decodeHeader),301 unmodified lines