-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathroute.tsx
More file actions
316 lines (300 loc) · 11.3 KB
/
route.tsx
File metadata and controls
316 lines (300 loc) · 11.3 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
import { ExclamationCircleIcon } from "@heroicons/react/20/solid";
import { BookOpenIcon } from "@heroicons/react/24/solid";
import { type MetaFunction, Outlet, useLocation, useNavigation, useParams } from "@remix-run/react";
import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { formatDuration } from "@trigger.dev/core/v3/utils/durations";
import { typedjson, useTypedLoaderData } from "remix-typedjson";
import { RunsIcon } from "~/assets/icons/RunsIcon";
import { BatchesNone } from "~/components/BlankStatePanels";
import { ListPagination } from "~/components/ListPagination";
import { AdminDebugTooltip } from "~/components/admin/debugTooltip";
import { MainCenteredContainer, PageBody, PageContainer } from "~/components/layout/AppLayout";
import { LinkButton } from "~/components/primitives/Buttons";
import { DateTime } from "~/components/primitives/DateTime";
import { NavBar, PageAccessories, PageTitle } from "~/components/primitives/PageHeader";
import { Paragraph } from "~/components/primitives/Paragraph";
import {
collapsibleHandleClassName,
RESIZABLE_PANEL_ANIMATION,
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "~/components/primitives/Resizable";
import { Spinner } from "~/components/primitives/Spinner";
import {
Table,
TableBlankRow,
TableBody,
TableCell,
TableCellMenu,
TableHeader,
TableHeaderCell,
TableRow,
} from "~/components/primitives/Table";
import { SimpleTooltip } from "~/components/primitives/Tooltip";
import { BatchFilters, BatchListFilters } from "~/components/runs/v3/BatchFilters";
import {
allBatchStatuses,
BatchStatusCombo,
descriptionForBatchStatus,
} from "~/components/runs/v3/BatchStatus";
import { LiveTimer } from "~/components/runs/v3/LiveTimer";
import { useEnvironment } from "~/hooks/useEnvironment";
import { useOrganization } from "~/hooks/useOrganizations";
import { useProject } from "~/hooks/useProject";
import { redirectWithErrorMessage } from "~/models/message.server";
import { findProjectBySlug } from "~/models/project.server";
import { findEnvironmentBySlug } from "~/models/runtimeEnvironment.server";
import { type BatchList, BatchListPresenter } from "~/presenters/v3/BatchListPresenter.server";
import { requireUserId } from "~/services/session.server";
import {
docsPath,
EnvironmentParamSchema,
v3BatchPath,
v3BatchRunsPath,
} from "~/utils/pathBuilder";
export const meta: MetaFunction = () => {
return [
{
title: `Batches | Trigger.dev`,
},
];
};
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const userId = await requireUserId(request);
const { projectParam, organizationSlug, envParam } = EnvironmentParamSchema.parse(params);
const project = await findProjectBySlug(organizationSlug, projectParam, userId);
if (!project) {
return redirectWithErrorMessage("/", request, "Project not found");
}
const environment = await findEnvironmentBySlug(project.id, envParam, userId);
if (!environment) {
throw new Error("Environment not found");
}
const url = new URL(request.url);
const s = {
cursor: url.searchParams.get("cursor") ?? undefined,
direction: url.searchParams.get("direction") ?? undefined,
statuses: url.searchParams.getAll("statuses"),
period: url.searchParams.get("period") ?? undefined,
from: url.searchParams.get("from") ?? undefined,
to: url.searchParams.get("to") ?? undefined,
id: url.searchParams.get("id") ?? undefined,
};
const filters = BatchListFilters.parse(s);
const presenter = new BatchListPresenter();
const list = await presenter.call({
userId,
projectId: project.id,
...filters,
friendlyId: filters.id,
environmentId: environment.id,
});
return typedjson(list);
};
export default function Page() {
const { batches, hasFilters, hasAnyBatches, filters, pagination } =
useTypedLoaderData<typeof loader>();
const { batchParam } = useParams();
const isShowingInspector = batchParam !== undefined;
return (
<PageContainer>
<NavBar>
<PageTitle title="Batches" />
<PageAccessories>
<AdminDebugTooltip />
<LinkButton
variant={"docs/small"}
LeadingIcon={BookOpenIcon}
to={docsPath("/triggering")}
>
Batches docs
</LinkButton>
</PageAccessories>
</NavBar>
<PageBody scrollable={false}>
{!hasAnyBatches ? (
<MainCenteredContainer className="max-w-md">
<BatchesNone />
</MainCenteredContainer>
) : (
<ResizablePanelGroup orientation="horizontal" className="max-h-full">
<ResizablePanel id="batches-main" min={"100px"}>
<div className="grid h-full max-h-full grid-rows-[auto_1fr] overflow-hidden">
<div className="flex items-start justify-between gap-x-2 p-2">
<BatchFilters hasFilters={hasFilters} />
<div className="flex items-center justify-end gap-x-2">
<ListPagination list={{ pagination }} />
</div>
</div>
<BatchesTable
batches={batches}
filters={filters}
hasFilters={hasFilters}
pagination={pagination}
hasAnyBatches={hasAnyBatches}
/>
</div>
</ResizablePanel>
<ResizableHandle
id="batches-handle"
className={collapsibleHandleClassName(isShowingInspector)}
/>
<ResizablePanel
id="batches-inspector"
min="370px"
default="370px"
className="overflow-hidden"
collapsible
collapsed={!isShowingInspector}
onCollapseChange={() => {}}
collapsedSize="0px"
collapseAnimation={RESIZABLE_PANEL_ANIMATION}
>
<div className="h-full" style={{ minWidth: 370 }}>
<Outlet />
</div>
</ResizablePanel>
</ResizablePanelGroup>
)}
</PageBody>
</PageContainer>
);
}
function BatchesTable({ batches, hasFilters, filters }: BatchList) {
const navigation = useNavigation();
const location = useLocation();
const isLoading =
navigation.state !== "idle" && navigation.location?.pathname === location.pathname;
const organization = useOrganization();
const project = useProject();
const environment = useEnvironment();
const { batchParam } = useParams();
return (
<Table className="max-h-full overflow-y-auto">
<TableHeader>
<TableRow>
<TableHeaderCell>ID</TableHeaderCell>
<TableHeaderCell
tooltip={
<div className="flex flex-col divide-y divide-grid-dimmed">
{allBatchStatuses.map((status) => (
<div
key={status}
className="grid grid-cols-[8rem_1fr] gap-x-2 py-2 first:pt-1 last:pb-1"
>
<div className="mb-0.5 flex items-center gap-1.5 whitespace-nowrap">
<BatchStatusCombo status={status} />
</div>
<Paragraph variant="extra-small" className="!text-wrap text-text-dimmed">
{descriptionForBatchStatus(status)}
</Paragraph>
</div>
))}
</div>
}
>
Status
</TableHeaderCell>
<TableHeaderCell>Runs</TableHeaderCell>
<TableHeaderCell>Duration</TableHeaderCell>
<TableHeaderCell>Created</TableHeaderCell>
<TableHeaderCell>Finished</TableHeaderCell>
<TableHeaderCell>
<span className="sr-only">Go to batch</span>
</TableHeaderCell>
</TableRow>
</TableHeader>
<TableBody>
{batches.length === 0 ? (
<TableBlankRow colSpan={8}>
<div className="flex items-center justify-center">
<Paragraph className="w-auto">No batches match these filters</Paragraph>
</div>
</TableBlankRow>
) : (
batches.map((batch) => {
const basePath = v3BatchPath(organization, project, environment, batch);
const inspectorPath = `${basePath}${location.search}`;
const runsPath = v3BatchRunsPath(organization, project, environment, batch);
const isSelected = batchParam === batch.friendlyId;
return (
<TableRow key={batch.id} className={isSelected ? "bg-grid-dimmed" : undefined}>
<TableCell to={inspectorPath} isTabbableCell>
{batch.friendlyId}
</TableCell>
<TableCell to={inspectorPath}>
{batch.batchVersion === "v1" ? (
<SimpleTooltip
content="Upgrade to the latest SDK for batch statuses to appear."
disableHoverableContent
button={
<span className="flex items-center gap-1">
<ExclamationCircleIcon className="size-4 text-text-dimmed" />
<span>Legacy batch</span>
</span>
}
/>
) : (
<SimpleTooltip
content={descriptionForBatchStatus(batch.status)}
disableHoverableContent
button={<BatchStatusCombo status={batch.status} />}
/>
)}
</TableCell>
<TableCell to={inspectorPath}>{batch.runCount}</TableCell>
<TableCell
to={inspectorPath}
className="w-[1%]"
actionClassName="pr-0 tabular-nums"
>
{batch.finishedAt ? (
formatDuration(new Date(batch.createdAt), new Date(batch.finishedAt), {
style: "short",
})
) : (
<LiveTimer startTime={new Date(batch.createdAt)} />
)}
</TableCell>
<TableCell to={inspectorPath}>
<DateTime date={batch.createdAt} />
</TableCell>
<TableCell to={inspectorPath}>
{batch.finishedAt ? <DateTime date={batch.finishedAt} /> : "–"}
</TableCell>
<BatchActionsCell runsPath={runsPath} />
</TableRow>
);
})
)}
{isLoading && (
<TableBlankRow
colSpan={8}
className="absolute left-0 top-0 flex h-full w-full items-center justify-center gap-2 bg-charcoal-900/90"
>
<Spinner /> <span className="text-text-dimmed">Loading…</span>
</TableBlankRow>
)}
</TableBody>
</Table>
);
}
function BatchActionsCell({ runsPath }: { runsPath: string }) {
return (
<TableCellMenu
isSticky
hiddenButtons={
<LinkButton
to={runsPath}
variant="minimal/small"
TrailingIcon={RunsIcon}
trailingIconClassName="text-runs"
className="text-text-bright"
>
<span>View runs</span>
</LinkButton>
}
/>
);
}