All files / core/blong-browser/src/components/Navigator Navigator.tsx

84.31% Statements 129/153
63.39% Branches 71/112
83.87% Functions 26/31
87.96% Lines 117/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386                                                                                                                                                                        29x 29x   29x 30x                   30x     29x 30x 30x 30x 28x   2x   2x 2x       29x                 63x 63x 34x     34x 34x 34x   63x                 8x 8x 2x 2x             8x 8x 2x 8x                                                 82x 23x       82x   82x 82x 8x 8x   82x 82x 82x       82x 29x 29x 63x 34x 34x     29x 29x     246x 82x   34x               82x 29x       82x 23x   2x 2x   2x     2x   2x     82x                               82x 8x 8x 8x 9x         9x 9x   8x               82x   8x 4x 4x 4x   4x 4x 6x 6x 6x 5x 5x 5x   5x 5x     4x   6x   4x 4x 4x                   82x   3x 3x 3x 3x 4x 4x 4x 3x         4x 1x 1x 1x 1x       1x 1x 1x   1x     3x 2x         82x   82x                     82x   82x                 8x 8x   8x 8x 8x                    
/**
 * Navigator — tree view for hierarchical data navigation.
 * Typically placed on the left side of an Explorer or the commander shell.
 *
 * Commander enhancements: lazy per-node children (`loadChildren`), controlled
 * `expandedKeys` / `onToggle`, permission pruning (`permissionField`) and
 * start-at-a-branch (`startKey`).
 *
 * Drill-path mirroring (used by the Commander): nodes track their parent
 * (`__parentKey` in `data`), the imperative `selectPath()` reveals (materializes
 * + expands + selects) a root→node path, and `onSelectPath` reports the full
 * path of the currently selected node — so a shell can mirror the tree like a
 * file explorer and offer "go to parent" navigation.
 */
import {Tree, type TreeNode} from '../../primereact/index.js';
import React, {
    useCallback,
    useEffect,
    useImperativeHandle,
    useMemo,
    useRef,
    useState,
} from 'react';
import {useAppStore} from '../../state/appStore.js';
 
/** One step of a tree path (root → node). `key`/`label` are tree keys/labels. */
export interface INavigatorPathItem {
    key: string;
    label: string;
    data: Record<string, unknown>;
}
 
/** Imperative handle — lets a shell drive the tree (drill / go-up / reveal). */
export interface INavigatorHandle {
    /** Reveal (materialize + expand + select) a root→node path. */
    selectPath: (path: INavigatorPathItem[]) => Promise<void>;
    /**
     * Resolve a "/"-joined label path to a root→node path (materializing lazy
     * children as needed) and reveal it. Returns false when a label doesn't match.
     */
    jumpTo: (labels: string[]) => Promise<boolean>;
}
 
export interface INavigatorProps {
    /** Fetch function returning tree nodes */
    fetch?: () => Promise<Record<string, unknown[]>>;
    /** Lazy child fetch — called when a node is expanded and has no children yet */
    loadChildren?: (node: Record<string, unknown>) => Promise<Record<string, unknown>[]>;
    /** Called with the selected node value */
    onSelect?: (node: Record<string, unknown> | null) => void;
    /** Called with the full root→selected path (empty when nothing is selected). */
    onSelectPath?: (path: INavigatorPathItem[]) => void;
    keyField?: string;
    parentField?: string;
    field?: string;
    title?: string;
    /** Expected key in fetch result */
    resultSet?: string;
    /** Data passed directly (alternative to fetch) */
    data?: Record<string, unknown>[];
    /** Controlled expanded keys (object keyed by node key) */
    expandedKeys?: Record<string, boolean>;
    /** Called when the expanded set changes */
    onToggle?: (keys: Record<string, boolean>) => void;
    /** Node field holding the permission name; nodes without access are pruned */
    permissionField?: string;
    /** Node key to select + expand on mount (start at a branch) */
    startKey?: string;
    /** Mark nodes as tree leaves (no expander) based on their data */
    isLeaf?: (node: Record<string, unknown>) => boolean;
    /** Inline style for the underlying PrimeReact Tree (e.g. border/padding). */
    treeStyle?: React.CSSProperties;
    className?: string;
    /** React 19 ref-as-prop — imperative handle for driving the tree. */
    ref?: React.Ref<INavigatorHandle>;
}
 
function buildTree(
    items: Record<string, unknown>[],
    keyField: string,
    parentField: string,
    labelField: string,
    isLeaf?: (node: Record<string, unknown>) => boolean,
): TreeNode[] {
    const roots: TreeNode[] = [];
    const map = new Map<unknown, TreeNode>();
 
    for (const item of items) {
        const node: TreeNode = {
            key: String(item[keyField]),
            label: String(item[labelField] ?? item[keyField]),
            data: item,
            children: [],
            // PrimeReact only renders the expander for empty-children nodes when
            // `leaf === false` — force it explicitly so lazy-loadable nodes show
            // an arrow (and expand without triggering drill-down/selection).
            ...(isLeaf ? {leaf: isLeaf(item)} : {}),
        };
        map.set(item[keyField], node);
    }
 
    for (const item of items) {
        const parentKey = item[parentField];
        const node = map.get(item[keyField])!;
        if (parentKey == null || !map.has(parentKey)) {
            roots.push(node);
        } else {
            const parent = map.get(parentKey)!;
            // Record the parent so a selected node's full path can be walked.
            node.data = {...(node.data as object), __parentKey: String(parent.key)};
            parent.children = [...(parent.children ?? []), node];
        }
    }
 
    return roots;
}
 
