Skip to content

Commit a0c1204

Browse files
committed
feat: dynamic parameter form for workspace updates
Updating a workspace whose template uses dynamic parameters now works like the dashboard. The extension evaluates the new template version with the current values, and opens a form only when they fail. If the build still rejects the values, the form reopens with the server's reasons. Servers before 2.24 lack the evaluate endpoint and keep the classic prompts. The update prompts now show the new template version's message, and a failed update shows the server's error detail instead of the HTTP status. The form follows the VS Code settings editor layout and uses a new ValidationMessage component in @repo/ui that matches VS Code's input validation styles.
1 parent 7ff5019 commit a0c1204

51 files changed

Lines changed: 2032 additions & 112 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/mocks/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export * from "./parameters";
12
export * from "./tasks";
23
export * from "./workspaces";

‎packages/mocks/src/parameters.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Test factories for dynamic template parameter types.
3+
*/
4+
5+
import type {
6+
FriendlyDiagnostic,
7+
PreviewParameter,
8+
PreviewParameterOption,
9+
} from "coder/site/src/api/typesGenerated";
10+
11+
export function previewParameter(
12+
overrides: Partial<PreviewParameter> = {},
13+
): PreviewParameter {
14+
return {
15+
name: "region",
16+
display_name: "",
17+
description: "",
18+
type: "string",
19+
form_type: "input",
20+
styling: {},
21+
mutable: true,
22+
default_value: { valid: true, value: "" },
23+
value: { valid: true, value: "" },
24+
icon: "",
25+
options: [],
26+
validations: [],
27+
required: false,
28+
order: 0,
29+
ephemeral: false,
30+
diagnostics: [],
31+
...overrides,
32+
};
33+
}
34+
35+
export function parameterOption(
36+
value: string,
37+
name = value,
38+
description = "",
39+
): PreviewParameterOption {
40+
return { name, description, value: { valid: true, value }, icon: "" };
41+
}
42+
43+
export function diagnostic(
44+
summary: string,
45+
severity: FriendlyDiagnostic["severity"] = "error",
46+
): FriendlyDiagnostic {
47+
return { severity, summary, detail: "", extra: { code: "" } };
48+
}

‎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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { defineCommand, defineRequest } from "../ipc/protocol";
2+
3+
import type { DynamicParametersResponse } from "coder/site/src/api/typesGenerated";
4+
5+
export type ParameterValues = Readonly<Record<string, string>>;
6+
7+
export interface WorkspaceUpdateInit {
8+
readonly workspaceName: string;
9+
/** Omitted parameters use the server's value. */
10+
readonly values: ParameterValues;
11+
/** The server's evaluation of `values`. */
12+
readonly evaluation: DynamicParametersResponse;
13+
readonly restart: boolean;
14+
}
15+
16+
export const WorkspaceUpdateApi = {
17+
init: defineRequest<void, WorkspaceUpdateInit>("workspaceUpdate/init"),
18+
evaluate: defineRequest<ParameterValues, DynamicParametersResponse>(
19+
"workspaceUpdate/evaluate",
20+
),
21+
submit: defineCommand<ParameterValues>("workspaceUpdate/submit"),
22+
cancel: defineCommand<void>("workspaceUpdate/cancel"),
23+
} as const;
24+
25+
export function hasErrorDiagnostics({
26+
parameters,
27+
diagnostics,
28+
}: DynamicParametersResponse): boolean {
29+
return [...diagnostics, ...parameters.flatMap((p) => p.diagnostics)].some(
30+
(d) => d.severity === "error",
31+
);
32+
}
33+
34+
/** Multi-select values are stored as a JSON-encoded string array. */
35+
export function parseMultiSelectValue(raw: string): string[] | null {
36+
try {
37+
const parsed: unknown = JSON.parse(raw);
38+
return Array.isArray(parsed) && parsed.every((v) => typeof v === "string")
39+
? parsed
40+
: null;
41+
} catch {
42+
return null;
43+
}
44+
}

‎packages/ui/src/components/Field/Field.css‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
}
55

66
.ui-field {
7+
--ui-field-gap: 4px;
8+
79
display: flex;
810
flex-direction: column;
9-
gap: 4px;
11+
gap: var(--ui-field-gap);
1012
}
1113

1214
.ui-field__description {
1315
color: var(--ui-description-foreground);
1416
}
1517

18+
/* Attaches to the control above, like VS Code's input validation. */
1619
.ui-field__error {
17-
color: var(--ui-error-foreground);
20+
margin-top: calc(-1 * var(--ui-field-gap));
1821
}

‎packages/ui/src/components/Field/Field.stories.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useId, useState } from "react";
33
import { PIXEL_ALL_THEMES } from "#storybook";
44

55
import { Input } from "../Input/Input";
6+
import { ValidationMessage } from "../ValidationMessage/ValidationMessage";
67

78
import { Field } from "./Field";
89

