Skip to content

Commit d8c367b

Browse files
committed
chore: move default code to it's own tf file
1 parent 6359483 commit d8c367b

File tree

3 files changed

+151
-147
lines changed

3 files changed

+151
-147
lines changed

src/examples/code/default.tf

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
version = "2.5.3"
6+
}
7+
}
8+
}
9+
10+
# This example shows new form types and conditionality using dynamic parameters.
11+
# When starting from scratch, retain the providers block above.
12+
#
13+
# In the playground, you can get started with:
14+
# - snippets for parameter types
15+
# - example forms for advanced use
16+
# - documentation for how and when to enable Dynamic Parameters.
17+
#
18+
# You can edit user data in the "users" tab, and share your form via URL.
19+
# Enjoy Dynamic Parameters!
20+
21+
locals {
22+
ides = [
23+
{
24+
name = "VSCode",
25+
value = "vscode",
26+
icon = "/icon/code.svg"
27+
},
28+
{
29+
name = "Jetbrains IntelliJ",
30+
value = "intellij",
31+
icon = "/icon/intellij.svg"
32+
},
33+
{
34+
name = "Cursor",
35+
value = "cursor",
36+
icon = "/icon/cursor.svg"
37+
},
38+
]
39+
40+
username = data.coder_workspace_owner.me.name
41+
is_admin = contains(data.coder_workspace_owner.me.groups, "admin")
42+
}
43+
44+
data "coder_workspace_owner" "me" {}
45+
46+
data "coder_parameter" "dropdown_picker" {
47+
name = "dropdown_picker"
48+
display_name = "Pick your next parameter!"
49+
description = "Hello ${username}, pick your next parameter using this `dropdown` parameter."
50+
form_type = "dropdown"
51+
mutable = true
52+
53+
option {
54+
value = "ide_picker"
55+
name = "IDE multi-select"
56+
}
57+
58+
option {
59+
value = "text_area"
60+
name = "Large text entry"
61+
}
62+
63+
option {
64+
value = "cpu_slider"
65+
name = "CPU slider"
66+
}
67+
}
68+
69+
data "coder_parameter" "ide_picker" {
70+
count = try(data.coder_parameter.dropdown_picker.value, "") == "ide_picker" ? 1 : 0
71+
72+
name = "ide_picker"
73+
display_name = "Pick your IDEs!"
74+
description = "This is created using the `form_type = 'multi-select'` option."
75+
76+
type = "list(string)"
77+
form_type = "multi-select"
78+
order = 2
79+
mutable = true
80+
81+
dynamic "option" {
82+
for_each = local.ides
83+
content {
84+
name = option.value.name
85+
value = option.value.value
86+
description = option.value.description
87+
icon = option.value.icon
88+
}
89+
}
90+
}
91+
92+
data "coder_parameter" "text_area" {
93+
count = try(data.coder_parameter.dropdown_picker.value, "") == "text_area" ? 1 : 0
94+
95+
name = "text_area"
96+
display_name = "Enter a large AI prompt or script!"
97+
description = "This is created using the `form_type = 'textarea'` option."
98+
99+
type = "string"
100+
form_type = "textarea"
101+
order = 2
102+
mutable = true
103+
104+
styling = jsonencode({
105+
placeholder = <<-EOT
106+
This is a large text entry, try it out!
107+
108+
Including support for multi-line text entry.
109+
EOT
110+
})
111+
}
112+
113+
data "coder_parameter" "cpu_slider" {
114+
count = try(data.coder_parameter.dropdown_picker.value, "") == "cpu_slider" ? 1 : 0
115+
name = "cpu_slider"
116+
display_name = "CPU Cores Slider"
117+
description = "This is created using the `form_type = 'slider'` option."
118+
119+
type = "number"
120+
form_type = "slider"
121+
order = 2
122+
default = 4
123+
mutable = true
124+
125+
validation {
126+
min = 2
127+
max = 8
128+
}
129+
}
130+
131+
data "coder_parameter" "admin_only" {
132+
count = local.is_admin ? 1 : 0
133+
134+
name = "admin_only"
135+
display_name = "Use imaginary experimental features?"
136+
description = "This option is only available to those in the 'admin' group. You can hide it by changing your user data. \n\n _This does not actually enable experimental features._"
137+
138+
type = "bool"
139+
form_type = "checkbox"
140+
default = false
141+
142+
order = 5
143+
}

src/server/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { Hono } from "hono";
44
import { renderToString } from "react-dom/server";
55
import { getShareData } from "./blob";
66
import { trimTrailingSlash } from "hono/trailing-slash";
7-
import { BaseHeader, defaultCode, getAssetPath, HmrScript } from "./utils";
7+
import { BaseHeader, getAssetPath, HmrScript } from "./utils";
88
import { notFound } from "./routes/404";
9+
import defaultExample from "@/examples/code/default.tf?raw";
910

1011
// This must be exported for the dev server to work
1112
export const app = new Hono();
@@ -38,7 +39,7 @@ app.get("/parameters/:shareId?/:example?", async (c, next) => {
3839
return examples[example] ?? null;
3940
}
4041

41-
return defaultCode;
42+
return defaultExample;
4243
};
4344

