Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

routes.ts

134 lines3.9 KB

1import type { Href, useRouter } from "expo-router";
2import { type EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
3import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
4​
5import type { SelectedThreadRef } from "../state/remote-runtime-types";
6​
7type Router = ReturnType<typeof useRouter>;
8​
9type ThreadRouteInput =
10 | Pick<SelectedThreadRef, "environmentId" | "threadId">
11 | Pick<EnvironmentThreadShell, "environmentId" | "id">;
12type PlainThreadRouteInput =
13 | {
14 environmentId: EnvironmentId;
15 threadId: ThreadId;
16 }
17 | {
18 environmentId: EnvironmentId;
19 id: ThreadId;
20 };
21​
22export function buildThreadRoutePath(input: ThreadRouteInput | PlainThreadRouteInput): string {
23 const environmentId = input.environmentId;
24 const threadId = "threadId" in input ? input.threadId : input.id;
25​
26 return `/threads/${encodeURIComponent(environmentId)}/${encodeURIComponent(threadId)}`;
28​
29export function buildThreadReviewRoutePath(
30 input: ThreadRouteInput | PlainThreadRouteInput,
31): string {
32 return `${buildThreadRoutePath(input)}/review`;
34​
35export function buildThreadFilesRoutePath(
36 input: ThreadRouteInput | PlainThreadRouteInput,
37 relativePath?: string | null,
38 line?: number | null,
39): string {
40 const basePath = `${buildThreadRoutePath(input)}/files`;
41 if (!relativePath) {
42 return basePath;
43 }
44​
45 const pathSegments = relativePath.split("/").filter((segment) => segment.length > 0);
46 if (pathSegments.length === 0) {
47 return basePath;
48 }
49​
50 const encodedPath = pathSegments.map(encodeURIComponent).join("/");
51 const lineParam =
52 Number.isFinite(line) && Number(line) > 0 ? `?line=${Math.floor(Number(line))}` : "";
53 return `${basePath}/${encodedPath}${lineParam}`;
55​
56export function buildThreadTerminalRoutePath(
57 input: ThreadRouteInput | PlainThreadRouteInput,
58 terminalId?: string | null,
59): string {
60 const basePath = `${buildThreadRoutePath(input)}/terminal`;
61 if (!terminalId) {
62 return basePath;
63 }
64​
65 return `${basePath}?terminalId=${encodeURIComponent(terminalId)}`;
67​
68/**
69 * Prefer this over {@link buildThreadTerminalRoutePath} with `router.push(string)` — Expo Router
70 * often does not merge query strings into `useLocalSearchParams`, which breaks terminal bootstrap
71 * (`requestedTerminalId` stays null while the UI assumes `default`).
72 */
73export function buildThreadTerminalNavigation(
74 input: ThreadRouteInput | PlainThreadRouteInput,
75 terminalId?: string | null,
76): Href {
77 const environmentId = String(input.environmentId);
78 const threadId = String("threadId" in input ? input.threadId : input.id);
79​
80 const params: { environmentId: string; threadId: string; terminalId?: string } = {
81 environmentId,
82 threadId,
83 };
84​
85 if (terminalId != null && terminalId !== "") {
86 params.terminalId = terminalId;
87 }
88​
89 return {
90 pathname: "/threads/[environmentId]/[threadId]/terminal",
91 params,
92 };
94​
95export function buildThreadFilesNavigation(
96 input: ThreadRouteInput | PlainThreadRouteInput,
97 relativePath?: string | null,
98 line?: number | null,
99): Href {
100 const environmentId = String(input.environmentId);
101 const threadId = String("threadId" in input ? input.threadId : input.id);
102 const path = relativePath?.split("/").filter((segment) => segment.length > 0) ?? [];
103​
104 if (path.length === 0) {
105 return {
106 pathname: "/threads/[environmentId]/[threadId]/files",
107 params: { environmentId, threadId },
108 };
109 }
110​
111 const params: {
112 environmentId: string;
113 threadId: string;
114 path: string[];
115 line?: string;
116 } = { environmentId, threadId, path };
117 if (Number.isFinite(line) && Number(line) > 0) {
118 params.line = String(Math.floor(Number(line)));
119 }
120​
121 return {
122 pathname: "/threads/[environmentId]/[threadId]/files/[...path]",
123 params,
124 };
126​
127export function dismissRoute(router: Router) {
128 if (router.canGoBack()) {
129 router.back();
130 return;
131 }
132​
133 router.replace("/");