-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathpanel-selection.tsx
More file actions
205 lines (191 loc) · 6.75 KB
/
Copy pathpanel-selection.tsx
File metadata and controls
205 lines (191 loc) · 6.75 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
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
/**
* Copyright (c) 2022—present Michael Dougall. All rights reserved.
*
* This repository utilizes multiple licenses across different directories. To
* see this files license find the nearest LICENSE file up the source tree.
*/
import { EraserIcon } from "@radix-ui/react-icons";
import { send } from "@triplex/bridge/host";
import { PropInput } from "@triplex/ux/inputs";
import { useEffect, useReducer } from "react";
import * as Accordion from "../../components/accordion";
import { IconButton } from "../../components/button";
import { useLazySubscription } from "../../hooks/ws";
import { useFilter } from "../../stores/filter-props";
import { sendVSCE } from "../../util/bridge";
import { useSceneSelected } from "../app-root/context";
import { type SceneSelected } from "../app-root/types";
import { propsByGroup } from "../element-props/group";
import { renderPropInputs } from "./inputs";
export function SelectionPanel() {
const selected = useSceneSelected();
return selected ? <SelectionPanelLoadable selected={selected} /> : null;
}
function LearnToUseCTA({ exportName }: { exportName: string }) {
return (
<div className="flex flex-col gap-2 px-4 py-3">
<strong>Component Controls</strong>
<span>
{`Props declared on ${exportName} will appear here that can be temporarily
set.`}
</span>
<a
className="text-link focus-visible:outline-selected focus:outline-[transparent]"
href="https://triplex.dev/docs/building-your-scene/scene/component-controls"
>
Learn how to use this feature.
</a>
</div>
);
}
function NoPropsCTA() {
return (
<div className="flex flex-col gap-3 px-4 py-3">
<span className="italic">This element has no props.</span>
<span>
Unexpected? Make sure to check the element is typed.{" "}
<a
className="text-link underline"
href="https://triplex.dev/resources/check-element-types"
>
Learn more
</a>
.
</span>
</div>
);
}
function SelectionPanelLoadable({ selected }: { selected: SceneSelected }) {
const filter = useFilter((state) => state.filter);
const setFilter = useFilter((state) => state.set);
const isPropsForComponent = "exportName" in selected;
const formKey = isPropsForComponent
? `${selected.path}${selected.exportName}`
: `${selected.path}${selected.column}${selected.line}`;
const [resetKey, resetForm] = useReducer((val: number) => val + 1, 0);
const props = useLazySubscription(
isPropsForComponent
? "/scene/:path/:exportName/props"
: selected.astPath
? "/scene/:path/object/:astPath"
: "/scene/:path/object/:line/:column",
selected,
);
const hasProps = !!props && props.props.length > 0;
const groupsDef = useLazySubscription("/prop-groups-def");
const groupedProps = propsByGroup(props?.props || [], {
expandAll: isPropsForComponent,
groupsDef,
});
useEffect(() => {
if (isPropsForComponent) {
return;
}
send("element-focused-props", {
props: props?.props.map((props) => props.name) || [],
});
return () => {
send("element-focused-props", { props: [] });
};
}, [isPropsForComponent, props?.props]);
return (
<>
{hasProps && (
<div className="flex gap-1 p-1.5">
<input
className="text-input focus:border-selected bg-input border-input placeholder:text-input-placeholder h-[26px] w-full rounded-sm border px-[9px] focus:outline-none"
onChange={(e) => {
setFilter(e.currentTarget.value);
}}
onFocus={(e) => e.stopPropagation()}
placeholder="Filter props..."
value={filter}
/>
{isPropsForComponent && (
<IconButton
actionId="contextpanel_provider_reset"
icon={EraserIcon}
label="Reset Component Props"
onClick={() => {
props.props.forEach((prop) => {
send("request-reset-prop", {
astPath: selected.astPath,
column: -1,
line: -1,
path: selected.path,
propName: prop.name,
});
});
resetForm();
}}
/>
)}
</div>
)}
{!hasProps && isPropsForComponent && (
<LearnToUseCTA exportName={selected.exportName} />
)}
{!hasProps && !isPropsForComponent && <NoPropsCTA />}
<div className="flex flex-col px-1.5" key={`${resetKey}${formKey}`}>
{groupedProps.map((group) => (
<Accordion.Root
defaultExpanded={group.defaultExpanded}
key={group.name}
>
<Accordion.Trigger actionId="scenepanel_element_togglepropgroup">
{group.name}
</Accordion.Trigger>
<Accordion.Content forcedExpanded={!!filter}>
{group.props.map((prop) => {
if (
!prop.name.toLowerCase().includes(filter?.toLowerCase() || "")
) {
return null;
}
const fieldKey = isPropsForComponent
? `${prop.name}${selected.exportName}`
: `${prop.name}${selected.column}${selected.line}`;
return (
<PropInput
key={fieldKey}
onChange={(propValue) => {
send("request-set-element-prop", {
...(isPropsForComponent
? {
astPath: selected.astPath,
column: -1,
line: -1,
path: selected.path,
}
: selected),
propName: prop.name,
propValue,
});
}}
onConfirm={(propValue) => {
if (isPropsForComponent) {
// Bail out as this isn't a persistent element.
return;
}
sendVSCE("element-set-prop", {
...selected,
propName: prop.name,
propValue,
});
}}
path={
isPropsForComponent ? selected.path : selected.parentPath
}
prop={prop}
>
{renderPropInputs}
</PropInput>
);
})}
</Accordion.Content>
</Accordion.Root>
))}
</div>
</>
);
}