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 | createNavigatorFactory, |
| 4 | NavigationContext, |
| 5 | NavigationRouteContext, |
| 6 | StackRouter, |
| 7 | useNavigationBuilder, |
| 8 | type NavigatorTypeBagBase, |
| 9 | type ParamListBase, |
| 10 | type StackActionHelpers, |
| 11 | type StackNavigationState, |
| 12 | type StackRouterOptions, |
| 13 | type StaticConfig, |
| 14 | type TypedNavigator, |
| 15 | } from "@react-navigation/native"; |
| 16 | import { |
| 17 | NativeStackView, |
| 18 | type NativeStackNavigationEventMap, |
| 19 | type NativeStackNavigationOptions, |
| 20 | type NativeStackNavigatorProps, |
| 21 | type NativeStackTypeBag, |
| 22 | } from "@react-navigation/native-stack"; |
| 23 | import { |
| 24 | Fragment, |
| 25 | use, |
| 26 | useEffect, |
| 27 | useRef, |
| 28 | useState, |
| 29 | type ComponentProps, |
| 30 | type ReactNode, |
| 31 | } from "react"; |
| 32 | import { withKnownPullRequestSummary } from "../../state/use-thread-pr"; |
| 33 | import { |
| 34 | currentWorkspaceSelections, |
| 35 | workspaceSelectionCheck, |
| 36 | } from "./workspace/workspace-selection-store"; |
| 37 | import { workspaceTabColumns, workspaceTabPage } from "./workspace/workspace-tab-navigation"; |
| 38 | import { Platform, StyleSheet, View } from "react-native"; |
| 39 | import { Split, type SplitHostCommands } from "react-native-screens"; |
| 40 | import { SafeAreaView } from "react-native-safe-area-context"; |
| 41 | import { GestureHandlerRootView } from "react-native-gesture-handler"; |
| 42 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 43 | import { NATIVE_INSPECTOR_MIN_MAIN_WIDTH } from "../../lib/layout"; |
| 44 | import { scopedThreadKey } from "../../lib/scopedEntities"; |
| 45 | import { WorkspaceTabsProvider, useWorkspaceTabs } from "./workspace/WorkspaceTabsContext"; |
| 46 | import { |
| 47 | WorkspaceNativeTabs, |
| 48 | WorkspaceSidebarTabsFooter, |
| 49 | } from "./workspace/WorkspaceNativeTabs.ios"; |
| 50 | import { workspaceUsesColumns } from "./workspace/workspace-tabs-state"; |
| 51 | import { useWorkspaceCompose } from "./workspace/useWorkspaceCompose"; |
| 52 | import { NAVIGATION_TAB_ORDER } from "./workspace/navigation-tab-order"; |
| 53 | import { selectedIssueKeyOf } from "../issues/issueParams"; |
| 54 | import { isSettingsRoute, settingsOpenIn } from "../settings/settings-navigation"; |
| 55 | import { workspaceRoutesOf } from "./workspace-overlay-routes"; |
| 56 | import { setNativelyPoppingWorkspaceRoute } from "./workspace/workspace-detail-selection"; |
| 57 | import { useDeviceType, useNativeLayoutMetrics } from "../../native/native-layout-metrics"; |
| 58 | |
| 59 | import { useAdaptiveWorkspaceLayout, useWorkspacePaneState } from "./AdaptiveWorkspaceLayout"; |
| 60 | import { WorkspacePaneDivider } from "./workspace-pane-divider"; |
| 61 | import { WorkspaceEmptyDetail } from "./WorkspaceEmptyDetail"; |
| 62 | import { NativeColumnContent as ColumnContent } from "../../native/NativeColumnContent.ios"; |
| 63 | import { |
| 64 | modalEnvelopeOptions, |
| 65 | V5CardStackView, |
| 66 | V5StackView, |
| 67 | } from "../../native/createV5StackNavigator.ios"; |
| 68 | import { |
| 69 | useNativeWorkspaceColumnsReady, |
| 70 | useNativeWorkspaceColumnsSupported, |
| 71 | } from "../../native/NativeWorkspaceColumns"; |
| 72 | import { V5StackHeader } from "../../native/V5StackHeader.ios"; |
| 73 | import { InspectorColumnScreens } from "./inspector-host"; |
| 74 | import { PaneContext } from "../../components/ui/Pane"; |
| 75 | import { useWorkspaceSidebarHeaderItem } from "./useWorkspaceSidebarHeaderItem"; |
| 76 | import type { AppNativeStackNavigationOptions } from "../../native/StackHeader"; |
| 77 | import { |
| 78 | collapsedSplitTransition, |
| 79 | nativeWorkspacePopAction, |
| 80 | projectWorkspaceStack, |
| 81 | partitionStackPresentations, |
| 82 | reconcileStackScreens, |
| 83 | type CollapsedSplitColumn, |
| 84 | isPresentedRoute, |
| 85 | } from "../../native/workspace-stack-projection"; |
| 86 | import { |
| 87 | NativePrimaryColumnContext, |
| 88 | NativeWorkspaceInspectorContext, |
| 89 | NativeWorkspaceModeContext, |
| 90 | NativeWorkspaceTabActivityContext, |
| 91 | WorkspaceSlotOptionsContext, |
| 92 | slotScreenOptions, |
| 93 | } from "../../native/v5-workspace-context"; |
| 94 | |
| 95 | type ViewProps = ComponentProps<typeof NativeStackView>; |
| 96 | type Descriptor = ViewProps["descriptors"][string]; |
| 97 | type Route = ViewProps["state"]["routes"][number]; |
| 98 | |
| 99 | const isPullRequestRoute = (route: Route) => |
| 100 | route.name === "PullRequest" || |
| 101 | route.name === "PullRequestFile" || |
| 102 | route.name === "PullRequestFiles"; |
| 103 | |
| 104 | const isIssueRoute = (route: Route) => route.name.startsWith("Issue"); |
| 105 | |
| 106 | // Every step of starting a task, from the project picker to the draft thread itself. |
| 107 | const isNewTaskRoute = (route: Route) => |
| 108 | route.name.startsWith("NewTask") || |
| 109 | route.name.startsWith("AddProject") || |
| 110 | (route.params !== undefined && "draft" in route.params && Boolean(route.params.draft)); |
| 111 | |
| 112 | /** Beside the list, the first Settings page shows on one native screen that swaps its content. */ |
| 113 | const SETTINGS_DETAIL_SCREEN = "settings-detail"; |
| 114 | |
| 115 | function ColumnScreen(props: { |
| 116 | readonly descriptor: Descriptor; |
| 117 | readonly screenKey?: string; |
| 118 | readonly primary?: boolean; |
| 119 | readonly attached?: boolean; |
| 120 | readonly animatesPush?: boolean; |
| 121 | readonly selectedThreadKey?: string | null; |
| 122 | readonly selectedPullRequestKey?: string | null; |
| 123 | readonly selectedIssueKey?: string | null; |
| 124 | readonly onDismiss: (routeKey: string, native: boolean) => void; |
| 125 | readonly onAppearance: ( |
| 126 | type: "willAppear" | "didAppear" | "willDisappear" | "didDisappear", |
| 127 | key: string, |
| 128 | ) => void; |
| 129 | readonly onCompose?: () => void; |
| 130 | readonly navigation: ViewProps["navigation"]; |
| 131 | readonly footer?: ReactNode; |
| 132 | }) { |
| 133 | const { descriptor } = props; |
| 134 | const { layout } = useAdaptiveWorkspaceLayout(); |
| 135 | // A collapsed split shows the primary column as the page itself, not as a sidebar. |
| 136 | const sidebar = props.primary === true && layout.usesSplitView; |
| 137 | // Options a screen set inside this workspace tab slot, over its route's. |
| 138 | const options = slotScreenOptions( |
| 139 | use(WorkspaceSlotOptionsContext), |
| 140 | descriptor.route.key, |
| 141 | descriptor.options, |
| 142 | ); |
| 143 | const [presented, setPresented] = useState(false); |
| 144 | const primaryColumn = props.primary |
| 145 | ? { |
| 146 | presented, |
| 147 | selectedThreadKey: props.selectedThreadKey ?? null, |
| 148 | selectedPullRequestKey: props.selectedPullRequestKey ?? null, |
| 149 | selectedIssueKey: props.selectedIssueKey ?? null, |
| 150 | } |
| 151 | : null; |
| 152 | const key = descriptor.route.key; |
| 153 | return ( |
| 154 | <Split.Screen |
| 155 | screenKey={props.screenKey ?? key} |
| 156 | activityMode={props.attached === false ? "detached" : "attached"} |
| 157 | animatesPush={props.animatesPush} |
| 158 | onDismiss={(event) => { |
| 159 | props.onDismiss(key, event.nativeEvent.isNativeDismiss); |
| 160 | }} |
| 161 | onWillAppear={() => { |
| 162 | props.onAppearance("willAppear", key); |
| 163 | props.navigation.emit({ type: "transitionStart", target: key, data: { closing: false } }); |
| 164 | }} |
| 165 | onDidAppear={() => { |
| 166 | setPresented(true); |
| 167 | props.onAppearance("didAppear", key); |
| 168 | props.navigation.emit({ type: "transitionEnd", target: key, data: { closing: false } }); |
| 169 | }} |
| 170 | onWillDisappear={() => { |
| 171 | setPresented(false); |
| 172 | props.onAppearance("willDisappear", key); |
| 173 | props.navigation.emit({ type: "transitionStart", target: key, data: { closing: true } }); |
| 174 | }} |
| 175 | onDidDisappear={() => { |
| 176 | props.onAppearance("didDisappear", key); |
| 177 | props.navigation.emit({ type: "transitionEnd", target: key, data: { closing: true } }); |
| 178 | }} |
| 179 | > |
| 180 | <PaneContext value={sidebar ? "sidebar" : "content"}> |
| 181 | <NavigationContext value={descriptor.navigation}> |
| 182 | <NavigationRouteContext value={descriptor.route}> |
| 183 | <V5StackHeader |
| 184 | options={options} |
| 185 | canGoBack={!props.primary} |
| 186 | column={props.primary ? "primary" : "detail"} |
| 187 | onCompose={isNewTaskRoute(descriptor.route) ? undefined : props.onCompose} |
| 188 | /> |
| 189 | <ColumnContent |
| 190 | primary={sidebar} |
| 191 | column={props.primary ? "primary" : undefined} |
| 192 | insetHorizontally={ |
| 193 | (options as AppNativeStackNavigationOptions).nativeContentInsetHorizontally |
| 194 | } |
| 195 | > |
| 196 | <NativePrimaryColumnContext value={primaryColumn}> |
| 197 | {/* The Settings slot swaps pages here; its column keeps its measured insets. */} |
| 198 | <Fragment key={key}>{descriptor.render()}</Fragment> |
| 199 | </NativePrimaryColumnContext> |
| 200 | </ColumnContent> |
| 201 | {props.footer} |
| 202 | </NavigationRouteContext> |
| 203 | </NavigationContext> |
| 204 | </PaneContext> |
| 205 | </Split.Screen> |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | function EmptyDetailHeader(props: { readonly onCompose: (() => void) | undefined }) { |
| 210 | const sidebarItem = useWorkspaceSidebarHeaderItem(); |
| 211 | return ( |
| 212 | <V5StackHeader |
| 213 | options={{ |
| 214 | headerShown: true, |
| 215 | title: "", |
| 216 | unstable_headerLeftItems: () => (sidebarItem ? [sidebarItem] : []), |
| 217 | }} |
| 218 | canGoBack={false} |
| 219 | column="detail" |
| 220 | onCompose={props.onCompose} |
| 221 | /> |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | function WorkspaceColumns( |
| 226 | props: ViewProps & { readonly primary: Route; readonly detail: Route[] }, |
| 227 | ) { |
| 228 | const gt = useGT(); |
| 229 | const { |
| 230 | layout, |
| 231 | setPrimarySidebarVisible, |
| 232 | toggleAuxiliaryPane, |
| 233 | startNewTask, |
| 234 | openWorkspaceTab, |
| 235 | nativeInspectorWidth, |
| 236 | setNativeInspectorWidth, |
| 237 | } = useAdaptiveWorkspaceLayout(); |
| 238 | const { panes, sidebarByOrientation } = useWorkspacePaneState(); |
| 239 | const deviceType = useDeviceType(); |
| 240 | const inspectorWidth = nativeInspectorWidth; |
| 241 | const resizeStartWidth = useRef(inspectorWidth); |
| 242 | const settingsDetailIn = (detail: readonly Route[]) => |
| 243 | layout.usesSplitView && detail[0] && isSettingsRoute(detail[0].name) ? detail[0] : undefined; |
| 244 | // Split screens must remain mounted while UIKit animates a detached route away. |
| 245 | const [screens, setScreens] = useState({ |
| 246 | routes: props.detail, |
| 247 | descriptors: props.descriptors, |
| 248 | observedRoutes: props.detail, |
| 249 | observedDescriptors: props.descriptors, |
| 250 | completed: new Set<string>(), |
| 251 | }); |
| 252 | if ( |
| 253 | screens.observedRoutes !== props.detail || |
| 254 | screens.observedDescriptors !== props.descriptors |
| 255 | ) { |
| 256 | const active = new Set(props.detail.map((route) => route.key)); |
| 257 | const settingsSlot = settingsDetailIn(props.detail); |
| 258 | const routes = reconcileStackScreens(screens.routes, props.detail, screens.completed).filter( |
| 259 | // The slot's native screen stays; the pages it showed, and any above them, just go. |
| 260 | (route) => !settingsSlot || active.has(route.key) || !isSettingsRoute(route.name), |
| 261 | ); |
| 262 | const retained = new Set(routes.map((route) => route.key)); |
| 263 | setScreens({ |
| 264 | routes, |
| 265 | descriptors: Object.fromEntries( |
| 266 | Object.entries({ ...screens.descriptors, ...props.descriptors }).filter(([key]) => |
| 267 | retained.has(key), |
| 268 | ), |
| 269 | ), |
| 270 | observedRoutes: props.detail, |
| 271 | observedDescriptors: props.descriptors, |
| 272 | completed: new Set([...screens.completed].filter((key) => active.has(key))), |
| 273 | }); |
| 274 | } |
| 275 | |
| 276 | const workspaceTabs = useWorkspaceTabs(); |
| 277 | const selectedTab = workspaceTabs?.state.selectedTab; |
| 278 | const settingsOpen = workspaceTabs?.settingsOpen ?? false; |
| 279 | const openNewTask = startNewTask; |
| 280 | // The cover detail rail needs room for thread tools above the fixed tabs. |
| 281 | // The unfolded Duo keeps compose in its detail bar. |
| 282 | const compose = useWorkspaceCompose(); |
| 283 | const detailCompose = deviceType === "duo-unfolded" ? compose?.onPress : undefined; |
| 284 | // Pages a hidden tab opens as it's shown belong to the tab, so they appear in place. |
| 285 | const headerOnly = use(NativeWorkspaceTabActivityContext)?.headerOnly === true; |
| 286 | const [shownPages, setShownPages] = useState({ headerOnly, keys: new Set<string>() }); |
| 287 | if (shownPages.headerOnly !== headerOnly) |
| 288 | setShownPages({ |
| 289 | headerOnly, |
| 290 | keys: headerOnly ? new Set() : new Set(props.detail.map((route) => route.key)), |
| 291 | }); |
| 292 | const inspector = use(NativeWorkspaceInspectorContext); |
| 293 | const hideInspector = () => { |
| 294 | if (inspector.visible) toggleAuxiliaryPane(); |
| 295 | }; |
| 296 | const hostRef = useRef<SplitHostCommands>(null); |
| 297 | const activeDetailKey = props.detail.at(-1)?.key; |
| 298 | const compactColumn = activeDetailKey ? "secondary" : "primary"; |
| 299 | const collapsed = useRef(!layout.usesSplitView); |
| 300 | const collapsedColumn = useRef<CollapsedSplitColumn>({ visible: compactColumn, leaving: null }); |
| 301 | const threadParams = props.detail.findLast((route) => route.name === "Thread")?.params; |
| 302 | const pullRequestParams = props.detail.findLast(isPullRequestRoute)?.params; |
| 303 | const selectedPullRequestKey = |
| 304 | pullRequestParams && |
| 305 | "environmentId" in pullRequestParams && |
| 306 | "host" in pullRequestParams && |
| 307 | "repository" in pullRequestParams && |
| 308 | "number" in pullRequestParams && |
| 309 | typeof pullRequestParams.environmentId === "string" && |
| 310 | typeof pullRequestParams.host === "string" && |
| 311 | typeof pullRequestParams.repository === "string" && |
| 312 | typeof pullRequestParams.number === "number" |
| 313 | ? `${pullRequestParams.environmentId}:${pullRequestParams.host}:${pullRequestParams.repository}:${pullRequestParams.number}` |
| 314 | : null; |
| 315 | const selectedIssueKey = selectedIssueKeyOf(props.detail.findLast(isIssueRoute)?.params); |
| 316 | const selectedThreadKey = |
| 317 | threadParams && |
| 318 | "environmentId" in threadParams && |
| 319 | "threadId" in threadParams && |
| 320 | typeof threadParams.environmentId === "string" && |
| 321 | typeof threadParams.threadId === "string" |
| 322 | ? scopedThreadKey( |
| 323 | EnvironmentId.make(threadParams.environmentId), |
| 324 | ThreadId.make(threadParams.threadId), |
| 325 | ) |
| 326 | : null; |
| 327 | const handleNativeDismiss = (key: string, native: boolean) => { |
| 328 | setScreens((current) => ({ |
| 329 | ...current, |
| 330 | routes: current.routes.filter((route) => route.key !== key), |
| 331 | completed: new Set([...current.completed, key]), |
| 332 | })); |
| 333 | if (native) { |
| 334 | const state = props.navigation.getState(); |
| 335 | const action = nativeWorkspacePopAction(state, key); |
| 336 | if (action) props.navigation.dispatch(action); |
| 337 | } |
| 338 | }; |
| 339 | const followCollapsedSplit = ( |
| 340 | type: "willAppear" | "didAppear" | "willDisappear" | "didDisappear", |
| 341 | key: string, |
| 342 | ) => { |
| 343 | if (!collapsed.current || layout.usesSplitView) return; |
| 344 | const transition = collapsedSplitTransition( |
| 345 | collapsedColumn.current, |
| 346 | { type, key }, |
| 347 | props.navigation.getState(), |
| 348 | props.primary.key, |
| 349 | ); |
| 350 | collapsedColumn.current = transition.column; |
| 351 | setNativelyPoppingWorkspaceRoute(transition.column.leaving); |
| 352 | if (transition.pop) props.navigation.dispatch(transition.pop); |
| 353 | }; |
| 354 | |
| 355 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- tells the native split host which column to show while collapsed |
| 356 | useEffect(() => { |
| 357 | if (layout.usesSplitView || collapsedColumn.current.visible === compactColumn) return; |
| 358 | collapsedColumn.current = { ...collapsedColumn.current, visible: compactColumn }; |
| 359 | hostRef.current?.show(compactColumn); |
| 360 | }, [compactColumn, layout.usesSplitView]); |
| 361 | |
| 362 | const primary = props.descriptors[props.primary.key]; |
| 363 | if (!primary) return null; |
| 364 | const primaryColumnWidth = (layout.listPaneWidth ?? 320) + (layout.listPaneGap ?? 0) / 2; |
| 365 | return ( |
| 366 | <Split.Host |
| 367 | ref={hostRef} |
| 368 | testID="adaptive-workspace-layout" |
| 369 | preferredSplitBehavior={ |
| 370 | !sidebarByOrientation && layout.variant === "medium" ? "overlay" : "tile" |
| 371 | } |
| 372 | portraitPreferredSplitBehavior={sidebarByOrientation ? "overlay" : undefined} |
| 373 | portraitPreferredDisplayMode={ |
| 374 | sidebarByOrientation ? sidebarDisplayMode(sidebarByOrientation.portrait, true) : undefined |
| 375 | } |
| 376 | primaryBackgroundStyle={deviceType === "pad" ? "none" : "default"} |
| 377 | topColumnForCollapsing={compactColumn} |
| 378 | onCollapse={() => { |
| 379 | collapsed.current = true; |
| 380 | collapsedColumn.current = { visible: compactColumn, leaving: null }; |
| 381 | setNativelyPoppingWorkspaceRoute(null); |
| 382 | }} |
| 383 | onExpand={() => { |
| 384 | collapsed.current = false; |
| 385 | setNativelyPoppingWorkspaceRoute(null); |
| 386 | // Native callbacks can outlive the render that created them, so decide from the live |
| 387 | // router state. An expanded split has columns, so its tab gets a page beside the list. |
| 388 | const live = props.navigation.getState(); |
| 389 | const { workspace } = workspaceRoutesOf(live.routes.slice(0, live.index + 1)); |
| 390 | if (workspace.some((route) => route.name !== "Home" && route.name !== "Settings")) return; |
| 391 | const tab = workspace.some((route) => route.name === "Settings") |
| 392 | ? "settings" |
| 393 | : NAVIGATION_TAB_ORDER.find((candidate) => workspaceTabs?.isTabActive(candidate)); |
| 394 | openWorkspaceTab(tab ?? "threads", { columns: true }); |
| 395 | }} |
| 396 | preferredDisplayMode={ |
| 397 | sidebarByOrientation |
| 398 | ? sidebarDisplayMode(sidebarByOrientation.landscape, false) |
| 399 | : sidebarDisplayMode( |
| 400 | // UIKit applies the mode as a collapsed split expands, before the new width reaches |
| 401 | // JS, so a collapsed split already carries the sidebar the expanded one will show. |
| 402 | (layout.usesSplitView |
| 403 | ? panes.primarySidebarVisible |
| 404 | : panes.primarySidebarWhenExpanded) || |
| 405 | (!layout.usesSplitView && !activeDetailKey), |
| 406 | layout.variant === "medium", |
| 407 | ) |
| 408 | } |
| 409 | onDisplayModeWillChange={({ nativeEvent }) => { |
| 410 | if (layout.variant !== "medium") return; |
| 411 | if (nativeEvent.nextDisplayMode === "oneOverSecondary") setPrimarySidebarVisible(true); |
| 412 | else if (nativeEvent.nextDisplayMode === "secondaryOnly") setPrimarySidebarVisible(false); |
| 413 | }} |
| 414 | displayModeButtonVisibility="never" |
| 415 | presentsWithGesture |
| 416 | showInspector={inspector.visible} |
| 417 | onInspectorHide={hideInspector} |
| 418 | columnMetrics={{ |
| 419 | minimumPrimaryColumnWidth: layout.variant === "medium" ? primaryColumnWidth : 280, |
| 420 | maximumPrimaryColumnWidth: |
| 421 | layout.variant === "medium" ? primaryColumnWidth : Math.max(380, primaryColumnWidth), |
| 422 | preferredPrimaryColumnWidthOrFraction: primaryColumnWidth, |
| 423 | minimumSecondaryColumnWidth: NATIVE_INSPECTOR_MIN_MAIN_WIDTH, |
| 424 | // UIKit can retain its automatic width despite a new preference; constrain both edges. |
| 425 | minimumInspectorColumnWidth: inspectorWidth, |
| 426 | maximumInspectorColumnWidth: inspectorWidth, |
| 427 | preferredInspectorColumnWidthOrFraction: inspectorWidth, |
| 428 | }} |
| 429 | > |
| 430 | <Split.Column> |
| 431 | <Split.Stack> |
| 432 | <ColumnScreen |
| 433 | descriptor={primary} |
| 434 | primary |
| 435 | footer={ |
| 436 | deviceType === "pad" ? ( |
| 437 | <View |
| 438 | pointerEvents="box-none" |
| 439 | style={{ position: "absolute", left: 0, right: 0, bottom: 0, height: 128 }} |
| 440 | > |
| 441 | <WorkspaceSidebarTabsFooter /> |
| 442 | </View> |
| 443 | ) : undefined |
| 444 | } |
| 445 | selectedThreadKey={selectedThreadKey} |
| 446 | selectedPullRequestKey={selectedPullRequestKey} |
| 447 | selectedIssueKey={selectedIssueKey} |
| 448 | onDismiss={handleNativeDismiss} |
| 449 | onAppearance={followCollapsedSplit} |
| 450 | navigation={props.navigation} |
| 451 | /> |
| 452 | </Split.Stack> |
| 453 | </Split.Column> |
| 454 | <Split.Column> |
| 455 | <Split.Stack> |
| 456 | <Split.Screen |
| 457 | screenKey="workspace-empty" |
| 458 | activityMode={layout.usesSplitView && !activeDetailKey ? "attached" : "detached"} |
| 459 | > |
| 460 | <PaneContext value="content"> |
| 461 | <EmptyDetailHeader onCompose={detailCompose} /> |
| 462 | </PaneContext> |
| 463 | <ColumnContent column="empty-detail"> |
| 464 | <WorkspaceEmptyDetail |
| 465 | tab={settingsOpen ? "settings" : selectedTab} |
| 466 | onStartNewTask={ |
| 467 | // Duo provides compose in the detail bar; iPad uses the empty-state action. |
| 468 | detailCompose ? undefined : openNewTask |
| 469 | } |
| 470 | /> |
| 471 | </ColumnContent> |
| 472 | </Split.Screen> |
| 473 | {screens.routes.map((route) => { |
| 474 | const descriptor = props.descriptors[route.key] ?? screens.descriptors[route.key]; |
| 475 | const inSettingsSlot = settingsDetailIn(props.detail)?.key === route.key; |
| 476 | const screenKey = inSettingsSlot ? SETTINGS_DETAIL_SCREEN : route.key; |
| 477 | return descriptor ? ( |
| 478 | <ColumnScreen |
| 479 | key={screenKey} |
| 480 | screenKey={screenKey} |
| 481 | descriptor={descriptor} |
| 482 | attached={props.detail.some((current) => current.key === route.key)} |
| 483 | animatesPush={!shownPages.keys.has(route.key)} |
| 484 | onDismiss={handleNativeDismiss} |
| 485 | onAppearance={followCollapsedSplit} |
| 486 | onCompose={isPullRequestRoute(route) ? undefined : detailCompose} |
| 487 | navigation={props.navigation} |
| 488 | /> |
| 489 | ) : null; |
| 490 | })} |
| 491 | </Split.Stack> |
| 492 | </Split.Column> |
| 493 | <InspectorColumnScreens |
| 494 | content={inspector.content} |
| 495 | onClose={hideInspector} |
| 496 | overlay={ |
| 497 | inspector.visible ? ( |
| 498 | <GestureHandlerRootView |
| 499 | style={[StyleSheet.absoluteFill, { zIndex: 100 }]} |
| 500 | pointerEvents="box-none" |
| 501 | collapsable={false} |
| 502 | > |
| 503 | <SafeAreaView style={{ flex: 1 }} edges={["left", "right"]} pointerEvents="box-none"> |
| 504 | <View className="flex-1" pointerEvents="box-none"> |
| 505 | <View |
| 506 | className="absolute inset-y-0 left-0 w-[22px] flex-row" |
| 507 | pointerEvents="box-none" |
| 508 | > |
| 509 | <WorkspacePaneDivider |
| 510 | accessibilityLabel={gt("Resize inspector")} |
| 511 | currentWidth={inspectorWidth} |
| 512 | resizeDirection={-1} |
| 513 | onResizeStart={() => { |
| 514 | resizeStartWidth.current = inspectorWidth; |
| 515 | }} |
| 516 | onResizeBy={(delta) => |
| 517 | setNativeInspectorWidth(resizeStartWidth.current + delta) |
| 518 | } |
| 519 | /> |
| 520 | </View> |
| 521 | </View> |
| 522 | </SafeAreaView> |
| 523 | </GestureHandlerRootView> |
| 524 | ) : null |
| 525 | } |
| 526 | /> |
| 527 | </Split.Host> |
| 528 | ); |
| 529 | } |
| 530 | |
| 531 | function WorkspaceStackView(props: ViewProps & { readonly nativeColumns: boolean }) { |
| 532 | const { layout } = useAdaptiveWorkspaceLayout(); |
| 533 | const deviceType = useDeviceType(); |
| 534 | const workspace = useWorkspaceTabs()!; |
| 535 | // Native dismiss commits the route pop after the transition. Restore the dock |
| 536 | // when the primary screen starts appearing, including interactive back gestures. |
| 537 | const [primaryVisible, setPrimaryVisible] = useState(false); |
| 538 | const metrics = useNativeLayoutMetrics(); |
| 539 | const projection = projectWorkspaceStack(props.state, (route) => { |
| 540 | return isPresentedRoute(props.descriptors[route.key]?.options); |
| 541 | }); |
| 542 | // The root's willDisappear reaches JS frames into the push, so hide the dock in the commit that |
| 543 | // pushes; the native events still own every later change, including back gestures. |
| 544 | const topDetailKey = projection.detail.at(-1)?.key; |
| 545 | const [pushedDetailKey, setPushedDetailKey] = useState(topDetailKey); |
| 546 | if (pushedDetailKey !== topDetailKey) { |
| 547 | setPushedDetailKey(topDetailKey); |
| 548 | if (topDetailKey !== undefined && primaryVisible) setPrimaryVisible(false); |
| 549 | } |
| 550 | // Detail-only histories must stay single-stack: placeholder descriptors cannot own sidebar actions or setOptions. |
| 551 | const primaryRoute = projection.primary; |
| 552 | if (!primaryRoute) return <V5StackView {...props} />; |
| 553 | const primaryDescriptor = props.descriptors[primaryRoute.key]!; |
| 554 | const descriptors = props.descriptors; |
| 555 | const baseRoute = { key: `${props.state.key}:workspace`, name: "Workspace" }; |
| 556 | const visibleRoute = projection.detail.at(-1) ?? primaryRoute; |
| 557 | const workspacePage = !workspaceUsesColumns(workspace.state.selectedTab, workspace.settingsOpen); |
| 558 | const baseDescriptor: Descriptor = { |
| 559 | ...primaryDescriptor, |
| 560 | navigation: props.descriptors[visibleRoute.key]!.navigation, |
| 561 | // Identify the real visible route under the stable envelope key, or useIsFocused closes media viewers. |
| 562 | route: visibleRoute, |
| 563 | options: { headerShown: false }, |
| 564 | render: () => ( |
| 565 | <WorkspaceNativeTabs |
| 566 | tabsInSidebarColumn={props.nativeColumns && !workspacePage && deviceType === "pad"} |
| 567 | hidden={ |
| 568 | projection.detail.length > 0 && |
| 569 | !primaryVisible && |
| 570 | !layout.usesSplitView && |
| 571 | (metrics?.verticalBarEdge ?? "none") === "none" |
| 572 | } |
| 573 | > |
| 574 | {(slotWorkspace, headerOnly) => { |
| 575 | // A tab not shown yet renders the page opening it shows, under the key it opens with, |
| 576 | // so that page is already on screen in the tab when it's selected. |
| 577 | const tab = slotWorkspace.settingsOpen ? "settings" : slotWorkspace.state.selectedTab; |
| 578 | const page = headerOnly |
| 579 | ? (workspaceTabPage(props.state, tab, currentWorkspaceSelections(), { |
| 580 | columns: workspaceTabColumns(Platform.OS, tab, layout.usesSplitView), |
| 581 | check: workspaceSelectionCheck, |
| 582 | seedPullRequest: withKnownPullRequestSummary, |
| 583 | }) as Route | undefined) |
| 584 | : undefined; |
| 585 | const detail = headerOnly ? (page ? [page] : []) : projection.detail; |
| 586 | const slotDescriptors = |
| 587 | page && !descriptors[page.key] |
| 588 | ? { ...descriptors, [page.key]: props.describe(page, true) } |
| 589 | : descriptors; |
| 590 | return props.nativeColumns && |
| 591 | workspaceUsesColumns(slotWorkspace.state.selectedTab, slotWorkspace.settingsOpen) ? ( |
| 592 | <WorkspaceColumns |
| 593 | {...props} |
| 594 | descriptors={slotDescriptors} |
| 595 | primary={primaryRoute} |
| 596 | detail={detail} |
| 597 | /> |
| 598 | ) : ( |
| 599 | <V5CardStackView |
| 600 | {...props} |
| 601 | onRootVisibilityChange={setPrimaryVisible} |
| 602 | onNativePopChange={setNativelyPoppingWorkspaceRoute} |
| 603 | descriptors={slotDescriptors} |
| 604 | state={{ |
| 605 | ...props.state, |
| 606 | routes: [primaryRoute, ...detail], |
| 607 | index: detail.length, |
| 608 | preloadedRoutes: [], |
| 609 | }} |
| 610 | /> |
| 611 | ); |
| 612 | }} |
| 613 | </WorkspaceNativeTabs> |
| 614 | ), |
| 615 | }; |
| 616 | const overlays = partitionStackPresentations(projection.overlays, (route) => { |
| 617 | return isPresentedRoute(props.descriptors[route.key]?.options); |
| 618 | }); |
| 619 | const routes = [baseRoute, ...overlays.map((group) => group[0]!)]; |
| 620 | return ( |
| 621 | <NativeStackView |
| 622 | presentationEnvelope |
| 623 | {...props} |
| 624 | state={{ ...props.state, routes, index: routes.length - 1, preloadedRoutes: [] }} |
| 625 | descriptors={{ |
| 626 | ...Object.fromEntries( |
| 627 | overlays.map((group) => { |
| 628 | const route = group[0]!; |
| 629 | const descriptor = props.descriptors[route.key]!; |
| 630 | const visibleRoute = group.at(-1)!; |
| 631 | return [ |
| 632 | route.key, |
| 633 | { |
| 634 | ...descriptor, |
| 635 | navigation: props.descriptors[visibleRoute.key]!.navigation, |
| 636 | route: visibleRoute, |
| 637 | options: modalEnvelopeOptions(descriptor.options), |
| 638 | // Before iOS 26 the JS columns wrap this whole navigator in the content pane. |
| 639 | render: () => ( |
| 640 | <PaneContext value={null}> |
| 641 | <V5CardStackView |
| 642 | {...props} |
| 643 | state={{ |
| 644 | ...props.state, |
| 645 | routes: group, |
| 646 | index: group.length - 1, |
| 647 | preloadedRoutes: [], |
| 648 | }} |
| 649 | /> |
| 650 | </PaneContext> |
| 651 | ), |
| 652 | }, |
| 653 | ]; |
| 654 | }), |
| 655 | ), |
| 656 | [baseRoute.key]: baseDescriptor, |
| 657 | }} |
| 658 | /> |
| 659 | ); |
| 660 | } |
| 661 | |
| 662 | function WorkspaceStackNavigator({ |
| 663 | id, |
| 664 | initialRouteName, |
| 665 | UNSTABLE_routeNamesChangeBehavior, |
| 666 | children, |
| 667 | layout, |
| 668 | screenListeners, |
| 669 | screenOptions, |
| 670 | screenLayout, |
| 671 | UNSTABLE_router, |
| 672 | ...rest |
| 673 | }: NativeStackNavigatorProps) { |
| 674 | const usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported(); |
| 675 | const nativeWorkspaceColumnsReady = useNativeWorkspaceColumnsReady(); |
| 676 | const { state, describe, descriptors, navigation, NavigationContent } = useNavigationBuilder< |
| 677 | StackNavigationState<ParamListBase>, |
| 678 | StackRouterOptions, |
| 679 | StackActionHelpers<ParamListBase>, |
| 680 | NativeStackNavigationOptions, |
| 681 | NativeStackNavigationEventMap |
| 682 | >(StackRouter, { |
| 683 | id, |
| 684 | initialRouteName, |
| 685 | UNSTABLE_routeNamesChangeBehavior, |
| 686 | children, |
| 687 | layout, |
| 688 | screenListeners, |
| 689 | screenOptions, |
| 690 | screenLayout, |
| 691 | UNSTABLE_router, |
| 692 | }); |
| 693 | if (!nativeWorkspaceColumnsReady) return null; |
| 694 | const settingsOpen = settingsOpenIn( |
| 695 | state.routes.slice(0, state.index + 1).filter((route) => { |
| 696 | return !isPresentedRoute(descriptors[route.key]?.options); |
| 697 | }), |
| 698 | ); |
| 699 | return ( |
| 700 | <NativeWorkspaceModeContext value={usesNativeWorkspaceColumns}> |
| 701 | <NavigationContent> |
| 702 | <WorkspaceTabsProvider settingsOpen={settingsOpen}> |
| 703 | <WorkspaceStackView |
| 704 | nativeColumns={usesNativeWorkspaceColumns} |
| 705 | {...rest} |
| 706 | state={state} |
| 707 | describe={describe} |
| 708 | descriptors={descriptors} |
| 709 | navigation={navigation} |
| 710 | /> |
| 711 | </WorkspaceTabsProvider> |
| 712 | </NavigationContent> |
| 713 | </NativeWorkspaceModeContext> |
| 714 | ); |
| 715 | } |
| 716 | |
| 717 | type WorkspaceTypeBag< |
| 718 | ParamList extends ParamListBase, |
| 719 | NavigatorID extends string | undefined, |
| 720 | > = Omit<NativeStackTypeBag<ParamList, NavigatorID>, "Navigator"> & { |
| 721 | Navigator: typeof WorkspaceStackNavigator; |
| 722 | }; |
| 723 | |
| 724 | export function createWorkspaceStackNavigator< |
| 725 | const ParamList extends ParamListBase, |
| 726 | const NavigatorID extends string | undefined = string | undefined, |
| 727 | const TypeBag extends NavigatorTypeBagBase = WorkspaceTypeBag<ParamList, NavigatorID>, |
| 728 | const Config extends StaticConfig<TypeBag> = StaticConfig<TypeBag>, |
| 729 | >(config?: Config): TypedNavigator<TypeBag, Config> { |
| 730 | return createNavigatorFactory(WorkspaceStackNavigator)(config); |
| 731 | } |
| 732 | |
| 733 | function sidebarDisplayMode(visible: boolean, overlay: boolean) { |
| 734 | if (!visible) return "secondaryOnly"; |
| 735 | return overlay ? "oneOverSecondary" : "oneBesideSecondary"; |
| 736 | } |