MCPcopy Index your code
hub / github.com/nukeop/nuclear / useCollapsibleText

Function useCollapsibleText

packages/ui/src/hooks/useCollapsibleText.ts:13–39  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

11};
12
13export const useCollapsibleText = (text: string): UseCollapsibleTextResult => {
14 const [isExpanded, setIsExpanded] = useState(false);
15
16 const { isCollapsible, collapsedText } = useMemo(() => {
17 const messageLines = text.split('\n');
18 const hasManyLines = messageLines.length > COLLAPSED_MAX_LINES;
19 const hasManyChars = text.length > COLLAPSED_MAX_CHARS;
20
21 if (!hasManyLines && !hasManyChars) {
22 return { isCollapsible: false, collapsedText: text };
23 }
24
25 const truncated = hasManyLines
26 ? messageLines.slice(0, COLLAPSED_MAX_LINES).join('\n')
27 : text.slice(0, COLLAPSED_MAX_CHARS) + '…';
28
29 return { isCollapsible: true, collapsedText: truncated };
30 }, [text]);
31
32 const toggle = useCallback(() => {
33 setIsExpanded((prev) => !prev);
34 }, []);
35
36 const displayedText = isCollapsible && !isExpanded ? collapsedText : text;
37
38 return { isCollapsible, displayedText, isExpanded, toggle };
39};

Callers 2

LogEntryImplFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected