|
| 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