4445
const exampleCode = await getExampleCode();
@@ -59,8 +60,11 @@ app.get("/parameters/:shareId?/:example?", async (c, next) => {
5960
</head>
6061
<body>
6162
<div id="root"></div>
62-
<script type="module">{`window.CODE = ${JSON.stringify(exampleCode)}`}</script>
63-
<script type="module" src={getAssetPath("/src/client/index.tsx", "client.js")}></script>
63+
<script type="module">{`window.CODE = ${JSON.stringify(exampleCode)}`}</script>
64+
<script
65+
type="module"
66+
src={getAssetPath("/src/client/index.tsx", "client.js")}
67+
></script>
6468
</body>
6569
</html>,
6670
),

src/server/utils.tsx

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -41,146 +41,3 @@ export const BaseHeader = () => {
4141
);
4242
};
4343

44-
export const defaultCode = `terraform {
45-
required_providers {
46-
coder = {
47-
source = "coder/coder"
48-
version = "2.5.3"
49-
}
50-
}
51-
}
52-
53-
# This example shows new form types and conditionality using dynamic parameters.
54-
# When starting from scratch, retain the providers block above.
55-
#
56-
# In the playground, you can get started with:
57-
# - snippets for parameter types
58-
# - example forms for advanced use
59-
# - documentation for how and when to enable Dynamic Parameters.
60-
#
61-
# You can edit user data in the "users" tab, and share your form via URL.
62-
# Enjoy Dynamic Parameters!
63-
64-
locals {
65-
ides = [
66-
{
67-
name = "VSCode",
68-
value = "vscode",
69-
icon = "/icon/code.svg"
70-
},
71-
{
72-
name = "Jetbrains IntelliJ",
73-
value = "intellij",
74-
icon = "/icon/intellij.svg"
75-
},
76-
{
77-
name = "Cursor",
78-
value = "cursor",
79-
icon = "/icon/cursor.svg"
80-
},
81-
]
82-
83-
username = data.coder_workspace_owner.me.name
84-
is_admin = contains(data.coder_workspace_owner.me.groups, "admin")
85-
}
86-
87-
data "coder_workspace_owner" "me" {}
88-
89-
data "coder_parameter" "dropdown_picker" {
90-
name = "dropdown_picker"
91-
display_name = "Pick your next parameter!"
92-
description = "Hello \${username}, pick your next parameter using this \`dropdown\` parameter."
93-
form_type = "dropdown"
94-
mutable = true
95-
96-
option {
97-
value = "ide_picker"
98-
name = "IDE multi-select"
99-
}
100-
101-
option {
102-
value = "text_area"
103-
name = "Large text entry"
104-
}
105-
106-
option {
107-
value = "cpu_slider"
108-
name = "CPU slider"
109-
}
110-
}
111-
112-
data "coder_parameter" "ide_picker" {
113-
count = try(data.coder_parameter.dropdown_picker.value, "") == "ide_picker" ? 1 : 0
114-
115-
name = "ide_picker"
116-
display_name = "Pick your IDEs!"
117-
description = "This is created using the \`form_type = 'multi-select'\` option."
118-
119-
type = "list(string)"
120-
form_type = "multi-select"
121-
order = 2
122-
mutable = true
123-
124-
dynamic "option" {
125-
for_each = local.ides
126-
content {
127-
name = option.value.name
128-
value = option.value.value
129-
description = option.value.description
130-
icon = option.value.icon
131-
}
132-
}
133-
}
134-
135-
data "coder_parameter" "text_area" {
136-
count = try(data.coder_parameter.dropdown_picker.value, "") == "text_area" ? 1 : 0
137-
138-
name = "text_area"
139-
display_name = "Enter a large AI prompt or script!"
140-
description = "This is created using the \`form_type = 'textarea'\` option."
141-
142-
type = "string"
143-
form_type = "textarea"
144-
order = 2
145-
mutable = true
146-
147-
styling = jsonencode({
148-
placeholder = <<-EOT
149-
This is a large text entry, try it out!
150-
151-
Including support for multi-line text entry.
152-
EOT
153-
})
154-
}
155-
156-
data "coder_parameter" "cpu_slider" {
157-
count = try(data.coder_parameter.dropdown_picker.value, "") == "cpu_slider" ? 1 : 0
158-
name = "cpu_slider"
159-
display_name = "CPU Cores Slider"
160-
description = "This is created using the \`form_type = 'slider'\` option."
161-
162-
type = "number"
163-
form_type = "slider"
164-
order = 2
165-
default = 4
166-
mutable = true
167-
168-
validation {
169-
min = 2
170-
max = 8
171-
}
172-
}
173-
174-
data "coder_parameter" "admin_only" {
175-
count = local.is_admin ? 1 : 0
176-
177-
name = "admin_only"
178-
display_name = "Use imaginary experimental features?"
179-
description = "This option is only available to those in the 'admin' group. You can hide it by changing your user data. \\n\\n _This does not actually enable experimental features._"
180-
181-
type = "bool"
182-
form_type = "checkbox"
183-
default = false
184-
185-
order = 5
186-
}`;

0 commit comments

Comments
 (0)