|
1 | 1 | import { TextAttributes } from '@opentui/core' |
2 | | -import { useMemo, type ReactNode } from 'react' |
| 2 | +import { useMemo, useCallback, type ReactNode } from 'react' |
3 | 3 | import React from 'react' |
4 | 4 |
|
5 | 5 | import { MessageBlock } from '../components/message-block' |
@@ -52,6 +52,34 @@ export const useMessageRenderer = ( |
52 | 52 | setUserOpenedAgents, |
53 | 53 | } = props |
54 | 54 |
|
| 55 | + const onToggleCollapsed = useCallback( |
| 56 | + (id: string) => { |
| 57 | + const wasCollapsed = collapsedAgents.has(id) |
| 58 | + setCollapsedAgents((prev) => { |
| 59 | + const next = new Set(prev) |
| 60 | + if (next.has(id)) { |
| 61 | + next.delete(id) |
| 62 | + } else { |
| 63 | + next.add(id) |
| 64 | + } |
| 65 | + return next |
| 66 | + }) |
| 67 | + // Track user interaction |
| 68 | + setUserOpenedAgents((prev) => { |
| 69 | + const next = new Set(prev) |
| 70 | + if (wasCollapsed) { |
| 71 | + // User is opening it, mark as user-opened |
| 72 | + next.add(id) |
| 73 | + } else { |
| 74 | + // User is closing it, remove from user-opened |
| 75 | + next.delete(id) |
| 76 | + } |
| 77 | + return next |
| 78 | + }) |
| 79 | + }, |
| 80 | + [collapsedAgents, setCollapsedAgents, setUserOpenedAgents], |
| 81 | + ) |
| 82 | + |
55 | 83 | return useMemo(() => { |
56 | 84 | const SIDE_GUTTER = 1 |
57 | 85 | const renderAgentMessage = ( |
@@ -384,30 +412,7 @@ export const useMessageRenderer = ( |
384 | 412 | markdownPalette={markdownPalette} |
385 | 413 | collapsedAgents={collapsedAgents} |
386 | 414 | 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 | | - }} |
| 415 | + onToggleCollapsed={onToggleCollapsed} |
411 | 416 | /> |
412 | 417 | </box> |
413 | 418 | </box> |
@@ -445,30 +450,7 @@ export const useMessageRenderer = ( |
445 | 450 | markdownPalette={markdownPalette} |
446 | 451 | collapsedAgents={collapsedAgents} |
447 | 452 | streamingAgents={streamingAgents} |
448 | | - onToggleCollapsed={(id: string) => { |
449 | | - const wasCollapsed = collapsedAgents.has(id) |
450 | | - setCollapsedAgents((prev) => { |
451 | | - const next = new Set(prev) |
452 | | - if (next.has(id)) { |
453 | | - next.delete(id) |
454 | | - } else { |
455 | | - next.add(id) |
456 | | - } |
457 | | - return next |
458 | | - }) |
459 | | - // Track user interaction |
460 | | - setUserOpenedAgents((prev) => { |
461 | | - const next = new Set(prev) |
462 | | - if (wasCollapsed) { |
463 | | - // User is opening it, mark as user-opened |
464 | | - next.add(id) |
465 | | - } else { |
466 | | - // User is closing it, remove from user-opened |
467 | | - next.delete(id) |
468 | | - } |
469 | | - return next |
470 | | - }) |
471 | | - }} |
| 453 | + onToggleCollapsed={onToggleCollapsed} |
472 | 454 | /> |
473 | 455 | </box> |
474 | 456 | )} |
@@ -508,5 +490,6 @@ export const useMessageRenderer = ( |
508 | 490 | setUserOpenedAgents, |
509 | 491 | setFocusedAgentId, |
510 | 492 | userOpenedAgents, |
| 493 | + onToggleCollapsed, |
511 | 494 | ]) |
512 | 495 | } |
0 commit comments