Skip to content
72 linesCodeBlameRaw
1/**
2 * Links into the app. A `g1t://` link names a site path, as on the desktop
3 * (apps/desktop/src-tauri/src/links.rs): `g1t://acme/-/chat/dm/chn_1` is
4 * the page `/acme/-/chat/dm/chn_1`. The app has screens for some of the
5 * site's pages; `routeFor` says which screen shows a site path, and the
6 * rest open on the site.
7 *
8 * Kept free of React Native imports so `npm test` runs it under Node.
9 */
10
11/** Where the app's OAuth sign-in comes back to. */
12export const OAUTH_CALLBACK_PATH = "/oauth/callback";
13
14/** Path segments a workspace or account can never be named (the site's own pages). */
15const NOT_OWNERS = new Set(["settings", "notifications", "oauth", "device", "login", "register", "-"]);
16
17/**
18 * The app's screen for a site path, or null when the app has none and the
19 * page should open on the site.
20 */
21export function routeFor(sitePath: string): string | null {
22 const [pathname, query = ""] = sitePath.split("?", 2);
23 const parts = pathname.split("/").filter(Boolean).map(decodeURIComponentSafe);
24 const keep = query ? `?${query}` : "";
25 if (parts.length === 0) return "/";
26 if (`/${parts.join("/")}` === OAUTH_CALLBACK_PATH) return OAUTH_CALLBACK_PATH + keep;
27 const [owner, second, third, fourth, fifth] = parts;
28 if (NOT_OWNERS.has(owner.toLowerCase())) return null;
29 const workspace = `workspace=${encodeURIComponent(owner)}`;
30
31 // A workspace's own pages: /<workspace>/-/<page>.
32 if (second === "-") {
33 if (third === "chat") {
34 if (!fourth) return `/chat?${workspace}`;
35 if (fourth === "dm" && fifth) return `/chat/${encodeURIComponent(fifth)}?${workspace}`;
36 if (fourth !== "browse" && fourth !== "dm" && !fifth) return `/chat/${encodeURIComponent(fourth)}?${workspace}`;
37 return null;
38 }
39 if (third === "overview" && !fourth) return `/code?${workspace}`;
40 if (third === "marketplace" || third === "integrations" || (third === "agents" && fourth === "skills")) {
41 return `/customize?${workspace}`;
42 }
43 return null;
44 }
45
46 // A repository's pages: /<owner>/<repo>[/pull/<n> | /issues/<n>].
47 if (!second) return null;
48 const repo = `/code/${encodeURIComponent(owner)}/${encodeURIComponent(second)}`;
49 if (!third) return repo;
50 if (third === "pull" && fourth && /^\d+$/.test(fourth) && !fifth) return `${repo}/pull/${fourth}`;
51 if (third === "issues" && fourth && /^\d+$/.test(fourth) && !fifth) return `${repo}/issues/${fourth}`;
52 return null;
53}
54
55/**
56 * The site path a `g1t://` link names, or null for any other link.
57 * `g1t://acme/-/chat?x` is `/acme/-/chat?x`.
58 */
59export function sitePathOf(link: string): string | null {
60 const match = /^g1t:\/\/([^?#]*)(\?[^#]*)?/i.exec(link);
61 if (!match) return null;
62 const path = `/${match[1].replace(/^\/+/, "")}`;
63 return path + (match[2] ?? "");
64}
65
66function decodeURIComponentSafe(part: string): string {
67 try {
68 return decodeURIComponent(part);
69 } catch {
70 return part;
71 }
72}