Skip to content

Commit 509f683

Browse files
committed
refactor: collect dynamic update parameters in a thin webview form
The form now only collects parameters, as a drop-in replacement for collectUpdateParameters, so dynamic-parameter templates reuse the existing updateWorkspace flow (one-build update, telemetry, build logs) instead of a second implementation. The webview evaluates through typed IPC requests with React Query, persists values across hides, and uses the shared UI controls.
1 parent 3eb7f66 commit 509f683

31 files changed

Lines changed: 1006 additions & 2537 deletions
Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
1-
import { defineCommand, defineNotification } from "../ipc/protocol";
1+
import { defineCommand, defineRequest } from "../ipc/protocol";
22

3-
import type {
4-
PreviewParameter,
5-
FriendlyDiagnostic,
6-
} from "coder/site/src/api/typesGenerated";
3+
import type { DynamicParametersResponse } from "coder/site/src/api/typesGenerated";
74

8-
export interface WorkspaceUpdateState {
5+
export type ParameterValues = Readonly<Record<string, string>>;
6+
7+
export interface WorkspaceUpdateInit {
98
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;
9+
/** The previous build's values, minus ephemeral ones. */
10+
readonly values: ParameterValues;
2111
}
2212

2313
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",
14+
init: defineRequest<void, WorkspaceUpdateInit>("workspaceUpdate/init"),
15+
evaluate: defineRequest<ParameterValues, DynamicParametersResponse>(
16+
"workspaceUpdate/evaluate",
3017
),
31-
retry: defineCommand<void>("workspaceUpdate/retry"),
32-
submit: defineCommand<{ revision: number }>("workspaceUpdate/submit"),
18+
submit: defineCommand<ParameterValues>("workspaceUpdate/submit"),
3319
cancel: defineCommand<void>("workspaceUpdate/cancel"),
3420
} as const;

‎packages/workspace-update/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@repo/shared": "workspace:*",
1414
"@repo/ui": "workspace:*",
1515
"@repo/webview-shared": "workspace:*",
16+
"@tanstack/react-query": "catalog:",
1617
"react": "catalog:",
1718
"react-dom": "catalog:"
1819
},
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1+
import { ErrorState, LoadingState } from "@repo/ui";
2+
import { useQuery } from "@tanstack/react-query";
3+
14
import { WorkspaceUpdateForm } from "./components/WorkspaceUpdateForm";
2-
import { useWorkspaceUpdate } from "./hooks/useWorkspaceUpdate";
5+
import { useWorkspaceUpdateApi } from "./hooks/useWorkspaceUpdateApi";
36

