-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathroute.tsx
More file actions
464 lines (441 loc) · 17.9 KB
/
route.tsx
File metadata and controls
464 lines (441 loc) · 17.9 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
import {
ArrowRightIcon,
ArrowUpCircleIcon,
BookOpenIcon,
ChatBubbleLeftEllipsisIcon,
InformationCircleIcon,
MapPinIcon,
} from "@heroicons/react/20/solid";
import { Form } from "@remix-run/react";
import { type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { tryCatch } from "@trigger.dev/core";
import { useState } from "react";
import { typedjson, useTypedLoaderData } from "remix-typedjson";
import { z } from "zod";
import { CloudProviderIcon } from "~/assets/icons/CloudProviderIcon";
import { FlagIcon } from "~/assets/icons/RegionIcons";
import { cloudProviderTitle } from "~/components/CloudProvider";
import { Feedback } from "~/components/Feedback";
import { V4Title } from "~/components/V4Badge";
import { AdminDebugTooltip } from "~/components/admin/debugTooltip";
import { MainCenteredContainer, PageBody, PageContainer } from "~/components/layout/AppLayout";
import { Badge } from "~/components/primitives/Badge";
import { Button, LinkButton } from "~/components/primitives/Buttons";
import { ClipboardField } from "~/components/primitives/ClipboardField";
import { CopyableText } from "~/components/primitives/CopyableText";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "~/components/primitives/Dialog";
import { InfoPanel } from "~/components/primitives/InfoPanel";
import { NavBar, PageAccessories, PageTitle } from "~/components/primitives/PageHeader";
import { Paragraph } from "~/components/primitives/Paragraph";
import * as Property from "~/components/primitives/PropertyTable";
import {
Table,
TableBlankRow,
TableBody,
TableCell,
TableCellMenu,
TableHeader,
TableHeaderCell,
TableRow,
} from "~/components/primitives/Table";
import { TextLink } from "~/components/primitives/TextLink";
import { useFeatures } from "~/hooks/useFeatures";
import { useOrganization } from "~/hooks/useOrganizations";
import { useHasAdminAccess } from "~/hooks/useUser";
import { redirectWithErrorMessage, redirectWithSuccessMessage } from "~/models/message.server";
import { findProjectBySlug } from "~/models/project.server";
import { type Region, RegionsPresenter } from "~/presenters/v3/RegionsPresenter.server";
import { requireUser } from "~/services/session.server";
import {
docsPath,
EnvironmentParamSchema,
ProjectParamSchema,
regionsPath,
v3BillingPath,
} from "~/utils/pathBuilder";
import { SetDefaultRegionService } from "~/v3/services/setDefaultRegion.server";
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const user = await requireUser(request);
const { projectParam } = ProjectParamSchema.parse(params);
const presenter = new RegionsPresenter();
const [error, result] = await tryCatch(
presenter.call({
userId: user.id,
projectSlug: projectParam,
isAdmin: user.admin || user.isImpersonating,
})
);
if (error) {
throw new Response(undefined, {
status: 400,
statusText: error.message,
});
}
return typedjson(result);
};
const FormSchema = z.object({
regionId: z.string(),
});
export const action = async ({ request, params }: ActionFunctionArgs) => {
const user = await requireUser(request);
const { organizationSlug, projectParam, envParam } = EnvironmentParamSchema.parse(params);
const project = await findProjectBySlug(organizationSlug, projectParam, user.id);
const redirectPath = regionsPath(
{ slug: organizationSlug },
{ slug: projectParam },
{ slug: envParam }
);
if (!project) {
throw redirectWithErrorMessage(redirectPath, request, "Project not found");
}
const formData = await request.formData();
const parsedFormData = FormSchema.safeParse(Object.fromEntries(formData));
if (!parsedFormData.success) {
throw redirectWithErrorMessage(redirectPath, request, "No region specified");
}
const service = new SetDefaultRegionService();
const [error, result] = await tryCatch(
service.call({
projectId: project.id,
regionId: parsedFormData.data.regionId,
isAdmin: user.admin || user.isImpersonating,
})
);
if (error) {
return redirectWithErrorMessage(redirectPath, request, error.message);
}
return redirectWithSuccessMessage(redirectPath, request, `Set ${result.name} as default`);
};
export default function Page() {
const { regions, isPaying } = useTypedLoaderData<typeof loader>();
const organization = useOrganization();
const isAdmin = useHasAdminAccess();
const { isManagedCloud } = useFeatures();
return (
<PageContainer>
<NavBar>
<PageTitle title={<V4Title>Regions</V4Title>} />
<PageAccessories>
<AdminDebugTooltip>
<Property.Table>
{regions.map((region) => (
<Property.Item key={region.id}>
<Property.Label>{region.name}</Property.Label>
<Property.Value>{region.id}</Property.Value>
</Property.Item>
))}
</Property.Table>
</AdminDebugTooltip>
</PageAccessories>
</NavBar>
<PageBody scrollable={false}>
<div className="grid max-h-full min-h-full grid-rows-[1fr]">
{regions.length === 0 ? (
<MainCenteredContainer className="max-w-md">
<div className="text-center">
<Paragraph>No regions found for this project.</Paragraph>
</div>
</MainCenteredContainer>
) : (
<>
<div className="overflow-x-auto">
<Table>
<TableHeader>
<TableRow>
<TableHeaderCell>Region</TableHeaderCell>
<TableHeaderCell>Cloud Provider</TableHeaderCell>
<TableHeaderCell>Location</TableHeaderCell>
<TableHeaderCell>Static IPs</TableHeaderCell>
{isAdmin && <TableHeaderCell>Admin</TableHeaderCell>}
<TableHeaderCell
alignment="right"
tooltip={
<div className="max-w-[12rem]">
<Paragraph variant="small">
When you trigger a run it will execute in your default region, unless
you override the region when triggering.
</Paragraph>
<LinkButton
variant="docs/small"
LeadingIcon={BookOpenIcon}
to={docsPath("triggering#region")}
className="mb-1 mt-3"
>
Read docs
</LinkButton>
</div>
}
>
Default region
</TableHeaderCell>
</TableRow>
</TableHeader>
<TableBody>
{regions.length === 0 ? (
<TableBlankRow colSpan={5}>
<Paragraph>There are no regions for this project</Paragraph>
</TableBlankRow>
) : (
regions.map((region) => {
return (
<TableRow key={region.id}>
<TableCell isTabbableCell>
<CopyableText value={region.name} />
</TableCell>
<TableCell>
{region.cloudProvider ? (
<span className="flex items-center gap-2">
<CloudProviderIcon
provider={region.cloudProvider}
className="size-6"
/>
{cloudProviderTitle(region.cloudProvider)}
</span>
) : (
"–"
)}
</TableCell>
<TableCell>
<span className="flex items-center gap-2">
{region.location ? (
<FlagIcon region={region.location} className="size-5" />
) : null}
{region.description ?? "–"}
</span>
</TableCell>
<TableCell>
{region.staticIPs === null ? (
<LinkButton
variant="secondary/small"
to={v3BillingPath(
organization,
"Upgrade your plan to unlock static IPs"
)}
LeadingIcon={ArrowUpCircleIcon}
leadingIconClassName="text-indigo-500"
>
Unlock static IPs
</LinkButton>
) : region.staticIPs !== undefined ? (
<ClipboardField
value={region.staticIPs}
variant={"secondary/small"}
/>
) : (
"Not available"
)}
</TableCell>
{isAdmin && (
<TableCell>{region.isHidden ? "Hidden" : "Visible"}</TableCell>
)}
{region.isDefault ? (
<TableCell alignment="right">
<Badge variant="small" className="inline-grid">
Default
</Badge>
</TableCell>
) : (
<TableCellMenu
className="pl-32"
isSticky
visibleButtons={
<SetDefaultDialog regions={regions} newDefaultRegion={region} />
}
/>
)}
</TableRow>
);
})
)}
<TableRow className="h-[3.125rem]">
<TableCell colSpan={isAdmin ? 5 : 4}>
<Paragraph variant="extra-small">Suggest a new region</Paragraph>
</TableCell>
<TableCellMenu
alignment="right"
isSticky
visibleButtons={
<Feedback
button={
<Button
variant="secondary/small"
LeadingIcon={ChatBubbleLeftEllipsisIcon}
leadingIconClassName="text-indigo-500"
>
Suggest a region…
</Button>
}
defaultValue="region"
/>
}
/>
</TableRow>
</TableBody>
</Table>
{isManagedCloud && (
<InfoPanel
icon={InformationCircleIcon}
iconClassName="size-4"
variant="minimal"
panelClassName="max-w-full gap-1"
>
<Paragraph variant="extra-small" className="flex items-baseline gap-x-0.5">
Trigger.dev is fully GDPR compliant. Learn more in our{" "}
<TextLink to="https://security.trigger.dev">security portal</TextLink> or{" "}
<Feedback
button={
<Paragraph
variant="extra-small"
className="cursor-pointer text-indigo-500 transition hover:text-indigo-400"
>
get in touch
</Paragraph>
}
defaultValue="help"
/>
.
</Paragraph>
</InfoPanel>
)}
</div>
</>
)}
</div>
</PageBody>
</PageContainer>
);
}
function SetDefaultDialog({
regions,
newDefaultRegion,
}: {
regions: Region[];
newDefaultRegion: Region;
}) {
const [isOpen, setIsOpen] = useState(false);
const currentDefaultRegion = regions.find((r) => r.isDefault);
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button
variant="secondary/small"
LeadingIcon={MapPinIcon}
leadingIconClassName="text-blue-500"
iconSpacing="gap-2"
className="pl-2"
>
Set as default…
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Set as default region</DialogTitle>
</DialogHeader>
<DialogDescription>
<Paragraph>
Are you sure you want to set {newDefaultRegion.name} as your new default region?
</Paragraph>
<div className="my-4 flex">
<div className="flex flex-1 flex-col rounded-md border border-grid-dimmed">
<div className="border-b border-grid-dimmed bg-charcoal-800 p-3 font-medium">
<Paragraph variant="small/bright">Current default</Paragraph>
</div>
<div className="border-b border-grid-dimmed p-3">
<Paragraph variant="small">{currentDefaultRegion?.name ?? "–"}</Paragraph>
</div>
<div className="border-b border-grid-dimmed p-3">
<Paragraph variant="small" className="flex items-center gap-2">
{currentDefaultRegion?.cloudProvider ? (
<>
<CloudProviderIcon
provider={currentDefaultRegion.cloudProvider}
className="size-6"
/>
{cloudProviderTitle(currentDefaultRegion.cloudProvider)}
</>
) : (
"–"
)}
</Paragraph>
</div>
<div className="p-3">
<Paragraph variant="small" className="flex items-center gap-2">
{currentDefaultRegion?.location ? (
<FlagIcon region={currentDefaultRegion.location} className="size-5" />
) : null}
{currentDefaultRegion?.description ?? "–"}
</Paragraph>
</div>
</div>
{/* Middle column with arrow */}
<div className="flex items-center justify-center px-3">
<div className="flex size-10 items-center justify-center rounded-full border border-grid-dimmed bg-charcoal-800 p-2">
<ArrowRightIcon className="size-4 text-text-dimmed" />
</div>
</div>
{/* Right column */}
<div className="flex flex-1 flex-col rounded-md border border-grid-dimmed">
<div className="border-b border-grid-dimmed bg-charcoal-800 p-3 font-medium">
<Paragraph variant="small/bright">New default</Paragraph>
</div>
<div className="border-b border-grid-dimmed p-3">
<Paragraph variant="small">{newDefaultRegion.name}</Paragraph>
</div>
<div className="border-b border-grid-dimmed p-3">
<Paragraph variant="small" className="flex items-center gap-2">
{newDefaultRegion.cloudProvider ? (
<>
<CloudProviderIcon
provider={newDefaultRegion.cloudProvider}
className="size-6"
/>
{cloudProviderTitle(newDefaultRegion.cloudProvider)}
</>
) : (
"–"
)}
</Paragraph>
</div>
<div className="p-3">
<Paragraph variant="small" className="flex items-center gap-2">
{newDefaultRegion.location ? (
<FlagIcon region={newDefaultRegion.location} className="size-5" />
) : null}
{newDefaultRegion.description ?? "–"}
</Paragraph>
</div>
</div>
</div>
<Paragraph>
Runs triggered from now on will execute in "{newDefaultRegion.name}", unless you{" "}
<TextLink to={docsPath("triggering#region")}>override when triggering</TextLink>.
</Paragraph>
</DialogDescription>
<DialogFooter>
<Button variant="secondary/medium" onClick={() => setIsOpen(false)}>
Cancel
</Button>
<Form method="post">
<Button
variant="primary/medium"
type="submit"
name="regionId"
shortcut={{ modifiers: ["mod"], key: "enter" }}
value={newDefaultRegion.id}
>
Set as default
</Button>
</Form>
</DialogFooter>
</DialogContent>
</Dialog>
);
}