1
+ import Box from "@material-ui/core/Box"
1
2
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"
2
8
import React from "react"
3
9
import { Link , useNavigate , useParams } from "react-router-dom"
4
10
import useSWR from "swr"
5
11
import * as TypesGen from "../../../../api/typesGenerated"
6
12
import { EmptyState } from "../../../../components/EmptyState/EmptyState"
7
13
import { ErrorSummary } from "../../../../components/ErrorSummary/ErrorSummary"
8
14
import { Header } from "../../../../components/Header/Header"
9
- import { FullScreenLoader } from "../../../../components/Loader/FullScreenLoader"
10
15
import { Margins } from "../../../../components/Margins/Margins"
11
16
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"
13
20
import { unsafeSWRArgument } from "../../../../util"
14
21
import { firstOrItem } from "../../../../util/array"
15
22
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
+
16
33
export const TemplatePage : React . FC = ( ) => {
17
34
const navigate = useNavigate ( )
18
35
const { template : templateName , organization : organizationName } = useParams ( )
@@ -32,73 +49,75 @@ export const TemplatePage: React.FC = () => {
32
49
( ) => `/api/v2/organizations/${ unsafeSWRArgument ( organizationInfo ) . id } /workspaces` ,
33
50
)
34
51
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
50
54
51
55
const createWorkspace = ( ) => {
52
56
navigate ( `/templates/${ organizationName } /${ templateName } /create` )
53
57
}
54
58
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
87
65
88
66
return (
89
67
< Stack spacing = { 4 } >
90
68
< Header
91
69
title = { firstOrItem ( templateName , "" ) }
92
70
description = { firstOrItem ( organizationName , "" ) }
93
- subTitle = { `${ perTemplateWorkspaces . length } workspaces` }
71
+ subTitle = { perTemplateWorkspaces ? `${ perTemplateWorkspaces . length } ${ Language . subtitlePosfix } ` : "" }
94
72
action = { {
95
73
text : "Create Workspace" ,
96
74
onClick : createWorkspace ,
97
75
} }
98
76
/>
99
77
100
78
< 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
+ ) }
102
121
</ Margins >
103
122
</ Stack >
104
123
)
0 commit comments