import React from 'react'; import type { NormalizedLogSection } from './types'; type FoldableSection = Pick< NormalizedLogSection, 'containerName' | 'isCompleted' | 'hasTerminatedWithError' >; const EMPTY_EXPANDED_SECTIONS = new Set(); const EMPTY_OVERRIDES: ReadonlyMap = new Map(); /** Default: in-progress open, completed folded. */ const isExpandedByDefault = (section: FoldableSection): boolean => !section.isCompleted || !!section.hasTerminatedWithError; const resolveIsExpanded = ( sections: readonly FoldableSection[], overrides: ReadonlyMap, index: number, ): boolean => { const override = overrides.get(index); return override !== undefined ? override : isExpandedByDefault(sections[index]); }; /** Stable identity key so log-content updates don't reset overrides. */ const sectionNamesKey = (sections: readonly FoldableSection[]): string => sections.map((s) => s.containerName).join('\0'); const isPrefixGrowth = (prev: readonly string[], next: readonly string[]): boolean => next.length >= prev.length && prev.every((name, i) => name === next[i]); /** Store only deviations from the default expand policy. */ const withOverride = ( prev: ReadonlyMap, index: number, expanded: boolean, defaultExpanded: boolean, ): ReadonlyMap => { if (expanded === defaultExpanded) { if (!prev.has(index)) return prev; const next = new Map(prev); next.delete(index); return next.size > 0 ? next : EMPTY_OVERRIDES; } if (prev.get(index) === expanded) return prev; const next = new Map(prev); next.set(index, expanded); return next; }; export const useSectionFold = (sections: readonly FoldableSection[]) => { const sectionsRef = React.useRef(sections); sectionsRef.current = sections; const [overrides, setOverrides] = React.useState>(EMPTY_OVERRIDES); const namesKey = sectionNamesKey(sections); const prevNamesRef = React.useRef(sections.map((s) => s.containerName)); React.useEffect(() => { const prevNames = prevNamesRef.current; const nextNames = namesKey === '' ? [] : namesKey.split('\0'); // Task switch: drop overrides. Progressive step append: keep them. if (!isPrefixGrowth(prevNames, nextNames) && !isPrefixGrowth(nextNames, prevNames)) { setOverrides(EMPTY_OVERRIDES); } else if (nextNames.length < prevNames.length) { setOverrides((prev) => { if (prev.size === 0) return prev; let changed = false; const pruned = new Map(); prev.forEach((value, index) => { if (index < nextNames.length) pruned.set(index, value); else changed = true; }); if (!changed) return prev; return pruned.size > 0 ? pruned : EMPTY_OVERRIDES; }); } prevNamesRef.current = nextNames; }, [namesKey]); const expandedSections = React.useMemo(() => { if (sections.length === 0) return EMPTY_EXPANDED_SECTIONS; const expanded = new Set(); for (let i = 0; i < sections.length; i++) { if (resolveIsExpanded(sections, overrides, i)) expanded.add(i); } return expanded; }, [sections, overrides]); const toggleSection = React.useCallback((sectionIndex: number) => { const currentSections = sectionsRef.current; if (sectionIndex < 0 || sectionIndex >= currentSections.length) return; setOverrides((prev) => { const defaultExpanded = isExpandedByDefault(currentSections[sectionIndex]); const nextExpanded = !resolveIsExpanded(currentSections, prev, sectionIndex); return withOverride(prev, sectionIndex, nextExpanded, defaultExpanded); }); }, []); const expandSection = React.useCallback((sectionIndex: number) => { const currentSections = sectionsRef.current; if (sectionIndex < 0 || sectionIndex >= currentSections.length) return; setOverrides((prev) => { if (resolveIsExpanded(currentSections, prev, sectionIndex)) return prev; return withOverride( prev, sectionIndex, true, isExpandedByDefault(currentSections[sectionIndex]), ); }); }, []); const toggleAllSections = React.useCallback(() => { const currentSections = sectionsRef.current; const fold = expandedSections.size >= currentSections.length; setOverrides((prev) => { let next = prev; for (let i = 0; i < currentSections.length; i++) { const defaultExpanded = isExpandedByDefault(currentSections[i]); next = withOverride(next, i, !fold, defaultExpanded); } return next; }); }, [expandedSections.size]); return React.useMemo( () => ({ expandedSections, toggleSection, expandSection, toggleAllSections }), [expandedSections, toggleSection, expandSection, toggleAllSections], ); };