| 1 | import type { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml"; |
| 2 | import { Static, useStdout } from "ink"; |
| 3 | import React, { useCallback, useEffect, useRef, useState } from "react"; |
| 4 | |
| 5 | import type { MCPService } from "../../services/MCPService.js"; |
| 6 | import { useTerminalSize } from "../hooks/useTerminalSize.js"; |
| 7 | import { IntroMessage } from "../IntroMessage.js"; |
| 8 | import type { DisplayMessage } from "../types.js"; |
| 9 | |
| 10 | interface StaticChatContentProps { |
| 11 | showIntroMessage: boolean; |
| 12 | config?: AssistantUnrolled; |
| 13 | model?: ModelConfig; |
| 14 | mcpService?: MCPService; |
| 15 | messages: DisplayMessage[]; |
| 16 | renderMessage: (message: DisplayMessage, index: number) => React.ReactElement; |
| 17 | } |
| 18 | |
| 19 | export const StaticChatContent: React.FC<StaticChatContentProps> = ({ |
| 20 | showIntroMessage, |
| 21 | config, |
| 22 | model, |
| 23 | mcpService, |
| 24 | messages, |
| 25 | renderMessage, |
| 26 | }) => { |
| 27 | const { columns, rows } = useTerminalSize(); |
| 28 | const { stdout } = useStdout(); |
| 29 | |
| 30 | // State for managing static refresh with key-based remounting (gemini-cli approach) |
| 31 | const [staticKey, setStaticKey] = useState(0); |
| 32 | const isInitialMount = useRef(true); |
| 33 | |
| 34 | // Refresh function that clears terminal and remounts Static component |
| 35 | const refreshStatic = useCallback(() => { |
| 36 | // Clear terminal completely before remounting |
| 37 | stdout.write("\x1b[2J\x1b[H"); |
| 38 | setStaticKey((prev) => prev + 1); |
| 39 | }, [stdout]); |
| 40 | |
| 41 | // Debounced terminal resize handler (300ms like gemini-cli) |
| 42 | useEffect(() => { |
| 43 | // Skip refreshing Static during first mount |
| 44 | if (isInitialMount.current) { |
| 45 | isInitialMount.current = false; |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Debounce so it doesn't fire too often during resize |
| 50 | const handler = setTimeout(() => { |
| 51 | refreshStatic(); |
| 52 | }, 300); |
| 53 | |
| 54 | return () => { |
| 55 | clearTimeout(handler); |
| 56 | }; |
| 57 | }, [columns, rows, refreshStatic]); |
| 58 | |
| 59 | // Create static items array (similar to gemini-cli's approach) |
| 60 | const staticItems = React.useMemo(() => { |
| 61 | const items: React.ReactElement[] = []; |
| 62 | |
| 63 | // Add intro message as first item if it should be shown |
| 64 | if (showIntroMessage && config && model && mcpService) { |
| 65 | items.push( |
| 66 | <IntroMessage |
| 67 | key="intro" |
| 68 | config={config} |
| 69 | model={model} |
| 70 | mcpService={mcpService} |
| 71 | />, |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | // Add all chat messages |
| 76 | messages.forEach((message, index) => { |
| 77 | items.push(renderMessage(message, index)); |
| 78 | }); |
| 79 | |
| 80 | return items; |
| 81 | }, [showIntroMessage, config, model, mcpService, messages, renderMessage]); |
| 82 | |
| 83 | return ( |
| 84 | <Static |
| 85 | key={staticKey} |
| 86 | items={staticItems} |
| 87 | style={{ |
| 88 | width: columns - 1, |
| 89 | textWrap: "wrap", |
| 90 | }} |
| 91 | > |
| 92 | {(item) => item} |
| 93 | </Static> |
| 94 | ); |
| 95 | }; |