[PR #31074] fix: resolve 'Expand all chunks' button not working #33078

Closed
opened 2026-02-21 20:52:37 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/31074

State: closed
Merged: Yes


Summary

Fixes #30817 - The 'Expand all chunks' button on the document detail page was not working.

Root Cause

The bug was caused by the context value object being recreated on every render without proper memoization. This prevented the isCollapsed state changes from properly propagating to ChunkContent components that consume the context via use-context-selector.

Changes

  1. Memoize context value: Added useMemo to memoize the SegmentListContext value, ensuring stable references and proper change detection with use-context-selector
  2. Stable callback: Created a stable toggleCollapsed callback using useCallback with functional state update to avoid closure issues
  3. Stable selector: Extracted the isCollapsed selector to a stable function outside the component to ensure consistent behavior with use-context-selector

Testing

  • Clicking the expand/collapse button now properly toggles all chunks between collapsed (2 lines) and expanded (20 lines) views
  • The context state changes propagate correctly to all ChunkContent components

Files Changed

  • web/app/components/datasets/documents/detail/completed/index.tsx
  • web/app/components/datasets/documents/detail/completed/segment-card/chunk-content.tsx
**Original Pull Request:** https://github.com/langgenius/dify/pull/31074 **State:** closed **Merged:** Yes --- ## Summary Fixes #30817 - The 'Expand all chunks' button on the document detail page was not working. ## Root Cause The bug was caused by the context value object being recreated on every render without proper memoization. This prevented the `isCollapsed` state changes from properly propagating to `ChunkContent` components that consume the context via `use-context-selector`. ## Changes 1. **Memoize context value**: Added `useMemo` to memoize the `SegmentListContext` value, ensuring stable references and proper change detection with `use-context-selector` 2. **Stable callback**: Created a stable `toggleCollapsed` callback using `useCallback` with functional state update to avoid closure issues 3. **Stable selector**: Extracted the `isCollapsed` selector to a stable function outside the component to ensure consistent behavior with `use-context-selector` ## Testing - Clicking the expand/collapse button now properly toggles all chunks between collapsed (2 lines) and expanded (20 lines) views - The context state changes propagate correctly to all `ChunkContent` components ## Files Changed - `web/app/components/datasets/documents/detail/completed/index.tsx` - `web/app/components/datasets/documents/detail/completed/segment-card/chunk-content.tsx`
yindo added the pull-request label 2026-02-21 20:52:37 -05:00
yindo closed this issue 2026-02-21 20:52:37 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#33078