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

main
ts 106 lines 3.82 KB
Raw
1 import { msg } from "gt-react-native";
2
3 export const NAVIGATION_TAB_ORDER = ["threads", "pullRequests", "issues", "usage"] as const;
4 export type NavigationTab = (typeof NAVIGATION_TAB_ORDER)[number];
5
6 const SHARED_SLOT_TABS = ["pullRequests", "issues"] as const;
7 export type SharedSlotTab = (typeof SHARED_SLOT_TABS)[number];
8
9 export function isSharedSlotTab(value: unknown): value is SharedSlotTab {
10 return SHARED_SLOT_TABS.some((tab) => tab === value);
11 }
12
13 /**
14 * Pull requests and issues share one slot in the tab bar. The open one holds it, or else the one
15 * opened last; this returns the other, which the bar leaves out.
16 */
17 export function sharedSlotAbsentee(selected: NavigationTab, last: unknown): SharedSlotTab {
18 const holder = isSharedSlotTab(selected) ? selected : last === "issues" ? last : "pullRequests";
19 return holder === "issues" ? "pullRequests" : "issues";
20 }
21
22 export function navigationCustomizationAvailable(
23 registeredSections: number = NAVIGATION_TAB_ORDER.length - 1,
24 ) {
25 return registeredSections > 4;
26 }
27
28 export const NAVIGATION_TAB_LABELS: Readonly<Record<NavigationTab, string>> = {
29 threads: msg("Threads", { $context: "navigation tab, list of chat threads" }),
30 pullRequests: msg("Pulls", { $context: "navigation tab, pull requests" }),
31 issues: msg("Issues", { $context: "navigation tab, issues from an issue tracker" }),
32 usage: msg("Usage", { $context: "navigation tab, provider usage limits" }),
33 };
34
35 function isNavigationTab(value: unknown): value is NavigationTab {
36 return NAVIGATION_TAB_ORDER.some((tab) => tab === value);
37 }
38
39 /** Complete older preferences without losing their order or retaining removed sections. */
40 export function resolveNavigationTabOrder(saved?: readonly unknown[]): readonly NavigationTab[] {
41 if (!saved?.length) return NAVIGATION_TAB_ORDER;
42 return [...new Set([...saved.filter(isNavigationTab), ...NAVIGATION_TAB_ORDER])];
43 }
44
45 export function moveNavigationTab(
46 saved: readonly unknown[] | undefined,
47 tab: NavigationTab,
48 position: number,
49 ): readonly NavigationTab[] {
50 const order = [...resolveNavigationTabOrder(saved)];
51 const from = order.indexOf(tab);
52 const to = Math.max(0, Math.min(order.length - 1, position));
53 order.splice(from, 1);
54 order.splice(to, 0, tab);
55 return order;
56 }
57
58 export function resolveHiddenNavigationTabs(saved?: readonly unknown[]): readonly NavigationTab[] {
59 return [...new Set(saved?.filter(isNavigationTab) ?? [])];
60 }
61
62 export function toggleNavigationTabVisibility(
63 saved: readonly unknown[] | undefined,
64 tab: NavigationTab,
65 ) {
66 const hidden = resolveHiddenNavigationTabs(saved);
67 return hidden.includes(tab) ? hidden.filter((candidate) => candidate !== tab) : [...hidden, tab];
68 }
69
70 export function enabledNavigationTabs(saved?: readonly unknown[], hidden?: readonly unknown[]) {
71 const excluded = resolveHiddenNavigationTabs(hidden);
72 return resolveNavigationTabOrder(saved).filter((tab) => !excluded.includes(tab));
73 }
74
75 export function visibleNavigationTabs(
76 saved?: readonly unknown[],
77 hidden?: readonly unknown[],
78 capacity = 4,
79 ) {
80 return enabledNavigationTabs(saved, hidden).slice(0, capacity);
81 }
82
83 export function overflowNavigationTabs(saved?: readonly unknown[], hidden?: readonly unknown[]) {
84 return enabledNavigationTabs(saved, hidden).slice(4);
85 }
86
87 export function selectedNavigationTab(
88 saved: readonly unknown[] | undefined,
89 selected: NavigationTab,
90 settingsOpen: boolean,
91 hidden?: readonly unknown[],
92 capacity = 4,
93 ): NavigationTab | "settings" {
94 return settingsOpen || !visibleNavigationTabs(saved, hidden, capacity).includes(selected)
95 ? "settings"
96 : selected;
97 }
98
99 export function resolveSelectedNavigationTab(
100 saved: readonly unknown[] | undefined,
101 hidden: readonly unknown[] | undefined,
102 selected: NavigationTab,
103 ) {
104 const enabled = enabledNavigationTabs(saved, hidden);
105 return enabled.includes(selected) ? selected : enabled[0];
106 }