-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathEnvironmentSelector.tsx
More file actions
315 lines (303 loc) · 11.7 KB
/
EnvironmentSelector.tsx
File metadata and controls
315 lines (303 loc) · 11.7 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { ChevronRightIcon, Cog8ToothIcon } from "@heroicons/react/20/solid";
import { DropdownIcon } from "~/assets/icons/DropdownIcon";
import { useNavigation } from "@remix-run/react";
import { useEffect, useRef, useState } from "react";
import { BranchEnvironmentIconSmall } from "~/assets/icons/EnvironmentIcons";
import { useEnvironment } from "~/hooks/useEnvironment";
import { useEnvironmentSwitcher } from "~/hooks/useEnvironmentSwitcher";
import { useFeatures } from "~/hooks/useFeatures";
import { useOrganization, type MatchedOrganization } from "~/hooks/useOrganizations";
import { useProject } from "~/hooks/useProject";
import { cn } from "~/utils/cn";
import { branchesPath, docsPath, v3BillingPath } from "~/utils/pathBuilder";
import { EnvironmentCombo, EnvironmentIcon, EnvironmentLabel, environmentFullTitle } from "../environments/EnvironmentLabel";
import { ButtonContent } from "../primitives/Buttons";
import { Header2 } from "../primitives/Headers";
import { Paragraph } from "../primitives/Paragraph";
import {
Popover,
PopoverContent,
PopoverMenuItem,
PopoverSectionHeader,
PopoverTrigger,
} from "../primitives/Popover";
import { TextLink } from "../primitives/TextLink";
import { SimpleTooltip } from "../primitives/Tooltip";
import { V4Badge } from "../V4Badge";
import { type SideMenuEnvironment, type SideMenuProject } from "./SideMenu";
import { Badge } from "../primitives/Badge";
export function EnvironmentSelector({
organization,
project,
environment,
className,
isCollapsed = false,
}: {
organization: MatchedOrganization;
project: SideMenuProject;
environment: SideMenuEnvironment;
className?: string;
isCollapsed?: boolean;
}) {
const { isManagedCloud } = useFeatures();
const [isMenuOpen, setIsMenuOpen] = useState(false);
const navigation = useNavigation();
const { urlForEnvironment } = useEnvironmentSwitcher();
useEffect(() => {
setIsMenuOpen(false);
}, [navigation.location?.pathname]);
const hasStaging = project.environments.some((env) => env.type === "STAGING");
return (
<Popover onOpenChange={(open) => setIsMenuOpen(open)} open={isMenuOpen}>
<SimpleTooltip
button={
<PopoverTrigger
className={cn(
"group flex h-8 items-center rounded pl-[0.4375rem] transition-colors hover:bg-charcoal-750",
isCollapsed ? "justify-center pr-0.5" : "justify-between pr-1",
className
)}
>
<span className="flex min-w-0 flex-1 items-center gap-1.5 overflow-hidden">
<EnvironmentIcon environment={environment} className="size-5 shrink-0" />
<span
className={cn(
"flex min-w-0 items-center overflow-hidden transition-all duration-200",
isCollapsed ? "max-w-0 opacity-0" : "max-w-[200px] opacity-100"
)}
>
<EnvironmentLabel environment={environment} className="text-2sm" disableTooltip />
</span>
</span>
<span
className={cn(
"overflow-hidden transition-all duration-200",
isCollapsed ? "max-w-0 opacity-0" : "max-w-[16px] opacity-100"
)}
>
<DropdownIcon className="size-4 min-w-4 text-text-dimmed transition group-hover:text-text-bright" />
</span>
</PopoverTrigger>
}
content={environmentFullTitle(environment)}
side="right"
sideOffset={8}
hidden={!isCollapsed}
buttonClassName="!h-8"
asChild
disableHoverableContent
/>
<PopoverContent
className="min-w-[14rem] overflow-y-auto p-0 scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600"
side={isCollapsed ? "right" : "bottom"}
sideOffset={isCollapsed ? 8 : 4}
align="start"
style={{ maxHeight: `calc(var(--radix-popover-content-available-height) - 10vh)` }}
>
<div className="flex flex-col gap-1 p-1">
{project.environments
.filter((env) => env.branchName === null)
.map((env) => {
switch (env.isBranchableEnvironment) {
case true: {
const branchEnvironments = project.environments.filter(
(e) => e.parentEnvironmentId === env.id
);
return (
<Branches
key={env.id}
parentEnvironment={env}
branchEnvironments={branchEnvironments}
currentEnvironment={environment}
/>
);
}
case false:
return (
<PopoverMenuItem
key={env.id}
to={urlForEnvironment(env)}
title={
<EnvironmentCombo environment={env} className="mx-auto grow text-2sm" />
}
isSelected={env.id === environment.id}
/>
);
}
})}
</div>
{!hasStaging && isManagedCloud && (
<>
<PopoverSectionHeader title="Additional environments" />
<div className="p-1">
<PopoverMenuItem
key="staging"
to={v3BillingPath(
organization,
"Upgrade to unlock a Staging environment for your projects."
)}
title={
<div className="flex w-full items-center justify-between">
<EnvironmentCombo environment={{ type: "STAGING" }} className="text-2sm" />
<span className="text-indigo-500">Upgrade</span>
</div>
}
isSelected={false}
/>
<PopoverMenuItem
key="preview"
to={v3BillingPath(
organization,
"Upgrade to unlock Preview environments for your projects."
)}
title={
<div className="flex w-full items-center justify-between">
<EnvironmentCombo environment={{ type: "PREVIEW" }} className="text-2sm" />
<span className="text-indigo-500">Upgrade</span>
</div>
}
isSelected={false}
/>
</div>
</>
)}
</PopoverContent>
</Popover>
);
}
function Branches({
parentEnvironment,
branchEnvironments,
currentEnvironment,
}: {
parentEnvironment: SideMenuEnvironment;
branchEnvironments: SideMenuEnvironment[];
currentEnvironment: SideMenuEnvironment;
}) {
const organization = useOrganization();
const project = useProject();
const environment = useEnvironment();
const { urlForEnvironment } = useEnvironmentSwitcher();
const navigation = useNavigation();
const [isMenuOpen, setMenuOpen] = useState(false);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
// Clear timeout on unmount
useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, []);
useEffect(() => {
setMenuOpen(false);
}, [navigation.location?.pathname]);
const handleMouseEnter = () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
setMenuOpen(true);
};
const handleMouseLeave = () => {
// Small delay before closing to allow moving to the content
timeoutRef.current = setTimeout(() => {
setMenuOpen(false);
}, 150);
};
const activeBranches = branchEnvironments.filter((env) => env.archivedAt === null);
const state =
branchEnvironments.length === 0
? "no-branches"
: activeBranches.length === 0
? "no-active-branches"
: "has-branches";
const currentBranchIsArchived = environment.archivedAt !== null;
return (
<Popover onOpenChange={(open) => setMenuOpen(open)} open={isMenuOpen}>
<div onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} className="flex">
<PopoverTrigger className="w-full justify-between overflow-hidden focus-custom">
<ButtonContent
variant="small-menu-item"
className="hover:bg-charcoal-750"
TrailingIcon={ChevronRightIcon}
trailingIconClassName="text-text-dimmed"
textAlignLeft
fullWidth
>
<EnvironmentCombo environment={parentEnvironment} className="mx-auto grow text-2sm" />
</ButtonContent>
</PopoverTrigger>
<PopoverContent
className="min-w-[16rem] overflow-y-auto p-0 scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600"
align="start"
style={{ maxHeight: `calc(var(--radix-popover-content-available-height) - 10vh)` }}
side="right"
alignOffset={0}
sideOffset={-4}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
<div className="flex flex-col gap-1 p-1">
{currentBranchIsArchived && (
<PopoverMenuItem
key={environment.id}
to={urlForEnvironment(environment)}
title={
<>
<span className="block w-full text-preview">{environment.branchName}</span>
<Badge variant="extra-small">Archived</Badge>
</>
}
icon={<BranchEnvironmentIconSmall className="size-4 shrink-0 text-preview" />}
isSelected={environment.id === currentEnvironment.id}
/>
)}
{state === "has-branches" ? (
<>
{branchEnvironments
.filter((env) => env.archivedAt === null)
.map((env) => (
<PopoverMenuItem
key={env.id}
to={urlForEnvironment(env)}
title={<span className="block w-full text-preview">{env.branchName}</span>}
icon={<BranchEnvironmentIconSmall className="size-4 shrink-0 text-preview" />}
isSelected={env.id === currentEnvironment.id}
/>
))}
</>
) : state === "no-branches" ? (
<div className="flex max-w-sm flex-col gap-1 p-2">
<div className="flex items-center gap-1">
<BranchEnvironmentIconSmall className="size-4 text-preview" />
<Header2>Create your first branch</Header2>
</div>
<Paragraph spacing variant="small">
Branches are a way to test new features in isolation before merging them into the
main environment.
</Paragraph>
<Paragraph variant="small">
Branches are only available when using <V4Badge inline /> or above. Read our{" "}
<TextLink to={docsPath("upgrade-to-v4")}>v4 upgrade guide</TextLink> to learn
more.
</Paragraph>
</div>
) : (
<div className="flex max-w-sm flex-col gap-1 p-2">
<Paragraph variant="extra-small">All branches are archived.</Paragraph>
</div>
)}
</div>
<div className="border-t border-charcoal-700 p-1">
<PopoverMenuItem
to={branchesPath(organization, project, environment)}
title="Manage branches"
icon={<Cog8ToothIcon className="size-4 text-text-dimmed" />}
leadingIconClassName="text-text-dimmed"
/>
</div>
</PopoverContent>
</div>
</Popover>
);
}