Skip to content
152 linesCodeBlameRaw
1/**
2 * A project's pages and the tabs within them, in one place: the sidebar
3 * lists the pages, and a page with more than one view shows them as tabs
4 * across its top, the way GitHub and Vercel do. Soon tabs come from the
5 * roadmap and open its page for them, under the same tabs.
6 */
7import type { Abilities, Capability, RepoMirror } from "@g1t/contracts";
8
9import { type RepoArea, offers } from "./mirror.ts";
10import { type RoadmapItem, roadmapIn } from "./roadmap.ts";
11
12export type Tab = {
13 label: string;
14 /** Under the project, without a leading slash; "" is its overview. */
15 path: string;
16 /** Other paths under which this tab is the current one. */
17 also?: string[];
18 soon?: boolean;
19 about?: string;
20 /** Members only. */
21 members?: boolean;
22 /** Current on its own path only, not on the paths under it. */
23 exact?: boolean;
24 /** Only for viewers whose role has this capability, once it is known. */
25 needs?: Capability;
26};
27
28export type Section = {
29 key: RoadmapItem["section"] | "Workflows" | "Settings";
30 /** The part of the repository it is about: a mirror shows the tabs of what it offers (lib/mirror.ts `offers`). */
31 area: RepoArea;
32 tabs: Tab[];
33};
34
35function soon(section: RoadmapItem["section"]): Tab[] {
36 return roadmapIn(section).map((item) => ({ label: item.title, path: `soon/${item.key}`, soon: true, about: item.summary }));
37}
38
39export const SECTIONS: Section[] = [
40 {
41 key: "Code",
42 area: "code",
43 tabs: [
44 { label: "Files", path: "code", also: ["tree", "blob"] },
45 { label: "Commits", path: "commits", also: ["commit"] },
46 { label: "Branches", path: "branches" },
47 { label: "Tags", path: "tags" },
48 { label: "Releases", path: "releases" },
49 { label: "Compare", path: "compare" },
50 // No Docs tab: code is not docs. Docs is a workspace mode, filtered by project there.
51 ],
52 },
53 {
54 key: "Issues",
55 area: "issues",
56 tabs: [
57 { label: "Issues", path: "issues" },
58 { label: "Outcomes", path: "plans" },
59 { label: "Milestones", path: "milestones" },
60 ...soon("Issues"),
61 ],
62 },
63 {
64 key: "Agents",
65 area: "agents",
66 tabs: [
67 { label: "At work", path: "agents" },
68 { label: "Sessions", path: "sessions" },
69 { label: "Memory", path: "memory" },
70 ...soon("Agents"),
71 ],
72 },
73 {
74 key: "Deployments",
75 area: "deployments",
76 tabs: [{ label: "Deployments", path: "deployments" }, ...soon("Deployments")],
77 },
78 {
79 key: "Observability",
80 area: "deployments",
81 tabs: [
82 { label: "Overview", path: "observability", exact: true, members: true },
83 { label: "Requests", path: "observability/requests", members: true },
84 { label: "Logs", path: "observability/logs", members: true },
85 { label: "Traces", path: "observability/traces", members: true },
86 { label: "Errors", path: "observability/errors", members: true },
87 { label: "Metrics", path: "observability/metrics", members: true },
88 { label: "Settings", path: "observability/settings", members: true, needs: "manage_integrations" },
89 ...soon("Observability"),
90 ],
91 },
92 {
93 key: "Security",
94 area: "security",
95 tabs: [
96 { label: "Overview", path: "security", members: true, needs: "security_alerts", exact: true },
97 { label: "Secret scanning", path: "security/secret-scanning", members: true, needs: "security_alerts" },
98 { label: "Code scanning", path: "security/code-scanning", members: true, needs: "security_alerts", also: ["security/pulls"] },
99 { label: "Vulnerabilities", path: "security/vulnerabilities", members: true, needs: "security_alerts" },
100 { label: "Dependency graph", path: "security/dependency-graph", members: true, needs: "security_alerts" },
101 { label: "Dependency updates", path: "security/dependency-updates", members: true, needs: "security_alerts" },
102 { label: "Advisories", path: "security/advisories", members: true, needs: "security_alerts", also: ["security/reports"] },
103 { label: "Settings", path: "security/settings", members: true, needs: "security_alerts" },
104 ...soon("Security"),
105 ],
106 },
107 {
108 key: "Insights",
109 area: "insights",
110 tabs: [
111 { label: "Contributors", path: "contributors" },
112 { label: "Activity", path: "activity" },
113 { label: "Stargazers", path: "stargazers" },
114 ...soon("Insights"),
115 ],
116 },
117];
118
119/** Pull requests and the merge queue share a page's tabs too. */
120export const PULLS: Section = {
121 key: "Code",
122 area: "pulls",
123 tabs: [
124 { label: "Pull requests", path: "pulls", also: ["pull"] },
125 { label: "Merge queue", path: "queue" },
126 ],
127};
128
129function matches(rest: string, tab: Tab): boolean {
130 return [tab.path, ...(tab.also ?? [])].some((path) => path !== "" && (rest === path || rest.startsWith(`${path}/`)));
131}
132
133/**
134 * The tabs for the page at `rest` (the path under the project), if it
135 * belongs to a page with more than one view. On a mirror, only the pages
136 * it offers now have tabs (`mirror`, the repository's).
137 */
138export function tabsFor(rest: string, member: boolean, can?: Partial<Abilities>, mirror?: RepoMirror | null): Tab[] | null {
139 for (const section of [...SECTIONS, PULLS]) {
140 if (!offers(mirror, section.area)) continue;
141 const visible = section.tabs.filter(
142 (tab) => (member || !tab.members) && (!tab.needs || !can || Boolean(can[tab.needs])),
143 );
144 if (visible.length > 1 && visible.some((tab) => matches(rest, tab))) return visible;
145 }
146 return null;
147}
148
149/** The first page of a section, for its sidebar link: a built one if any. */
150export function entryOf(key: Section["key"]): Tab | undefined {
151 return SECTIONS.find((section) => section.key === key)?.tabs[0];
152}