1
- import Box from "@material-ui/core/Box"
2
- import Table from "@material-ui/core/Table"
3
- import TableBody from "@material-ui/core/TableBody"
4
- import TableCell from "@material-ui/core/TableCell"
5
- import TableHead from "@material-ui/core/TableHead"
6
- import TableRow from "@material-ui/core/TableRow"
7
1
import React from "react"
8
- import { Link } from "react-router-dom"
9
2
import useSWR from "swr"
10
3
import * as TypesGen from "../../api/typesGenerated"
11
- import { CodeExample } from "../../components/CodeExample/CodeExample"
12
- import { EmptyState } from "../../components/EmptyState/EmptyState"
13
4
import { ErrorSummary } from "../../components/ErrorSummary/ErrorSummary"
14
5
import { Header } from "../../components/Header/Header"
15
6
import { Margins } from "../../components/Margins/Margins"
16
7
import { Stack } from "../../components/Stack/Stack"
17
- import { TableHeaderRow } from "../../components/TableHeaders/TableHeaders"
18
- import { TableLoader } from "../../components/TableLoader/TableLoader"
19
- import { TableTitle } from "../../components/TableTitle/TableTitle"
8
+ import { TemplatesTable } from "../../components/TemplatesTable/TemplatesTable"
20
9
21
10
export const Language = {
22
11
title : "Templates" ,
@@ -29,66 +18,19 @@ export const Language = {
29
18
30
19
export const TemplatesPage : React . FC = ( ) => {
31
20
const { data : orgs , error : orgsError } = useSWR < TypesGen . Organization [ ] , Error > ( "/api/v2/users/me/organizations" )
32
- const { data : templates , error } = useSWR < TypesGen . Template [ ] | null , Error > (
33
- orgs ? `/api/v2/organizations/${ orgs [ 0 ] . id } /templates` : null ,
21
+ const { data : templates , error } = useSWR < TypesGen . Template [ ] | undefined , Error > (
22
+ orgs ? `/api/v2/organizations/${ orgs [ 0 ] . id } /templates` : undefined ,
34
23
)
35
- const isLoading = ! templates || ! orgs
36
24
const subTitle = templates ? `${ templates . length } ${ Language . totalLabel } ` : undefined
37
25
const hasError = orgsError || error
38
- // Create a dictionary of organization ID -> organization Name
39
- // Needed to properly construct links to dive into a template
40
- const orgDictionary =
41
- orgs &&
42
- orgs . reduce ( ( acc : Record < string , string > , curr : TypesGen . Organization ) => {
43
- return {
44
- ...acc ,
45
- [ curr . id ] : curr . name ,
46
- }
47
- } , { } )
48
26
49
27
return (
50
28
< Stack spacing = { 4 } >
51
29
< Header title = { Language . title } subTitle = { subTitle } />
52
30
< Margins >
53
31
{ error && < ErrorSummary error = { error } /> }
54
32
{ orgsError && < ErrorSummary error = { orgsError } /> }
55
- { ! hasError && (
56
- < Table >
57
- < TableHead >
58
- < TableTitle title = { Language . tableTitle } />
59
- < TableHeaderRow >
60
- < TableCell size = "small" > { Language . nameLabel } </ TableCell >
61
- </ TableHeaderRow >
62
- </ TableHead >
63
- < TableBody >
64
- { isLoading && < TableLoader /> }
65
- { templates &&
66
- orgs &&
67
- orgDictionary &&
68
- templates . map ( ( t ) => (
69
- < TableRow key = { t . id } >
70
- < TableCell >
71
- < Link to = { `/templates/${ orgDictionary [ t . organization_id ] } /${ t . name } ` } > { t . name } </ Link >
72
- </ TableCell >
73
- </ TableRow >
74
- ) ) }
75
-
76
- { templates && templates . length === 0 && (
77
- < TableRow >
78
- < TableCell colSpan = { 999 } >
79
- < Box p = { 4 } >
80
- < EmptyState
81
- message = { Language . emptyMessage }
82
- description = { Language . emptyDescription }
83
- cta = { < CodeExample code = "coder templates create" /> }
84
- />
85
- </ Box >
86
- </ TableCell >
87
- </ TableRow >
88
- ) }
89
- </ TableBody >
90
- </ Table >
91
- ) }
33
+ { ! hasError && < TemplatesTable organizations = { orgs } templates = { templates } /> }
92
34
</ Margins >
93
35
</ Stack >
94
36
)
0 commit comments