Skip to content

Commit c76d68c

Browse files
committed
Refactor template page to use MUI table
1 parent 40afc47 commit c76d68c

File tree

1 file changed

+70
-51
lines changed

1 file changed

+70
-51
lines changed

site/src/pages/TemplatesPages/OrganizationPage/TemplatePage/TemplatePage.tsx

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1+
import Box from "@material-ui/core/Box"
12
import Button from "@material-ui/core/Button"
3+
import Table from "@material-ui/core/Table"
4+
import TableBody from "@material-ui/core/TableBody"
5+
import TableCell from "@material-ui/core/TableCell"
6+
import TableHead from "@material-ui/core/TableHead"
7+
import TableRow from "@material-ui/core/TableRow"
28
import React from "react"
39
import { Link, useNavigate, useParams } from "react-router-dom"
410
import useSWR from "swr"
511
import * as TypesGen from "../../../../api/typesGenerated"
612
import { EmptyState } from "../../../../components/EmptyState/EmptyState"
713
import { ErrorSummary } from "../../../../components/ErrorSummary/ErrorSummary"
814
import { Header } from "../../../../components/Header/Header"
9-
import { FullScreenLoader } from "../../../../components/Loader/FullScreenLoader"
1015
import { Margins } from "../../../../components/Margins/Margins"
1116
import { Stack } from "../../../../components/Stack/Stack"
12-
import { Column, Table } from "../../../../components/Table/Table"
17+
import { TableHeaderRow } from "../../../../components/TableHeaders/TableHeaders"
18+
import { TableLoader } from "../../../../components/TableLoader/TableLoader"
19+
import { TableTitle } from "../../../../components/TableTitle/TableTitle"
1320
import { unsafeSWRArgument } from "../../../../util"
1421
import { firstOrItem } from "../../../../util/array"
1522

23+
export const Language = {
24+
tableTitle: "Workspaces",
25+
nameLabel: "Name",
26+
emptyMessage: "No workspaces have been created yet",
27+
emptyDescription: "Create a workspace to get started",
28+
totalLabel: "total",
29+
ctaAction: "Create workspace",
30+
subtitlePosfix: "workspaces",
31+
}
32+
1633
export const TemplatePage: React.FC = () => {
1734
const navigate = useNavigate()
1835
const { template: templateName, organization: organizationName } = useParams()
@@ -32,73 +49,75 @@ export const TemplatePage: React.FC = () => {
3249
() => `/api/v2/organizations/${unsafeSWRArgument(organizationInfo).id}/workspaces`,
3350
)
3451

35-
if (organizationError) {
36-
return <ErrorSummary error={organizationError} />
37-
}
38-
39-
if (templateError) {
40-
return <ErrorSummary error={templateError} />
41-
}
42-
43-
if (workspacesError) {
44-
return <ErrorSummary error={workspacesError} />
45-
}
46-
47-
if (!templateInfo || !workspaces) {
48-
return <FullScreenLoader />
49-
}
52+
const hasError = organizationError || templateError || workspacesError
53+
const isLoading = !templateInfo || !workspaces
5054

5155
const createWorkspace = () => {
5256
navigate(`/templates/${organizationName}/${templateName}/create`)
5357
}
5458

55-
const emptyState = (
56-
<EmptyState
57-
message="No workspaces have been created yet"
58-
description="Create a workspace to get started"
59-
cta={
60-
<Button variant="contained" color="primary" onClick={createWorkspace}>
61-
Create workspace
62-
</Button>
63-
}
64-
/>
65-
)
66-
67-
const columns: Column<TypesGen.Workspace>[] = [
68-
{
69-
key: "name",
70-
name: "Name",
71-
renderer: (nameField: string | TypesGen.WorkspaceBuild, workspace: TypesGen.Workspace) => {
72-
return <Link to={`/workspaces/${workspace.id}`}>{nameField}</Link>
73-
},
74-
},
75-
]
76-
77-
const perTemplateWorkspaces = workspaces.filter((workspace) => {
78-
return workspace.template_id === templateInfo.id
79-
})
80-
81-
const tableProps = {
82-
title: "Workspaces",
83-
columns,
84-
data: perTemplateWorkspaces,
85-
emptyState: emptyState,
86-
}
59+
const perTemplateWorkspaces =
60+
workspaces && templateInfo
61+
? workspaces.filter((workspace) => {
62+
return workspace.template_id === templateInfo.id
63+
})
64+
: undefined
8765

8866
return (
8967
<Stack spacing={4}>
9068
<Header
9169
title={firstOrItem(templateName, "")}
9270
description={firstOrItem(organizationName, "")}
93-
subTitle={`${perTemplateWorkspaces.length} workspaces`}
71+
subTitle={perTemplateWorkspaces ? `${perTemplateWorkspaces.length} ${Language.subtitlePosfix}` : ""}
9472
action={{
9573
text: "Create Workspace",
9674
onClick: createWorkspace,
9775
}}
9876
/>
9977

10078
<Margins>
101-
<Table {...tableProps} />
79+
{organizationError && <ErrorSummary error={organizationError} />}
80+
{templateError && <ErrorSummary error={templateError} />}
81+
{workspacesError && <ErrorSummary error={workspacesError} />}
82+
{!hasError && (
83+
<Table>
84+
<TableHead>
85+
<TableTitle title={Language.tableTitle} />
86+
<TableHeaderRow>
87+
<TableCell size="small">{Language.nameLabel}</TableCell>
88+
</TableHeaderRow>
89+
</TableHead>
90+
<TableBody>
91+
{isLoading && <TableLoader />}
92+
{workspaces &&
93+
workspaces.map((w) => (
94+
<TableRow key={w.id}>
95+
<TableCell>
96+
<Link to={`/workspaces/${w.id}`}>{w.name}</Link>
97+
</TableCell>
98+
</TableRow>
99+
))}
100+
101+
{workspaces && workspaces.length === 0 && (
102+
<TableRow>
103+
<TableCell colSpan={999}>
104+
<Box p={4}>
105+
<EmptyState
106+
message={Language.emptyMessage}
107+
description={Language.emptyDescription}
108+
cta={
109+
<Button variant="contained" color="primary" onClick={createWorkspace}>
110+
{Language.ctaAction}
111+
</Button>
112+
}
113+
/>
114+
</Box>
115+
</TableCell>
116+
</TableRow>
117+
)}
118+
</TableBody>
119+
</Table>
120+
)}
102121
</Margins>
103122
</Stack>
104123
)

0 commit comments

Comments
 (0)