Skip to content

Commit 8c9fb00

Browse files
committed
feat: add dynamic workspace update parameter editor
1 parent 96dd908 commit 8c9fb00

30 files changed

Lines changed: 2797 additions & 25 deletions

‎packages/shared/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ export type {
3333
// Workspaces types and API
3434
export * from "./workspaces/types";
3535
export { WorkspacesApi } from "./workspaces/api";
36+
37+
export * from "./workspaceUpdate/api";
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { defineCommand, defineNotification } from "../ipc/protocol";
2+
3+
import type {
4+
PreviewParameter,
5+
FriendlyDiagnostic,
6+
} from "coder/site/src/api/typesGenerated";
7+
8+
export interface WorkspaceUpdateState {
9+
readonly workspaceName: string;
10+
readonly templateVersionId: string;
11+
readonly revision: number;
12+
readonly status:
13+
"loading" | "evaluating" | "ready" | "submitting" | "success" | "error";
14+
readonly parameters: readonly PreviewParameter[];
15+
readonly inputs: Readonly<Record<string, string>>;
16+
readonly locked: readonly string[];
17+
readonly diagnostics: readonly FriendlyDiagnostic[];
18+
readonly error?: string;
19+
readonly canSubmit: boolean;
20+
readonly canRetry?: boolean;
21+
}
22+
23+
export const WorkspaceUpdateApi = {
24+
stateChanged: defineNotification<WorkspaceUpdateState>(
25+
"workspaceUpdate/stateChanged",
26+
),
27+
ready: defineCommand<void>("workspaceUpdate/ready"),
28+
change: defineCommand<{ name: string; value: string }>(
29+
"workspaceUpdate/change",
30+
),
31+
retry: defineCommand<void>("workspaceUpdate/retry"),
32+
submit: defineCommand<{ revision: number }>("workspaceUpdate/submit"),
33+
cancel: defineCommand<void>("workspaceUpdate/cancel"),
34+
} as const;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@repo/workspace-update",
3+
"version": "1.0.0",
4+
"description": "Coder workspace update parameter form webview",
5+
"private": true,
6+
"type": "module",
7+
"scripts": {
8+
"build": "vite build",
9+
"dev": "vite build --watch",
10+
"typecheck": "tsc --noEmit"
11+
},
12+
"dependencies": {
13+
"@repo/shared": "workspace:*",
14+
"@repo/ui": "workspace:*",
15+
"@repo/webview-shared": "workspace:*",
16+
"react": "catalog:",
17+
"react-dom": "catalog:"
18+
},
19+
"devDependencies": {
20+
"@repo/storybook-utils": "workspace:*",
21+
"@rolldown/plugin-babel": "catalog:",
22+
"@types/react": "catalog:",
23+
"@types/react-dom": "catalog:",
24+
"@vitejs/plugin-react": "catalog:",
25+
"babel-plugin-react-compiler": "catalog:",
26+
"typescript": "catalog:",
27+
"vite": "catalog:"
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { WorkspaceUpdateForm } from "./components/WorkspaceUpdateForm";
2+
import { useWorkspaceUpdate } from "./hooks/useWorkspaceUpdate";
3+
4+
export default function App(): React.JSX.Element {
5+
const { api, state } = useWorkspaceUpdate();
6+
7+
if (!state) {
8+
return (
9+
<div className="workspace-update-app__loading">
10+
Loading workspace update…
11+
</div>
12+
);
13+
}
14+
15+
return (
16+
<WorkspaceUpdateForm
17+
state={state}
18+
onChange={api.change}
19+
onCancel={api.cancel}
20+
onRetry={api.retry}
21+
onSubmit={(revision) => api.submit({ revision })}
22+
/>
23+
);
24+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { useState } from "react";
2+
3+
import { WorkspaceUpdateForm } from "./WorkspaceUpdateForm";
4+
5+
import type { WorkspaceUpdateState } from "@repo/shared";
6+
import type { Meta, StoryObj } from "@storybook/react-vite";
7+
8+
const parameter = (
9+
name: string,
10+
formType: WorkspaceUpdateState["parameters"][number]["form_type"],
11+
options: ReadonlyArray<{
12+
name: string;
13+
value: string;
14+
description?: string;
15+
}> = [],
16+
): WorkspaceUpdateState["parameters"][number] => ({
17+
name,
18+
display_name: name,
19+
description: `Configure ${name}.`,
20+
type: formType === "slider" ? "number" : "string",
21+
form_type: formType,
22+
styling: {},
23+
mutable: true,
24+
default_value: { valid: false, value: "" },
25+
value: { valid: false, value: "" },
26+
icon: "",
27+
options: options.map((option) => ({
28+
name: option.name,
29+
description: option.description ?? "",
30+
value: { valid: true, value: option.value },
31+
icon: "",
32+
})),
33+
validations: [],
34+
required: false,
35+
order: 0,
36+
ephemeral: false,
37+
diagnostics: [],
38+
});
39+
40+
const state: WorkspaceUpdateState = {
41+
workspaceName: "dev",
42+
templateVersionId: "template-version-1",
43+
revision: 3,
44+
status: "ready",
45+
inputs: {
46+
region: "us-east",
47+
autoscale: "true",
48+
cores: "4",
49+
tags: '["team,blue", "staging"]',
50+
features: '["metrics"]',
51+
},
52+
locked: ["locked"],
53+
diagnostics: [],
54+
canSubmit: true,
55+
parameters: [
56+
parameter("region", "radio", [
57+
{ name: "US East", value: "us-east" },
58+
{ name: "Europe", value: "eu-west", description: "Frankfurt" },
59+
]),
60+
parameter("autoscale", "switch"),
61+
parameter("cores", "slider"),
62+
parameter("notes", "textarea"),
63+
parameter("features", "multi-select", [
64+
{ name: "Metrics", value: "metrics" },
65+
{ name: "Tracing", value: "tracing" },
66+
]),
67+
parameter("tags", "tag-select"),
68+
{ ...parameter("token", "input"), styling: { mask_input: true } },
69+
{ ...parameter("locked", "input"), mutable: false },
70+
],
71+
};
72+
73+
function InteractiveForm(): React.JSX.Element {
74+
const [inputs, setInputs] = useState(state.inputs);
75+
return (
76+
<WorkspaceUpdateForm
77+
state={{ ...state, inputs }}
78+
onChange={({ name, value }) =>
79+
setInputs((current) => ({ ...current, [name]: value }))
80+
}
81+
onCancel={() => undefined}
82+
onRetry={() => undefined}
83+
onSubmit={() => undefined}
84+
/>
85+
);
86+
}
87+
88+
const meta: Meta<typeof InteractiveForm> = {
89+
title: "Workspace Update/Form",
90+
component: InteractiveForm,
91+
};
92+
93+
export default meta;
94+
type Story = StoryObj<typeof InteractiveForm>;
95+
96+
export const Ready: Story = {};
97+
98+
export const Evaluating: Story = {
99+
render: () => (
100+
<WorkspaceUpdateForm
101+
state={{ ...state, status: "evaluating" }}
102+
onChange={() => undefined}
103+
onCancel={() => undefined}
104+
onRetry={() => undefined}
105+
onSubmit={() => undefined}
106+
/>
107+
),
108+
};
109+
110+
export const Error: Story = {
111+
render: () => (
112+
<WorkspaceUpdateForm
113+
state={{
114+
...state,
115+
status: "error",
116+
parameters: [],
117+
error: "The template is unavailable.",
118+
}}
119+
onChange={() => undefined}
120+
onCancel={() => undefined}
121+
onRetry={() => undefined}
122+
onSubmit={() => undefined}
123+
/>
124+
),
125+
};

0 commit comments

Comments
 (0)