Repository navigation
Expand file tree
/
Copy pathWorkspacesButton.tsx
More file actions
218 lines (203 loc) · 5.93 KB
/
Copy pathWorkspacesButton.tsx
File metadata and controls
218 lines (203 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { type PropsWithChildren, type ReactNode, useState } from "react";
import { type Template } from "api/typesGenerated";
import { type UseQueryResult } from "react-query";
import {
Link as RouterLink,
LinkProps as RouterLinkProps,
} from "react-router-dom";
import Box from "@mui/system/Box";
import Button from "@mui/material/Button";
import Link from "@mui/material/Link";
import AddIcon from "@mui/icons-material/AddOutlined";
import OpenIcon from "@mui/icons-material/OpenInNewOutlined";
import { Loader } from "components/Loader/Loader";
import { OverflowY } from "components/OverflowY/OverflowY";
import { EmptyState } from "components/EmptyState/EmptyState";
import { Avatar } from "components/Avatar/Avatar";
import { SearchBox } from "./WorkspacesSearchBox";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "components/Popover/Popover";
const ICON_SIZE = 18;
type TemplatesQuery = UseQueryResult<Template[]>;
type WorkspacesButtonProps = PropsWithChildren<{
templatesFetchStatus: TemplatesQuery["status"];
templates: TemplatesQuery["data"];
}>;
export function WorkspacesButton({
children,
templatesFetchStatus,
templates,
}: WorkspacesButtonProps) {
// Dataset should always be small enough that client-side filtering should be
// good enough. Can swap out down the line if it becomes an issue
const [searchTerm, setSearchTerm] = useState("");
const processed = sortTemplatesByUsersDesc(templates ?? [], searchTerm);
let emptyState: ReactNode = undefined;
if (templates?.length === 0) {
emptyState = (
<EmptyState
message="No templates yet"
cta={
<Link to="/templates" component={RouterLink}>
Create one now.
</Link>
}
/>
);
} else if (processed.length === 0) {
emptyState = <EmptyState message="No templates match your text" />;
}
return (
<Popover>
<PopoverTrigger>
<Button startIcon={<AddIcon />} variant="contained">
{children}
</Button>
</PopoverTrigger>
<PopoverContent horizontal="right">
<SearchBox
value={searchTerm}
onValueChange={(newValue) => setSearchTerm(newValue)}
placeholder="Type/select a workspace template"
label="Template select for workspace"
sx={{ flexShrink: 0, columnGap: 1.5 }}
/>
<OverflowY
maxHeight={380}
sx={{
display: "flex",
flexDirection: "column",
paddingY: 1,
}}
>
{templatesFetchStatus === "loading" ? (
<Loader size={14} />
) : (
<>
{processed.map((template) => (
<WorkspaceResultsRow key={template.id} template={template} />
))}
{emptyState}
</>
)}
</OverflowY>
<Box
css={(theme) => ({
padding: "8px 0",
borderTop: `1px solid ${theme.palette.divider}`,
})}
>
<PopoverLink
to="/templates"
css={(theme) => ({
display: "flex",
alignItems: "center",
columnGap: 12,
color: theme.palette.primary.main,
})}
>
<OpenIcon css={{ width: 14, height: 14 }} />
<span>See all templates</span>
</PopoverLink>
</Box>
</PopoverContent>
</Popover>
);
}
function WorkspaceResultsRow({ template }: { template: Template }) {
return (
<PopoverLink
to={`/templates/${template.name}/workspace`}
css={{
display: "flex",
gap: 12,
alignItems: "center",
}}
>
<Avatar
src={template.icon}
fitImage
alt={template.display_name || "Coder template"}
sx={{
width: `${ICON_SIZE}px`,
height: `${ICON_SIZE}px`,
fontSize: `${ICON_SIZE * 0.5}px`,
fontWeight: 700,
}}
>
{template.display_name || "-"}
</Avatar>
<Box
css={(theme) => ({
color: theme.palette.text.primary,
display: "flex",
flexDirection: "column",
lineHeight: "140%",
fontSize: 14,
overflow: "hidden",
})}
>
<span css={{ whiteSpace: "nowrap", textOverflow: "ellipsis" }}>
{template.display_name || template.name || "[Unnamed]"}
</span>
<span
css={(theme) => ({
fontSize: 13,
color: theme.palette.text.secondary,
})}
>
{/*
* There are some templates that have -1 as their user count –
* basically functioning like a null value in JS. Can safely just
* treat them as if they were 0.
*/}
{template.active_user_count <= 0 ? "No" : template.active_user_count}{" "}
developer
{template.active_user_count === 1 ? "" : "s"}
</span>
</Box>
</PopoverLink>
);
}
function PopoverLink(props: RouterLinkProps) {
return (
<RouterLink
{...props}
css={(theme) => ({
color: theme.palette.text.primary,
padding: "8px 16px",
fontSize: 14,
outline: "none",
textDecoration: "none",
"&:focus": {
backgroundColor: theme.palette.action.focus,
},
"&:hover": {
textDecoration: "none",
backgroundColor: theme.palette.action.hover,
},
})}
/>
);
}
function sortTemplatesByUsersDesc(
templates: readonly Template[],
searchTerm: string,
) {
const allWhitespace = /^\s+$/.test(searchTerm);
if (allWhitespace) {
return templates;
}
const termMatcher = new RegExp(searchTerm.replaceAll(/[^\w]/g, "."), "i");
return templates
.filter(
(template) =>
termMatcher.test(template.display_name) ||
termMatcher.test(template.name),
)
.sort((t1, t2) => t2.active_user_count - t1.active_user_count)
.slice(0, 10);
}