Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
142 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 } from "@g1t/contracts";
8
9import { type RoadmapItem, roadmapIn } from "./roadmap.ts";
10
11export type Tab = {
12 label: string;
13 /** Under the project, without a leading slash; "" is its overview. */
14 path: string;
15 /** Other paths under which this tab is the current one. */
16 also?: string[];
17 soon?: boolean;
18 about?: string;
19 /** Members only. */
20 members?: boolean;
21 /** Current on its own path only, not on the paths under it. */
22 exact?: boolean;
23 /** Only for viewers whose role has this capability, once it is known. */
24 needs?: Capability;
25};
26
27export type Section = {
28 key: RoadmapItem["section"] | "Workflows" | "Settings";
29 tabs: Tab[];
30};
31
32function soon(section: RoadmapItem["section"]): Tab[] {
33 return roadmapIn(section).map((item) => ({ label: item.title, path: `soon/${item.key}`, soon: true, about: item.summary }));
34}
35
36/**
37 * The project's pages, built on first use rather than when the module
38 * loads: the soon tabs read the roadmap, and a bundle that splits the two
39 * into chunks that import each other could otherwise read it before it is
40 * there (a page that throws as it loads, so nothing on it works).
41 */
42let built: Section[] | null = null;
43export function sections(): Section[] {
44 return (built ??= [
45 {
46 key: "Code",
47 tabs: [
48 { label: "Files", path: "code", also: ["tree", "blob"] },
49 { label: "Commits", path: "commits", also: ["commit"] },
50 { label: "Branches", path: "branches" },
51 { label: "Tags", path: "tags" },
52 { label: "Releases", path: "releases" },
53 { label: "Compare", path: "compare" },
54 // No Docs tab: code is not docs. Docs is a workspace mode, filtered by project there.
55 ],
56 },
57 {
58 key: "Issues",
59 tabs: [
60 { label: "Issues", path: "issues" },
61 { label: "Milestones", path: "milestones" },
62 ...soon("Issues"),
63 ],
64 },
65 {
66 key: "Deployments",
67 tabs: [{ label: "Deployments", path: "deployments" }, ...soon("Deployments")],
68 },
69 {
70 key: "Observability",
71 tabs: [
72 { label: "Overview", path: "observability", exact: true, members: true },
73 { label: "Requests", path: "observability/requests", members: true },
74 { label: "Logs", path: "observability/logs", members: true },
75 { label: "Traces", path: "observability/traces", members: true },
76 { label: "Errors", path: "observability/errors", members: true },
77 { label: "Metrics", path: "observability/metrics", members: true },
78 { label: "Settings", path: "observability/settings", members: true, needs: "manage_integrations" },
79 ...soon("Observability"),
80 ],
81 },
82 {
83 key: "Security",
84 tabs: [
85 { label: "Overview", path: "security", members: true, needs: "security_alerts", exact: true },
86 { label: "Secret scanning", path: "security/secret-scanning", members: true, needs: "security_alerts" },
87 { label: "Code scanning", path: "security/code-scanning", members: true, needs: "security_alerts", also: ["security/pulls"] },
88 { label: "Vulnerabilities", path: "security/vulnerabilities", members: true, needs: "security_alerts" },
89 { label: "Dependency graph", path: "security/dependency-graph", members: true, needs: "security_alerts" },
90 { label: "Dependency updates", path: "security/dependency-updates", members: true, needs: "security_alerts" },
91 { label: "Advisories", path: "security/advisories", members: true, needs: "security_alerts", also: ["security/reports"] },
92 { label: "Settings", path: "security/settings", members: true, needs: "security_alerts" },
93 ...soon("Security"),
94 ],
95 },
96 {
97 key: "Insights",
98 tabs: [
99 { label: "Contributors", path: "contributors" },
100 { label: "Activity", path: "activity" },
101 { label: "Stargazers", path: "stargazers" },
102 ...soon("Insights"),
103 ],
104 },
105 ]);
106}
107
108/** Pull requests, the work on them, the merge queue and sessions share a page's tabs too. */
109export const PULLS: Section = {
110 key: "Code",
111 tabs: [
112 { label: "Pull requests", path: "pulls", also: ["pull"] },
113 { label: "Work", path: "work" },
114 { label: "Merge queue", path: "queue" },
115 { label: "Sessions", path: "sessions" },
116 ],
117};
118
119function matches(rest: string, tab: Tab): boolean {
120 return [tab.path, ...(tab.also ?? [])].some((path) => path !== "" && (rest === path || rest.startsWith(`${path}/`)));
121}
122
123/**
124 * The tabs for the page at `rest` (the path under the project), if it
125 * belongs to a page with more than one view.
126 */
127export function tabsFor(rest: string, member: boolean, can?: Partial<Abilities>, features?: { issues?: boolean }): Tab[] | null {
128 for (const section of [...sections(), PULLS]) {
129 // Issues turned off (a fork's default): no Issues tabs, though what was opened stays readable.
130 if (section.key === "Issues" && features?.issues === false) continue;
131 const visible = section.tabs.filter(
132 (tab) => (member || !tab.members) && (!tab.needs || !can || Boolean(can[tab.needs])),
133 );
134 if (visible.length > 1 && visible.some((tab) => matches(rest, tab))) return visible;
135 }
136 return null;
137}
138
139/** The first page of a section, for its sidebar link: a built one if any. */
140export function entryOf(key: Section["key"]): Tab | undefined {
141 return sections().find((section) => section.key === key)?.tabs[0];
142}