| 1 | import { Box, Text } from "ink"; |
| 2 | import React from "react"; |
| 3 | |
| 4 | import { UpdateServiceState } from "src/services/types.js"; |
| 5 | |
| 6 | import { ConfigSelector } from "../ConfigSelector.js"; |
| 7 | import type { NavigationScreen } from "../context/NavigationContext.js"; |
| 8 | import { DiffViewer } from "../DiffViewer.js"; |
| 9 | import { FreeTrialTransitionUI } from "../FreeTrialTransitionUI.js"; |
| 10 | import { MCPSelector } from "../MCPSelector.js"; |
| 11 | import { ModelSelector } from "../ModelSelector.js"; |
| 12 | import type { ConfigOption, ModelOption } from "../types/selectorTypes.js"; |
| 13 | import { UpdateSelector } from "../UpdateSelector.js"; |
| 14 | import { UserInput } from "../UserInput.js"; |
| 15 | |
| 16 | import { SessionSelectorWithLoading } from "./SessionSelectorWithLoading.js"; |
| 17 | import { ToolPermissionSelector } from "./ToolPermissionSelector.js"; |
| 18 | |
| 19 | interface ScreenContentProps { |
| 20 | isScreenActive: (screen: NavigationScreen) => boolean; |
| 21 | navState: any; |
| 22 | services: any; |
| 23 | handleLoginTokenSubmit: (token: string) => void; |
| 24 | handleConfigSelect: (config: ConfigOption) => Promise<void>; |
| 25 | handleModelSelect: (model: ModelOption) => Promise<void>; |
| 26 | handleSessionSelect: (sessionId: string) => Promise<void>; |
| 27 | handleReload: () => Promise<void>; |
| 28 | closeCurrentScreen: () => void; |
| 29 | activePermissionRequest: any; |
| 30 | handleToolPermissionResponse: ( |
| 31 | requestId: string, |
| 32 | approved: boolean, |
| 33 | createPolicy?: boolean, |
| 34 | stopStream?: boolean, |
| 35 | ) => void; |
| 36 | handleUserMessage: (message: string, imageMap?: Map<string, Buffer>) => void; |
| 37 | isWaitingForResponse: boolean; |
| 38 | isCompacting: boolean; |
| 39 | inputMode: boolean; |
| 40 | handleInterrupt: () => void; |
| 41 | handleFileAttached: (filePath: string, content: string) => void; |
| 42 | isInputDisabled: boolean; |
| 43 | wasInterrupted?: boolean; |
| 44 | isRemoteMode: boolean; |
| 45 | onImageInClipboardChange?: (hasImage: boolean) => void; |
| 46 | diffContent?: string; |
| 47 | } |
| 48 | |
| 49 | function hideScreenContent(state?: UpdateServiceState) { |
| 50 | return ( |
| 51 | (state?.status === "checking" && state?.autoUpdate) || |
| 52 | (state?.isAutoUpdate && |
| 53 | (state?.status === "updating" || state?.status === "updated")) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | export const ScreenContent: React.FC<ScreenContentProps> = ({ |
| 58 | isScreenActive, |
| 59 | navState, |
| 60 | services, |
| 61 | handleLoginTokenSubmit, |
| 62 | handleConfigSelect, |
| 63 | handleModelSelect, |
| 64 | handleSessionSelect, |
| 65 | handleReload, |
| 66 | closeCurrentScreen, |
| 67 | activePermissionRequest, |
| 68 | handleToolPermissionResponse, |
| 69 | handleUserMessage, |
| 70 | isWaitingForResponse, |
| 71 | isCompacting, |
| 72 | inputMode, |
| 73 | handleInterrupt, |
| 74 | handleFileAttached, |
| 75 | isInputDisabled, |
| 76 | wasInterrupted = false, |
| 77 | isRemoteMode, |
| 78 | onImageInClipboardChange, |
| 79 | diffContent, |
| 80 | }) => { |
| 81 | if (hideScreenContent(services.update)) { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | // Login prompt |
| 86 | if (isScreenActive("login") && navState.screenData) { |
| 87 | return ( |
| 88 | <Box |
| 89 | paddingX={1} |
| 90 | borderStyle="round" |
| 91 | borderColor="yellow" |
| 92 | flexDirection="column" |
| 93 | gap={1} |
| 94 | > |
| 95 | <Text color="yellow" bold> |
| 96 | Login Required |
| 97 | </Text> |
| 98 | <Text>{navState.screenData.text}</Text> |
| 99 | <UserInput |
| 100 | onSubmit={handleLoginTokenSubmit} |
| 101 | isWaitingForResponse={false} |
| 102 | inputMode={true} |
| 103 | assistant={services.config?.config || undefined} |
| 104 | disabled={false} |
| 105 | placeholder="Enter your token..." |
| 106 | hideNormalUI={true} |
| 107 | /> |
| 108 | </Box> |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | // Config selector |
| 113 | if (isScreenActive("config")) { |
| 114 | return ( |
| 115 | <ConfigSelector |
| 116 | onSelect={handleConfigSelect} |
| 117 | onCancel={closeCurrentScreen} |
| 118 | /> |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | if (isScreenActive("mcp")) { |
| 123 | return <MCPSelector onCancel={closeCurrentScreen} />; |
| 124 | } |
| 125 | |
| 126 | if (isScreenActive("update")) { |
| 127 | return <UpdateSelector onCancel={closeCurrentScreen} />; |
| 128 | } |
| 129 | |
| 130 | // Model selector |
| 131 | if (isScreenActive("model")) { |
| 132 | return ( |
| 133 | <ModelSelector |
| 134 | onSelect={handleModelSelect} |
| 135 | onCancel={closeCurrentScreen} |
| 136 | /> |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | // Session selector |
| 141 | if (isScreenActive("session")) { |
| 142 | return ( |
| 143 | <SessionSelectorWithLoading |
| 144 | onSelect={handleSessionSelect} |
| 145 | onExit={closeCurrentScreen} |
| 146 | /> |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | // Free trial transition UI |
| 151 | if (isScreenActive("free-trial")) { |
| 152 | return <FreeTrialTransitionUI onReload={handleReload} />; |
| 153 | } |
| 154 | |
| 155 | // Diff viewer overlay |
| 156 | if (isScreenActive("diff")) { |
| 157 | return ( |
| 158 | <DiffViewer |
| 159 | diffContent={diffContent || ""} |
| 160 | onClose={closeCurrentScreen} |
| 161 | /> |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | // Chat screen with input area |
| 166 | if (isScreenActive("chat")) { |
| 167 | if (activePermissionRequest) { |
| 168 | return ( |
| 169 | <ToolPermissionSelector |
| 170 | toolName={activePermissionRequest.toolName} |
| 171 | toolArgs={activePermissionRequest.toolArgs} |
| 172 | requestId={activePermissionRequest.requestId} |
| 173 | toolCallPreview={activePermissionRequest.toolCallPreview} |
| 174 | hasDynamicEvaluation={activePermissionRequest.hasDynamicEvaluation} |
| 175 | onResponse={handleToolPermissionResponse} |
| 176 | /> |
| 177 | ); |
| 178 | } |
| 179 | return ( |
| 180 | <UserInput |
| 181 | onSubmit={handleUserMessage} |
| 182 | isWaitingForResponse={isWaitingForResponse} |
| 183 | isCompacting={isCompacting} |
| 184 | inputMode={inputMode} |
| 185 | onInterrupt={handleInterrupt} |
| 186 | assistant={services.config?.config || undefined} |
| 187 | wasInterrupted={wasInterrupted} |
| 188 | onFileAttached={handleFileAttached} |
| 189 | disabled={isInputDisabled} |
| 190 | isRemoteMode={isRemoteMode} |
| 191 | onImageInClipboardChange={onImageInClipboardChange} |
| 192 | /> |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | return null; |
| 197 | }; |