11import { TextAttributes } from '@opentui/core'
2- import { useMemo , type ReactNode } from 'react'
2+ import { useCallback , useMemo , type ReactNode } from 'react'
33import React from 'react'
44
55import { MessageBlock } from '../components/message-block'
@@ -182,22 +182,15 @@ export const useMessageRenderer = (
182182 style = { {
183183 flexDirection : 'row' ,
184184 alignSelf : 'flex-start' ,
185- backgroundColor : isCollapsed
186- ? theme . muted
187- : theme . success ,
185+ backgroundColor : isCollapsed ? theme . muted : theme . success ,
188186 paddingLeft : 1 ,
189187 paddingRight : 1 ,
190188 } }
191189 onMouseDown = { handleTitleClick }
192190 >
193191 < text style = { { wrapMode : 'word' } } >
194- < span fg = { theme . foreground } >
195- { isCollapsed ? '▸ ' : '▾ ' }
196- </ span >
197- < span
198- fg = { theme . foreground }
199- attributes = { TextAttributes . BOLD }
200- >
192+ < span fg = { theme . foreground } > { isCollapsed ? '▸ ' : '▾ ' } </ span >
193+ < span fg = { theme . foreground } attributes = { TextAttributes . BOLD } >
201194 { agentInfo . agentName }
202195 </ span >
203196 </ text >
@@ -243,10 +236,7 @@ export const useMessageRenderer = (
243236 >
244237 { agentChildren . map ( ( childAgent , idx ) => (
245238 < box key = { childAgent . id } style = { { flexShrink : 0 } } >
246- { renderMessageWithAgents (
247- childAgent ,
248- depth + 1 ,
249- ) }
239+ { renderMessageWithAgents ( childAgent , depth + 1 ) }
250240 </ box >
251241 ) ) }
252242 </ box >
@@ -263,16 +253,13 @@ export const useMessageRenderer = (
263253 const isAgent = message . variant === 'agent'
264254
265255 if ( isAgent ) {
266- return renderAgentMessage (
267- message ,
268- depth ,
269- )
256+ return renderAgentMessage ( message , depth )
270257 }
271258
272259 const isAi = message . variant === 'ai'
273260 const isUser = message . variant === 'user'
274261 const isError = message . variant === 'error'
275-
262+
276263 // Check if this is a mode divider message
277264 if (
278265 message . blocks &&
@@ -294,11 +281,7 @@ export const useMessageRenderer = (
294281 : isAi
295282 ? theme . foreground
296283 : theme . foreground
297- const timestampColor = isError
298- ? 'red'
299- : isAi
300- ? theme . muted
301- : theme . muted
284+ const timestampColor = isError ? 'red' : isAi ? theme . muted : theme . muted
302285 const estimatedMessageWidth = availableWidth
303286 const codeBlockWidth = Math . max ( 10 , estimatedMessageWidth - 8 )
304287 const paletteForMessage : MarkdownPalette = {
@@ -317,6 +300,34 @@ export const useMessageRenderer = (
317300 const hasAgentChildren = agentChildren . length > 0
318301 const showVerticalLine = isUser
319302
303+ const onToggleCollapsed = useCallback (
304+ ( id : string ) => {
305+ const wasCollapsed = collapsedAgents . has ( id )
306+ setCollapsedAgents ( ( prev ) => {
307+ const next = new Set ( prev )
308+ if ( next . has ( id ) ) {
309+ next . delete ( id )
310+ } else {
311+ next . add ( id )
312+ }
313+ return next
314+ } )
315+ // Track user interaction
316+ setUserOpenedAgents ( ( prev ) => {
317+ const next = new Set ( prev )
318+ if ( wasCollapsed ) {
319+ // User is opening it, mark as user-opened
320+ next . add ( id )
321+ } else {
322+ // User is closing it, remove from user-opened
323+ next . delete ( id )
324+ }
325+ return next
326+ } )
327+ } ,
328+ [ collapsedAgents , setCollapsedAgents , setUserOpenedAgents ] ,
329+ )
330+
320331 return (
321332 < box
322333 key = { message . id }
@@ -384,30 +395,7 @@ export const useMessageRenderer = (
384395 markdownPalette = { markdownPalette }
385396 collapsedAgents = { collapsedAgents }
386397 streamingAgents = { streamingAgents }
387- onToggleCollapsed = { ( id : string ) => {
388- const wasCollapsed = collapsedAgents . has ( id )
389- setCollapsedAgents ( ( prev ) => {
390- const next = new Set ( prev )
391- if ( next . has ( id ) ) {
392- next . delete ( id )
393- } else {
394- next . add ( id )
395- }
396- return next
397- } )
398- // Track user interaction
399- setUserOpenedAgents ( ( prev ) => {
400- const next = new Set ( prev )
401- if ( wasCollapsed ) {
402- // User is opening it, mark as user-opened
403- next . add ( id )
404- } else {
405- // User is closing it, remove from user-opened
406- next . delete ( id )
407- }
408- return next
409- } )
410- } }
398+ onToggleCollapsed = { onToggleCollapsed }
411399 />
412400 </ box >
413401 </ box >
@@ -478,10 +466,7 @@ export const useMessageRenderer = (
478466 < box style = { { flexDirection : 'column' , width : '100%' , gap : 0 } } >
479467 { agentChildren . map ( ( agent , idx ) => (
480468 < box key = { agent . id } style = { { width : '100%' } } >
481- { renderMessageWithAgents (
482- agent ,
483- depth + 1 ,
484- ) }
469+ { renderMessageWithAgents ( agent , depth + 1 ) }
485470 </ box >
486471 ) ) }
487472 </ box >
0 commit comments