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
| 1 | import { useGT } from "gt-react-native"; |
| 2 | import { |
| 3 | WorkspaceColumnsRendererContext, |
| 4 | WorkspacePresentationHostContext, |
| 5 | } from "./workspace-presentation-context"; |
| 6 | import { useAndroidFloatingTabs } from "../settings/appearance/AppearancePreferencesProvider"; |
| 7 | import { SafeAreaInsetsContext, useSafeAreaInsets } from "react-native-safe-area-context"; |
| 8 | import { |
| 9 | AdaptiveWorkspaceContext, |
| 10 | WorkspacePaneStateContext, |
| 11 | useAdaptiveWorkspaceLayout, |
| 12 | useWorkspacePaneState, |
| 13 | type AdaptiveWorkspaceActions, |
| 14 | } from "./adaptive-workspace-context"; |
| 15 | import { threadSelectionDeferral } from "./deferred-navigation"; |
| 16 | import { uuidv4 } from "../../lib/uuid"; |
| 17 | import { EnvironmentId, ThreadId, type SidebarProjectGroupingMode } from "@t3tools/contracts"; |
| 18 | import { useAtomValue } from "@effect/atom-react"; |
| 19 | import { NavigationContext, NavigationRouteContext, useNavigation } from "@react-navigation/native"; |
| 20 | import { |
| 21 | use, |
| 22 | useEffect, |
| 23 | useEffectEvent, |
| 24 | useLayoutEffect, |
| 25 | useMemo, |
| 26 | useRef, |
| 27 | useState, |
| 28 | type ReactNode, |
| 29 | } from "react"; |
| 30 | import { |
| 31 | BackHandler, |
| 32 | Keyboard, |
| 33 | Platform, |
| 34 | Pressable, |
| 35 | useWindowDimensions, |
| 36 | View, |
| 37 | } from "react-native"; |
| 38 | import Animated, { |
| 39 | runOnJS, |
| 40 | useAnimatedStyle, |
| 41 | useDerivedValue, |
| 42 | useSharedValue, |
| 43 | withTiming, |
| 44 | } from "react-native-reanimated"; |
| 45 | import { AsyncResult } from "effect/reactivity"; |
| 46 | |
| 47 | import { |
| 48 | deriveFileInspectorPaneLayout, |
| 49 | alignListPaneToHinge, |
| 50 | deriveLayout, |
| 51 | type VerticalHinge, |
| 52 | deriveWorkspacePaneLayout, |
| 53 | isPrimarySidebarPinned, |
| 54 | isWorkspaceListPath, |
| 55 | nativeInspectorColumnWidth, |
| 56 | } from "../../lib/layout"; |
| 57 | import type { ThreadListFilters } from "../../persistence/mobile-preferences"; |
| 58 | import { appAtomRegistry } from "../../state/atom-registry"; |
| 59 | import { useProjects } from "../../state/entities"; |
| 60 | import { environmentThreadShells } from "../../state/threads"; |
| 61 | import { withKnownPullRequestSummary } from "../../state/use-thread-pr"; |
| 62 | import { mobilePreferencesAtom } from "../../state/preferences"; |
| 63 | import { savedConnectionsByIdAtom } from "../../state/use-remote-environment-registry"; |
| 64 | import { workspaceConnections } from "../../state/workspace"; |
| 65 | import { |
| 66 | DEFAULT_MOBILE_PROJECT_GROUPING_SETTINGS, |
| 67 | resolveMobileProjectGroupingSettings, |
| 68 | } from "../../state/project-grouping"; |
| 69 | import { |
| 70 | parseActiveThreadPath, |
| 71 | useHardwareKeyboardCommand, |
| 72 | } from "../keyboard/hardwareKeyboardCommands"; |
| 73 | import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing"; |
| 74 | import { useEnabledNavigationTabs } from "./workspace/navigation-tabs"; |
| 75 | import { FLOATING_TABS, floatingTabsMetrics } from "./workspace/floating-workspace-tabs"; |
| 76 | import { |
| 77 | AndroidWorkspaceNavigation, |
| 78 | FloatingWorkspaceTabs, |
| 79 | ANDROID_WORKSPACE_RAIL_WIDTH, |
| 80 | } from "./workspace/AndroidWorkspaceNavigation"; |
| 81 | import { |
| 82 | selectWorkspaceDetail, |
| 83 | type WorkspaceDetail, |
| 84 | type WorkspaceDetailSheet, |
| 85 | } from "./workspace/workspace-detail-selection"; |
| 86 | import { startOpeningDetail } from "./workspace/useSelectWorkspaceDetail"; |
| 87 | import { useWorkspaceTabs } from "./workspace/WorkspaceTabsContext"; |
| 88 | import { HomeListOptionsProvider } from "../home/home-list-options"; |
| 89 | import { buildHomeProjectScopes, resolveHomeProjectFilterScope } from "../home/homeThreadList"; |
| 90 | import { newTaskDestination, newTaskFilteredProject } from "../threads/new-task-project-selection"; |
| 91 | import { RenderErrorBoundary, RenderFailureView } from "../../components/RenderErrorBoundary"; |
| 92 | import { PaneContext } from "../../components/ui/Pane"; |
| 93 | import { WORKSPACE_PANE_TIMING } from "./workspace-pane-animation"; |
| 94 | import { WorkspaceInspectorPane } from "./workspace-inspector-pane"; |
| 95 | import { WorkspaceInspectorSheet } from "./workspace-inspector-sheet"; |
| 96 | import type { InspectorContent } from "./inspector-panes"; |
| 97 | import { |
| 98 | WorkspaceContentWidthContext, |
| 99 | WorkspaceSettledContentWidthContext, |
| 100 | } from "./workspace-content-width"; |
| 101 | import { |
| 102 | NativeWorkspaceModeContext, |
| 103 | NativeWorkspaceInspectorContext, |
| 104 | } from "../../native/v5-workspace-context"; |
| 105 | |
| 106 | import { useNativeLayoutMetrics } from "../../native/native-layout-metrics"; |
| 107 | import { AndroidHingeObserver } from "../../native/AndroidHingeObserver"; |
| 108 | import { WindowHingeContext } from "../../native/window-hinge-context"; |
| 109 | import { useAndroidWorkspaceRoot } from "./workspace/AndroidWorkspaceTabsProvider"; |
| 110 | import { dispatchRootStackReset, rootStackState } from "../../lib/root-stack-reset"; |
| 111 | import { |
| 112 | currentWorkspaceSelections, |
| 113 | workspaceSelectionCheck, |
| 114 | } from "./workspace/workspace-selection-store"; |
| 115 | import { |
| 116 | workspaceTabColumns, |
| 117 | workspaceTabNavigationState, |
| 118 | } from "./workspace/workspace-tab-navigation"; |
| 119 | import { workspaceTabOf, type WorkspaceTab } from "./workspace/workspace-tabs-state"; |
| 120 | import { workspaceRoutesOf } from "./workspace-overlay-routes"; |
| 121 | import { SettingsWorkspaceLayout } from "../settings/SettingsWorkspaceLayout"; |
| 122 | import { WorkspaceThreadSidebar } from "./workspace/WorkspaceThreadSidebar"; |
| 123 | |
| 124 | export { useAdaptiveWorkspaceLayout, useWorkspacePaneState }; |
| 125 | |
| 126 | /** |
| 127 | * Shows these panes in the workspace's inspector while this screen is focused. Register on focus |
| 128 | * events because frozen blurred screens may never clean up. |
| 129 | */ |
| 130 | export function Inspector(props: { |
| 131 | readonly content: InspectorContent; |
| 132 | /** Opens the column the first time this key registers while its route stays on the stack. */ |
| 133 | readonly revealKey?: string; |
| 134 | }) { |
| 135 | const { registerWorkspaceInspector } = useAdaptiveWorkspaceLayout(); |
| 136 | const navigation = use(NavigationContext); |
| 137 | const route = use(NavigationRouteContext); |
| 138 | const content: InspectorContent = { |
| 139 | ...props.content, |
| 140 | resetKeys: [route?.key, ...(props.content.resetKeys ?? [])], |
| 141 | panes: props.content.panes.map((pane) => ({ |
| 142 | ...pane, |
| 143 | render: () => ( |
| 144 | <NavigationContext.Provider value={navigation}> |
| 145 | <NavigationRouteContext.Provider value={route}> |
| 146 | {pane.render()} |
| 147 | </NavigationRouteContext.Provider> |
| 148 | </NavigationContext.Provider> |
| 149 | ), |
| 150 | })), |
| 151 | }; |
| 152 | const focusedRef = useRef(false); |
| 153 | const deactivateRef = useRef<(() => void) | null>(null); |
| 154 | |
| 155 | const syncRegistration = useEffectEvent(() => { |
| 156 | if (!focusedRef.current) { |
| 157 | deactivateRef.current?.(); |
| 158 | return; |
| 159 | } |
| 160 | deactivateRef.current = registerWorkspaceInspector(content, route?.key, props.revealKey); |
| 161 | }); |
| 162 | |
| 163 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Navigation focus events own the inspector registration lifetime. |
| 164 | useEffect(() => { |
| 165 | if (navigation === undefined) return; |
| 166 | const focus = () => { |
| 167 | if (focusedRef.current) return; |
| 168 | focusedRef.current = true; |
| 169 | syncRegistration(); |
| 170 | }; |
| 171 | const blur = () => { |
| 172 | focusedRef.current = false; |
| 173 | syncRegistration(); |
| 174 | }; |
| 175 | if (navigation.isFocused()) focus(); |
| 176 | const unsubscribeFocus = navigation.addListener("focus", focus); |
| 177 | const unsubscribeBlur = navigation.addListener("blur", blur); |
| 178 | return () => { |
| 179 | unsubscribeFocus(); |
| 180 | unsubscribeBlur(); |
| 181 | focusedRef.current = false; |
| 182 | deactivateRef.current?.(); |
| 183 | deactivateRef.current = null; |
| 184 | }; |
| 185 | }, [navigation]); |
| 186 | |
| 187 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Changed column content replaces the focused inspector registration. |
| 188 | useEffect(() => { |
| 189 | if (focusedRef.current) { |
| 190 | syncRegistration(); |
| 191 | } |
| 192 | // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Changed column content replaces its existing native workspace registration. |
| 193 | }, [props.content, props.revealKey]); |
| 194 | return null; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Actions whose identities never change, so a pane toggle doesn't re-render every screen that |
| 199 | * holds one. Each call runs the latest render's handler. |
| 200 | */ |
| 201 | function useStableActions(actions: Required<AdaptiveWorkspaceActions>): AdaptiveWorkspaceActions { |
| 202 | const latest = useRef(actions); |
| 203 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- points the stable actions at the handlers this commit rendered |
| 204 | useLayoutEffect(() => { |
| 205 | latest.current = actions; |
| 206 | }); |
| 207 | const [stable] = useState((): AdaptiveWorkspaceActions => ({ |
| 208 | startNewTask: () => latest.current.startNewTask(), |
| 209 | selectDetail: (detail, sheet) => latest.current.selectDetail(detail, sheet), |
| 210 | registerWorkspaceInspector: (content, routeKey, revealKey) => |
| 211 | latest.current.registerWorkspaceInspector(content, routeKey, revealKey), |
| 212 | setPrimarySidebarSearchQuery: (query) => latest.current.setPrimarySidebarSearchQuery(query), |
| 213 | showInspector: () => latest.current.showInspector(), |
| 214 | toggleAuxiliaryPane: () => latest.current.toggleAuxiliaryPane(), |
| 215 | togglePrimarySidebar: () => latest.current.togglePrimarySidebar(), |
| 216 | revealPrimarySidebar: () => latest.current.revealPrimarySidebar(), |
| 217 | setPrimarySidebarVisible: (visible) => latest.current.setPrimarySidebarVisible(visible), |
| 218 | openWorkspaceTab: (tab, options) => latest.current.openWorkspaceTab(tab, options), |
| 219 | setAuxiliaryPaneWidth: (width) => latest.current.setAuxiliaryPaneWidth(width), |
| 220 | setNativeInspectorWidth: (width) => latest.current.setNativeInspectorWidth(width), |
| 221 | })); |
| 222 | return stable; |
| 223 | } |
| 224 | |
| 225 | export function AdaptiveWorkspaceLayout(props: { |
| 226 | readonly children: ReactNode; |
| 227 | readonly pathname: string; |
| 228 | readonly workspaceRouteKey: string | undefined; |
| 229 | readonly workspaceRouteName: string | undefined; |
| 230 | }) { |
| 231 | const preferencesResult = useAtomValue(mobilePreferencesAtom); |
| 232 | if (!AsyncResult.isSuccess(preferencesResult)) { |
| 233 | return AsyncResult.isFailure(preferencesResult) ? ( |
| 234 | <AdaptiveWorkspaceLayoutContent |
| 235 | {...props} |
| 236 | projectGroupingMode={DEFAULT_MOBILE_PROJECT_GROUPING_SETTINGS.sidebarProjectGroupingMode} |
| 237 | /> |
| 238 | ) : null; |
| 239 | } |
| 240 | const groupingSettings = resolveMobileProjectGroupingSettings(preferencesResult.value); |
| 241 | return ( |
| 242 | <AdaptiveWorkspaceLayoutContent |
| 243 | {...props} |
| 244 | projectGroupingMode={groupingSettings.sidebarProjectGroupingMode} |
| 245 | savedThreadListFilters={preferencesResult.value.threadListFilters} |
| 246 | /> |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | function AdaptiveWorkspaceLayoutContent( |
| 251 | props: { |
| 252 | readonly children: ReactNode; |
| 253 | readonly pathname: string; |
| 254 | readonly workspaceRouteKey: string | undefined; |
| 255 | readonly workspaceRouteName: string | undefined; |
| 256 | } & { |
| 257 | readonly projectGroupingMode: SidebarProjectGroupingMode; |
| 258 | readonly savedThreadListFilters?: ThreadListFilters | undefined; |
| 259 | }, |
| 260 | ) { |
| 261 | const gt = useGT(); |
| 262 | const floatingTabsEnabled = useAndroidFloatingTabs(); |
| 263 | const projectGroupingMode = props.projectGroupingMode; |
| 264 | const nativeWorkspace = use(NativeWorkspaceModeContext); |
| 265 | const usesPresentationHost = use(WorkspacePresentationHostContext); |
| 266 | const windowDimensions = useWindowDimensions(); |
| 267 | const nativeMetrics = useNativeLayoutMetrics(); |
| 268 | const { width: viewportWidth, height } = nativeMetrics ?? windowDimensions; |
| 269 | const pathname = props.pathname; |
| 270 | const workspace = useWorkspaceTabs(); |
| 271 | // Settings and Usage occupy the workspace rather than a list/detail pair. |
| 272 | const androidSettingsOpen = useAndroidWorkspaceRoot()?.settingsOpen ?? false; |
| 273 | const safeAreaInsets = useSafeAreaInsets(); |
| 274 | const workspacePage = |
| 275 | Platform.OS === "android" && (androidSettingsOpen || workspace?.state.selectedTab === "usage"); |
| 276 | const navigation = useNavigation(); |
| 277 | const [primarySidebarPreferredVisible, setPrimarySidebarPreferredVisible] = useState(true); |
| 278 | const [fileInspectorPreferredVisible, setFileInspectorPreferredVisible] = useState(true); |
| 279 | const [nativeInspectorWidthFraction, setNativeInspectorWidthFraction] = useState(0.4); |
| 280 | const horizontalSafeArea = |
| 281 | (nativeMetrics?.safeArea.left ?? 0) + (nativeMetrics?.safeArea.right ?? 0); |
| 282 | const inspectorWidthFor = (fraction: number) => |
| 283 | nativeInspectorColumnWidth({ viewportWidth, horizontalSafeArea, fraction }); |
| 284 | const nativeInspectorWidth = inspectorWidthFor(nativeInspectorWidthFraction); |
| 285 | const [fileInspectorPreferredWidth, setFileInspectorPreferredWidth] = useState<number | null>( |
| 286 | null, |
| 287 | ); |
| 288 | const [primarySidebarSearchQuery, setPrimarySidebarSearchQuery] = useState(""); |
| 289 | const [railOverlay, setRailOverlay] = useState(false); |
| 290 | // The compiler can't memoize these derivations: they stay live across later hook calls. Fresh |
| 291 | // objects here change the workspace context on every render, so every workspace screen |
| 292 | // re-renders on any layout state change, and the inspector registration then loops: each |
| 293 | // re-register re-renders its screen, which hands over a new inspector renderer. |
| 294 | const baseLayout = useMemo( |
| 295 | () => deriveLayout({ width: viewportWidth, height, nativeMetrics, nativeWorkspace }), |
| 296 | [height, viewportWidth, nativeMetrics, nativeWorkspace], |
| 297 | ); |
| 298 | // The overlay sidebar closes when a page opens from it, and stays open on a list or on the |
| 299 | // page a tab switch opened (`keepOpenFor`, set by that switch). |
| 300 | const [overlaySidebar, setOverlaySidebar] = useState({ |
| 301 | active: baseLayout.variant === "medium", |
| 302 | pathname, |
| 303 | visible: isWorkspaceListPath(pathname), |
| 304 | keepOpenFor: null as string | null, |
| 305 | }); |
| 306 | if ( |
| 307 | overlaySidebar.active !== (baseLayout.variant === "medium") || |
| 308 | (overlaySidebar.active && overlaySidebar.pathname !== pathname) |
| 309 | ) { |
| 310 | setOverlaySidebar({ |
| 311 | active: baseLayout.variant === "medium", |
| 312 | pathname, |
| 313 | visible: |
| 314 | isWorkspaceListPath(pathname) || |
| 315 | (overlaySidebar.keepOpenFor !== null && |
| 316 | overlaySidebar.keepOpenFor === props.workspaceRouteKey), |
| 317 | keepOpenFor: null, |
| 318 | }); |
| 319 | } |
| 320 | const preferredSidebarVisible = |
| 321 | baseLayout.variant === "medium" ? overlaySidebar.visible : primarySidebarPreferredVisible; |
| 322 | const primarySidebarPinned = isPrimarySidebarPinned({ |
| 323 | pathname, |
| 324 | nativeWorkspace, |
| 325 | usesSplitView: baseLayout.usesSplitView, |
| 326 | }); |
| 327 | const showPrimarySidebar = !workspacePage && (primarySidebarPinned || preferredSidebarVisible); |
| 328 | // Medium windows float the sidebar over a page; beside a list it keeps its room. |
| 329 | const sidebarFloats = |
| 330 | baseLayout.variant === "medium" && |
| 331 | (nativeWorkspace || (!primarySidebarPinned && !workspacePage)); |
| 332 | const shouldRenderNavigationRail = |
| 333 | Platform.OS === "android" && baseLayout.usesSplitView && !floatingTabsEnabled; |
| 334 | const enabledTabs = useEnabledNavigationTabs(); |
| 335 | const { scale, fabSize } = useAndroidControlSizing(); |
| 336 | const floatingTabs = floatingTabsMetrics(enabledTabs.length + 1, scale, fabSize); |
| 337 | // The rail stays whether or not the sidebar shows; only the list pane comes and goes. |
| 338 | const navigationRailWidth = shouldRenderNavigationRail ? ANDROID_WORKSPACE_RAIL_WIDTH : 0; |
| 339 | const [hinge, setHinge] = useState<VerticalHinge | null>(null); |
| 340 | const [windowHinge, setWindowHinge] = useState<VerticalHinge | null>(null); |
| 341 | const handleHingeChange = (next: VerticalHinge | null, nextWindow: VerticalHinge | null) => { |
| 342 | setHinge(next); |
| 343 | setWindowHinge(nextWindow); |
| 344 | }; |
| 345 | const layout = useMemo(() => { |
| 346 | const aligned = alignListPaneToHinge({ |
| 347 | layout: baseLayout, |
| 348 | hinge, |
| 349 | viewportWidth, |
| 350 | leadingWidth: navigationRailWidth, |
| 351 | }); |
| 352 | if ( |
| 353 | !floatingTabsEnabled || |
| 354 | !aligned.usesSplitView || |
| 355 | aligned.listPaneGap !== undefined || |
| 356 | aligned.listPaneWidth === null || |
| 357 | aligned.listPaneWidth >= floatingTabs.row || |
| 358 | viewportWidth - floatingTabs.row < 320 |
| 359 | ) |
| 360 | return aligned; |
| 361 | return { ...aligned, listPaneWidth: floatingTabs.row }; |
| 362 | }, [ |
| 363 | baseLayout, |
| 364 | hinge, |
| 365 | navigationRailWidth, |
| 366 | viewportWidth, |
| 367 | floatingTabs.row, |
| 368 | floatingTabsEnabled, |
| 369 | ]); |
| 370 | // The rail keeps its room even while the sidebar floats; only the list pane overlays the page. |
| 371 | const width = viewportWidth - navigationRailWidth; |
| 372 | // The sidebar stays mounted under a workspace page, so its list is ready on the way back. |
| 373 | const shouldRenderPrimarySidebar = layout.usesSplitView; |
| 374 | const fileInspector = useMemo( |
| 375 | () => |
| 376 | deriveFileInspectorPaneLayout({ |
| 377 | layout, |
| 378 | viewportWidth: width, |
| 379 | preferredWidth: fileInspectorPreferredWidth ?? undefined, |
| 380 | reservedLeadingWidth: |
| 381 | shouldRenderPrimarySidebar && showPrimarySidebar && !sidebarFloats |
| 382 | ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0) |
| 383 | : 0, |
| 384 | }), |
| 385 | [ |
| 386 | fileInspectorPreferredWidth, |
| 387 | layout, |
| 388 | showPrimarySidebar, |
| 389 | shouldRenderPrimarySidebar, |
| 390 | sidebarFloats, |
| 391 | width, |
| 392 | ], |
| 393 | ); |
| 394 | const [workspaceInspector, setWorkspaceInspector] = useState<{ |
| 395 | readonly content: InspectorContent; |
| 396 | readonly active: boolean; |
| 397 | readonly routeKey: string | undefined; |
| 398 | } | null>(null); |
| 399 | // Without a column the inspector is a sheet, opened only on request for the route on top, and |
| 400 | // shown again when that route returns to the top. |
| 401 | const [inspectorSheetRoute, setInspectorSheetRoute] = useState<string | null>(null); |
| 402 | const inspectorSheetOpen = |
| 403 | !fileInspector.supported && |
| 404 | !workspacePage && |
| 405 | inspectorSheetRoute !== null && |
| 406 | workspaceInspector?.active === true && |
| 407 | workspaceInspector.routeKey === inspectorSheetRoute; |
| 408 | const inspectorActive = workspaceInspector?.active ?? false; |
| 409 | const panes = useMemo( |
| 410 | () => |
| 411 | deriveWorkspacePaneLayout({ |
| 412 | layout, |
| 413 | viewportWidth: width, |
| 414 | horizontalSafeArea, |
| 415 | nativeWorkspace, |
| 416 | primarySidebarFloats: sidebarFloats, |
| 417 | primarySidebarPreferredVisible: showPrimarySidebar, |
| 418 | // Only a column a screen fills makes room, so a stale preference can't hide the sidebar. |
| 419 | auxiliaryPanePreferredVisible: |
| 420 | !workspacePage && inspectorActive && fileInspectorPreferredVisible, |
| 421 | auxiliaryPanePreferredWidth: fileInspectorPreferredWidth ?? undefined, |
| 422 | nativeInspectorWidth, |
| 423 | }), |
| 424 | [ |
| 425 | nativeInspectorWidth, |
| 426 | horizontalSafeArea, |
| 427 | fileInspectorPreferredVisible, |
| 428 | fileInspectorPreferredWidth, |
| 429 | inspectorActive, |
| 430 | layout, |
| 431 | nativeWorkspace, |
| 432 | showPrimarySidebar, |
| 433 | sidebarFloats, |
| 434 | width, |
| 435 | workspacePage, |
| 436 | ], |
| 437 | ); |
| 438 | // Routes still on the stack remember their reveal, so going back to one keeps the user's choice. |
| 439 | const revealedInspectors = useRef<ReadonlyMap<string, string>>(new Map()); |
| 440 | const workspaceInspectorOwner = useRef<symbol | null>(null); |
| 441 | const registerWorkspaceInspector = ( |
| 442 | content: InspectorContent, |
| 443 | routeKey: string | undefined, |
| 444 | revealKey?: string, |
| 445 | ) => { |
| 446 | const owner = Symbol("workspace-inspector"); |
| 447 | workspaceInspectorOwner.current = owner; |
| 448 | setWorkspaceInspector({ content, active: true, routeKey }); |
| 449 | if ( |
| 450 | routeKey !== undefined && |
| 451 | revealKey !== undefined && |
| 452 | revealedInspectors.current.get(routeKey) !== revealKey |
| 453 | ) { |
| 454 | const state = rootStackState(navigation); |
| 455 | const live = new Set(state?.routes.slice(0, state.index + 1).map((route) => route.key)); |
| 456 | revealedInspectors.current = new Map( |
| 457 | [...revealedInspectors.current, [routeKey, revealKey] as const].filter( |
| 458 | ([key]) => live.has(key) || key === routeKey, |
| 459 | ), |
| 460 | ); |
| 461 | setFileInspectorPreferredVisible(true); |
| 462 | } |
| 463 | |
| 464 | return () => { |
| 465 | // Outgoing screens deactivate after incoming registration; only the current owner may deactivate. |
| 466 | if (workspaceInspectorOwner.current !== owner) { |
| 467 | return; |
| 468 | } |
| 469 | setWorkspaceInspector((current) => (current === null ? null : { ...current, active: false })); |
| 470 | }; |
| 471 | }; |
| 472 | const handleWorkspaceInspectorClosed = () => { |
| 473 | setWorkspaceInspector((current) => (current !== null && !current.active ? null : current)); |
| 474 | }; |
| 475 | const setPrimarySidebarVisible = (visible: boolean) => { |
| 476 | if (layout.variant === "medium") { |
| 477 | setOverlaySidebar((current) => ({ ...current, visible })); |
| 478 | } else { |
| 479 | setPrimarySidebarPreferredVisible(visible); |
| 480 | } |
| 481 | }; |
| 482 | const togglePrimarySidebar = () => { |
| 483 | if (primarySidebarPinned) { |
| 484 | return; |
| 485 | } |
| 486 | if (!panes.primarySidebarVisible && panes.primarySidebarSuppressedByAuxiliary) { |
| 487 | setFileInspectorPreferredVisible(false); |
| 488 | setPrimarySidebarPreferredVisible(true); |
| 489 | return; |
| 490 | } |
| 491 | setPrimarySidebarVisible(!preferredSidebarVisible); |
| 492 | }; |
| 493 | const revealPrimarySidebar = () => { |
| 494 | if (panes.primarySidebarSuppressedByAuxiliary) { |
| 495 | setFileInspectorPreferredVisible(false); |
| 496 | } |
| 497 | setPrimarySidebarVisible(true); |
| 498 | }; |
| 499 | const handleToggleSidebarCommand = () => { |
| 500 | togglePrimarySidebar(); |
| 501 | return true; |
| 502 | }; |
| 503 | const sidebarCommands = primarySidebarPinned ? [] : (["toggleSidebar"] as const); |
| 504 | useHardwareKeyboardCommand(sidebarCommands, handleToggleSidebarCommand); |
| 505 | // Resizing into a column closes the sheet without this, so the inspector moves into the column. |
| 506 | const dismissInspectorSheet = () => { |
| 507 | setFileInspectorPreferredVisible(false); |
| 508 | setInspectorSheetRoute(null); |
| 509 | }; |
| 510 | const showInspector = () => { |
| 511 | setFileInspectorPreferredVisible(true); |
| 512 | if (fileInspector.supported) { |
| 513 | setInspectorSheetRoute(null); |
| 514 | return; |
| 515 | } |
| 516 | const state = rootStackState(navigation); |
| 517 | const top = workspaceRoutesOf(state?.routes.slice(0, state.index + 1) ?? []).workspace.at(-1); |
| 518 | setInspectorSheetRoute(top?.key ?? null); |
| 519 | }; |
| 520 | const handleOpenFilesCommand = () => { |
| 521 | const activeThread = parseActiveThreadPath(pathname); |
| 522 | if (!layout.usesSplitView || !fileInspector.supported || activeThread === null) { |
| 523 | return false; |
| 524 | } |
| 525 | showInspector(); |
| 526 | if (props.workspaceRouteName === "ThreadFiles" || props.workspaceRouteName === "ThreadFile") { |
| 527 | return true; |
| 528 | } |
| 529 | navigation.navigate("ThreadFiles", activeThread); |
| 530 | return true; |
| 531 | }; |
| 532 | useHardwareKeyboardCommand("files", handleOpenFilesCommand); |
| 533 | const toggleAuxiliaryPane = () => { |
| 534 | if (fileInspector.supported) { |
| 535 | setFileInspectorPreferredVisible((current) => !current); |
| 536 | setInspectorSheetRoute(null); |
| 537 | } else if (inspectorSheetOpen) dismissInspectorSheet(); |
| 538 | else showInspector(); |
| 539 | }; |
| 540 | /** |
| 541 | * The one way to open a workspace tab: replaces the workspace with the tab's list and, when the |
| 542 | * window has columns, the page it last showed, keeping any sheet on top. An overlay sidebar |
| 543 | * stays open over that page. |
| 544 | */ |
| 545 | const openWorkspaceTab = ( |
| 546 | tab: WorkspaceTab | "settings", |
| 547 | options?: { readonly columns?: boolean }, |
| 548 | ) => { |
| 549 | const state = rootStackState(navigation); |
| 550 | if (!state) return; |
| 551 | const { workspace, overlays } = workspaceRoutesOf(state.routes.slice(0, state.index + 1)); |
| 552 | const next = workspaceTabNavigationState( |
| 553 | { ...state, routes: workspace }, |
| 554 | tab, |
| 555 | currentWorkspaceSelections(), |
| 556 | { |
| 557 | columns: workspaceTabColumns(Platform.OS, tab, options?.columns ?? layout.usesSplitView), |
| 558 | check: workspaceSelectionCheck, |
| 559 | overlays, |
| 560 | seedPullRequest: withKnownPullRequestSummary, |
| 561 | }, |
| 562 | ); |
| 563 | if (layout.variant === "medium") { |
| 564 | const opened = next.routes.at(-1 - overlays.length)?.key ?? null; |
| 565 | setOverlaySidebar((current) => ({ ...current, visible: true, keepOpenFor: opened })); |
| 566 | } |
| 567 | dispatchRootStackReset(navigation, state, next); |
| 568 | }; |
| 569 | const handleOpenSettings = () => openWorkspaceTab("settings"); |
| 570 | |
| 571 | /** The one way to show a page in the detail column; see `selectWorkspaceDetail`. */ |
| 572 | function handleSelectDetail(detail: WorkspaceDetail, sheet?: WorkspaceDetailSheet) { |
| 573 | threadSelectionDeferral.run(() => { |
| 574 | if (Platform.OS === "android") Keyboard.dismiss(); |
| 575 | setFileInspectorPreferredVisible(false); |
| 576 | setInspectorSheetRoute(null); |
| 577 | if (layout.variant === "medium") setPrimarySidebarVisible(false); |
| 578 | workspace?.dispatch({ type: "select", tab: workspaceTabOf(detail.name) }); |
| 579 | startOpeningDetail(detail); |
| 580 | navigation.dispatch(selectWorkspaceDetail(detail, sheet)); |
| 581 | }); |
| 582 | } |
| 583 | const handleSelectThreadRoute = (params: ReactNavigation.RootParamList["Thread"]) => |
| 584 | handleSelectDetail({ name: "Thread", params }); |
| 585 | |
| 586 | const projects = useProjects(); |
| 587 | const compactAndroidSettings = |
| 588 | Platform.OS === "android" && androidSettingsOpen && !layout.usesSplitView; |
| 589 | // Reads the open page when pressed, so the layout context doesn't change on every navigation. |
| 590 | const handleStartNewTask = () => { |
| 591 | const state = rootStackState(navigation); |
| 592 | const currentRoute = workspaceRoutesOf( |
| 593 | state?.routes.slice(0, state.index + 1) ?? [], |
| 594 | ).workspace.at(-1); |
| 595 | const params = |
| 596 | currentRoute?.name === "Thread" |
| 597 | ? (currentRoute.params as ReactNavigation.RootParamList["Thread"]) |
| 598 | : undefined; |
| 599 | const first = (value: string | string[] | undefined) => |
| 600 | Array.isArray(value) ? value[0] : value; |
| 601 | const environmentId = first(params?.environmentId); |
| 602 | const threadId = first(params?.threadId); |
| 603 | // The full index, not the sidebar list: subagent and archived threads are current threads too. |
| 604 | const projectId = |
| 605 | environmentId && threadId |
| 606 | ? appAtomRegistry |
| 607 | .get( |
| 608 | environmentThreadShells.environmentThreadIndexAtom(EnvironmentId.make(environmentId)), |
| 609 | ) |
| 610 | .get(ThreadId.make(threadId))?.projectId |
| 611 | : undefined; |
| 612 | const preferences = appAtomRegistry.get(mobilePreferencesAtom); |
| 613 | const saved = AsyncResult.isSuccess(preferences) ? preferences.value : undefined; |
| 614 | // Resolve the thread list's filter as useHomeListOptions does: a removed connection's filter reads as all. |
| 615 | const listFilters = saved?.threadListFilters; |
| 616 | const listEnvironmentId = |
| 617 | listFilters?.environmentId != null && |
| 618 | appAtomRegistry.get(savedConnectionsByIdAtom)[listFilters.environmentId] !== undefined |
| 619 | ? listFilters.environmentId |
| 620 | : null; |
| 621 | const filteredScope = resolveHomeProjectFilterScope( |
| 622 | buildHomeProjectScopes({ projects, environmentId: listEnvironmentId, projectGroupingMode }), |
| 623 | projects, |
| 624 | { selectedProjectKey: listFilters?.projectKey ?? null, projectGroupingMode }, |
| 625 | ); |
| 626 | const connectedEnvironmentIds = new Set( |
| 627 | appAtomRegistry |
| 628 | .get(workspaceConnections.environmentsAtom) |
| 629 | .filter((environment) => environment.connectionState === "connected") |
| 630 | .map((environment) => environment.environmentId), |
| 631 | ); |
| 632 | handleSelectThreadRoute( |
| 633 | newTaskDestination({ |
| 634 | projects, |
| 635 | requestId: uuidv4(), |
| 636 | useLastProject: saved?.useLastProjectForNewThreads, |
| 637 | currentProject: environmentId && projectId ? { environmentId, projectId } : undefined, |
| 638 | filteredProject: filteredScope |
| 639 | ? newTaskFilteredProject( |
| 640 | filteredScope, |
| 641 | listEnvironmentId, |
| 642 | connectedEnvironmentIds, |
| 643 | saved?.lastThreadProject, |
| 644 | ) |
| 645 | : undefined, |
| 646 | lastProject: saved?.lastThreadProject, |
| 647 | }), |
| 648 | ); |
| 649 | }; |
| 650 | |
| 651 | useHardwareKeyboardCommand("newTask", () => { |
| 652 | handleStartNewTask(); |
| 653 | return true; |
| 654 | }); |
| 655 | |
| 656 | const inspectorColumnTargetWidth = panes.auxiliaryPaneVisible |
| 657 | ? (panes.auxiliaryPaneWidth ?? 0) |
| 658 | : 0; |
| 659 | // Android's split content is a rounded card: keep it off the screen edge like the list card. |
| 660 | const contentEndGap = Platform.OS === "android" && layout.usesSplitView ? 4 : 0; |
| 661 | // Android's split is one strip: a sidebar slot of fixed width, then the detail pane. The |
| 662 | // sidebar never resizes; both slide by one offset, so together they always fill the width. |
| 663 | const stripSidebar = |
| 664 | Platform.OS === "android" && |
| 665 | layout.usesSplitView && |
| 666 | layout.listPaneWidth !== null && |
| 667 | !workspacePage && |
| 668 | !sidebarFloats; |
| 669 | const sidebarSlotWidth = (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0); |
| 670 | // The pane takes its new width in the commit that starts the sidebar moving, so it rewraps as |
| 671 | // the strip starts to slide rather than after it lands. |
| 672 | const contentSpansSidebar = stripSidebar && !panes.primarySidebarVisible; |
| 673 | const contentTargetWidth = !layout.usesSplitView |
| 674 | ? null |
| 675 | : Math.max( |
| 676 | 0, |
| 677 | (contentSpansSidebar ? width : panes.contentPaneWidth) - |
| 678 | inspectorColumnTargetWidth - |
| 679 | contentEndGap, |
| 680 | ); |
| 681 | // A workspace page (Usage) lays out at this width even while hidden behind another tab, so |
| 682 | // opening it widens nothing it measured. |
| 683 | const workspacePageWidth = !layout.usesSplitView |
| 684 | ? null |
| 685 | : workspacePage |
| 686 | ? contentTargetWidth |
| 687 | : Math.max( |
| 688 | 0, |
| 689 | viewportWidth - |
| 690 | (shouldRenderNavigationRail ? ANDROID_WORKSPACE_RAIL_WIDTH : 0) - |
| 691 | contentEndGap, |
| 692 | ); |
| 693 | const [listPaneSizing, setListPaneSizing] = useState({ |
| 694 | viewportWidth, |
| 695 | workspacePage, |
| 696 | sidebarVisible: panes.primarySidebarVisible, |
| 697 | listPaneTarget: layout.listPaneWidth, |
| 698 | listPaneFloor: layout.listPaneWidth, |
| 699 | listPaneCeiling: layout.listPaneWidth, |
| 700 | revision: 0, |
| 701 | }); |
| 702 | if ( |
| 703 | Platform.OS === "android" && |
| 704 | (listPaneSizing.viewportWidth !== viewportWidth || |
| 705 | listPaneSizing.workspacePage !== workspacePage || |
| 706 | listPaneSizing.sidebarVisible !== panes.primarySidebarVisible || |
| 707 | listPaneSizing.listPaneTarget !== layout.listPaneWidth) |
| 708 | ) { |
| 709 | // A book-posture hinge resizes the visible list pane instead of revealing it. |
| 710 | const resizingSidebar = |
| 711 | listPaneSizing.viewportWidth === viewportWidth && |
| 712 | listPaneSizing.workspacePage === workspacePage && |
| 713 | listPaneSizing.sidebarVisible && |
| 714 | panes.primarySidebarVisible && |
| 715 | listPaneSizing.listPaneTarget !== layout.listPaneWidth; |
| 716 | setListPaneSizing({ |
| 717 | viewportWidth, |
| 718 | workspacePage, |
| 719 | sidebarVisible: panes.primarySidebarVisible, |
| 720 | listPaneTarget: layout.listPaneWidth, |
| 721 | // Retargets widen the list-width span because the animated hinge edge may still be inside it. |
| 722 | listPaneFloor: resizingSidebar |
| 723 | ? Math.min(listPaneSizing.listPaneFloor ?? 0, layout.listPaneWidth ?? 0) |
| 724 | : layout.listPaneWidth, |
| 725 | listPaneCeiling: resizingSidebar |
| 726 | ? Math.max(listPaneSizing.listPaneCeiling ?? 0, layout.listPaneWidth ?? 0) |
| 727 | : layout.listPaneWidth, |
| 728 | revision: listPaneSizing.revision + 1, |
| 729 | }); |
| 730 | } |
| 731 | const settleListPane = (revision: number) => { |
| 732 | setListPaneSizing((current) => |
| 733 | current.revision !== revision || |
| 734 | (current.listPaneFloor === current.listPaneTarget && |
| 735 | current.listPaneCeiling === current.listPaneTarget) |
| 736 | ? current |
| 737 | : { |
| 738 | ...current, |
| 739 | listPaneFloor: current.listPaneTarget, |
| 740 | listPaneCeiling: current.listPaneTarget, |
| 741 | }, |
| 742 | ); |
| 743 | }; |
| 744 | const listPaneFloor = listPaneSizing.listPaneFloor ?? 0; |
| 745 | const listPaneCeiling = listPaneSizing.listPaneCeiling ?? 0; |
| 746 | const listPaneRevision = listPaneSizing.revision; |
| 747 | // Android lays the content out at its new width as soon as a pane toggles, then holds it |
| 748 | // against the edge that stays put while the other one slides, so nothing reflows after the |
| 749 | // panes land. A sliding sidebar uncovers the strip it is about to fill, or covers the content |
| 750 | // until it is gone. |
| 751 | const contentLeftTarget = sidebarFloats |
| 752 | ? navigationRailWidth |
| 753 | : navigationRailWidth + |
| 754 | (shouldRenderPrimarySidebar && !workspacePage && panes.primarySidebarVisible |
| 755 | ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0) |
| 756 | : 0); |
| 757 | const [contentAnchor, setContentAnchor] = useState({ |
| 758 | viewportWidth, |
| 759 | left: contentLeftTarget, |
| 760 | inspector: inspectorColumnTargetWidth, |
| 761 | end: false, |
| 762 | }); |
| 763 | if ( |
| 764 | Platform.OS === "android" && |
| 765 | (contentAnchor.viewportWidth !== viewportWidth || |
| 766 | contentAnchor.left !== contentLeftTarget || |
| 767 | contentAnchor.inspector !== inspectorColumnTargetWidth) |
| 768 | ) { |
| 769 | setContentAnchor({ |
| 770 | viewportWidth, |
| 771 | left: contentLeftTarget, |
| 772 | inspector: inspectorColumnTargetWidth, |
| 773 | end: |
| 774 | contentAnchor.viewportWidth === viewportWidth && |
| 775 | contentAnchor.inspector === inspectorColumnTargetWidth, |
| 776 | }); |
| 777 | } |
| 778 | |
| 779 | const renderedSidebarWidth = useSharedValue( |
| 780 | panes.primarySidebarVisible ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0) : 0, |
| 781 | ); |
| 782 | const renderedRailWidth = useSharedValue(navigationRailWidth); |
| 783 | const leavingWorkspacePage = useRef(false); |
| 784 | // The slide starts after the commit that lays the pane out, not in the press handler: Reanimated |
| 785 | // commits the shadow tree every frame, and that starves a large React commit until it stops. |
| 786 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- animates the Reanimated rail and sidebar width shared values toward the layout's targets |
| 787 | useEffect(() => { |
| 788 | if (nativeWorkspace) return; |
| 789 | renderedRailWidth.set(withTiming(navigationRailWidth, WORKSPACE_PANE_TIMING)); |
| 790 | // A workspace page hides the sidebar through React instead, in the same commit that mounts |
| 791 | // the page. Animating it here would hide it a few frames before the page appears. |
| 792 | if (workspacePage) { |
| 793 | leavingWorkspacePage.current = true; |
| 794 | return; |
| 795 | } |
| 796 | const snap = leavingWorkspacePage.current; |
| 797 | leavingWorkspacePage.current = false; |
| 798 | const targetWidth = panes.primarySidebarVisible |
| 799 | ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0) |
| 800 | : 0; |
| 801 | // The content beside it already took its final width when the page closed. |
| 802 | renderedSidebarWidth.set( |
| 803 | withTiming(targetWidth, snap ? { duration: 0 } : WORKSPACE_PANE_TIMING, (finished) => { |
| 804 | // Only a hinge resize widens the list's span; a toggle has nothing to settle. |
| 805 | if (finished && Platform.OS === "android" && listPaneFloor !== listPaneCeiling) { |
| 806 | runOnJS(settleListPane)(listPaneRevision); |
| 807 | } |
| 808 | }), |
| 809 | ); |
| 810 | }, [ |
| 811 | nativeWorkspace, |
| 812 | navigationRailWidth, |
| 813 | layout.listPaneWidth, |
| 814 | layout.listPaneGap, |
| 815 | panes.primarySidebarVisible, |
| 816 | renderedSidebarWidth, |
| 817 | renderedRailWidth, |
| 818 | settleListPane, |
| 819 | listPaneFloor, |
| 820 | listPaneCeiling, |
| 821 | listPaneRevision, |
| 822 | workspacePage, |
| 823 | ]); |
| 824 | const railAnimatedStyle = useAnimatedStyle(() => ({ width: renderedRailWidth.value })); |
| 825 | const floatingSidebarAnimatedStyle = useAnimatedStyle(() => ({ |
| 826 | left: sidebarFloats ? renderedRailWidth.value : 0, |
| 827 | })); |
| 828 | const floatingSidebarOpen = sidebarFloats && panes.primarySidebarVisible; |
| 829 | // Its own value, so a sidebar that was beside a list doesn't dim the page as it leaves. |
| 830 | const scrimOpacity = useSharedValue(0); |
| 831 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- animates the Reanimated scrim with the floating sidebar |
| 832 | useEffect(() => { |
| 833 | scrimOpacity.set(withTiming(floatingSidebarOpen ? 0.32 : 0, WORKSPACE_PANE_TIMING)); |
| 834 | }, [floatingSidebarOpen, scrimOpacity]); |
| 835 | const scrimAnimatedStyle = useAnimatedStyle(() => ({ opacity: scrimOpacity.value })); |
| 836 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Back closes a floating sidebar before it leaves the page |
| 837 | useEffect(() => { |
| 838 | if (!floatingSidebarOpen || nativeWorkspace) return; |
| 839 | const subscription = BackHandler.addEventListener("hardwareBackPress", () => { |
| 840 | // A sheet or modal over the workspace takes Back first. |
| 841 | const state = rootStackState(navigation); |
| 842 | if (state && workspaceRoutesOf(state.routes.slice(0, state.index + 1)).overlays.length > 0) { |
| 843 | return false; |
| 844 | } |
| 845 | setPrimarySidebarVisible(false); |
| 846 | return true; |
| 847 | }); |
| 848 | return () => subscription.remove(); |
| 849 | }, [floatingSidebarOpen, nativeWorkspace, navigation, setPrimarySidebarVisible]); |
| 850 | // Showing or hiding the sidebar clips a fixed-width list. A hinge resize instead tracks the |
| 851 | // animated edge, so the header actions slide with it rather than jumping or being covered. |
| 852 | const listPaneGap = layout.listPaneGap ?? 0; |
| 853 | const listPaneAnimatedStyle = useAnimatedStyle(() => ({ |
| 854 | width: Math.min( |
| 855 | listPaneCeiling, |
| 856 | Math.max( |
| 857 | listPaneFloor, |
| 858 | (stripSidebar ? sidebarSlotWidth : renderedSidebarWidth.value) - listPaneGap, |
| 859 | ), |
| 860 | ), |
| 861 | })); |
| 862 | // How far the strip has slid: 0 with the sidebar shown, minus its width with it gone. |
| 863 | const stripOffset = useDerivedValue(() => |
| 864 | stripSidebar ? renderedSidebarWidth.value - sidebarSlotWidth : 0, |
| 865 | ); |
| 866 | const sidebarAnimatedStyle = useAnimatedStyle(() => ({ |
| 867 | // Fading a UIKit glass ancestor can leave remounted header effects missing. |
| 868 | opacity: |
| 869 | stripSidebar || Platform.OS === "ios" || floatingTabsEnabled |
| 870 | ? 1 |
| 871 | : Math.min(1, renderedSidebarWidth.value / 80), |
| 872 | width: stripSidebar ? sidebarSlotWidth : renderedSidebarWidth.value, |
| 873 | flexShrink: stripSidebar ? 0 : 1, |
| 874 | transform: [{ translateX: stripOffset.value }], |
| 875 | })); |
| 876 | |
| 877 | // Clip the navigator at its settled width so native headers and markdown reflow only once per pane toggle. |
| 878 | const renderedInspectorWidth = useSharedValue(inspectorColumnTargetWidth); |
| 879 | const renderedContentWidth = useDerivedValue(() => |
| 880 | Math.max( |
| 881 | 0, |
| 882 | viewportWidth - |
| 883 | renderedRailWidth.value - |
| 884 | (workspacePage || sidebarFloats ? 0 : renderedSidebarWidth.value) - |
| 885 | renderedInspectorWidth.value, |
| 886 | ), |
| 887 | ); |
| 888 | // Android can resize the native row before React removes its split panes on a fold. |
| 889 | const androidSplit = Platform.OS === "android" && layout.usesSplitView; |
| 890 | // In the strip the column itself slides with the sidebar: Android only delivers touches inside a |
| 891 | // parent's bounds, so content may not slide out of its column. Every branch sets every key: |
| 892 | // Reanimated keeps a key a branch drops, so a fold mid-slide would leave the column shifted. |
| 893 | const androidContentSlotStyle = useAnimatedStyle(() => |
| 894 | !androidSplit |
| 895 | ? { width: "100%" as const, marginRight: 0, transform: [{ translateX: 0 }] } |
| 896 | : stripSidebar |
| 897 | ? { |
| 898 | // Wide enough for the pane at either width; the margin gives the sidebar's share |
| 899 | // back to the row, so a column after it stays put while the strip slides. |
| 900 | width: Math.max( |
| 901 | 0, |
| 902 | viewportWidth - renderedRailWidth.value - renderedInspectorWidth.value, |
| 903 | ), |
| 904 | marginRight: -sidebarSlotWidth, |
| 905 | transform: [{ translateX: stripOffset.value }], |
| 906 | } |
| 907 | : { width: renderedContentWidth.value, marginRight: 0, transform: [{ translateX: 0 }] }, |
| 908 | ); |
| 909 | // While the sidebar animates, clip the settled navigator instead of reflowing its text every frame. |
| 910 | // In the strip it lifts entirely: the content outgrows its column, and a cap that follows |
| 911 | // React's width would clip the wider layout for the frame it lands. |
| 912 | const androidNavigatorLimit = useAnimatedStyle(() => ({ |
| 913 | maxWidth: stripSidebar |
| 914 | ? viewportWidth |
| 915 | : androidSplit && |
| 916 | contentTargetWidth !== null && |
| 917 | renderedContentWidth.value < contentTargetWidth |
| 918 | ? contentTargetWidth |
| 919 | : ("100%" as const), |
| 920 | })); |
| 921 | |
| 922 | // Rotating flips the overlay; until the new size reaches JS, predict the reset above. |
| 923 | const sidebarOverRotates = |
| 924 | nativeWorkspace && nativeMetrics?.hasHinge === true && layout.usesSplitView; |
| 925 | // The current orientation's panes already account for an inspector that needs the sidebar's room. |
| 926 | const sidebarInLandscape = |
| 927 | baseLayout.variant === "medium" |
| 928 | ? primarySidebarPinned || primarySidebarPreferredVisible |
| 929 | : panes.primarySidebarVisible; |
| 930 | const sidebarInPortrait = |
| 931 | primarySidebarPinned || |
| 932 | (baseLayout.variant === "medium" ? overlaySidebar.visible : isWorkspaceListPath(pathname)); |
| 933 | const sidebarByOrientation = useMemo( |
| 934 | () => |
| 935 | sidebarOverRotates ? { landscape: sidebarInLandscape, portrait: sidebarInPortrait } : null, |
| 936 | [sidebarInLandscape, sidebarInPortrait, sidebarOverRotates], |
| 937 | ); |
| 938 | |
| 939 | const actions = useStableActions({ |
| 940 | startNewTask: handleStartNewTask, |
| 941 | selectDetail: handleSelectDetail, |
| 942 | registerWorkspaceInspector, |
| 943 | setPrimarySidebarSearchQuery, |
| 944 | showInspector, |
| 945 | toggleAuxiliaryPane, |
| 946 | togglePrimarySidebar, |
| 947 | revealPrimarySidebar, |
| 948 | setPrimarySidebarVisible, |
| 949 | openWorkspaceTab, |
| 950 | setAuxiliaryPaneWidth: setFileInspectorPreferredWidth, |
| 951 | setNativeInspectorWidth: (width: number) => |
| 952 | setNativeInspectorWidthFraction(inspectorWidthFor(width / viewportWidth) / viewportWidth), |
| 953 | }); |
| 954 | const contextValue = useMemo( |
| 955 | () => ({ ...actions, layout, nativeInspectorWidth, workspacePageWidth }), |
| 956 | [actions, layout, nativeInspectorWidth, workspacePageWidth], |
| 957 | ); |
| 958 | const sidebarToggleable = layout.usesSplitView && !primarySidebarPinned && !workspacePage; |
| 959 | const paneState = useMemo( |
| 960 | () => ({ |
| 961 | panes, |
| 962 | fileInspector, |
| 963 | primarySidebarSearchQuery, |
| 964 | sidebarToggleable, |
| 965 | sidebarByOrientation, |
| 966 | }), |
| 967 | [panes, fileInspector, primarySidebarSearchQuery, sidebarToggleable, sidebarByOrientation], |
| 968 | ); |
| 969 | |
| 970 | const inspectorSheet = ( |
| 971 | <WorkspaceInspectorSheet |
| 972 | open={inspectorSheetOpen} |
| 973 | onClose={dismissInspectorSheet} |
| 974 | content={workspaceInspector?.content} |
| 975 | /> |
| 976 | ); |
| 977 | if (nativeWorkspace) { |
| 978 | return ( |
| 979 | <HomeListOptionsProvider |
| 980 | projectGroupingMode={projectGroupingMode} |
| 981 | savedFilters={props.savedThreadListFilters} |
| 982 | > |
| 983 | <AdaptiveWorkspaceContext value={contextValue}> |
| 984 | <WorkspacePaneStateContext value={paneState}> |
| 985 | <NativeWorkspaceInspectorContext |
| 986 | value={{ |
| 987 | // Without a column the sheet shows it; the hidden native column must not mount it too. |
| 988 | content: fileInspector.supported ? workspaceInspector?.content : undefined, |
| 989 | visible: inspectorColumnTargetWidth > 0, |
| 990 | }} |
| 991 | > |
| 992 | {props.children} |
| 993 | {inspectorSheet} |
| 994 | </NativeWorkspaceInspectorContext> |
| 995 | </WorkspacePaneStateContext> |
| 996 | </AdaptiveWorkspaceContext> |
| 997 | </HomeListOptionsProvider> |
| 998 | ); |
| 999 | } |
| 1000 | const renderColumns = (children: ReactNode, sidebar?: ReactNode) => ( |
| 1001 | <View |
| 1002 | testID="adaptive-workspace-layout" |
| 1003 | className={Platform.OS === "android" ? "flex-1 flex-row bg-header" : "flex-1 flex-row"} |
| 1004 | > |
| 1005 | <AndroidHingeObserver onChange={handleHingeChange} /> |
| 1006 | {shouldRenderNavigationRail ? ( |
| 1007 | <Animated.View className="shrink self-stretch" style={railAnimatedStyle} /> |
| 1008 | ) : null} |
| 1009 | {shouldRenderPrimarySidebar && layout.listPaneWidth !== null ? ( |
| 1010 | <Animated.View |
| 1011 | className="self-stretch overflow-hidden android:shrink" |
| 1012 | accessibilityElementsHidden={workspacePage || !panes.primarySidebarVisible} |
| 1013 | collapsable={false} |
| 1014 | importantForAccessibility={ |
| 1015 | !workspacePage && panes.primarySidebarVisible ? "auto" : "no-hide-descendants" |
| 1016 | } |
| 1017 | pointerEvents={!workspacePage && panes.primarySidebarVisible ? "auto" : "none"} |
| 1018 | // Under a workspace page the sidebar leaves the row but keeps its size, so its |
| 1019 | // lists never lay out at zero size and are ready, unchanged, on the way back. The |
| 1020 | // content pane, laid out after it, covers it, in the same mount as the page. |
| 1021 | style={[ |
| 1022 | workspacePage ? { position: "absolute", top: 0, bottom: 0, left: 0 } : null, |
| 1023 | sidebarFloats |
| 1024 | ? { position: "absolute", top: 0, bottom: 0, zIndex: 2, elevation: 8 } |
| 1025 | : null, |
| 1026 | floatingSidebarAnimatedStyle, |
| 1027 | sidebarAnimatedStyle, |
| 1028 | ]} |
| 1029 | > |
| 1030 | <Animated.View className="flex-1" style={listPaneAnimatedStyle}> |
| 1031 | <PaneContext value="sidebar"> |
| 1032 | <RenderErrorBoundary |
| 1033 | renderFallback={(fallback) => ( |
| 1034 | <RenderFailureView |
| 1035 | {...fallback} |
| 1036 | title={gt("The sidebar couldn't be displayed")} |
| 1037 | exit={{ label: gt("Open settings"), onPress: handleOpenSettings }} |
| 1038 | /> |
| 1039 | )} |
| 1040 | > |
| 1041 | {/* Without a sidebar route (iPadOS before 26) the columns draw the thread list. */} |
| 1042 | {sidebar ?? <WorkspaceThreadSidebar />} |
| 1043 | </RenderErrorBoundary> |
| 1044 | </PaneContext> |
| 1045 | {/* Keep the pill under the content throughout the sidebar's clipping animation. */} |
| 1046 | {floatingTabsEnabled && !workspacePage ? ( |
| 1047 | <FloatingWorkspaceTabs |
| 1048 | paneWidth={layout.listPaneWidth} |
| 1049 | onNewThread={handleStartNewTask} |
| 1050 | onSelect={revealPrimarySidebar} |
| 1051 | /> |
| 1052 | ) : null} |
| 1053 | </Animated.View> |
| 1054 | </Animated.View> |
| 1055 | ) : null} |
| 1056 | <Animated.View |
| 1057 | className={ |
| 1058 | Platform.OS === "android" |
| 1059 | ? "max-w-full shrink-0 overflow-hidden bg-header" |
| 1060 | : "flex-1 overflow-hidden bg-screen" |
| 1061 | } |
| 1062 | style={Platform.OS === "android" ? androidContentSlotStyle : undefined} |
| 1063 | collapsable={false} |
| 1064 | importantForAccessibility={floatingSidebarOpen ? "no-hide-descendants" : "auto"} |
| 1065 | accessibilityElementsHidden={floatingSidebarOpen} |
| 1066 | > |
| 1067 | <Animated.View |
| 1068 | collapsable={false} |
| 1069 | style={[ |
| 1070 | contentTargetWidth !== null |
| 1071 | ? { |
| 1072 | flex: 1, |
| 1073 | width: contentTargetWidth, |
| 1074 | // The strip's column is wider than the pane; the pane sits at its start. |
| 1075 | alignSelf: contentAnchor.end && !stripSidebar ? "flex-end" : "flex-start", |
| 1076 | marginRight: contentAnchor.end && !stripSidebar ? contentEndGap : 0, |
| 1077 | } |
| 1078 | : { flex: 1 }, |
| 1079 | Platform.OS === "android" ? androidNavigatorLimit : undefined, |
| 1080 | ]} |
| 1081 | > |
| 1082 | {/* Android holds the content still, so its floating controls keep its width. */} |
| 1083 | <WorkspaceContentWidthContext |
| 1084 | value={layout.usesSplitView && Platform.OS !== "android" ? renderedContentWidth : null} |
| 1085 | > |
| 1086 | <WorkspaceSettledContentWidthContext |
| 1087 | value={Platform.OS === "android" && layout.usesSplitView ? contentTargetWidth : null} |
| 1088 | > |
| 1089 | {/* Compact Settings keeps the bottom navigation under all of its pages. */} |
| 1090 | <SafeAreaInsetsContext |
| 1091 | value={ |
| 1092 | floatingTabsEnabled && |
| 1093 | (compactAndroidSettings || (layout.usesSplitView && workspacePage)) |
| 1094 | ? { |
| 1095 | ...safeAreaInsets, |
| 1096 | bottom: |
| 1097 | safeAreaInsets.bottom + |
| 1098 | FLOATING_TABS.bottom + |
| 1099 | FLOATING_TABS.edge + |
| 1100 | floatingTabs.height, |
| 1101 | } |
| 1102 | : compactAndroidSettings |
| 1103 | ? { ...safeAreaInsets, bottom: 0 } |
| 1104 | : safeAreaInsets |
| 1105 | } |
| 1106 | > |
| 1107 | <View className="flex-1"> |
| 1108 | <PaneContext value="content"> |
| 1109 | <SettingsWorkspaceLayout>{children}</SettingsWorkspaceLayout> |
| 1110 | </PaneContext> |
| 1111 | </View> |
| 1112 | </SafeAreaInsetsContext> |
| 1113 | {floatingTabsEnabled && |
| 1114 | (compactAndroidSettings || (layout.usesSplitView && workspacePage)) ? ( |
| 1115 | <FloatingWorkspaceTabs |
| 1116 | paneWidth={layout.usesSplitView ? (layout.listPaneWidth ?? undefined) : undefined} |
| 1117 | fillWidth={compactAndroidSettings} |
| 1118 | onNewThread={handleStartNewTask} |
| 1119 | /> |
| 1120 | ) : compactAndroidSettings ? ( |
| 1121 | <AndroidWorkspaceNavigation onNewThread={handleStartNewTask} /> |
| 1122 | ) : null} |
| 1123 | </WorkspaceSettledContentWidthContext> |
| 1124 | </WorkspaceContentWidthContext> |
| 1125 | </Animated.View> |
| 1126 | </Animated.View> |
| 1127 | {sidebarFloats ? ( |
| 1128 | <Animated.View |
| 1129 | className="absolute inset-0 bg-black" |
| 1130 | pointerEvents={floatingSidebarOpen ? "auto" : "none"} |
| 1131 | style={[{ zIndex: 1 }, scrimAnimatedStyle]} |
| 1132 | > |
| 1133 | <Pressable |
| 1134 | accessibilityLabel={gt("Hide sidebar")} |
| 1135 | className="flex-1" |
| 1136 | onPress={() => setPrimarySidebarVisible(false)} |
| 1137 | /> |
| 1138 | </Animated.View> |
| 1139 | ) : null} |
| 1140 | <PaneContext value="inspector"> |
| 1141 | <WorkspaceInspectorPane |
| 1142 | renderedInspectorWidth={renderedInspectorWidth} |
| 1143 | active={workspaceInspector?.active ?? false} |
| 1144 | panes={panes} |
| 1145 | content={workspaceInspector?.content} |
| 1146 | setAuxiliaryPaneWidth={setFileInspectorPreferredWidth} |
| 1147 | onClose={() => setFileInspectorPreferredVisible(false)} |
| 1148 | onClosed={handleWorkspaceInspectorClosed} |
| 1149 | /> |
| 1150 | </PaneContext> |
| 1151 | {inspectorSheet} |
| 1152 | {shouldRenderNavigationRail && navigationRailWidth > 0 ? ( |
| 1153 | // Its trailing inset clips the old rail as soon as Android fits the workspace to a cover display. |
| 1154 | <View |
| 1155 | className="absolute inset-y-0 left-0 overflow-hidden" |
| 1156 | style={[ |
| 1157 | railOverlay |
| 1158 | ? { right: 0 } |
| 1159 | : { right: viewportWidth - navigationRailWidth, maxWidth: navigationRailWidth }, |
| 1160 | sidebarFloats ? { zIndex: 3, elevation: 8 } : null, |
| 1161 | ]} |
| 1162 | > |
| 1163 | <AndroidWorkspaceNavigation |
| 1164 | onNewThread={handleStartNewTask} |
| 1165 | rail |
| 1166 | onSelect={revealPrimarySidebar} |
| 1167 | onOverlayChange={setRailOverlay} |
| 1168 | /> |
| 1169 | </View> |
| 1170 | ) : null} |
| 1171 | </View> |
| 1172 | ); |
| 1173 | |
| 1174 | return ( |
| 1175 | <HomeListOptionsProvider |
| 1176 | projectGroupingMode={projectGroupingMode} |
| 1177 | savedFilters={props.savedThreadListFilters} |
| 1178 | > |
| 1179 | <AdaptiveWorkspaceContext value={contextValue}> |
| 1180 | <WorkspacePaneStateContext value={paneState}> |
| 1181 | <WindowHingeContext value={windowHinge}> |
| 1182 | {usesPresentationHost ? ( |
| 1183 | <WorkspaceColumnsRendererContext value={renderColumns}> |
| 1184 | {props.children} |
| 1185 | </WorkspaceColumnsRendererContext> |
| 1186 | ) : ( |
| 1187 | renderColumns(props.children) |
| 1188 | )} |
| 1189 | </WindowHingeContext> |
| 1190 | </WorkspacePaneStateContext> |
| 1191 | </AdaptiveWorkspaceContext> |
| 1192 | </HomeListOptionsProvider> |
| 1193 | ); |
| 1194 | } |