Line data Source code
1 : import * as React from 'react';
2 : import * as styles from './json_view.module.css';
3 :
4 : import { useJsonViewerState } from './json_view_state.js';
5 : import { useNestedExpansionState } from './json_nested_state.js';
6 : import { JsonBracketsClose } from './json_nested_open.js';
7 :
8 : interface JsonNestedCloseProps {
9 : expandKey: string;
10 : level: number;
11 : parentValue?: object;
12 : value: object;
13 : keyName?: (string | number);
14 : keyPath: (string | number)[];
15 : }
16 :
17 1 : export function JsonNestedClose(props: JsonNestedCloseProps) {
18 : // Is the node expanded?
19 1 : const expands = useNestedExpansionState();
20 1 : const { variant, collapsed, shouldExpandNodeInitially } = useJsonViewerState();
21 1 : const defaultExpanded =
22 1 : typeof collapsed === 'boolean' ? !collapsed : typeof collapsed === 'number' ? props.level <= collapsed : true;
23 1 : const isExpanded = expands[props.expandKey] ?? (shouldExpandNodeInitially ? true : defaultExpanded);
24 1 : const shouldExpand =
25 1 : shouldExpandNodeInitially && shouldExpandNodeInitially(isExpanded, {
26 1 : level: props.level,
27 1 : keyPath: props.keyPath,
28 1 : keyName: props.keyName,
29 1 : value: props.value,
30 1 : parentValue: props.parentValue,
31 1 : });
32 :
33 1 : if (expands[props.expandKey] === undefined && !shouldExpand) {
34 1 : return null;
35 : }
36 : // The explorer has no closing brackets
37 1 : if (variant === 'explorer') {
38 1 : return null;
39 : }
40 :
41 : // Is the node empty?
42 1 : const len = Object.keys(props.value!).length;
43 1 : if (!isExpanded || len === 0) {
44 1 : return null;
45 : }
46 :
47 1 : const compProps = {
48 1 : keyName: props.keyName,
49 1 : keyPath: props.keyPath,
50 1 : value: props.value,
51 1 : parentValue: props.parentValue,
52 1 : };
53 1 : const isArray = Array.isArray(props.value);
54 1 : const isMySet = props.value instanceof Set;
55 1 : return (
56 1 : <div className={styles.object_close}>
57 1 : <JsonBracketsClose isBrackets={isArray || isMySet} {...compProps} isVisible={true} />
58 1 : </div>
59 : );
60 1 : };
|