-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathroute.tsx
More file actions
551 lines (509 loc) · 19.4 KB
/
route.tsx
File metadata and controls
551 lines (509 loc) · 19.4 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import { conform, useForm } from "@conform-to/react";
import { parse } from "@conform-to/zod";
import {
BookOpenIcon,
InformationCircleIcon,
LockClosedIcon,
MagnifyingGlassIcon,
PencilSquareIcon,
PlusIcon,
TrashIcon,
} from "@heroicons/react/20/solid";
import { Form, type MetaFunction, Outlet, useActionData, useFetcher, useNavigation } from "@remix-run/react";
import {
type ActionFunctionArgs,
type LoaderFunctionArgs,
json,
} from "@remix-run/server-runtime";
import { useEffect, useMemo, useState } from "react";
import { typedjson, useTypedLoaderData } from "remix-typedjson";
import { z } from "zod";
import { EnvironmentCombo } from "~/components/environments/EnvironmentLabel";
import { PageBody, PageContainer } from "~/components/layout/AppLayout";
import { Button, LinkButton } from "~/components/primitives/Buttons";
import { ClipboardField } from "~/components/primitives/ClipboardField";
import { CopyableText } from "~/components/primitives/CopyableText";
import { Dialog, DialogContent, DialogHeader, DialogTrigger } from "~/components/primitives/Dialog";
import { Fieldset } from "~/components/primitives/Fieldset";
import { FormButtons } from "~/components/primitives/FormButtons";
import { FormError } from "~/components/primitives/FormError";
import { Header2 } from "~/components/primitives/Headers";
import { Hint } from "~/components/primitives/Hint";
import { InfoPanel } from "~/components/primitives/InfoPanel";
import { Input } from "~/components/primitives/Input";
import { InputGroup } from "~/components/primitives/InputGroup";
import { Label } from "~/components/primitives/Label";
import { NavBar, PageAccessories, PageTitle } from "~/components/primitives/PageHeader";
import { Paragraph } from "~/components/primitives/Paragraph";
import { Switch } from "~/components/primitives/Switch";
import {
Table,
TableBody,
TableCell,
TableCellMenu,
TableHeader,
TableHeaderCell,
TableRow,
} from "~/components/primitives/Table";
import { SimpleTooltip } from "~/components/primitives/Tooltip";
import { prisma } from "~/db.server";
import { useEnvironment } from "~/hooks/useEnvironment";
import { useFuzzyFilter } from "~/hooks/useFuzzyFilter";
import { useOrganization } from "~/hooks/useOrganizations";
import { useProject } from "~/hooks/useProject";
import { redirectWithSuccessMessage } from "~/models/message.server";
import {
type EnvironmentVariableWithSetValues,
EnvironmentVariablesPresenter,
} from "~/presenters/v3/EnvironmentVariablesPresenter.server";
import { requireUserId } from "~/services/session.server";
import { cn } from "~/utils/cn";
import {
EnvironmentParamSchema,
ProjectParamSchema,
docsPath,
v3EnvironmentVariablesPath,
v3NewEnvironmentVariablesPath,
} from "~/utils/pathBuilder";
import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server";
import {
DeleteEnvironmentVariableValue,
EditEnvironmentVariableValue,
} from "~/v3/environmentVariables/repository";
export const meta: MetaFunction = () => {
return [
{
title: `Environment variables | Trigger.dev`,
},
];
};
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const userId = await requireUserId(request);
const { projectParam } = ProjectParamSchema.parse(params);
try {
const presenter = new EnvironmentVariablesPresenter();
const { environmentVariables, environments, hasStaging } = await presenter.call({
userId,
projectSlug: projectParam,
});
return typedjson({
environmentVariables,
environments,
hasStaging,
});
} catch (error) {
console.error(error);
throw new Response(undefined, {
status: 400,
statusText: "Something went wrong, if this problem persists please contact support.",
});
}
};
const schema = z.discriminatedUnion("action", [
z.object({ action: z.literal("edit"), ...EditEnvironmentVariableValue.shape }),
z.object({
action: z.literal("delete"),
key: z.string(),
...DeleteEnvironmentVariableValue.shape,
}),
]);
export const action = async ({ request, params }: ActionFunctionArgs) => {
const userId = await requireUserId(request);
const { organizationSlug, projectParam, envParam } = EnvironmentParamSchema.parse(params);
if (request.method.toUpperCase() !== "POST") {
return { status: 405, body: "Method Not Allowed" };
}
const formData = await request.formData();
const submission = parse(formData, { schema });
if (!submission.value) {
return json(submission);
}
const project = await prisma.project.findUnique({
where: {
slug: params.projectParam,
organization: {
members: {
some: {
userId,
},
},
},
},
select: {
id: true,
},
});
if (!project) {
submission.error.key = ["Project not found"];
return json(submission);
}
switch (submission.value.action) {
case "edit": {
const repository = new EnvironmentVariablesRepository(prisma);
const result = await repository.editValue(project.id, submission.value);
if (!result.success) {
submission.error.key = [result.error];
return json(submission);
}
return json({ ...submission, success: true });
}
case "delete": {
const repository = new EnvironmentVariablesRepository(prisma);
const result = await repository.deleteValue(project.id, submission.value);
if (!result.success) {
submission.error.key = [result.error];
return json(submission);
}
return redirectWithSuccessMessage(
v3EnvironmentVariablesPath(
{ slug: organizationSlug },
{ slug: projectParam },
{ slug: envParam }
),
request,
`Deleted ${submission.value.key} environment variable`
);
}
}
};
export default function Page() {
const [revealAll, setRevealAll] = useState(false);
const { environmentVariables, environments } = useTypedLoaderData<typeof loader>();
const organization = useOrganization();
const project = useProject();
const environment = useEnvironment();
const { filterText, setFilterText, filteredItems } =
useFuzzyFilter<EnvironmentVariableWithSetValues>({
items: environmentVariables,
keys: ["key", "value"],
});
// Add isFirst and isLast to each environment variable
// They're set based on if they're the first or last time that `key` has been seen in the list
const groupedEnvironmentVariables = useMemo(() => {
// Create a map to track occurrences of each key
const keyOccurrences = new Map<string, number>();
// First pass: count total occurrences of each key
filteredItems.forEach((variable) => {
keyOccurrences.set(variable.key, (keyOccurrences.get(variable.key) || 0) + 1);
});
// Second pass: add isFirstTime, isLastTime, and occurrences flags
const seenKeys = new Set<string>();
const currentOccurrences = new Map<string, number>();
return filteredItems.map((variable) => {
// Track current occurrence number for this key
const currentCount = (currentOccurrences.get(variable.key) || 0) + 1;
currentOccurrences.set(variable.key, currentCount);
const totalOccurrences = keyOccurrences.get(variable.key) || 1;
const isFirstTime = !seenKeys.has(variable.key);
const isLastTime = currentCount === totalOccurrences;
if (isFirstTime) {
seenKeys.add(variable.key);
}
return {
...variable,
isFirstTime,
isLastTime,
occurences: totalOccurrences,
};
});
}, [filteredItems]);
return (
<PageContainer>
<NavBar>
<PageTitle title="Environment variables" />
<PageAccessories>
<LinkButton
LeadingIcon={BookOpenIcon}
to={docsPath("v3/deploy-environment-variables")}
variant="docs/small"
>
Environment variables docs
</LinkButton>
</PageAccessories>
</NavBar>
<PageBody scrollable={false}>
<div className={cn("flex h-full flex-col")}>
{environmentVariables.length > 0 && (
<div className="flex items-center justify-between gap-2 px-2 py-2">
<Input
placeholder="Search variables"
variant="tertiary"
icon={MagnifyingGlassIcon}
fullWidth={true}
value={filterText}
onChange={(e) => setFilterText(e.target.value)}
autoFocus
/>
<div className="flex items-center justify-end gap-2">
<Switch
variant="small"
label="Reveal values"
checked={revealAll}
onCheckedChange={(e) => setRevealAll(e.valueOf())}
/>
<LinkButton
to={v3NewEnvironmentVariablesPath(organization, project, environment)}
variant="primary/small"
LeadingIcon={PlusIcon}
shortcut={{ key: "n" }}
>
Add new
</LinkButton>
</div>
</div>
)}
<Table containerClassName={cn(filteredItems.length === 0 && "border-t-0")}>
<TableHeader>
<TableRow>
<TableHeaderCell className="w-[25%]">Key</TableHeaderCell>
<TableHeaderCell className="w-[55%]">Value</TableHeaderCell>
<TableHeaderCell className="w-[20%]">Environment</TableHeaderCell>
<TableHeaderCell hiddenLabel className="pl-24">
Actions
</TableHeaderCell>
</TableRow>
</TableHeader>
<TableBody>
{groupedEnvironmentVariables.length > 0 ? (
groupedEnvironmentVariables.map((variable) => {
const cellClassName = "py-2";
let borderedCellClassName = "";
if (variable.occurences > 1) {
borderedCellClassName =
"relative after:absolute after:bottom-0 after:left-0 after:right-0 after:h-px after:bg-grid-bright group-hover/table-row:after:bg-grid-bright group-hover/table-row:before:bg-grid-bright";
if (variable.isLastTime) {
borderedCellClassName = "";
} else if (variable.isFirstTime) {
}
} else {
}
return (
<TableRow
key={`${variable.id}-${variable.environment.id}`}
className={
variable.isLastTime ? "after:bg-charcoal-600" : "after:bg-transparent"
}
>
<TableCell className={cellClassName}>
{variable.isFirstTime ? (
<CopyableText value={variable.key} className="font-mono" />
) : null}
</TableCell>
<TableCell
className={cn(cellClassName, borderedCellClassName, "after:left-3")}
>
{variable.isSecret ? (
<SimpleTooltip
button={
<div className="flex items-center gap-x-1">
<LockClosedIcon className="size-3 text-text-dimmed" />
<span className="text-xs text-text-dimmed">Secret</span>
</div>
}
content="This variable is secret and cannot be revealed."
/>
) : (
<ClipboardField
secure={!revealAll}
value={variable.value}
variant={"secondary/small"}
fullWidth={true}
/>
)}
</TableCell>
<TableCell className={cn(cellClassName, borderedCellClassName)}>
<EnvironmentCombo environment={variable.environment} className="text-sm" />
</TableCell>
<TableCellMenu
className={cn(cellClassName, borderedCellClassName)}
isSticky
hiddenButtons={
<>
<EditEnvironmentVariablePanel
variable={variable}
revealAll={revealAll}
/>
<DeleteEnvironmentVariableButton variable={variable} />
</>
}
/>
</TableRow>
);
})
) : (
<TableRow>
<TableCell colSpan={4}>
{environmentVariables.length === 0 ? (
<div className="flex flex-col items-center justify-center gap-y-4 py-8">
<Header2>You haven't set any environment variables yet.</Header2>
<LinkButton
to={v3NewEnvironmentVariablesPath(organization, project, environment)}
variant="primary/medium"
LeadingIcon={PlusIcon}
shortcut={{ key: "n" }}
>
Add new
</LinkButton>
</div>
) : (
<div className="flex flex-col items-center justify-center gap-y-4 py-8">
<Paragraph>No variables match your search.</Paragraph>
</div>
)}
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
<div className="-mt-px w-full border-t border-grid-dimmed">
<InfoPanel icon={InformationCircleIcon} variant="minimal" panelClassName="max-w-fit">
Dev environment variables specified here will be overridden by ones in your .env file
when running locally.
</InfoPanel>
</div>
</div>
<Outlet />
</PageBody>
</PageContainer>
);
}
function EditEnvironmentVariablePanel({
variable,
revealAll,
}: {
variable: EnvironmentVariableWithSetValues;
revealAll: boolean;
}) {
const [isOpen, setIsOpen] = useState(false);
const [isSecret, setIsSecret] = useState(variable.isSecret);
const fetcher = useFetcher<typeof action>();
const lastSubmission = fetcher.data as any;
const isLoading = fetcher.state !== "idle";
// Close dialog on successful submission
useEffect(() => {
if (lastSubmission?.success && fetcher.state === "idle") {
setIsOpen(false);
}
}, [lastSubmission?.success, fetcher.state]);
const [form, { id, environmentId, value }] = useForm({
id: `edit-environment-variable-${variable.id}-${variable.environment.id}`,
// TODO: type this
lastSubmission: lastSubmission as any,
onValidate({ formData }) {
return parse(formData, { schema });
},
shouldRevalidate: "onSubmit",
});
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button variant="small-menu-item" LeadingIcon={PencilSquareIcon} fullWidth textAlignLeft>
Edit
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>Edit environment variable</DialogHeader>
<fetcher.Form method="post" {...form.props}>
<input type="hidden" name="action" value="edit" />
<input type="hidden" name="isSecret" value={isSecret ? "true" : "false"} />
<input {...conform.input(id, { type: "hidden" })} value={variable.id} />
<input
{...conform.input(environmentId, { type: "hidden" })}
value={variable.environment.id}
/>
<FormError id={id.errorId}>{id.error}</FormError>
<FormError id={environmentId.errorId}>{environmentId.error}</FormError>
<Fieldset>
<InputGroup fullWidth className="mt-2 gap-0">
<Label>Key</Label>
<CopyableText value={variable.key} className="w-fit font-mono text-sm" />
</InputGroup>
<InputGroup fullWidth>
<Label>Environment</Label>
<EnvironmentCombo environment={variable.environment} className="text-sm" />
</InputGroup>
<InputGroup className="w-auto">
<Switch
variant="medium"
label={<span className="text-text-bright">Secret value</span>}
checked={isSecret}
disabled={variable.isSecret}
className="-ml-2 inline-flex w-fit"
onCheckedChange={setIsSecret}
/>
<Hint className="-mt-1">
{variable.isSecret
? "This variable is secret and cannot be changed back."
: "Once enabled, the value will be hidden and cannot be revealed again."}
</Hint>
</InputGroup>
<InputGroup fullWidth>
<Label>Value</Label>
<Input
{...conform.input(value, { type: "text" })}
placeholder={variable.isSecret ? "Set new secret value" : "Not set"}
defaultValue={variable.value}
type={"text"}
/>
<FormError id={value.errorId}>{value.error}</FormError>
</InputGroup>
<FormError>{form.error}</FormError>
<FormButtons
confirmButton={
<Button type="submit" variant="primary/medium" disabled={isLoading}>
{isLoading ? "Saving…" : "Save"}
</Button>
}
cancelButton={
<Button onClick={() => setIsOpen(false)} variant="tertiary/medium" type="button">
Cancel
</Button>
}
/>
</Fieldset>
</fetcher.Form>
</DialogContent>
</Dialog>
);
}
function DeleteEnvironmentVariableButton({
variable,
}: {
variable: EnvironmentVariableWithSetValues;
}) {
const lastSubmission = useActionData();
const navigation = useNavigation();
const isLoading =
navigation.state !== "idle" &&
navigation.formMethod === "post" &&
navigation.formData?.get("action") === "delete";
const [form, { id }] = useForm({
id: "delete-environment-variable",
// TODO: type this
lastSubmission: lastSubmission as any,
onValidate({ formData }) {
return parse(formData, { schema });
},
shouldRevalidate: "onSubmit",
});
return (
<Form method="post" {...form.props}>
<input type="hidden" name="id" value={variable.id} />
<input type="hidden" name="key" value={variable.key} />
<input type="hidden" name="environmentId" value={variable.environment.id} />
<Button
name="action"
value="delete"
type="submit"
variant="small-menu-item"
fullWidth
textAlignLeft
LeadingIcon={TrashIcon}
leadingIconClassName="text-rose-500 group-hover/button:text-text-bright transition-colors"
className="ml-0.5 transition-colors group-hover/button:bg-error"
>
{isLoading ? "Deleting" : "Delete"}
</Button>
</Form>
);
}