@@ -30,7 +31,11 @@ const FieldStates = (): React.JSX.Element => {
3031
<Field
3132
label="CPU cores"
3233
htmlFor={coresId}
33-
error="Value must be between 1 and 16."
34+
error={
35+
<ValidationMessage severity="error">
36+
Value must be between 1 and 16.
37+
</ValidationMessage>
38+
}
3439
errorId={`${coresId}-error`}
3540
>
3641
<Input

‎packages/ui/src/components/Field/Field.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface FieldProps extends ComponentProps<"div"> {
1414
description?: ReactNode;
1515
/** ID for the description; associate it with the control via aria-describedby. */
1616
descriptionId?: string;
17+
/** Usually one or more `ValidationMessage`s. */
1718
error?: ReactNode;
1819
/** ID for the error; the consumer owns aria-describedby and aria-invalid. */
1920
errorId?: string;
@@ -22,7 +23,7 @@ export interface FieldProps extends ComponentProps<"div"> {
2223
}
2324

2425
/* Lays out a labelled control like a settings-editor entry: semibold label,
25-
control, then muted description or error text. */
26+
muted description, control, then validation messages flush below it. */
2627
export function Field({
2728
label,
2829
htmlFor,
@@ -37,12 +38,12 @@ export function Field({
3738
return (
3839
<div {...props} className={cx("ui-field", className)}>
3940
{label !== undefined && <Label htmlFor={htmlFor}>{label}</Label>}
40-
{children}
4141
{description !== undefined && (
4242
<div id={descriptionId} className="ui-field__description">
4343
{description}
4444
</div>
4545
)}
46+
{children}
4647
{error !== undefined && (
4748
<div id={errorId} className="ui-field__error">
4849
{error}

‎packages/ui/src/components/Select/Select.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
outline: 0;
1515
}
1616

17+
.ui-select__trigger[aria-invalid="true"] {
18+
border-color: var(--ui-input-validation-error-border);
19+
}
20+
1721
.ui-select__trigger:focus {
1822
border-color: var(--ui-focus-border);
1923
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Matches VS Code's .monaco-inputbox-message. */
2+
.ui-validation-message {
3+
box-sizing: border-box;
4+
margin-top: -1px;
5+
padding: 0.4em;
6+
color: var(--ui-validation-foreground);
7+
background: var(--ui-validation-background);
8+
border: 1px solid var(--ui-validation-border);
9+
font-size: 12px;
10+
line-height: 17px;
11+
overflow-wrap: break-word;
12+
}
13+
14+
.ui-validation-message--info {
15+
--ui-validation-foreground: var(--ui-input-validation-info-foreground);
16+
--ui-validation-background: var(--ui-input-validation-info-background);
17+
--ui-validation-border: var(--ui-input-validation-info-border);
18+
}
19+
20+
.ui-validation-message--warning {
21+
--ui-validation-foreground: var(--ui-input-validation-warning-foreground);
22+
--ui-validation-background: var(--ui-input-validation-warning-background);
23+
--ui-validation-border: var(--ui-input-validation-warning-border);
24+
}
25+
26+
.ui-validation-message--error {
27+
--ui-validation-foreground: var(--ui-input-validation-error-foreground);
28+
--ui-validation-background: var(--ui-input-validation-error-background);
29+
--ui-validation-border: var(--ui-input-validation-error-border);
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { PIXEL_ALL_THEMES } from "#storybook";
2+
3+
import { Input } from "../Input/Input";
4+
5+
import {
6+
ValidationMessage,
7+
type ValidationSeverity,
8+
} from "./ValidationMessage";
9+
10+
import type { Meta, StoryObj } from "@storybook/react-vite";
11+
12+
const MESSAGES: ReadonlyArray<{
13+
severity: ValidationSeverity;
14+
value: string;
15+
message: string;
16+
}> = [
17+
{ severity: "info", value: "dev", message: "Names are case-sensitive." },
18+
{
19+
severity: "warning",
20+
value: "4",
21+
message: "Fewer than 8 cores may slow down builds.",
22+
},
23+
{
24+
severity: "error",
25+
value: "32",
26+
message: "Value must be between 1 and 16.",
27+
},
28+
];
29+
30+
const Messages = (): React.JSX.Element => (
31+
<div style={{ display: "grid", gap: "16px", width: "260px" }}>
32+
{MESSAGES.map(({ severity, value, message }) => (
33+
<div key={severity}>
34+
<Input
35+
aria-label={severity}
36+
aria-invalid={severity === "error" || undefined}
37+
value={value}
38+
onChange={() => undefined}
39+
/>
40+
<ValidationMessage severity={severity}>{message}</ValidationMessage>
41+
</div>
42+
))}
43+
</div>
44+
);
45+
46+
const meta: Meta<typeof Messages> = {
47+
title: "UI/ValidationMessage",
48+
component: Messages,
49+
parameters: { pixel: PIXEL_ALL_THEMES },
50+
};
51+
export default meta;
52+
type Story = StoryObj<typeof Messages>;
53+
54+
export const Severities: Story = {};

0 commit comments

Comments
 (0)