Skip to content

feat: display specific errors if templates page fails #4023

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 10 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Surface templates page errors
  • Loading branch information
presleyp committed Sep 9, 2022
commit 36cb15e51a82713629c4a391435b3b8e1303976b
8 changes: 4 additions & 4 deletions site/src/pages/TemplatesPage/TemplatesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import { XServiceContext } from "../../xServices/StateContext"
import { templatesMachine } from "../../xServices/templates/templatesXService"
import { TemplatesPageView } from "./TemplatesPageView"

const TemplatesPage: React.FC = () => {
export const TemplatesPage: React.FC = () => {
Copy link
Member

@Kira-Pilot Kira-Pilot Sep 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to update the import of TemplatesPage in AppRouter.tsx and in TemplatesPage.test.tsx.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah, thanks!

const xServices = useContext(XServiceContext)
const [authState] = useActor(xServices.authXService)
const [templatesState] = useMachine(templatesMachine)
const { templates, getOrganizationsError, getTemplatesError } = templatesState.context

return (
<>
<Helmet>
<title>{pageTitle("Templates")}</title>
</Helmet>
<TemplatesPageView
templates={templatesState.context.templates}
templates={templates}
canCreateTemplate={authState.context.permissions?.createTemplates}
loading={templatesState.hasTag("loading")}
error={getOrganizationsError ?? getTemplatesError}
/>
</>
)
}

export default TemplatesPage
5 changes: 4 additions & 1 deletion site/src/pages/TemplatesPage/TemplatesPageView.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentMeta, Story } from "@storybook/react"
import { MockTemplate } from "../../testHelpers/entities"
import { makeMockApiError, MockTemplate } from "../../testHelpers/entities"
import { TemplatesPageView, TemplatesPageViewProps } from "./TemplatesPageView"

