|
| 1 | +import type { Interpolation, Theme } from "@emotion/react"; |
| 2 | +import type { FC } from "react"; |
| 3 | +import { Link, useNavigate, useSearchParams } from "react-router-dom"; |
| 4 | +import type { Template, TemplateExample } from "api/typesGenerated"; |
| 5 | +import { ErrorAlert } from "components/Alert/ErrorAlert"; |
| 6 | +import { |
| 7 | + HelpTooltip, |
| 8 | + HelpTooltipContent, |
| 9 | + HelpTooltipLink, |
| 10 | + HelpTooltipLinksGroup, |
| 11 | + HelpTooltipText, |
| 12 | + HelpTooltipTitle, |
| 13 | + HelpTooltipTrigger, |
| 14 | +} from "components/HelpTooltip/HelpTooltip"; |
| 15 | +import { Margins } from "components/Margins/Margins"; |
| 16 | +import { |
| 17 | + PageHeader, |
| 18 | + PageHeaderSubtitle, |
| 19 | + PageHeaderTitle, |
| 20 | +} from "components/PageHeader/PageHeader"; |
| 21 | +import { Stack } from "components/Stack/Stack"; |
| 22 | +import { TemplateCard } from "modules/templates/TemplateCard/TemplateCard"; |
| 23 | +import { docs } from "utils/docs"; |
| 24 | +import { CreateTemplateButton } from "../CreateTemplateButton"; |
| 25 | +import { EmptyTemplates } from "../EmptyTemplates"; |
| 26 | + |
| 27 | +export const Language = { |
| 28 | + templateTooltipTitle: "What is template?", |
| 29 | + templateTooltipText: |
| 30 | + "With templates you can create a common configuration for your workspaces using Terraform.", |
| 31 | + templateTooltipLink: "Manage templates", |
| 32 | +}; |
| 33 | + |
| 34 | +const TemplateHelpTooltip: FC = () => { |
| 35 | + return ( |
| 36 | + <HelpTooltip> |
| 37 | + <HelpTooltipTrigger /> |
| 38 | + <HelpTooltipContent> |
| 39 | + <HelpTooltipTitle>{Language.templateTooltipTitle}</HelpTooltipTitle> |
| 40 | + <HelpTooltipText>{Language.templateTooltipText}</HelpTooltipText> |
| 41 | + <HelpTooltipLinksGroup> |
| 42 | + <HelpTooltipLink href={docs("/templates")}> |
| 43 | + {Language.templateTooltipLink} |
| 44 | + </HelpTooltipLink> |
| 45 | + </HelpTooltipLinksGroup> |
| 46 | + </HelpTooltipContent> |
| 47 | + </HelpTooltip> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +export interface TemplatesPageViewProps { |
| 52 | + templates: Template[] | undefined; |
| 53 | + examples: TemplateExample[] | undefined; |
| 54 | + canCreateTemplates: boolean; |
| 55 | + error?: unknown; |
| 56 | +} |
| 57 | + |
| 58 | +export type TemplatesByOrg = Record<string, number>; |
| 59 | + |
| 60 | +const getTemplatesByOrg = (templates: Template[]): TemplatesByOrg => { |
| 61 | + const orgs: TemplatesByOrg = { |
| 62 | + all: 0 |
| 63 | + } |
| 64 | + |
| 65 | + templates.forEach((template) => { |
| 66 | + if (orgs[template.organization_name]) { |
| 67 | + orgs[template.organization_name] += 1 |
| 68 | + } else { |
| 69 | + orgs[template.organization_name] = 1; |
| 70 | + } |
| 71 | + |
| 72 | + orgs.all += 1; |
| 73 | + }) |
| 74 | + |
| 75 | + return orgs; |
| 76 | +} |
| 77 | + |
| 78 | +export const TemplatesPageView: FC<TemplatesPageViewProps> = ({ |
| 79 | + templates, |
| 80 | + examples, |
| 81 | + canCreateTemplates, |
| 82 | + error, |
| 83 | +}) => { |
| 84 | + const [urlParams] = useSearchParams(); |
| 85 | + const isEmpty = templates && templates.length === 0; |
| 86 | + const navigate = useNavigate(); |
| 87 | + |
| 88 | + const activeOrg = urlParams.get("org") ?? "all"; |
| 89 | + |
| 90 | + const templatesByOrg = getTemplatesByOrg(templates ?? []); |
| 91 | + |
| 92 | + return ( |
| 93 | + <Margins> |
| 94 | + <PageHeader |
| 95 | + actions={ |
| 96 | + canCreateTemplates && <CreateTemplateButton onNavigate={navigate} /> |
| 97 | + } |
| 98 | + > |
| 99 | + <PageHeaderTitle> |
| 100 | + <Stack spacing={1} direction="row" alignItems="center"> |
| 101 | + Templates |
| 102 | + <TemplateHelpTooltip /> |
| 103 | + </Stack> |
| 104 | + </PageHeaderTitle> |
| 105 | + {templates && templates.length > 0 && ( |
| 106 | + <PageHeaderSubtitle> |
| 107 | + Select a template to create a workspace. |
| 108 | + </PageHeaderSubtitle> |
| 109 | + )} |
| 110 | + </PageHeader> |
| 111 | + |
| 112 | + {Boolean(error) && <ErrorAlert error={error} />} |
| 113 | + |
| 114 | + <Stack direction="row" spacing={4} alignItems="flex-start"> |
| 115 | + <Stack |
| 116 | + css={{ width: 208, flexShrink: 0, position: "sticky", top: 48 }} |
| 117 | + > |
| 118 | + <span css={styles.filterCaption}>ORGANIZATION</span> |
| 119 | + {Object.keys(templatesByOrg).map((org) => ( |
| 120 | + <Link |
| 121 | + key={org} |
| 122 | + to={`?org=${org}`} |
| 123 | + css={[ |
| 124 | + styles.tagLink, |
| 125 | + org === activeOrg && styles.tagLinkActive, |
| 126 | + ]} |
| 127 | + > |
| 128 | + {org === 'all' ? 'All Organizations' : org} ({templatesByOrg[org] ?? 0}) |
| 129 | + </Link> |
| 130 | + ))} |
| 131 | + </Stack> |
| 132 | + |
| 133 | + |
| 134 | + <div |
| 135 | + css={{ |
| 136 | + display: "flex", |
| 137 | + flexWrap: "wrap", |
| 138 | + gap: 32, |
| 139 | + height: "max-content", |
| 140 | + }} |
| 141 | + > |
| 142 | + {isEmpty ? ( |
| 143 | + <EmptyTemplates |
| 144 | + canCreateTemplates={canCreateTemplates} |
| 145 | + examples={examples ?? []} |
| 146 | + /> |
| 147 | + ) : (templates && |
| 148 | + templates.map((template) => ( |
| 149 | + <TemplateCard |
| 150 | + css={(theme) => ({ |
| 151 | + backgroundColor: theme.palette.background.paper, |
| 152 | + })} |
| 153 | + template={template} |
| 154 | + key={template.id} |
| 155 | + /> |
| 156 | + )))} |
| 157 | + </div> |
| 158 | + </Stack> |
| 159 | + </Margins> |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +const styles = { |
| 164 | + filterCaption: (theme) => ({ |
| 165 | + textTransform: "uppercase", |
| 166 | + fontWeight: 600, |
| 167 | + fontSize: 12, |
| 168 | + color: theme.palette.text.secondary, |
| 169 | + letterSpacing: "0.1em", |
| 170 | + }), |
| 171 | + tagLink: (theme) => ({ |
| 172 | + color: theme.palette.text.secondary, |
| 173 | + textDecoration: "none", |
| 174 | + fontSize: 14, |
| 175 | + textTransform: "capitalize", |
| 176 | + |
| 177 | + "&:hover": { |
| 178 | + color: theme.palette.text.primary, |
| 179 | + }, |
| 180 | + }), |
| 181 | + tagLinkActive: (theme) => ({ |
| 182 | + color: theme.palette.text.primary, |
| 183 | + fontWeight: 600, |
| 184 | + }), |
| 185 | + secondary: (theme) => ({ |
| 186 | + color: theme.palette.text.secondary, |
| 187 | + }), |
| 188 | + actionButton: (theme) => ({ |
| 189 | + transition: "none", |
| 190 | + color: theme.palette.text.secondary, |
| 191 | + "&:hover": { |
| 192 | + borderColor: theme.palette.text.primary, |
| 193 | + }, |
| 194 | + }), |
| 195 | +} satisfies Record<string, Interpolation<Theme>>; |
0 commit comments