Skip to content

Commit 3d01957

Browse files
committed
feat: snippets are aware of existing order and names
1 parent a5145a0 commit 3d01957

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

src/client/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export const App = () => {
195195

196196
<ResizablePanelGroup direction={"horizontal"}>
197197
{/* EDITOR */}
198-
<Editor code={code} setCode={setCode} />
198+
<Editor code={code} setCode={setCode} parameters={parameters} />
199199

200200
<ResizableHandle className="bg-surface-quaternary" />
201201

src/client/Editor.tsx

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
TooltipTrigger,
1515
} from "@/client/components/Tooltip";
1616
import { useTheme } from "@/client/contexts/theme";
17-
import { snippets } from "@/client/snippets";
17+
import { snippets, type SnippetFunc } from "@/client/snippets";
1818
import { cn } from "@/utils/cn";
1919
import { Editor as MonacoEditor } from "@monaco-editor/react";
2020
import {
@@ -27,28 +27,45 @@ import {
2727
} from "lucide-react";
2828
import { type FC, useEffect, useRef, useState } from "react";
2929
import { useEditor } from "@/client/contexts/editor";
30+
import type { ParameterWithSource } from "@/gen/types";
3031

3132
type EditorProps = {
3233
code: string;
3334
setCode: React.Dispatch<React.SetStateAction<string>>;
35+
parameters: ParameterWithSource[];
3436
};
3537

36-
export const Editor: FC<EditorProps> = ({ code, setCode }) => {
38+
export const Editor: FC<EditorProps> = ({ code, setCode, parameters }) => {
3739
const { appliedTheme } = useTheme();
3840
const editorRef = useEditor();
3941

42+
const [tab, setTab] = useState(() => "code");
43+
4044
const [codeCopied, setCodeCopied] = useState(() => false);
4145
const copyTimeoutId = useRef<ReturnType<typeof setTimeout> | undefined>(
4246
undefined,
4347
);
4448

45-
const [tab, setTab] = useState(() => "code");
46-
4749
const onCopy = () => {
4850
navigator.clipboard.writeText(code);
4951
setCodeCopied(() => true);
5052
};
5153

54+
const onAddSnippet = (name: string, snippet: SnippetFunc) => {
55+
const nextInOrder =
56+
parameters.reduce(
57+
(highestOrder, parameter) =>
58+
highestOrder < parameter.order ? parameter.order : highestOrder,
59+
0,
60+
) + 1;
61+
62+
const nameCount = parameters.filter((p) => p.name.startsWith(name)).length;
63+
64+
setCode(
65+
`${code.trimEnd()}\n\n${snippet(nameCount > 0 ? `${name}-${nameCount}` : name, nextInOrder)}\n`,
66+
);
67+
};
68+
5269
useEffect(() => {
5370
if (!codeCopied) {
5471
return;
@@ -99,17 +116,20 @@ export const Editor: FC<EditorProps> = ({ code, setCode }) => {
99116

100117
<DropdownMenuPortal>
101118
<DropdownMenuContent align="start">
102-
{snippets.map(({ label, icon: Icon, snippet }, index) => (
103-
<DropdownMenuItem
104-
key={index}
105-
onClick={() =>
106-
setCode(`${code.trimEnd()}\n\n${snippet()}\n`)
107-
}
108-
>
109-
<Icon size={24} />
110-
{label}
111-
</DropdownMenuItem>
112-
))}
119+
{snippets.map(
120+
({ name, label, icon: Icon, snippet }, index) => (
121+
<DropdownMenuItem
122+
key={index}
123+
onClick={() =>
124+
// setCode(`${code.trimEnd()}\n\n${snippet()}\n`)
125+
onAddSnippet(name, snippet)
126+
}
127+
>
128+
<Icon size={24} />
129+
{label}
130+
</DropdownMenuItem>
131+
),
132+
)}
113133
</DropdownMenuContent>
114134
</DropdownMenuPortal>
115135
</DropdownMenu>

src/client/snippets.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ export const defaultCode = `terraform {
1818
}
1919
}`;
2020

21-
type SnippetFunc = (name?: string, order?: number) => string;
21+
export type SnippetFunc = (name?: string, order?: number) => string;
2222
type Snippet = {
23+
name: string;
2324
label: string;
2425
icon: typeof RadioIcon;
2526
snippet: SnippetFunc;
@@ -210,41 +211,49 @@ export const slider: SnippetFunc = (
210211

211212
export const snippets: Snippet[] = [
212213
{
214+
name: "text-input",
213215
label: "Text Input",
214216
icon: TextCursorInputIcon,
215217
snippet: input,
216218
},
217219
{
220+
name: "textarea",
218221
label: "Textarea",
219222
icon: LetterTextIcon,
220223
snippet: textarea,
221224
},
222225
{
226+
name: "radio",
223227
label: "Radio",
224228
icon: RadioIcon,
225229
snippet: radio,
226230
},
227231
{
232+
name: "switch",
228233
label: "Multi-select",
229234
icon: SquareMousePointerIcon,
230235
snippet: multiSelect,
231236
},
232237
{
238+
name: "tag-select",
233239
label: "Tag-select",
234240
icon: TagIcon,
235241
snippet: tagSelect,
236242
},
237243
{
244+
name: "switch",
238245
label: "Switch",
239246
icon: ToggleLeftIcon,
240247
snippet: switchInput,
241248
},
242249
{
250+
name: "dropdown",
243251
label: "Dropdown",
244252
icon: ChevronDownIcon,
245253
snippet: dropdown,
246254
},
247255
{
256+
name: "slider",
248257
label: "Slider",
249258
icon: Settings2Icon,
250259
snippet: slider,

0 commit comments

Comments
 (0)