import type { ErrorInfo, ReactNode } from "react"; import { Component, useMemo } from "react"; import { Box, Group, Text, Tree, useTree } from "@mantine/core"; import type { MantineSpacing, RenderTreeNodePayload, TreeNodeData } from "@mantine/core"; import { IconChevronRight } from "@tabler/icons-react"; import { buildTrustedRecursiveList } from "./recursive-list-builder"; import type { TrustedRecursiveListProps } from "./recursive-list-types"; const HARD_MAX_DEPTH = 32; export { buildTrustedRecursiveList }; export type { RecursiveListBuildOptions, RecursiveListMetadata, TrustedRecursiveListProps, } from "./recursive-list-types"; export function TrustedRecursiveList({ ...props }: TrustedRecursiveListProps) { return ( ); } function TrustedRecursiveListInner({ nodes, defaultExpandedDepth = 4, indent = "lg", gap = "xs", showLines = false, rootProps = {}, }: TrustedRecursiveListProps) { const initialExpandedState = useMemo( () => collectInitialExpandedState(nodes, clampInteger(defaultExpandedDepth, 0, HARD_MAX_DEPTH, 4)), [defaultExpandedDepth, nodes], ); const tree = useTree({ initialExpandedState }); return ( } /> ); } class RecursiveListErrorBoundary extends Component< { children: ReactNode; resetKey: unknown; code: string }, { error: Error | null; resetKey: unknown } > { public state = { error: null, resetKey: this.props.resetKey } as { error: Error | null; resetKey: unknown }; public static getDerivedStateFromProps( props: Readonly<{ children: ReactNode; resetKey: unknown; code: string }>, state: Readonly<{ error: Error | null; resetKey: unknown }>, ) { if (props.resetKey === state.resetKey) return null; return { error: null, resetKey: props.resetKey }; } public static getDerivedStateFromError(error: Error) { return { error }; } public componentDidCatch(_error: Error, _info: ErrorInfo) {} public render() { return this.state.error ? ( {this.props.code}: {this.state.error.message} ) : ( this.props.children ); } } function TrustedRecursiveListNode({ node, expanded, hasChildren, elementProps, gap, }: RenderTreeNodePayload & { gap: MantineSpacing }) { return ( {hasChildren && ( )} {node.label} ); } function collectInitialExpandedState(nodes: readonly TreeNodeData[], depthLimit: number) { const expanded: Record = {}; const visit = (values: readonly TreeNodeData[], depth: number) => { for (const node of values) { if (!node.children?.length || depth >= depthLimit) continue; expanded[node.value] = true; visit(node.children, depth + 1); } }; visit(nodes, 0); return expanded; } function clampInteger(value: number, minimum: number, maximum: number, fallback: number) { if (!Number.isFinite(value)) return fallback; return Math.min(maximum, Math.max(minimum, Math.trunc(value))); }