export default {
Expand Down Expand Up @@ -48,3 +48,6 @@ EmptyCanCreate.args = {

export const EmptyCannotCreate = Template.bind({})
EmptyCannotCreate.args = {}

export const Error = Template.bind({})
Error.args = { error: makeMockApiError({ message: "Something went wrong fetching templates." }) }
171 changes: 89 additions & 82 deletions site/src/pages/TemplatesPage/TemplatesPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import TableHead from "@material-ui/core/TableHead"
import TableRow from "@material-ui/core/TableRow"
import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight"
import useTheme from "@material-ui/styles/useTheme"
import { ErrorSummary } from "components/ErrorSummary/ErrorSummary"
import { FC } from "react"
import { useNavigate } from "react-router-dom"
import { createDayString } from "util/createDayString"
Expand Down Expand Up @@ -76,12 +77,14 @@ export interface TemplatesPageViewProps {
loading?: boolean
canCreateTemplate?: boolean
templates?: TypesGen.Template[]
error?: Error | unknown
}

export const TemplatesPageView: FC<React.PropsWithChildren<TemplatesPageViewProps>> = (props) => {
const styles = useStyles()
const navigate = useNavigate()
const theme: Theme = useTheme()
const empty = !props.loading && !props.error && !props.templates?.length

return (
<Margins>
Expand Down Expand Up @@ -113,94 +116,98 @@ export const TemplatesPageView: FC<React.PropsWithChildren<TemplatesPageViewProp
)}
</PageHeader>

<TableContainer>
<Table>
<TableHead>
<TableRow>
<TableCell width="50%">{Language.nameLabel}</TableCell>
<TableCell width="16%">{Language.usedByLabel}</TableCell>
<TableCell width="16%">{Language.lastUpdatedLabel}</TableCell>
<TableCell width="16%">{Language.createdByLabel}</TableCell>
<TableCell width="1%"></TableCell>
</TableRow>
</TableHead>
<TableBody>
{props.loading && <TableLoader />}
{!props.loading && !props.templates?.length && (
{props.error ? (
<ErrorSummary error={props.error} />
) : (
<TableContainer>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this triple, JSX ternary a little confusing and personally think this is a good candidate for early return.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you do the early return? I'm not seeing a good way of doing it since the margins and page header need to be there in each case. I could put those in a const but at that point I'd rather put the content (error or data) in a const and still use the conditional so you can easily see what's definitely there vs what's conditional.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might move the margins and page header into TemplatesPage.tsx - they seem like page scaffolding to me. Then, one could decompose the TemplatesPageView file:

export const TemplatesPageView = () => {
  if (props.getOrganizationsError) {
    return <ErrorSummary />
  }

  if (props.getTemplatesError) {
    return <ErrorSummary />
  }

  if (empty) {
    return (
      <TableContainer>
        <LoadingTableBody />
      </TableContainer>
    )
  }

  return (
    <TableContainer>
      <TemplateTableBody templates={templates} />
    </TableContainer>
  )
}

I think the benefit here is we are writing state in a declarative way - it is apparent at a glance what states the developer should be aware of. Let me know what you think!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this looks nice and readable, but the scaffolding is in the view for storybook reasons. I'm open to a refactor but I think I'll leave it for future work so I can get the error handling in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good!

<Table>
<TableHead>
<TableRow>
<TableCell colSpan={999}>
<EmptyState
message={Language.emptyMessage}
description={
props.canCreateTemplate
? Language.emptyDescription
: Language.emptyViewNoPerms
}
descriptionClassName={styles.emptyDescription}
cta={<CodeExample code="coder templates init" />}
/>
</TableCell>
<TableCell width="50%">{Language.nameLabel}</TableCell>
<TableCell width="16%">{Language.usedByLabel}</TableCell>
<TableCell width="16%">{Language.lastUpdatedLabel}</TableCell>
<TableCell width="16%">{Language.createdByLabel}</TableCell>
<TableCell width="1%"></TableCell>
</TableRow>
)}
{props.templates?.map((template) => {
const templatePageLink = `/templates/${template.name}`
const hasIcon = template.icon && template.icon !== ""

return (
<TableRow
key={template.id}
hover
data-testid={`template-${template.id}`}
tabIndex={0}
onKeyDown={(event) => {
if (event.key === "Enter") {
navigate(templatePageLink)
}
}}
className={styles.clickableTableRow}
>
<TableCellLink to={templatePageLink}>
<AvatarData
title={template.name}
subtitle={template.description}
highlightTitle
avatar={
hasIcon ? (
<div className={styles.templateIconWrapper}>
<img alt="" src={template.icon} />
</div>
) : undefined
</TableHead>
<TableBody>
{props.loading && <TableLoader />}
{empty && (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about making a parent table component that accepts a child table body component? That child component could be a loading component, or a data-filled component. I was wondering if breaking apart state into components here might make this a little easier to read and reason about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea, can you make a ticket for it? We could apply it in multiple places.

<TableRow>
<TableCell colSpan={999}>
<EmptyState
message={Language.emptyMessage}
description={
props.canCreateTemplate
? Language.emptyDescription
: Language.emptyViewNoPerms
}
descriptionClassName={styles.emptyDescription}
cta={<CodeExample code="coder templates init" />}
/>
</TableCellLink>
</TableCell>
</TableRow>
)}
{props.templates?.map((template) => {
const templatePageLink = `/templates/${template.name}`
const hasIcon = template.icon && template.icon !== ""

<TableCellLink to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{Language.developerCount(template.workspace_owner_count)}
</span>
</TableCellLink>
return (
<TableRow
key={template.id}
hover
data-testid={`template-${template.id}`}
tabIndex={0}
onKeyDown={(event) => {
if (event.key === "Enter") {
navigate(templatePageLink)
}
}}
className={styles.clickableTableRow}
>
<TableCellLink to={templatePageLink}>
<AvatarData
title={template.name}
subtitle={template.description}
highlightTitle
avatar={
hasIcon ? (
<div className={styles.templateIconWrapper}>
<img alt="" src={template.icon} />
</div>
) : undefined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a ternary here.

}
/>
</TableCellLink>

<TableCellLink data-chromatic="ignore" to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{createDayString(template.updated_at)}
</span>
</TableCellLink>
<TableCellLink to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{template.created_by_name}
</span>
</TableCellLink>
<TableCellLink to={templatePageLink}>
<div className={styles.arrowCell}>
<KeyboardArrowRight className={styles.arrowRight} />
</div>
</TableCellLink>
</TableRow>
)
})}
</TableBody>
</Table>
</TableContainer>
<TableCellLink to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{Language.developerCount(template.workspace_owner_count)}
</span>
</TableCellLink>

<TableCellLink data-chromatic="ignore" to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{createDayString(template.updated_at)}
</span>
</TableCellLink>
<TableCellLink to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{template.created_by_name}
</span>
</TableCellLink>
<TableCellLink to={templatePageLink}>
<div className={styles.arrowCell}>
<KeyboardArrowRight className={styles.arrowRight} />
</div>
</TableCellLink>
</TableRow>
)
})}
</TableBody>
</Table>
</TableContainer>
)}
</Margins>
)
}
Expand Down
122 changes: 62 additions & 60 deletions site/src/xServices/templates/templatesXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +6,92 @@ interface TemplatesContext {
organizations?: TypesGen.Organization[]
templates?: TypesGen.Template[]
canCreateTemplate?: boolean
permissionsError?: Error | unknown
organizationsError?: Error | unknown
templatesError?: Error | unknown
getOrganizationsError?: Error | unknown
getTemplatesError?: Error | unknown
}

export const templatesMachine = createMachine(
export const templatesMachine =
/** @xstate-layout N4IgpgJg5mDOIC5QBcwFsAOAbAhq2AysnmAHQzLICWAdlAPIBOUONVAXnlQPY2wDEEXmVoA3bgGsyFJizadqveEhAZusKopqJQAD0QAWAMwHSATgDsANgCsAJgt2rRuwAYAHBYsAaEAE9EAEZAu1IDMwiIm3dXG0DjdwBfRN9UTFx8IhJyMEpaBmZWDi4lfjBGRm5GUmw8ADMqtBzkWSKFHj4dVXVNDq79BGNTS1sHJxcPL18AhBMjUhtXJaWrOzM7O3cDG2TU9FrM4lRm6joAFX2MuEFhUjFJaVyL9JJlUDUNLX6gpeH1wJcIQs7giVmmiBB5kilgM7iMHmcOxSIDSBzgWWOFFOUGeaIE5Uq1QODUYTQouKub26nz6KgGgV+ULsAOZDhBZjB-kQbhspGWSxC0W2VncSORNG4EDgXVRlIxjzydFa8hKnRUH16vG+gzs4IQwTMYWhjgsRjm9l2KMur3lJ3yFNeXQ1XzpiCsVlcpFW4QMFnisJZeo5UMicQsNjMgRsSL2L0O2SENDATp6Lr0QTcFjCa2cfqsgRNeuC7j5-Ncq3WmwMgUtsptRzIBKqKZpWtd+sz2Y5RjzBYceo2WbNw9hrijZtr1vjqBbmu07cC7iLSWSiSAA */
createMachine(
{
tsTypes: {} as import("./templatesXService.typegen").Typegen0,
schema: {
context: {} as TemplatesContext,
services: {} as {
getOrganizations: {
data: TypesGen.Organization[]
}
getPermissions: {
data: boolean
}
getTemplates: {
data: TypesGen.Template[]
}
},
tsTypes: {} as import("./templatesXService.typegen").Typegen0,
schema: {
context: {} as TemplatesContext,
services: {} as {
getOrganizations: {
data: TypesGen.Organization[]
}
getTemplates: {
data: TypesGen.Template[]
}
},
id: "templatesState",
initial: "gettingOrganizations",
states: {
gettingOrganizations: {
entry: "clearOrganizationsError",
invoke: {
src: "getOrganizations",
id: "getOrganizations",
onDone: [
{
actions: ["assignOrganizations", "clearOrganizationsError"],
target: "gettingTemplates",
},
],
onError: [
{
actions: "assignOrganizationsError",
target: "error",
},
],
},
tags: "loading",
},
id: "templatesState",
initial: "gettingOrganizations",
states: {
gettingOrganizations: {
entry: "clearGetOrganizationsError",
invoke: {
src: "getOrganizations",
id: "getOrganizations",
onDone: [
{
actions: ["assignOrganizations"],
target: "gettingTemplates",
},
],
onError: [
{
actions: "assignGetOrganizationsError",
target: "error",
},
],
},
gettingTemplates: {
entry: "clearTemplatesError",
invoke: {
src: "getTemplates",
id: "getTemplates",
onDone: {
tags: "loading",
},
gettingTemplates: {
entry: "clearGetTemplatesError",
invoke: {
src: "getTemplates",
id: "getTemplates",
onDone: [
{
actions: ["assignTemplates"],
target: "done",
actions: ["assignTemplates", "clearTemplatesError"],
},
onError: {
],
onError: [
{
actions: "assignGetTemplatesError",
target: "error",
actions: "assignTemplatesError",
},
},
tags: "loading",
],
},
done: {},
error: {},
tags: "loading",
},
done: {},
error: {},
},
},
{
actions: {
assignOrganizations: assign({
organizations: (_, event) => event.data,
}),
assignOrganizationsError: assign({
organizationsError: (_, event) => event.data,
assignGetOrganizationsError: assign({
getOrganizationsError: (_, event) => event.data,
}),
clearOrganizationsError: assign((context) => ({
clearGetOrganizationsError: assign((context) => ({
...context,
organizationsError: undefined,
getOrganizationsError: undefined,
})),
assignTemplates: assign({
templates: (_, event) => event.data,
}),
assignTemplatesError: assign({
templatesError: (_, event) => event.data,
assignGetTemplatesError: assign({
getTemplatesError: (_, event) => event.data,
}),
clearTemplatesError: (context) => assign({ ...context, getWorkspacesError: undefined }),
clearGetTemplatesError: (context) => assign({ ...context, getTemplatesError: undefined }),
},
services: {
getOrganizations: API.getOrganizations,
Expand Down