Skip to content

Commit 3117742

Browse files
committed
Show display_name field in the template settings
1 parent 1b6d0c3 commit 3117742

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

coderd/templates.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,18 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
478478
return nil
479479
}
480480

481-
// Update template metadata -- empty fields are not overwritten.
481+
// Update template metadata -- empty fields are not overwritten,
482+
// except for display_name, icon, and default_ttl.
483+
// The exception is required to clear content of these fields with UI.
482484
name := req.Name
483485
displayName := req.DisplayName
484486
desc := req.Description
485487
icon := req.Icon
486-
maxTTL := time.Duration(req.DefaultTTLMillis) * time.Millisecond
488+
defaultTTLMillis := time.Duration(req.DefaultTTLMillis) * time.Millisecond
487489

488490
if name == "" {
489491
name = template.Name
490492
}
491-
if displayName == "" {
492-
displayName = template.DisplayName
493-
}
494493
if desc == "" {
495494
desc = template.Description
496495
}
@@ -502,7 +501,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
502501
DisplayName: displayName,
503502
Description: desc,
504503
Icon: icon,
505-
DefaultTtl: int64(maxTTL),
504+
DefaultTtl: int64(defaultTTLMillis),
506505
})
507506
if err != nil {
508507
return err

site/src/pages/TemplateSettingsPage/TemplateSettingsForm.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import { Stack } from "components/Stack/Stack"
1212
import { FormikContextType, FormikTouched, useFormik } from "formik"
1313
import { FC, useRef, useState } from "react"
1414
import { colors } from "theme/colors"
15-
import { getFormHelpers, nameValidator, onChangeTrimmed } from "util/formUtils"
15+
import { getFormHelpers, nameValidator, templateDisplayNameValidator, onChangeTrimmed } from "util/formUtils"
1616
import * as Yup from "yup"
1717

1818
export const Language = {
1919
nameLabel: "Name",
20+
displayNameLabel: "Display name",
2021
descriptionLabel: "Description",
2122
defaultTtlLabel: "Auto-stop default",
2223
iconLabel: "Icon",
@@ -36,6 +37,7 @@ const MS_HOUR_CONVERSION = 3600000
3637

3738
export const validationSchema = Yup.object({
3839
name: nameValidator(Language.nameLabel),
40+
display_name: templateDisplayNameValidator(Language.displayNameLabel),
3941
description: Yup.string().max(
4042
MAX_DESCRIPTION_CHAR_LIMIT,
4143
Language.descriptionMaxError,
@@ -105,6 +107,16 @@ export const TemplateSettingsForm: FC<TemplateSettingsForm> = ({
105107
variant="outlined"
106108
/>
107109

110+
<TextField
111+
{...getFieldHelpers("display_name")}
112+
disabled={isSubmitting}
113+
onChange={onChangeTrimmed(form)}
114+
autoFocus
115+
fullWidth
116+
label={Language.displayNameLabel}
117+
variant="outlined"
118+
/>
119+
108120
<TextField
109121
{...getFieldHelpers("description")}
110122
multiline

site/src/pages/TemplatesPage/TemplatesPageView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export const TemplatesPageView: FC<
209209
>
210210
<TableCellLink to={templatePageLink}>
211211
<AvatarData
212-
title={template.name}
212+
title={template.display_name.length > 0 ? template.display_name : template.name}
213213
subtitle={template.description}
214214
highlightTitle
215215
avatar={

site/src/util/formUtils.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ export const Language = {
1919
nameInvalidChars: (name: string): string => {
2020
return `${name} must start with a-Z or 0-9 and can contain a-Z, 0-9 or -`
2121
},
22-
nameTooLong: (name: string): string => {
23-
return `${name} cannot be longer than 32 characters`
22+
nameTooLong: (name: string, len: number): string => {
23+
return `${name} cannot be longer than ${len} characters`
24+
},
25+
templateDisplayNameInvalidChars: (name: string): string => {
26+
return `${name} must start with a-Z or 0-9 and can contain a-Z, 0-9 or -`
2427
},
2528
}
2629

@@ -74,15 +77,20 @@ export const onChangeTrimmed =
7477
form.handleChange(event)
7578
}
7679

77-
// REMARK: Keep in sync with coderd/httpapi/httpapi.go#L40
80+
// REMARK: Keep these consts in sync with coderd/httpapi/httpapi.go
7881
const maxLenName = 32
79-
80-
// REMARK: Keep in sync with coderd/httpapi/httpapi.go#L18
82+
const templateDisplayNameMaxLength = 64
8183
const usernameRE = /^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$/
84+
const templateDisplayNameRE = /^[^\s](.*[^\s])?$/
8285

8386
// REMARK: see #1756 for name/username semantics
8487
export const nameValidator = (name: string): Yup.StringSchema =>
8588
Yup.string()
8689
.required(Language.nameRequired(name))
8790
.matches(usernameRE, Language.nameInvalidChars(name))
88-
.max(maxLenName, Language.nameTooLong(name))
91+
.max(maxLenName, Language.nameTooLong(name, maxLenName))
92+
93+
export const templateDisplayNameValidator = (displayName: string): Yup.StringSchema =>
94+
Yup.string()
95+
.matches(templateDisplayNameRE, Language.templateDisplayNameInvalidChars(displayName))
96+
.max(templateDisplayNameMaxLength, Language.nameTooLong(displayName, templateDisplayNameMaxLength))

0 commit comments

Comments
 (0)