Skip to content

feat: add snippets for all form types and add docs links #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export const App = () => {
Coder
</a>
<a
href="https://coder.com"
href="https://coder.com/docs/admin/templates/extending-templates/parameters"
target="_blank"
rel="noreferrer"
className="font-light text-content-secondary text-sm hover:text-content-primary"
Expand All @@ -195,7 +195,7 @@ export const App = () => {

<ResizablePanelGroup direction={"horizontal"}>
{/* EDITOR */}
<Editor code={code} setCode={setCode} />
<Editor code={code} setCode={setCode} parameters={parameters} />

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

Expand Down
66 changes: 30 additions & 36 deletions src/client/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,56 @@ import {
TooltipTrigger,
} from "@/client/components/Tooltip";
import { useTheme } from "@/client/contexts/theme";
import { multiSelect, radio, switchInput, textInput } from "@/client/snippets";
import type { ParameterFormType } from "@/gen/types";
import { snippets, type SnippetFunc } from "@/client/snippets";
import { cn } from "@/utils/cn";
import { Editor as MonacoEditor } from "@monaco-editor/react";
import {
CheckIcon,
ChevronDownIcon,
CopyIcon,
FileJsonIcon,
RadioIcon,
SettingsIcon,
SquareMousePointerIcon,
TextCursorInputIcon,
ToggleLeftIcon,
ZapIcon,
} from "lucide-react";
import { type FC, useEffect, useRef, useState } from "react";
import { useEditor } from "@/client/contexts/editor";
import type { ParameterWithSource } from "@/gen/types";

type EditorProps = {
code: string;
setCode: React.Dispatch<React.SetStateAction<string>>;
parameters: ParameterWithSource[];
};

export const Editor: FC<EditorProps> = ({ code, setCode }) => {
export const Editor: FC<EditorProps> = ({ code, setCode, parameters }) => {
const { appliedTheme } = useTheme();
const editorRef = useEditor();

const [tab, setTab] = useState(() => "code");

const [codeCopied, setCodeCopied] = useState(() => false);
const copyTimeoutId = useRef<ReturnType<typeof setTimeout> | undefined>(
undefined,
);

const [tab, setTab] = useState(() => "code");

const onCopy = () => {
navigator.clipboard.writeText(code);
setCodeCopied(() => true);
};

const onAddSnippet = (formType: ParameterFormType) => {
if (formType === "input") {
setCode(`${code.trimEnd()}\n\n${textInput}\n`);
} else if (formType === "radio") {
setCode(`${code.trimEnd()}\n\n${radio}\n`);
} else if (formType === "multi-select") {
setCode(`${code.trimEnd()}\n\n${multiSelect}\n`);
} else if (formType === "switch") {
setCode(`${code.trimEnd()}\n\n${switchInput}\n`);
}
const onAddSnippet = (name: string, snippet: SnippetFunc) => {
const nextInOrder =
parameters.reduce(
(highestOrder, parameter) =>
highestOrder < parameter.order ? parameter.order : highestOrder,
0,
) + 1;

const nameCount = parameters.filter((p) => p.name.startsWith(name)).length;

setCode(
`${code.trimEnd()}\n\n${snippet(nameCount > 0 ? `${name}-${nameCount}` : name, nextInOrder)}\n`,
);
};

useEffect(() => {
Expand Down Expand Up @@ -116,23 +116,17 @@ export const Editor: FC<EditorProps> = ({ code, setCode }) => {

<DropdownMenuPortal>
<DropdownMenuContent align="start">
<DropdownMenuItem onClick={() => onAddSnippet("input")}>
<TextCursorInputIcon width={24} height={24} />
Text input
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => onAddSnippet("multi-select")}
>
<SquareMousePointerIcon width={24} height={24} />
Multi-select
</DropdownMenuItem>
<DropdownMenuItem onClick={() => onAddSnippet("radio")}>
<RadioIcon width={24} height={24} />
Radio
</DropdownMenuItem>
<DropdownMenuItem onClick={() => onAddSnippet("switch")}>
<ToggleLeftIcon width={24} height={24} /> Switches
</DropdownMenuItem>
{snippets.map(
({ name, label, icon: Icon, snippet }, index) => (
<DropdownMenuItem
key={index}
onClick={() => onAddSnippet(name, snippet)}
>
<Icon size={24} />
{label}
</DropdownMenuItem>
),
)}
</DropdownMenuContent>
</DropdownMenuPortal>
</DropdownMenu>
Expand Down
4 changes: 3 additions & 1 deletion src/client/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ const PreviewEmptyState = () => {
</p>
</div>
<a
href="#todo"
href="https://coder.com/docs/admin/templates/extending-templates/parameters"
target="_blank"
rel="noreferrer"
className="flex items-center gap-0.5 text-content-link text-sm"
>
Read the docs
Expand Down
Loading