Open History to see the last commit
1import type {
2 EnvironmentProject,
3 EnvironmentThreadShell,
4} from "@t3tools/client-runtime/state/shell";
5import { EnvironmentId, ThreadId, type SidebarProjectGroupingMode } from "@t3tools/contracts";
6import { useAtomValue } from "@effect/atom-react";
7import { useFocusEffect } from "@react-navigation/native";
8import {
9 CommonActions,
10 NavigationContext,
11 NavigationRouteContext,
12 StackActions,
13 useNavigation,
14} from "@react-navigation/native";
15import {
16 createContext,
17 use,
18 useCallback,
19 useEffect,
20 useMemo,
21 useRef,
22 useState,
23 type ReactNode,
24} from "react";
25import { Platform, useWindowDimensions, View } from "react-native";
26import Animated, {
27 runOnJS,
28 useAnimatedStyle,
29 useDerivedValue,
30 useSharedValue,
31 withTiming,
32} from "react-native-reanimated";
33import { AsyncResult } from "effect/reactivity";
34
35import {
36 deriveFileInspectorPaneLayout,
37 alignListPaneToHinge,
38 deriveLayout,
39 type VerticalHinge,
40 deriveWorkspacePaneLayout,
41 isPrimarySidebarPinned,
42 type FileInspectorPaneLayout,
43 type Layout,
44 type WorkspaceAuxiliaryPaneRole,
45 type WorkspacePaneLayout,
46} from "../../lib/layout";
47import {
48 resolveThreadSelectionNavigationAction,
49 resolveThreadSelectionOverlayState,
50} from "../../lib/adaptive-navigation";
51import { scopedThreadKey } from "../../lib/scopedEntities";
52import type { ThreadListFilters } from "../../persistence/mobile-preferences";
53import { mobilePreferencesAtom } from "../../state/preferences";
54import {
55 DEFAULT_MOBILE_PROJECT_GROUPING_SETTINGS,
56 resolveMobileProjectGroupingSettings,
57} from "../../state/project-grouping";
58import {
59 parseActiveThreadPath,
60 useHardwareKeyboardCommand,
61} from "../keyboard/hardwareKeyboardCommands";
62import { AndroidHomeFabLayout } from "../home/AndroidHomeFab";
63import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing";
64import {
65 AndroidWorkspaceNavigation,
66 ANDROID_WORKSPACE_RAIL_WIDTH,
67} from "../workspace/AndroidWorkspaceNavigation";
68import { AndroidWorkspaceSidebar } from "../workspace/AndroidWorkspaceSidebar";
69import { useAndroidWorkspaceSelection } from "../workspace/useAndroidWorkspaceSelection";
70import { HomeListOptionsProvider } from "../home/home-list-options";
71import { ThreadNavigationSidebar } from "../threads/ThreadNavigationSidebar";
72import { RenderErrorBoundary, RenderFailureView } from "../../components/RenderErrorBoundary";
73import { WORKSPACE_PANE_TIMING } from "./workspace-pane-animation";
74import { WorkspaceInspectorPane } from "./workspace-inspector-pane";
75import { WorkspaceContentWidthContext } from "./workspace-content-width";
76import {
77 NativeWorkspaceModeContext,
78 NativeWorkspaceInspectorContext,
79} from "../../native/v5-workspace-context";
80
81import { useNativeLayoutMetrics } from "../../native/native-layout-metrics";
82import { AndroidHingeObserver } from "../../native/AndroidHingeObserver";
83import { WindowHingeContext } from "../../native/window-hinge-context";
84
85interface AdaptiveWorkspaceContextValue {
86 readonly layout: Layout;
87 readonly panes: WorkspacePaneLayout;
88 readonly fileInspector: FileInspectorPaneLayout;
89 readonly primarySidebarSearchQuery: string;
90 readonly selectThread: (thread: EnvironmentThreadShell) => void;
91 readonly activateAuxiliaryPaneRole: (role: WorkspaceAuxiliaryPaneRole) => () => void;
92 readonly registerWorkspaceInspector: (render: () => ReactNode) => () => void;
93 readonly setPrimarySidebarSearchQuery: (query: string) => void;
94 readonly showAuxiliaryPane: (role: WorkspaceAuxiliaryPaneRole) => void;
95 readonly toggleAuxiliaryPane: () => void;
96 readonly togglePrimarySidebar: () => void;
97 readonly setAuxiliaryPaneWidth: (width: number) => void;
98}
99
100const compactLayout = deriveLayout({ width: 0, height: 0 });
101const compactPanes = deriveWorkspacePaneLayout({
102 layout: compactLayout,
103 viewportWidth: 0,
104 primarySidebarPreferredVisible: true,
105 auxiliaryPanePreferredVisible: true,
106});
107const compactFileInspector = deriveFileInspectorPaneLayout({
108 layout: compactLayout,
109 viewportWidth: 0,
110});
111const AdaptiveWorkspaceContext = createContext<AdaptiveWorkspaceContextValue>({
112 layout: compactLayout,
113 panes: compactPanes,
114 fileInspector: compactFileInspector,
115 primarySidebarSearchQuery: "",
116 selectThread: () => undefined,
117 activateAuxiliaryPaneRole: () => () => undefined,
118 registerWorkspaceInspector: () => () => undefined,
119 setPrimarySidebarSearchQuery: () => undefined,
120 showAuxiliaryPane: () => undefined,
121 toggleAuxiliaryPane: () => undefined,
122 togglePrimarySidebar: () => undefined,
123 setAuxiliaryPaneWidth: () => undefined,
124});
125
126export function useAdaptiveWorkspaceLayout(): AdaptiveWorkspaceContextValue {
127 return use(AdaptiveWorkspaceContext);
128}
129
130export function useAdaptiveWorkspacePaneRole(role: WorkspaceAuxiliaryPaneRole) {
131 const { activateAuxiliaryPaneRole } = useAdaptiveWorkspaceLayout();
132
133 useFocusEffect(
134 useCallback(() => activateAuxiliaryPaneRole(role), [activateAuxiliaryPaneRole, role]),
135 );
136}
137
138/**
139 * The column renders BESIDE the navigator — outside any screen — so the
140 * registering screen's navigation and route contexts are captured here and
141 * re-provided around the portal content.
142 *
143 * Registration is FOCUS-scoped, driven by navigation events rather than the
144 * screen's own render cycle: react-native-screens freezes blurred screens, so a
145 * cleanup that depends on the blurred subtree re-rendering never runs and would
146 * leak the pane into the next route.
147 */
148export function useRegisterWorkspaceInspector(render: (() => ReactNode) | undefined) {
149 const { registerWorkspaceInspector } = useAdaptiveWorkspaceLayout();
150 const navigation = use(NavigationContext);
151 const route = use(NavigationRouteContext);
152
153 const wrappedRender = useMemo(() => {
154 if (render === undefined) {
155 return undefined;
156 }
157 return () => (
158 <NavigationContext.Provider value={navigation}>
159 <NavigationRouteContext.Provider value={route}>{render()}</NavigationRouteContext.Provider>
160 </NavigationContext.Provider>
161 );
162 }, [navigation, render, route]);
163
164 const wrappedRenderRef = useRef(wrappedRender);
165 wrappedRenderRef.current = wrappedRender;
166 const focusedRef = useRef(false);
167 const deactivateRef = useRef<(() => void) | null>(null);
168
169 const syncRegistration = useCallback(() => {
170 if (!focusedRef.current || wrappedRenderRef.current === undefined) {
171 deactivateRef.current?.();
172 return;
173 }
174 deactivateRef.current = registerWorkspaceInspector(wrappedRenderRef.current);
175 }, [registerWorkspaceInspector]);
176
177 useFocusEffect(
178 useCallback(() => {
179 focusedRef.current = true;
180 syncRegistration();
181 return () => {
182 focusedRef.current = false;
183 syncRegistration();
184 };
185 }, [syncRegistration]),
186 );
187
188 useEffect(() => {
189 if (focusedRef.current) {
190 syncRegistration();
191 }
192 }, [syncRegistration, wrappedRender]);
193
194 useEffect(
195 () => () => {
196 deactivateRef.current?.();
197 deactivateRef.current = null;
198 },
199 [],
200 );
201}
202
203export function AdaptiveWorkspaceLayout(props: {
204 readonly children: ReactNode;
205 readonly pathname: string;
206 readonly workspaceRouteKey: string | undefined;
207 readonly selectedPullRequestKey?: string | null;
208}) {
209 const preferencesResult = useAtomValue(mobilePreferencesAtom);
210 if (!AsyncResult.isSuccess(preferencesResult)) {
211 return AsyncResult.isFailure(preferencesResult) ? (
212 <AdaptiveWorkspaceLayoutContent
213 {...props}
214 projectGroupingMode={DEFAULT_MOBILE_PROJECT_GROUPING_SETTINGS.sidebarProjectGroupingMode}
215 />
216 ) : null;
217 }
218 const groupingSettings = resolveMobileProjectGroupingSettings(preferencesResult.value);
219 return (
220 <AdaptiveWorkspaceLayoutContent
221 {...props}
222 projectGroupingMode={groupingSettings.sidebarProjectGroupingMode}
223 savedThreadListFilters={preferencesResult.value.threadListFilters}
224 />
225 );
226}
227
228function AdaptiveWorkspaceLayoutContent(
229 props: {
230 readonly children: ReactNode;
231 readonly pathname: string;
232 readonly workspaceRouteKey: string | undefined;
233 readonly selectedPullRequestKey?: string | null;
234 } & {
235 readonly projectGroupingMode: SidebarProjectGroupingMode;
236 readonly savedThreadListFilters?: ThreadListFilters | undefined;
237 },
238) {
239 const projectGroupingMode = props.projectGroupingMode;
240 const nativeWorkspace = use(NativeWorkspaceModeContext);
241 const windowDimensions = useWindowDimensions();
242 const nativeMetrics = useNativeLayoutMetrics();
243 const { width: viewportWidth, height } = nativeMetrics ?? windowDimensions;
244 const { scale: androidControlScale } = useAndroidControlSizing();
245 const pathname = props.pathname;
246 const navigation = useNavigation();
247 const activeRoleOwner = useRef<symbol | null>(null);
248 const [primarySidebarPreferredVisible, setPrimarySidebarPreferredVisible] = useState(true);
249 const [supplementaryPanePreferredVisible, setSupplementaryPanePreferredVisible] = useState(true);
250 const [supplementaryPanePreferredWidth, setSupplementaryPanePreferredWidth] = useState<
251 number | null
252 >(null);
253 const [fileInspectorPreferredVisible, setFileInspectorPreferredVisible] = useState(true);
254 const [fileInspectorPreferredWidth, setFileInspectorPreferredWidth] = useState<number | null>(
255 null,
256 );
257 const [primarySidebarSearchQuery, setPrimarySidebarSearchQuery] = useState("");
258 const [focusedAuxiliaryPaneRole, setFocusedAuxiliaryPaneRole] =
259 useState<WorkspaceAuxiliaryPaneRole | null>(null);
260 const baseLayout = useMemo(
261 () => deriveLayout({ width: viewportWidth, height, nativeMetrics }),
262 [height, viewportWidth, nativeMetrics],
263 );
264 const primarySidebarPinned = isPrimarySidebarPinned({
265 pathname,
266 nativeWorkspace,
267 usesSplitView: baseLayout.usesSplitView,
268 });
269 const showPrimarySidebar = primarySidebarPinned || primarySidebarPreferredVisible;
270 const shouldRenderNavigationRail = Platform.OS === "android" && baseLayout.usesSplitView;
271 const navigationRailWidth =
272 shouldRenderNavigationRail && showPrimarySidebar
273 ? ANDROID_WORKSPACE_RAIL_WIDTH * Math.max(1, androidControlScale)
274 : 0;
275 const [hinge, setHinge] = useState<VerticalHinge | null>(null);
276 const [windowHinge, setWindowHinge] = useState<VerticalHinge | null>(null);
277 const handleHingeChange = useCallback(
278 (next: VerticalHinge | null, nextWindow: VerticalHinge | null) => {
279 setHinge(next);
280 setWindowHinge(nextWindow);
281 },
282 [],
283 );
284 const layout = useMemo(
285 () =>
286 alignListPaneToHinge({
287 layout: baseLayout,
288 hinge,
289 viewportWidth,
290 leadingWidth: navigationRailWidth,
291 }),
292 [baseLayout, hinge, viewportWidth, navigationRailWidth],
293 );
294 const width = viewportWidth - navigationRailWidth;
295 const shouldRenderPrimarySidebar = layout.usesSplitView;
296 const fileInspector = useMemo(
297 () =>
298 deriveFileInspectorPaneLayout({
299 layout,
300 viewportWidth: width,
301 preferredWidth: fileInspectorPreferredWidth ?? undefined,
302 reservedLeadingWidth:
303 shouldRenderPrimarySidebar && showPrimarySidebar
304 ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0)
305 : 0,
306 }),
307 [fileInspectorPreferredWidth, layout, showPrimarySidebar, shouldRenderPrimarySidebar, width],
308 );
309 const auxiliaryPaneRole: WorkspaceAuxiliaryPaneRole =
310 focusedAuxiliaryPaneRole ?? (/\/files(?:\/|$)/.test(pathname) ? "inspector" : "supplementary");
311 const auxiliaryPanePreferredVisible =
312 auxiliaryPaneRole === "inspector"
313 ? fileInspectorPreferredVisible
314 : supplementaryPanePreferredVisible;
315 const auxiliaryPanePreferredWidth =
316 auxiliaryPaneRole === "inspector"
317 ? fileInspectorPreferredWidth
318 : supplementaryPanePreferredWidth;
319 const panes = useMemo(
320 () =>
321 deriveWorkspacePaneLayout({
322 layout,
323 viewportWidth: width,
324 primarySidebarPreferredVisible: showPrimarySidebar,
325 auxiliaryPanePreferredVisible,
326 auxiliaryPaneRole,
327 auxiliaryPanePreferredWidth: auxiliaryPanePreferredWidth ?? undefined,
328 }),
329 [
330 auxiliaryPanePreferredVisible,
331 auxiliaryPaneRole,
332 auxiliaryPanePreferredWidth,
333 layout,
334 showPrimarySidebar,
335 width,
336 ],
337 );
338 const activeThread = parseActiveThreadPath(pathname);
339 const environmentId = activeThread?.environmentId ?? null;
340 const threadId = activeThread?.threadId ?? null;
341 const selectedThreadKey = useMemo(() => {
342 if (environmentId === null || threadId === null) {
343 return null;
344 }
345 try {
346 return scopedThreadKey(EnvironmentId.make(environmentId), ThreadId.make(threadId));
347 } catch {
348 return null;
349 }
350 }, [environmentId, threadId]);
351 const [workspaceInspector, setWorkspaceInspector] = useState<{
352 readonly render: () => ReactNode;
353 readonly active: boolean;
354 } | null>(null);
355 const workspaceInspectorOwner = useRef<symbol | null>(null);
356 const registerWorkspaceInspector = useCallback((render: () => ReactNode) => {
357 const owner = Symbol("workspace-inspector");
358 workspaceInspectorOwner.current = owner;
359 setWorkspaceInspector({ render, active: true });
360
361 return () => {
362 // During a push/replace the outgoing screen deactivates AFTER the
363 // incoming screen registered — only the current owner may deactivate.
364 if (workspaceInspectorOwner.current !== owner) {
365 return;
366 }
367 setWorkspaceInspector((current) => (current === null ? null : { ...current, active: false }));
368 };
369 }, []);
370 const handleWorkspaceInspectorClosed = useCallback(() => {
371 setWorkspaceInspector((current) => (current !== null && !current.active ? null : current));
372 }, []);
373 const activateAuxiliaryPaneRole = useCallback((role: WorkspaceAuxiliaryPaneRole) => {
374 const owner = Symbol(role);
375 activeRoleOwner.current = owner;
376 setFocusedAuxiliaryPaneRole(role);
377
378 return () => {
379 if (activeRoleOwner.current !== owner) {
380 return;
381 }
382 activeRoleOwner.current = null;
383 setFocusedAuxiliaryPaneRole(null);
384 };
385 }, []);
386 const togglePrimarySidebar = useCallback(() => {
387 if (primarySidebarPinned) {
388 return;
389 }
390 if (!panes.primarySidebarVisible && panes.primarySidebarSuppressedByAuxiliary) {
391 setFileInspectorPreferredVisible(false);
392 setPrimarySidebarPreferredVisible(true);
393 return;
394 }
395 setPrimarySidebarPreferredVisible((current) => !current);
396 }, [
397 panes.primarySidebarSuppressedByAuxiliary,
398 panes.primarySidebarVisible,
399 primarySidebarPinned,
400 ]);
401 const revealPrimarySidebar = useCallback(() => {
402 if (panes.primarySidebarSuppressedByAuxiliary) {
403 setFileInspectorPreferredVisible(false);
404 }
405 setPrimarySidebarPreferredVisible(true);
406 }, [panes.primarySidebarSuppressedByAuxiliary]);
407 const handleToggleSidebarCommand = useCallback(() => {
408 togglePrimarySidebar();
409 return true;
410 }, [togglePrimarySidebar]);
411 const sidebarCommands = useMemo(
412 () => (primarySidebarPinned ? [] : (["toggleSidebar"] as const)),
413 [primarySidebarPinned],
414 );
415 useHardwareKeyboardCommand(sidebarCommands, handleToggleSidebarCommand);
416 const showAuxiliaryPane = useCallback((role: WorkspaceAuxiliaryPaneRole) => {
417 if (role === "inspector") {
418 setFocusedAuxiliaryPaneRole("inspector");
419 setFileInspectorPreferredVisible(true);
420 return;
421 }
422 setFocusedAuxiliaryPaneRole("supplementary");
423 setSupplementaryPanePreferredVisible(true);
424 }, []);
425 const handleOpenFilesCommand = useCallback(() => {
426 const activeThread = parseActiveThreadPath(pathname);
427 if (!layout.usesSplitView || !fileInspector.supported || activeThread === null) {
428 return false;
429 }
430 showAuxiliaryPane("inspector");
431 if (/\/files(?:\/|$)/.test(pathname)) {
432 return true;
433 }
434 navigation.navigate("ThreadFiles", activeThread);
435 return true;
436 }, [fileInspector.supported, layout.usesSplitView, pathname, navigation, showAuxiliaryPane]);
437 useHardwareKeyboardCommand("files", handleOpenFilesCommand);
438 const toggleAuxiliaryPane = useCallback(() => {
439 if (auxiliaryPaneRole === "inspector") {
440 setFileInspectorPreferredVisible((current) => !current);
441 return;
442 }
443 setSupplementaryPanePreferredVisible((current) => !current);
444 }, [auxiliaryPaneRole]);
445 const setAuxiliaryPaneWidth = useCallback(
446 (nextWidth: number) => {
447 if (auxiliaryPaneRole === "inspector") {
448 setFileInspectorPreferredWidth(nextWidth);
449 return;
450 }
451 setSupplementaryPanePreferredWidth(nextWidth);
452 },
453 [auxiliaryPaneRole],
454 );
455 const handleOpenSettings = useCallback(() => {
456 navigation.navigate("SettingsSheet", {
457 screen: "SettingsContent",
458 params: { screen: "Settings" },
459 });
460 }, [navigation]);
461
462 const handleStartNewTask = useCallback(() => {
463 navigation.navigate("NewTaskSheet", { screen: "NewTask" });
464 }, [navigation]);
465
466 // Minted here (root stack navigation) so the sidebar pane stays free of
467 // navigation hooks — on iOS it renders inside an independent nav tree.
468 const handleOpenEnvironmentSettings = useCallback(() => {
469 navigation.navigate("SettingsSheet", {
470 screen: "SettingsContent",
471 params: { screen: "SettingsEnvironments" },
472 });
473 }, [navigation]);
474
475 const handleNewThreadOnBranch = useCallback(
476 (thread: EnvironmentThreadShell) => {
477 navigation.navigate("NewTaskSheet", {
478 screen: "NewTaskDraft",
479 params: {
480 environmentId: String(thread.environmentId),
481 projectId: String(thread.projectId),
482 branch: thread.branch,
483 worktreePath: thread.worktreePath,
484 },
485 });
486 },
487 [navigation],
488 );
489
490 const handleNewThreadInProject = useCallback(
491 (project: EnvironmentProject) => {
492 navigation.navigate("NewTaskSheet", {
493 screen: "NewTaskDraft",
494 params: {
495 environmentId: String(project.environmentId),
496 projectId: String(project.id),
497 title: project.title,
498 },
499 });
500 },
501 [navigation],
502 );
503
504 const inspectorColumnTargetWidth =
505 workspaceInspector !== null && workspaceInspector.active && panes.auxiliaryPaneVisible
506 ? (panes.auxiliaryPaneWidth ?? 0)
507 : 0;
508 const contentTargetWidth = layout.usesSplitView
509 ? Math.max(0, panes.contentPaneWidth - inspectorColumnTargetWidth)
510 : null;
511 const [contentSizing, setContentSizing] = useState({
512 viewportWidth,
513 target: contentTargetWidth,
514 sidebarVisible: panes.primarySidebarVisible,
515 width: contentTargetWidth,
516 listPaneTarget: layout.listPaneWidth,
517 listPaneFloor: layout.listPaneWidth,
518 listPaneCeiling: layout.listPaneWidth,
519 revision: 0,
520 });
521 if (
522 Platform.OS === "android" &&
523 (contentSizing.viewportWidth !== viewportWidth ||
524 contentSizing.target !== contentTargetWidth ||
525 contentSizing.sidebarVisible !== panes.primarySidebarVisible ||
526 contentSizing.listPaneTarget !== layout.listPaneWidth)
527 ) {
528 const sameViewport = contentSizing.viewportWidth === viewportWidth;
529 const openingSidebar =
530 sameViewport && !contentSizing.sidebarVisible && panes.primarySidebarVisible;
531 // A book-posture hinge resizes the visible list pane instead of revealing it.
532 const resizingSidebar =
533 sameViewport &&
534 contentSizing.sidebarVisible &&
535 panes.primarySidebarVisible &&
536 contentSizing.listPaneTarget !== layout.listPaneWidth;
537 const keepWide =
538 (openingSidebar || resizingSidebar) &&
539 contentSizing.width !== null &&
540 contentTargetWidth !== null;
541 setContentSizing({
542 viewportWidth,
543 target: contentTargetWidth,
544 sidebarVisible: panes.primarySidebarVisible,
545 // Clip the wide navigator while the sidebar opens or widens, then reflow once at the
546 // end. Narrowing it first exposes an empty strip before the sidebar arrives.
547 width: keepWide
548 ? Math.max(contentSizing.width ?? 0, contentTargetWidth ?? 0)
549 : contentTargetWidth,
550 listPaneTarget: layout.listPaneWidth,
551 // While a hinge resizes the list, its width follows the pane edge within this span. A
552 // retarget mid-animation widens the span, since the edge is still somewhere inside it.
553 listPaneFloor: resizingSidebar
554 ? Math.min(contentSizing.listPaneFloor ?? 0, layout.listPaneWidth ?? 0)
555 : layout.listPaneWidth,
556 listPaneCeiling: resizingSidebar
557 ? Math.max(contentSizing.listPaneCeiling ?? 0, layout.listPaneWidth ?? 0)
558 : layout.listPaneWidth,
559 revision: contentSizing.revision + 1,
560 });
561 }
562 const settleContentWidth = useCallback((revision: number) => {
563 setContentSizing((current) =>
564 current.revision !== revision ||
565 (current.width === current.target &&
566 current.listPaneFloor === current.listPaneTarget &&
567 current.listPaneCeiling === current.listPaneTarget)
568 ? current
569 : {
570 ...current,
571 width: current.target,
572 listPaneFloor: current.listPaneTarget,
573 listPaneCeiling: current.listPaneTarget,
574 },
575 );
576 }, []);
577 const listPaneFloor = contentSizing.listPaneFloor ?? 0;
578 const listPaneCeiling = contentSizing.listPaneCeiling ?? 0;
579 const contentSettledWidth = Platform.OS === "android" ? contentSizing.width : contentTargetWidth;
580 const contentRevision = contentSizing.revision;
581
582 const renderedSidebarWidth = useSharedValue(
583 panes.primarySidebarVisible ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0) : 0,
584 );
585 const renderedRailWidth = useSharedValue(navigationRailWidth);
586 useEffect(() => {
587 if (nativeWorkspace) return;
588 const targetWidth = panes.primarySidebarVisible
589 ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0)
590 : 0;
591 renderedSidebarWidth.value = withTiming(targetWidth, WORKSPACE_PANE_TIMING, (finished) => {
592 if (finished && Platform.OS === "android") runOnJS(settleContentWidth)(contentRevision);
593 });
594 renderedRailWidth.set(withTiming(navigationRailWidth, WORKSPACE_PANE_TIMING));
595 }, [
596 nativeWorkspace,
597 navigationRailWidth,
598 layout.listPaneWidth,
599 layout.listPaneGap,
600 panes.primarySidebarVisible,
601 renderedSidebarWidth,
602 renderedRailWidth,
603 settleContentWidth,
604 contentRevision,
605 ]);
606 const railAnimatedStyle = useAnimatedStyle(() => ({ width: renderedRailWidth.value }));
607 // Showing or hiding the sidebar clips a fixed-width list. A hinge resize instead tracks the
608 // animated edge, so the header actions slide with it rather than jumping or being covered.
609 const listPaneGap = layout.listPaneGap ?? 0;
610 const listPaneAnimatedStyle = useAnimatedStyle(() => ({
611 width: Math.min(
612 listPaneCeiling,
613 Math.max(listPaneFloor, renderedSidebarWidth.value - listPaneGap),
614 ),
615 }));
616 const sidebarAnimatedStyle = useAnimatedStyle(() => ({
617 // Fading a UIKit glass ancestor can leave remounted header effects missing.
618 opacity: Platform.OS === "ios" ? 1 : Math.min(1, renderedSidebarWidth.value / 80),
619 width: renderedSidebarWidth.value,
620 }));
621
622 // The navigator (native header + markdown feed) lays out ONCE per pane toggle
623 // instead of re-measuring on every animation frame — the animating columns
624 // merely clip/reveal it over a matching background.
625 const renderedInspectorWidth = useSharedValue(inspectorColumnTargetWidth);
626 const renderedContentWidth = useDerivedValue(() =>
627 Math.max(
628 0,
629 viewportWidth -
630 renderedRailWidth.value -
631 renderedSidebarWidth.value -
632 renderedInspectorWidth.value,
633 ),
634 );
635
636 const selectAndroidWorkspace = useAndroidWorkspaceSelection();
637 const handleSelectThread = useCallback(
638 (thread: EnvironmentThreadShell) => {
639 const params = {
640 environmentId: String(thread.environmentId),
641 threadId: String(thread.id),
642 };
643 if (Platform.OS === "android") {
644 setFileInspectorPreferredVisible(false);
645 selectAndroidWorkspace({ name: "Thread", params });
646 return;
647 }
648 const navigationAction = resolveThreadSelectionNavigationAction({
649 usesSplitView: layout.usesSplitView,
650 pathname,
651 });
652 const overlayState = resolveThreadSelectionOverlayState({
653 state: navigation.getState(),
654 workspaceRouteKey: props.workspaceRouteKey,
655 action: navigationAction,
656 params,
657 });
658 if (overlayState !== null) {
659 setFileInspectorPreferredVisible(false);
660 navigation.dispatch(CommonActions.reset(overlayState));
661 return;
662 }
663 if (navigationAction === "set-params") {
664 const nextThreadKey = scopedThreadKey(thread.environmentId, thread.id);
665 if (nextThreadKey === selectedThreadKey) {
666 return;
667 }
668 setFileInspectorPreferredVisible(false);
669 navigation.navigate("Thread", params);
670 return;
671 }
672 if (navigationAction === "replace") {
673 setFileInspectorPreferredVisible(false);
674 navigation.dispatch(StackActions.replace("Thread", params));
675 return;
676 }
677 navigation.navigate("Thread", params);
678 },
679 [
680 layout.usesSplitView,
681 pathname,
682 navigation,
683 selectedThreadKey,
684 props.workspaceRouteKey,
685 selectAndroidWorkspace,
686 ],
687 );
688
689 const contextValue = useMemo(
690 () => ({
691 layout,
692 panes,
693 fileInspector,
694 primarySidebarSearchQuery,
695 selectThread: handleSelectThread,
696 activateAuxiliaryPaneRole,
697 registerWorkspaceInspector,
698 setPrimarySidebarSearchQuery,
699 showAuxiliaryPane,
700 toggleAuxiliaryPane,
701 togglePrimarySidebar,
702 setAuxiliaryPaneWidth,
703 }),
704 [
705 activateAuxiliaryPaneRole,
706 fileInspector,
707 handleSelectThread,
708 layout,
709 panes,
710 primarySidebarSearchQuery,
711 registerWorkspaceInspector,
712 showAuxiliaryPane,
713 setPrimarySidebarSearchQuery,
714 setAuxiliaryPaneWidth,
715 toggleAuxiliaryPane,
716 togglePrimarySidebar,
717 ],
718 );
719
720 if (nativeWorkspace) {
721 return (
722 <HomeListOptionsProvider
723 projectGroupingMode={projectGroupingMode}
724 savedFilters={props.savedThreadListFilters}
725 >
726 <AdaptiveWorkspaceContext value={contextValue}>
727 <NativeWorkspaceInspectorContext
728 value={{ render: workspaceInspector?.render, visible: inspectorColumnTargetWidth > 0 }}
729 >
730 {props.children}
731 </NativeWorkspaceInspectorContext>
732 </AdaptiveWorkspaceContext>
733 </HomeListOptionsProvider>
734 );
735 }
736 return (
737 <HomeListOptionsProvider
738 projectGroupingMode={projectGroupingMode}
739 savedFilters={props.savedThreadListFilters}
740 >
741 <AdaptiveWorkspaceContext.Provider value={contextValue}>
742 <WindowHingeContext value={windowHinge}>
743 <View
744 testID="adaptive-workspace-layout"
745 className={Platform.OS === "android" ? "flex-1 flex-row bg-header" : "flex-1 flex-row"}
746 >
747 <AndroidHingeObserver onChange={handleHingeChange} />
748 {shouldRenderNavigationRail ? (
749 <Animated.View
750 className="self-stretch overflow-hidden"
751 accessibilityElementsHidden={navigationRailWidth === 0}
752 importantForAccessibility={navigationRailWidth > 0 ? "auto" : "no-hide-descendants"}
753 pointerEvents={navigationRailWidth > 0 ? "auto" : "none"}
754 style={railAnimatedStyle}
755 >
756 <AndroidWorkspaceNavigation rail onSelect={revealPrimarySidebar} />
757 </Animated.View>
758 ) : null}
759 {shouldRenderPrimarySidebar && layout.listPaneWidth !== null ? (
760 <Animated.View
761 className="self-stretch overflow-hidden"
762 accessibilityElementsHidden={!panes.primarySidebarVisible}
763 collapsable={false}
764 importantForAccessibility={
765 panes.primarySidebarVisible ? "auto" : "no-hide-descendants"
766 }
767 pointerEvents={panes.primarySidebarVisible ? "auto" : "none"}
768 style={sidebarAnimatedStyle}
769 >
770 <Animated.View className="flex-1" style={listPaneAnimatedStyle}>
771 <RenderErrorBoundary
772 renderFallback={(fallback) => (
773 <RenderFailureView
774 {...fallback}
775 title="The sidebar couldn't be displayed"
776 exit={{ label: "Open settings", onPress: handleOpenSettings }}
777 />
778 )}
779 >
780 <AndroidWorkspaceSidebar
781 onRequestVisibility={revealPrimarySidebar}
782 selectedPullRequestKey={props.selectedPullRequestKey ?? null}
783 >
784 <AndroidHomeFabLayout sidebar onStartNewTask={handleStartNewTask}>
785 <ThreadNavigationSidebar
786 width={layout.listPaneWidth}
787 visible={panes.primarySidebarVisible}
788 onRequestVisibility={revealPrimarySidebar}
789 selectedThreadKey={selectedThreadKey}
790 onOpenSettings={handleOpenSettings}
791 onOpenEnvironmentSettings={handleOpenEnvironmentSettings}
792 onNewThreadInProject={handleNewThreadInProject}
793 onNewThreadOnBranch={handleNewThreadOnBranch}
794 onSelectThread={handleSelectThread}
795 onSearchQueryChange={setPrimarySidebarSearchQuery}
796 searchQuery={primarySidebarSearchQuery}
797 />
798 </AndroidHomeFabLayout>
799 </AndroidWorkspaceSidebar>
800 </RenderErrorBoundary>
801 </Animated.View>
802 </Animated.View>
803 ) : null}
804 <View
805 className={
806 Platform.OS === "android"
807 ? "flex-1 overflow-hidden bg-header"
808 : "flex-1 overflow-hidden bg-screen"
809 }
810 collapsable={false}
811 >
812 <View
813 collapsable={false}
814 style={
815 contentSettledWidth !== null
816 ? {
817 flex: 1,
818 width: contentSettledWidth,
819 }
820 : { flex: 1 }
821 }
822 >
823 <WorkspaceContentWidthContext
824 value={layout.usesSplitView ? renderedContentWidth : null}
825 >
826 {props.children}
827 </WorkspaceContentWidthContext>
828 </View>
829 </View>
830 <WorkspaceInspectorPane
831 pathname={props.pathname}
832 renderedInspectorWidth={renderedInspectorWidth}
833 active={workspaceInspector?.active ?? false}
834 panes={panes}
835 renderInspector={workspaceInspector?.render}
836 setAuxiliaryPaneWidth={setAuxiliaryPaneWidth}
837 onClosed={handleWorkspaceInspectorClosed}
838 />
839 </View>
840 </WindowHingeContext>
841 </AdaptiveWorkspaceContext.Provider>
842 </HomeListOptionsProvider>
843 );
844}