import { useCallback, useMemo, useState } from 'react' import { computeActiveTerminalKeyboardLift } from '../terminal/terminal-keyboard-avoidance-lift' import { terminalPaneDirection, terminalPaneKeyboardLayout } from '../terminal/session/mobile-terminal-pane-layout' import { Animated, View, Text, Pressable, ActivityIndicator } from 'react-native' import { saveTerminalTextScale } from '../storage/preferences' import { MobileBrowserPane } from '../browser/MobileBrowserPane' import { TerminalPaneView } from '../terminal/session/TerminalPaneView' import { MobileNativeChatOverlay } from './native-chat/MobileNativeChatOverlay' import { mobileSessionNativeChatHostOutage } from './native-chat/mobile-native-chat-host-outage' import { MobileAgentMessageProvider } from './native-chat/transcript/MobileAgentMessageProvider' import { MobileNativeChatForkProvider } from './MobileNativeChatForkProvider' import { useMobileAgentMessageNavigation } from './use-mobile-agent-message-navigation' import { MobileNativeChatDictationPreviewContext } from './native-chat/composer/mobile-native-chat-dictation-preview-context' import { MobileDictationMeterContext } from '../dictation/mobile-dictation-meter-context' import { resolveMobileNativeChatSessionId } from './native-chat/mobile-native-chat-eligibility' import { colors } from '../theme/mobile-theme' import { useMobileColorScheme } from '../theme/use-mobile-color-scheme' import { useMobileTranslation } from '../i18n/use-mobile-translation' import { styles } from './mobile-session-styles' import type { MobileSessionController } from './use-mobile-session-controller' import { useMobileSessionContentKeyboardLift } from '../terminal/session/use-mobile-session-keyboard-lift' import { useOpenUrlInOrca } from '../browser/use-open-url-in-orca' import { useWorkspaceExecutionHost } from '../navigation/workspace-execution-host' import { useOpenHostAccounts } from '../accounts/use-open-mobile-accounts' import { useHostProfile } from '../transport/hosts/use-host-profile' import { useHostDisplay } from '../transport/hosts/use-host-display' import { FileReader } from '../files/session-documents/MobileSessionFileReader' import { MobileSessionContentState } from './MobileSessionContentState' import { MarkdownReader } from '../files/session-documents/MobileSessionMarkdownReader' import { markdownTabDraftKey } from '../drafts/document-edit-draft-rows' import { loadMarkdownPreviewImage } from '../files/mobile-markdown-image-source' export function MobileSessionActiveContent({ controller }: { controller: MobileSessionController }) { useMobileColorScheme() const t = useMobileTranslation() const executionHost = useWorkspaceExecutionHost() const [paneFrame, setPaneFrame] = useState({ width: 0, height: 0 }) const paneDirection = terminalPaneDirection(paneFrame, controller.terminalSplitLayout) const { hostId, worktreeId, client, terminals, terminalTextScale, setTerminalTextScale, terminalTextScaleRevisionRef, activeHandle, terminalSplitActive, visibleTerminalHandles, notifyTerminalPaneFrame, diffComments, diffCommentBusy, createError, setCreateError, setShowCreateTabDrawer, dictationMode, dictationEnabled, toastMessage, handleTerminalInteraction, handleTerminalTap, browserScreencastSupported, showToast, nativeChatSendError, nativeChatOverlayInputLockReason, nativeChatController, dictation, chatDictationPreview, handleDictationToggle, handleDictationPressIn, handleDictationPressOut, readMarkdownTab, addDiffCommentForFile, deleteDiffCommentForFile, copyDiffCommentsToClipboard, sendDiffCommentsToAgent, sendingDiffCommentIds, updateMarkdownLocalContent, copyMarkdownText, copyMarkdownPath, discardMarkdownLocalContent, saveMarkdownTab, setTerminalWebViewRef, handleTerminalWebReady, notifyTerminalFrame, notifyTerminalCellBoxChange, handleFileTap, handleNativeChatFileTap, handleTerminalOpenUrl, handleTerminalInput, handleTerminalQueryReply, handleSelectionMode, handleSelectionCopy, handleSelectionEvicted, handleClipboardWrite, handleModesChanged, handleKeyboardAvoidanceMetrics, handleHaptic, nativeChatImages, nativeChatStructuredComposer, activeMarkdownTab, activeFileTab, activeBrowserTab, activePendingTerminalTab, isPendingTerminalRecoveryParked, retryPendingTerminalRecovery, showLoadingState, showEmptyState, createTabBusy } = controller const { keyboardLift, activeTerminalKeyboardLift, toastAnimatedStyle } = useMobileSessionContentKeyboardLift(controller) const openUrlInOrca = useOpenUrlInOrca(controller) const hostProfile = useHostProfile(hostId) const hostTitle = useHostDisplay(hostProfile).title const nativeChatHostOutage = mobileSessionNativeChatHostOutage(controller, hostTitle, t) const nativeChatImageSource = useMemo( () => (client ? { client, worktreeId } : undefined), [client, worktreeId] ) const openNativeChatAccounts = useOpenHostAccounts(controller.router, hostId) const navigateToSender = useCallback( (senderWorktreeId: string, paneKey: string) => { const execution = executionHost ? `&executionHost=${encodeURIComponent(executionHost)}` : '' controller.router.push( `/h/${encodeURIComponent(hostId)}/session/${encodeURIComponent(senderWorktreeId)}?paneKey=${encodeURIComponent(paneKey)}${execution}` ) }, [controller.router, hostId, executionHost] ) const openSender = useMobileAgentMessageNavigation( client, navigateToSender, JSON.stringify([hostId, executionHost, worktreeId, controller.activeSessionTab?.id]), controller.showToast, controller ) const { presentedConnState, showOfflineState, forceReconnectHost } = controller const toast = toastMessage ? ( {toastMessage} ) : null const content = showLoadingState || showEmptyState || showOfflineState ? ( 0 ? { hostId, worktreeId } : null} loading={showLoadingState} creating={createTabBusy} createError={createError} onReconnect={forceReconnectHost ? () => void forceReconnectHost(hostId) : null} onRetryTabs={() => void retryPendingTerminalRecovery()} onCreateTab={() => { setCreateError('') setShowCreateTabDrawer(true) }} /> ) : activeMarkdownTab ? ( void readMarkdownTab(activeMarkdownTab)} onChange={(content) => updateMarkdownLocalContent(activeMarkdownTab.id, content)} onSave={() => void saveMarkdownTab(activeMarkdownTab)} onCopy={(text) => void copyMarkdownText(text)} onCopyPath={() => void copyMarkdownPath(activeMarkdownTab)} onDiscard={() => discardMarkdownLocalContent(activeMarkdownTab)} loadImageSource={(path) => loadMarkdownPreviewImage(client, worktreeId, activeMarkdownTab.relativePath, path) } keyboardLift={keyboardLift} /> {toast} ) : activeFileTab ? ( {toast} ) : activeBrowserTab ? ( {/* Why: pane owns imperative frame refs; don't render a stale frame while the old stream effect cleans up. */} {toast} ) : activePendingTerminalTab ? ( {!isPendingTerminalRecoveryParked && ( )} {isPendingTerminalRecoveryParked ? t( 'session.activeContent.terminalTakingLonger', 'Terminal is taking longer than expected' ) : activePendingTerminalTab.title || t('session.activeContent.loadingTerminal', 'Loading terminal')} {isPendingTerminalRecoveryParked && ( [ styles.createButton, pressed && styles.newTerminalButtonPressed ]} onPress={() => void retryPendingTerminalRecovery()} > {t('common.retry', 'Retry')} )} ) : ( {terminals.map((terminal) => ( { // Why: pinch-to-zoom reports a new preset; persist it so the size sticks across panes and launches. terminalTextScaleRevisionRef.current += 1 setTerminalTextScale(scale) void saveTerminalTextScale(scale) }} onRef={setTerminalWebViewRef} onWebReady={handleTerminalWebReady} onCellBoxChange={notifyTerminalCellBoxChange} onSelectionMode={handleSelectionMode} onSelectionCopy={handleSelectionCopy} onSelectionEvicted={handleSelectionEvicted} onClipboardWrite={handleClipboardWrite} onModesChanged={handleModesChanged} onKeyboardAvoidanceMetrics={handleKeyboardAvoidanceMetrics} onHaptic={handleHaptic} onTerminalInput={handleTerminalInput} onTerminalQueryReply={handleTerminalQueryReply} onTerminalInteraction={handleTerminalInteraction} onTerminalTap={handleTerminalTap} onFileTap={handleFileTap} onOpenUrl={handleTerminalOpenUrl} /> ))} void controller.handleCreateTerminal(undefined, { startupCommand: command, startupCommandDelivery: 'shell-ready', terminalView: true }) } // Accounts open on the paired host, so a chat another server runs offers none. onOpenAccounts={executionHost === undefined ? openNativeChatAccounts : undefined} /> {toast} ) return ( { const { width, height } = e.nativeEvent.layout notifyTerminalFrame({ width, height }) setPaneFrame((previous) => previous.width === width && previous.height === height ? previous : { width, height } ) }} > {content} ) }