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

AdaptiveWorkspaceLayout.tsx

856 lines31.5 KB

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