/** Recursively drop nodes whose permission is not granted. */
function filterTree(
    nodes: TreeNode[],
    permissionField: string | undefined,
    canShow: (permission?: unknown) => boolean,
): TreeNode[] {
    const result: TreeNode[] = [];
    for (const node of nodes) {
        const permission = permissionField
            ? (node.data as Record<string, unknown> | undefined)?.[permissionField]
            : undefined;
        Iif (!canShow(permission)) continue;
        const children = filterTree(node.children ?? [], permissionField, canShow);
        result.push({...node, children});
    }
    return result;
}
 
/** Return a new tree with the node at `key` having `children` attached. */
function updateNodeChildren(
    nodes: TreeNode[],
    key: string,
    children: TreeNode[],
): TreeNode[] {
    return nodes.map(node => {
        if (node.key === key) return {...node, children};
        Eif (node.children?.length)
            return {...node, children: updateNodeChildren(node.children, key, children)};
        return node;
    });
}
 
/** Depth-first lookup of a node by key. */
function findNode(nodes: TreeNode[], key: string): TreeNode | undefined {
    for (const node of nodes) {
        if (node.key === key) return node;
        const found = findNode(node.children ?? [], key);
        if (found) return found;
    }
    return undefined;
}
 
export function Navigator({
        data: staticData,
        fetch: fetchFn,
        loadChildren,
        onSelect,
        onSelectPath,
        keyField = 'id',
        parentField = 'parentId',
        field = 'name',
        title,
        resultSet,
        permissionField,
        startKey,
        expandedKeys: expandedKeysProp,
        onToggle,
        isLeaf,
        treeStyle,
        className,
        ref,
    }: INavigatorProps) {
    const [treeData, setTreeData] = useState<TreeNode[]>(() =>
        staticData ? buildTree(staticData, keyField, parentField, field, isLeaf) : [],
    );
    // Latest tree mirror so async materialization (`selectPath`) never reads a
    // stale snapshot while awaiting a `loadChildren` fetch.
    const treeRef = useRef<TreeNode[]>(treeData);
    // key → TreeNode index, kept in sync with `treeData` (parent walking).
    const nodeMapRef = useRef(new Map<string, TreeNode>());
    const setTree = useCallback((next: TreeNode[]) => {
        treeRef.current = next;
        setTreeData(next);
    }, []);
    const [loading, setLoading] = useState(false);
    const [selectedKey, setSelectedKey] = useState<string | null>(startKey ?? null);
    const [expandedKeys, setExpandedKeys] = useState<Record<string, boolean>>(
        startKey ? {[startKey]: true} : {},
    );
 
    useEffect(() => {
        const map = new Map<string, TreeNode>();
        const walk = (nodes: TreeNode[]) => {
            for (const node of nodes) {
                map.set(String(node.key), node);
                walk(node.children ?? []);
            }
        };
        walk(treeData);
        nodeMapRef.current = map;
    }, [treeData]);
 
    const permissions = useAppStore(s => s.auth.permissions);
    const canShow = useCallback(
        (permission?: unknown) => {
            Eif (permission === undefined || permission === null || permission === '') return true;
            if (typeof permissions === 'boolean') return permissions;
            return (permissions as Record<string, boolean>)[String(permission)] === true;
        },
        [permissions],
    );
 
    // Prune permission-denied nodes before rendering.
    const visibleTree = useMemo(
        () => filterTree(treeData, permissionField, canShow),
        [treeData, permissionField, canShow],
    );
 
    React.useEffect(() => {
        if (!fetchFn) return;
        // eslint-disable-next-line @eslint-react/set-state-in-effect
        setLoading(true);
        void fetchFn()
            .then(result => {
                const items = resultSet
                    ? (result[resultSet] as Record<string, unknown>[])
                    : (Object.values(result)[0] as Record<string, unknown>[]);
                setTree(buildTree(items ?? [], keyField, parentField, field, isLeaf));
            })
            .finally(() => setLoading(false));
    }, [fetchFn, keyField, parentField, field, resultSet, isLeaf, setTree]);
 
    const loadChildrenForKey = useCallback(
        (key: string) => {
            if (!loadChildren) return;
            const node = findNode(treeData, key);
            if (!node || (node.children?.length ?? 0) > 0) return;
            void loadChildren(node.data as Record<string, unknown>).then(children => {
                const built = buildTree(children ?? [], keyField, parentField, field, isLeaf).map(
                    child => ({...child, data: {...(child.data as object), __parentKey: String(node.key)}}),
                );
                setTree(updateNodeChildren(treeData, key, built));
            });
        },
        [loadChildren, treeData, keyField, parentField, field, isLeaf, setTree],
    );
 
    /** Walk up the `__parentKey` chain to build the root→node path. */
    const pathForKey = useCallback((key: string): INavigatorPathItem[] => {
        const path: INavigatorPathItem[] = [];
        let cur = nodeMapRef.current.get(key);
        while (cur) {
            path.unshift({
                key: String(cur.key),
                label: String(cur.label ?? cur.key),
                data: (cur.data as Record<string, unknown>) ?? {},
            });
            const parentKey = (cur.data as Record<string, unknown> | undefined)?.['__parentKey'];
            cur = typeof parentKey === 'string' ? nodeMapRef.current.get(parentKey) : undefined;
        }
        return path;
    }, []);
 
    /**
     * Reveal a root→node path: lazily materialize each node's children so the
     * tree mirrors the drill (file-explorer style), expand every path node and
     * select the deepest one.
     */
    const selectPath = useCallback(
        async (path: INavigatorPathItem[]) => {
            if (!path.length) {
                setSelectedKey(null);
                onSelectPath?.([]);
                return;
            }
            let tree = treeRef.current;
            for (const item of path) {
                const node = findNode(tree, item.key);
                Iif (!node) return; // ancestor not materialized — nothing to reveal
                if (!node.leaf && (node.children?.length ?? 0) === 0) {
                    const children = (await loadChildren?.(node.data as Record<string, unknown>)) ?? [];
                    const built = buildTree(children, keyField, parentField, field, isLeaf).map(
                        child => ({...child, data: {...(child.data as object), __parentKey: String(node.key)}}),
                    );
                    tree = updateNodeChildren(tree, item.key, built);
                    setTree(tree);
                }
            }
            setExpandedKeys(prev => ({
                ...prev,
                ...Object.fromEntries(path.map(p => [p.key, true])),
            }));
            const last = path[path.length - 1];
            setSelectedKey(last.key);
            onSelectPath?.(path);
        },
        [loadChildren, keyField, parentField, field, isLeaf, onSelectPath, setTree],
    );
 
    /**
     * Resolve a label path (e.g. `['Kubernetes (dev)', 'prod', 'deploy']`) to a
     * root→node path, materializing lazy children level-by-level, then reveal it.
     * Returns false when any label fails to match.
     */
    const jumpTo = useCallback(
        async (labels: string[]): Promise<boolean> => {
            Iif (!labels.length) return true;
            const path: INavigatorPathItem[] = [];
            let nodes = treeRef.current;
            for (let i = 0; i < labels.length; i++) {
                const target = labels[i].trim().toLowerCase();
                const found = nodes.find(n => String(n.label).trim().toLowerCase() === target);
                if (!found) return false;
                path.push({
                    key: String(found.key),
                    label: String(found.label),
                    data: (found.data as Record<string, unknown>) ?? {},
                });
                if (i < labels.length - 1) {
                    let children = found.children ?? [];
                    Eif (children.length === 0 && !found.leaf) {
                        const kids = (await loadChildren?.(found.data as Record<string, unknown>)) ?? [];
                        children = buildTree(kids, keyField, parentField, field, isLeaf).map(child => ({
                            ...child,
                            data: {...(child.data as object), __parentKey: String(found.key)},
                        }));
                        const next = updateNodeChildren(treeRef.current, String(found.key), children);
                        treeRef.current = next;
                        setTree(next);
                    }
                    nodes = children;
                }
            }
            await selectPath(path);
            return true;
        },
        [loadChildren, keyField, parentField, field, isLeaf, selectPath, setTree],
    );
 
    useImperativeHandle(ref, () => ({selectPath, jumpTo}), [selectPath, jumpTo]);
 
    const handleToggle = useCallback(
        (e: {value: Record<string, boolean>}) => {
            const next = e.value;
            const newly = Object.keys(next).filter(key => next[key] && !expandedKeys[key]);
            setExpandedKeys(next);
            onToggle?.(next);
            for (const key of newly) loadChildrenForKey(key);
        },
        [expandedKeys, onToggle, loadChildrenForKey],
    );
 
    const controlledExpanded = expandedKeysProp ?? expandedKeys;
 
    return (
        <div className={`blong-navigator ${className ?? ''}`}>
            {title && <h4 className="blong-navigator__title">{title}</h4>}
            <Tree
                value={visibleTree}
                loading={loading}
                selectionMode="single"
                selectionKeys={selectedKey ?? undefined}
                onSelectionChange={e => {
                    const key = (e.value as string) ?? null;
                    setSelectedKey(key);
                    // Find the node data
                    const node = key ? nodeMapRef.current.get(key) : undefined;
                    onSelect?.((node?.data as Record<string, unknown>) ?? null);
                    onSelectPath?.(key ? pathForKey(key) : []);
                }}
                expandedKeys={controlledExpanded}
                onToggle={handleToggle}
                className="blong-navigator__tree"
                style={treeStyle}
            />
        </div>
    );
}