47
export default function App(): React.JSX.Element {
5-
const { api, state } = useWorkspaceUpdate();
8+
const api = useWorkspaceUpdateApi();
9+
const init = useQuery({ queryKey: ["init"], queryFn: () => api.init() });
610

7-
if (!state) {
11+
if (init.isPending) {
12+
return <LoadingState label="Loading parameters" />;
13+
}
14+
if (init.isError) {
815
return (
9-
<div className="workspace-update-app__loading">
10-
Loading workspace update…
11-
</div>
16+
<ErrorState
17+
title="Could not load workspace parameters"
18+
description={init.error.message}
19+
onRetry={() => void init.refetch()}
20+
/>
1221
);
1322
}
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-
);
23+
return <WorkspaceUpdateForm init={init.data} />;
2424
}
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
import {
2+
Checkbox,
3+
Field,
4+
Input,
5+
PasswordInput,
6+
Select,
7+
SelectContent,
8+
SelectItem,
9+
SelectTrigger,
10+
SelectValue,
11+
Textarea,
12+
} from "@repo/ui";
13+
import { useId } from "react";
14+
15+
import type {
16+
FriendlyDiagnostic,
17+
PreviewParameter,
18+
} from "coder/site/src/api/typesGenerated";
19+
20+
interface ParameterFieldProps {
21+
parameter: PreviewParameter;
22+
value: string;
23+
disabled: boolean;
24+
onChange: (value: string) => void;
25+
}
26+
27+
interface ParameterControlProps extends ParameterFieldProps {
28+
id: string;
29+
label: string;
30+
describedBy: string | undefined;
31+
invalid: boolean;
32+
}
33+
34+
export function formatDiagnostics(
35+
diagnostics: readonly FriendlyDiagnostic[],
36+
): string | undefined {
37+
const text = diagnostics
38+
.map((d) => [d.summary, d.detail].filter(Boolean).join(": "))
39+
.join(" ");
40+
return text || undefined;
41+
}
42+
43+
export function ParameterField(props: ParameterFieldProps): React.JSX.Element {
44+
const { parameter } = props;
45+
const id = useId();
46+
const label =
47+
parameter.styling.label || parameter.display_name || parameter.name;
48+
const error = formatDiagnostics(
49+
parameter.diagnostics.filter((d) => d.severity === "error"),
50+
);
51+
const description =
52+
[
53+
parameter.description,
54+
!parameter.mutable && "This value cannot be changed after the update.",
55+
parameter.ephemeral && "This value resets on the next build.",
56+
]
57+
.filter(Boolean)
58+
.join(" ") || undefined;
59+
const descriptionId = `${id}-description`;
60+
const errorId = `${id}-error`;
61+
const isCheckbox =
62+
parameter.form_type === "checkbox" || parameter.form_type === "switch";
63+
64+
return (
65+
<Field
66+
label={
67+
isCheckbox ? undefined : `${label}${parameter.required ? " *" : ""}`
68+
}
69+
htmlFor={id}
70+
description={description}
71+
descriptionId={descriptionId}
72+
error={error}
73+
errorId={errorId}
74+
>
75+
<ParameterControl
76+
{...props}
77+
id={id}
78+
label={label}
79+
describedBy={
80+
[description && descriptionId, error && errorId]
81+
.filter(Boolean)
82+
.join(" ") || undefined
83+
}
84+
invalid={error !== undefined}
85+
/>
86+
</Field>
87+
);
88+
}
89+
90+
function ParameterControl({
91+
parameter,
92+
value,
93+
disabled,
94+
onChange,
95+
id,
96+
label,
97+
describedBy,
98+
invalid,
99+
}: ParameterControlProps): React.JSX.Element | null {
100+
const control = {
101+
id,
102+
disabled,
103+
"aria-describedby": describedBy,
104+
"aria-invalid": invalid || undefined,
105+
};
106+
const options = parameter.options.map((option) => ({
107+
value: option.value.value,
108+
label: option.name || option.value.value,
109+
description: option.description || undefined,
110+
}));
111+
112+
switch (parameter.form_type) {
113+
case "error":
114+
return null;
115+
case "checkbox":
116+
case "switch":
117+
return (
118+
<Checkbox
119+
{...control}
120+
checked={value === "true"}
121+
onChange={(checked) => onChange(String(checked))}
122+
>
123+
{label}
124+
</Checkbox>
125+
);
126+
case "dropdown":
127+
case "radio":
128+
return (
129+
<Select
130+
items={options}
131+
value={value || null}
132+
onValueChange={(next) => onChange(next ?? "")}
133+
disabled={disabled}
134+
>
135+
<SelectTrigger {...control} aria-label={label}>
136+
<SelectValue placeholder="Select an option" />
137+
</SelectTrigger>
138+
<SelectContent>
139+
{options.map((option) => (
140+
<SelectItem
141+
key={option.value}
142+
value={option.value}
143+
description={option.description}
144+
>
145+
{option.label}
146+
</SelectItem>
147+
))}
148+
</SelectContent>
149+
</Select>
150+
);
151+
case "multi-select": {
152+
const selected = parseList(value);
153+
return (
154+
<div role="group" aria-label={label} className="parameter-options">
155+
{options.map((option) => (
156+
<Checkbox
157+
key={option.value}
158+
disabled={disabled}
159+
checked={selected.includes(option.value)}
160+
onChange={(checked) =>
161+
onChange(
162+
JSON.stringify(
163+
checked
164+
? [...selected, option.value]
165+
: selected.filter((v) => v !== option.value),
166+
),
167+
)
168+
}
169+
>
170+
{option.label}
171+
</Checkbox>
172+
))}
173+
</div>
174+
);
175+
}
176+
case "textarea":
177+
return (
178+
<Textarea
179+
{...control}
180+
value={value}
181+
onChange={onChange}
182+
placeholder={parameter.styling.placeholder}
183+
/>
184+
);
185+
case "":
186+
case "input":
187+
case "slider":
188+
case "tag-select":
189+
if (parameter.styling.mask_input) {
190+
return (
191+
<PasswordInput
192+
{...control}
193+
value={value}
194+
onChange={onChange}
195+
placeholder={parameter.styling.placeholder}
196+
/>
197+
);
198+
}
199+
return (
200+
<Input
201+
{...control}
202+
type={parameter.type === "number" ? "number" : "text"}
203+
value={value}
204+
onChange={onChange}
205+
placeholder={
206+
parameter.styling.placeholder ||
207+
(parameter.form_type === "tag-select"
208+
? '["value", "another value"]'
209+
: undefined)
210+
}
211+
/>
212+
);
213+
}
214+
}
215+
216+
function parseList(value: string): string[] {
217+
try {
218+
const parsed: unknown = JSON.parse(value);
219+
return Array.isArray(parsed) ? parsed.map(String) : [];
220+
} catch {
221+
return [];
222+
}
223+
}

0 commit comments

Comments
 (0)