Skip to content

feat: Display and edit template icons in the UI #3598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions site/src/pages/TemplateSettingsPage/TemplateSettingsForm.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import InputAdornment from "@material-ui/core/InputAdornment"
import { makeStyles } from "@material-ui/core/styles"
import TextField from "@material-ui/core/TextField"
import { Template, UpdateTemplateMeta } from "api/typesGenerated"
import { FormFooter } from "components/FormFooter/FormFooter"
Expand All @@ -10,6 +12,7 @@ import * as Yup from "yup"
export const Language = {
nameLabel: "Name",
descriptionLabel: "Description",
iconLabel: "Icon",
maxTtlLabel: "Max TTL",
// This is the same from the CLI on https://github.com/coder/coder/blob/546157b63ef9204658acf58cb653aa9936b70c49/cli/templateedit.go#L59
maxTtlHelperText: "Edit the template maximum time before shutdown in milliseconds",
Expand Down Expand Up @@ -45,6 +48,7 @@ export const TemplateSettingsForm: FC<TemplateSettingsForm> = ({
name: template.name,
description: template.description,
max_ttl_ms: template.max_ttl_ms,
icon: template.icon,
},
validationSchema,
onSubmit: (data) => {
Expand All @@ -53,6 +57,8 @@ export const TemplateSettingsForm: FC<TemplateSettingsForm> = ({
initialTouched,
})
const getFieldHelpers = getFormHelpersWithError<UpdateTemplateMeta>(form, error)
const styles = useStyles()
const hasIcon = form.values.icon && form.values.icon !== ""

return (
<form onSubmit={form.handleSubmit} aria-label={Language.formAriaLabel}>
Expand All @@ -77,6 +83,29 @@ export const TemplateSettingsForm: FC<TemplateSettingsForm> = ({
rows={2}
/>

<TextField
{...getFieldHelpers("icon")}
disabled={isSubmitting}
fullWidth
label={Language.iconLabel}
variant="outlined"
InputProps={{
endAdornment: hasIcon ? (
<InputAdornment position="end">
<img
alt=""
src={form.values.icon}
className={styles.adornment}
// This prevent browser to display the ugly error icon if the
// image path is wrong or user didn't finish typing the url
onError={(e) => (e.currentTarget.style.display = "none")}
onLoad={(e) => (e.currentTarget.style.display = "inline")}
/>
</InputAdornment>
) : undefined,
}}
/>

<TextField
{...getFieldHelpers("max_ttl_ms")}
helperText={Language.maxTtlHelperText}
Expand All @@ -92,3 +121,10 @@ export const TemplateSettingsForm: FC<TemplateSettingsForm> = ({
</form>
)
}

const useStyles = makeStyles((theme) => ({
adornment: {
width: theme.spacing(3),
height: theme.spacing(3),
},
}))
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const fillAndSubmitForm = async ({
name,
description,
max_ttl_ms,
icon,
}: Omit<Required<UpdateTemplateMeta>, "min_autostart_interval_ms">) => {
const nameField = await screen.findByLabelText(FormLanguage.nameLabel)
await userEvent.clear(nameField)
Expand All @@ -32,6 +33,10 @@ const fillAndSubmitForm = async ({
await userEvent.clear(descriptionField)
await userEvent.type(descriptionField, description)

const iconField = await screen.findByLabelText(FormLanguage.iconLabel)
await userEvent.clear(iconField)
await userEvent.type(iconField, icon)

const maxTtlField = await screen.findByLabelText(FormLanguage.maxTtlLabel)
await userEvent.clear(maxTtlField)
await userEvent.type(maxTtlField, max_ttl_ms.toString())
Expand All @@ -54,7 +59,7 @@ describe("TemplateSettingsPage", () => {
name: "edited-template-name",
description: "Edited description",
max_ttl_ms: 4000,
icon: "/icons/new-icon.png",
icon: "/icon/code.svg",
}
jest.spyOn(API, "updateTemplateMeta").mockResolvedValueOnce({
...MockTemplate,
Expand Down
2 changes: 2 additions & 0 deletions site/src/pages/TemplatesPage/TemplatesPageView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ AllStates.args = {
{
...MockTemplate,
description: "🚀 Some magical template that does some magical things!",
icon: "/icon/goland.svg",
},
{
...MockTemplate,
workspace_owner_count: 150,
description: "😮 Wow, this one has a bunch of usage!",
icon: "",
},
],
}
Expand Down
15 changes: 15 additions & 0 deletions site/src/pages/TemplatesPage/TemplatesPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ export const TemplatesPageView: FC<TemplatesPageViewProps> = (props) => {
)}
{props.templates?.map((template) => {
const templatePageLink = `/templates/${template.name}`
const hasIcon = template.icon && template.icon !== ""

return (
<TableRow
key={template.id}
Expand All @@ -160,6 +162,13 @@ export const TemplatesPageView: FC<TemplatesPageViewProps> = (props) => {
title={template.name}
subtitle={template.description}
highlightTitle
avatar={
hasIcon ? (
<div className={styles.templateIconWrapper}>
<img alt="" src={template.icon} />
</div>
) : undefined
}
/>
</TableCellLink>

Expand Down Expand Up @@ -211,4 +220,10 @@ const useStyles = makeStyles((theme) => ({
arrowCell: {
display: "flex",
},
templateIconWrapper: {
// Same size then the avatar component
width: 36,
height: 36,
padding: 2,
},
}))