This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathDefinitionsContainer.tsx
More file actions
124 lines (118 loc) · 3.69 KB
/
DefinitionsContainer.tsx
File metadata and controls
124 lines (118 loc) · 3.69 KB
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
import { useRef } from 'react';
import InspectableMapping from '../../../mappings/InspectableMapping';
import { mapDefinitions, NamedGenerable } from '../../state/DefinitionStore';
import { useStoreActions, useStoreState } from '../../state/Hooks';
import AddButton from '../atoms/AddButton';
import ComponentInfo from '../atoms/ComponentInfo';
import Definition from '../atoms/Definition';
import { Empty } from '../atoms/Empty';
import {
InspectorDefinitionMenu,
InspectorDefinitionMenuNav,
} from '../menus/definitions/InspectorDefinitionMenu';
import { navSubTypeMenu } from '../menus/SubTypeMenu';
import CollapsibleList from './CollapsibleList';
import GuideContainer from './GuideContainer';
export interface DefinitionsProps {
type: InspectableMapping;
expanded?: boolean;
onChange?: (expanded: boolean) => void;
}
const DefinitionsContainer = ({
type,
expanded,
onChange,
}: DefinitionsProps) => {
// the definitions of the current type of inspectable mapping
const definitions = useStoreState((store) => store.definitions);
const navigateTo = useStoreActions((actions) => actions.navigateTo);
const guideStep = useStoreState((state) => state.guideStep);
const ref = useRef(null);
const hasDefinitions = Object.values(definitions[type.key]).length > 0;
const requirements: string[] = [];
type.requirements?.forEach((requirement) => {
if (!requirement.test(definitions)) {
requirements.push(requirement.message);
}
});
/**
* Navigate to inspector definition menu,
* will go to a subtype page if the InspectableMapping type
* has subtypes defined.
*/
const navigateToInspector = () => {
navigateTo(
type.subtypes
? navSubTypeMenu({
typePage: type.subtypes?.component,
menuPage: InspectorDefinitionMenu,
menuProps: { dataType: type, index: -1 },
})
: {
component: InspectorDefinitionMenuNav,
props: { dataType: type, index: -1 },
},
);
};
return (
<div ref={ref} className="w-full px-4 pb-0">
{type.guide && guideStep === type.guide.step && (
<GuideContainer target={ref}>{type.guide.info}</GuideContainer>
)}
<CollapsibleList
title={type.name.plural}
expanded={expanded}
className="py-4"
titleFont="font-bold"
classNameExpanded="py-4 "
onChange={onChange}
pinned={
<AddButton
disabled={requirements.length > 0}
className="flex ml-auto"
onClick={navigateToInspector}
/>
}
titleExpanded={
<>
{hasDefinitions && (
<div className="p-2 px-8">
<ComponentInfo type={type} />
</div>
)}
</>
}
>
<div className="w-full pl-2 pt-2">
{hasDefinitions ? (
mapDefinitions<NamedGenerable>(
definitions[type.key],
(definition, index) => (
<Definition
data={definition}
key={definition.name}
type={type}
index={index}
/>
),
)
) : (
<Empty
label={`No ${type.name.plural.toLowerCase()} yet`}
Logo={type.components.icon}
description={
<>
<ComponentInfo type={type} />
<br />
{requirements.length > 0 && requirements}
</>
}
/>
)}
</div>
</CollapsibleList>
<div className="w-full border-b border-circle-gray-300"></div>
</div>
);
};
export default DefinitionsContainer;