The best way to control Claude Code, Codex and more from any computer or phone

main
ts 40 lines 1.49 KB
Raw
1 import type { PreviewSessionSnapshot } from "@t3tools/contracts";
2 import { isLoopbackHost } from "@t3tools/shared/preview";
3 import { msg } from "gt-react-native";
4
5 import { m, type MessageResolver } from "../../i18n/messages";
6
7 export function latestBrowserTab(tabs: ReadonlyArray<PreviewSessionSnapshot>) {
8 return tabs.reduce<PreviewSessionSnapshot | null>(
9 (latest, tab) => (latest === null || tab.updatedAt > latest.updatedAt ? tab : latest),
10 null,
11 );
12 }
13
14 export function browserTabUrl(tab: PreviewSessionSnapshot) {
15 return tab.navStatus._tag === "Idle" ? "" : tab.navStatus.url;
16 }
17
18 export function browserTabTitle(tab: PreviewSessionSnapshot, resolve: MessageResolver = m) {
19 if (tab.navStatus._tag === "Idle") return resolve(msg("New tab"));
20 if (tab.navStatus.title.trim()) return tab.navStatus.title;
21 try {
22 return new URL(tab.navStatus.url).host || tab.navStatus.url;
23 } catch {
24 return tab.navStatus.url;
25 }
26 }
27
28 /** The tab's URL for this device's tunnelled webview, when it is one of the environment's loopback pages. */
29 export function loopbackUrlForDevice(value: string): string | null {
30 try {
31 const url = new URL(value);
32 if (!/^https?:$/.test(url.protocol)) return null;
33 // The unspecified address would name this device's own interfaces, so use localhost instead.
34 if (url.hostname === "0.0.0.0" || url.hostname === "[::]") url.hostname = "localhost";
35 if (!isLoopbackHost(url.hostname)) return null;
36 return url.toString();
37 } catch {
38 return null;
39 }
40 }