@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 171 lines 5.56 KB
Raw
1 import {
2 NAVIGATION_TAB_ORDER,
3 navigationCustomizationAvailable,
4 resolveNavigationTabOrder,
5 type NavigationTab,
6 } from "./navigation-tab-order";
7
8 export function resolveWorkspaceTabOrder(saved: readonly unknown[] | undefined) {
9 return navigationCustomizationAvailable()
10 ? resolveNavigationTabOrder(saved)
11 : NAVIGATION_TAB_ORDER;
12 }
13
14 export type WorkspaceTab = NavigationTab;
15 /** Lists with their own search: the searchable tabs and the Settings list. */
16 export type WorkspaceSearchTab = WorkspaceTab | "settings";
17 export type WorkspaceSearchMode = "resting" | "active" | "expanded";
18
19 /** Settings, Chats, pull requests and issues pair a list with a detail column; Usage is one page. */
20 export function workspaceUsesColumns(tab: WorkspaceTab, settingsOpen = false) {
21 return settingsOpen || tab !== "usage";
22 }
23
24 export interface WorkspaceTabState {
25 readonly query: string;
26 readonly searchMode: WorkspaceSearchMode;
27 readonly scrollOffset: number;
28 readonly searchEntryOffset: number;
29 }
30
31 export interface WorkspaceTabsState {
32 readonly selectedTab: WorkspaceTab;
33 readonly selectionRevision: number;
34 readonly tabs: Readonly<Record<WorkspaceSearchTab, WorkspaceTabState>>;
35 }
36
37 function emptyTab(): WorkspaceTabState {
38 return { query: "", searchMode: "resting", scrollOffset: 0, searchEntryOffset: 0 };
39 }
40
41 /** The list a workspace page belongs to. */
42 export function workspaceTabOf(routeName: string | undefined): WorkspaceTab {
43 if (routeName === "Issue") return "issues";
44 return routeName === "PullRequest" ? "pullRequests" : "threads";
45 }
46
47 export function initialWorkspaceTabsState(
48 selectedTab: WorkspaceTab = "threads",
49 ): WorkspaceTabsState {
50 return {
51 selectedTab,
52 selectionRevision: 0,
53 tabs: {
54 threads: emptyTab(),
55 pullRequests: emptyTab(),
56 issues: emptyTab(),
57 usage: emptyTab(),
58 settings: emptyTab(),
59 },
60 };
61 }
62
63 export type WorkspaceTabsAction =
64 | { readonly type: "select"; readonly tab: WorkspaceTab }
65 | { readonly type: "search"; readonly tab?: WorkspaceSearchTab }
66 | { readonly type: "query"; readonly query: string; readonly tab?: WorkspaceSearchTab }
67 | { readonly type: "expand"; readonly tab?: WorkspaceSearchTab }
68 | { readonly type: "cancel"; readonly tab?: WorkspaceSearchTab; readonly scrollToTop?: boolean }
69 | { readonly type: "scroll"; readonly tab: WorkspaceSearchTab; readonly offset: number };
70
71 export function workspaceTabsReducer(
72 state: WorkspaceTabsState,
73 action: WorkspaceTabsAction,
74 ): WorkspaceTabsState {
75 if (action.type === "select")
76 return action.tab === state.selectedTab
77 ? state
78 : {
79 ...state,
80 selectedTab: action.tab,
81 selectionRevision: state.selectionRevision + 1,
82 };
83 const tab = action.tab ?? state.selectedTab;
84 if (tab === "usage") return state;
85 const current = state.tabs[tab];
86 let next: WorkspaceTabState;
87 switch (action.type) {
88 case "search":
89 next = {
90 ...current,
91 searchMode: "active",
92 searchEntryOffset:
93 current.searchMode === "resting" ? current.scrollOffset : current.searchEntryOffset,
94 };
95 break;
96 case "query":
97 next = { ...current, query: action.query };
98 break;
99 case "expand":
100 if (current.searchMode === "resting") return state;
101 next = { ...current, searchMode: "expanded" };
102 break;
103 case "cancel":
104 next = {
105 ...current,
106 query: "",
107 searchMode: "resting",
108 scrollOffset: action.scrollToTop ? 0 : current.searchEntryOffset,
109 searchEntryOffset: action.scrollToTop ? 0 : current.searchEntryOffset,
110 };
111 break;
112 case "scroll":
113 if (current.scrollOffset === action.offset) return state;
114 next = { ...current, scrollOffset: action.offset };
115 break;
116 }
117 return { ...state, tabs: { ...state.tabs, [tab]: next } };
118 }
119
120 /**
121 * iOS 27's integrated search X closes the field with only a blur, leaving `expanded`.
122 * An empty one has nothing to restore, except where the caller keeps it open (the Duo).
123 */
124 export function shouldRestoreNativeSearch(tab: WorkspaceTabState, keepsEmptySearchOpen: boolean) {
125 if (tab.searchMode === "resting") return false;
126 return tab.searchMode === "active" || tab.query !== "" || keepsEmptySearchOpen;
127 }
128
129 export const WORKSPACE_TOOLBAR = { height: 48, expandedGap: 10, edge: 16, gap: 8 };
130 export const WORKSPACE_DOCK = { height: 48, tabsWidth: 176, gap: 12, bottomGap: 6 };
131
132 export function workspaceDockBottomClearance(input: {
133 readonly bottomInset: number;
134 readonly keyboardHeight: number;
135 }) {
136 return 64 + Math.max(input.bottomInset, input.keyboardHeight) + 16;
137 }
138
139 export function workspaceSearchBottomClearance(input: {
140 readonly bottomInset: number;
141 readonly bottomBarInset?: number;
142 readonly keyboardHeight: number;
143 }) {
144 const bottom = input.bottomBarInset ?? Math.max(input.bottomInset, 12);
145 // AboveKeyboard lifts the field to an 8-point gap above the keyboard.
146 return (
147 Math.max(bottom, input.keyboardHeight > 0 ? input.keyboardHeight + 8 : 0) +
148 WORKSPACE_TOOLBAR.height +
149 16
150 );
151 }
152
153 export function workspaceToolbarHeight(mode: WorkspaceSearchMode): number {
154 return mode === "expanded"
155 ? WORKSPACE_TOOLBAR.height * 2 + WORKSPACE_TOOLBAR.expandedGap
156 : WORKSPACE_TOOLBAR.height;
157 }
158
159 export const NATIVE_WORKSPACE_TAB_SLOTS = [
160 "first",
161 "second",
162 "third",
163 "fourth",
164 "settings",
165 ] as const;
166
167 export function workspaceTabForNativeSlot(order: readonly WorkspaceTab[], slot: string) {
168 if (slot === "settings") return "settings";
169 const index = NATIVE_WORKSPACE_TAB_SLOTS.findIndex((candidate) => candidate === slot);
170 return order[index] ?? null;
171 }