-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathLoopsListView.tsx
More file actions
360 lines (338 loc) · 10.9 KB
/
Copy pathLoopsListView.tsx
File metadata and controls
360 lines (338 loc) · 10.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
import {
ChatCircleDotsIcon,
CloudIcon,
PlusIcon,
RepeatIcon,
StopIcon,
} from "@phosphor-icons/react";
import type { LoopSchemas } from "@posthog/api-client/loops";
import type { UserBasic } from "@posthog/shared/domain-types";
import { useOrgMembers } from "@posthog/ui/features/canvas/hooks/useOrgMembers";
import { StopCloudRunDialog } from "@posthog/ui/features/sessions/components/StopCloudRunDialog";
import { useSetHeaderContent } from "@posthog/ui/hooks/useSetHeaderContent";
import { Button } from "@posthog/ui/primitives/Button";
import { toast } from "@posthog/ui/primitives/toast";
import {
navigateToNewLoop,
navigateToTaskDetail,
} from "@posthog/ui/router/navigationBridge";
import { Flex, Heading, Text } from "@radix-ui/themes";
import { useMemo, useState } from "react";
import { useLoopBuilderSessions } from "../hooks/useLoopBuilderSessions";
import { useLoopLimits, useLoops } from "../hooks/useLoops";
import {
type LoopBuilderSession,
useLoopBuilderSessionStore,
} from "../loopBuilderSessionStore";
import { useLoopDraftStore } from "../loopDraftStore";
import type { LoopTemplate } from "../loopTemplates";
import { LoopBuilderComposer } from "./LoopBuilderComposer";
import { LoopsEmptyNotice, LoopsSkeleton } from "./LoopFallbacks";
import { LoopRow } from "./LoopRow";
import { LoopsEmptyState } from "./LoopsEmptyState";
import { LoopTemplatesSection } from "./LoopTemplatesSection";
/** Copy shown when the project is at its loop cap. `max` comes from the backend so the number
* never drifts from the limit the server actually enforces. */
function loopLimitReason(max: number): string {
return `You've reached the limit of ${max} loops for this project. Delete one to add another.`;
}
const EMPTY_MEMBERS: UserBasic[] = [];
const EMPTY_BUILDER_SESSIONS: LoopBuilderSession[] = [];
const SECTION_PREVIEW_COUNT = 5;
function startBlankLoop(): void {
useLoopDraftStore.getState().setPrefill(null);
navigateToNewLoop();
}
function resumeBuilderSession(taskId: string): void {
navigateToTaskDetail(taskId);
}
function removeBuilderSession(taskId: string): void {
useLoopBuilderSessionStore.getState().removeSession(taskId);
}
function startLoopFromTemplate(template: LoopTemplate): void {
useLoopDraftStore
.getState()
.setPrefill({ description: template.description, ...template.build() });
navigateToNewLoop();
}
export function LoopsListView() {
const { data: loops, isLoading, isError, error } = useLoops();
const limits = useLoopLimits();
const limitReason =
limits?.atLimit === true ? loopLimitReason(limits.max) : null;
const headerContent = useMemo(
() => (
<Flex align="center" gap="2" className="w-full min-w-0">
<RepeatIcon size={12} className="shrink-0 text-gray-10" />
<Text
className="truncate whitespace-nowrap font-medium text-[13px]"
title="Loops"
>
Loops
</Text>
</Flex>
),
[],
);
useSetHeaderContent(headerContent);
const builderSessions = useLoopBuilderSessions();
const allLoops = loops ?? [];
const teamLoops = allLoops.filter((loop) => loop.visibility === "team");
const {
members,
isLoading: membersLoading,
isError: membersError,
isComplete: membersComplete,
} = useOrgMembers({ enabled: teamLoops.length > 0 });
return (
<LoopsListViewPresentation
loops={allLoops}
isLoading={isLoading}
error={isError ? error : null}
limitReason={limitReason}
members={members}
membersLoading={membersLoading}
membersError={membersError}
membersComplete={membersComplete}
builderSessions={builderSessions}
onStartBlank={startBlankLoop}
onStartFromTemplate={startLoopFromTemplate}
onResumeBuilderSession={resumeBuilderSession}
onBuilderSessionStopped={removeBuilderSession}
/>
);
}
interface LoopsListViewPresentationProps {
loops: LoopSchemas.Loop[];
isLoading?: boolean;
error?: unknown;
limitReason?: string | null;
members?: UserBasic[];
membersLoading?: boolean;
membersError?: boolean;
membersComplete?: boolean;
builderSessions?: LoopBuilderSession[];
onStartBlank: () => void;
onStartFromTemplate: (template: LoopTemplate) => void;
onResumeBuilderSession?: (taskId: string) => void;
onBuilderSessionStopped?: (taskId: string) => void;
}
export function LoopsListViewPresentation({
loops,
isLoading = false,
error = null,
limitReason = null,
members = EMPTY_MEMBERS,
membersLoading = false,
membersError = false,
membersComplete = true,
builderSessions = EMPTY_BUILDER_SESSIONS,
onStartBlank,
onStartFromTemplate,
onResumeBuilderSession,
onBuilderSessionStopped,
}: LoopsListViewPresentationProps) {
const personalLoops = loops.filter((loop) => loop.visibility === "personal");
const teamLoops = loops.filter((loop) => loop.visibility === "team");
return (
<Flex direction="column" className="h-full min-h-0">
<div className="min-h-0 flex-1 overflow-auto">
<Flex
direction="column"
gap="6"
className="mx-auto w-full max-w-5xl px-8 py-8"
>
<Flex align="center" justify="between" gap="3">
<Flex direction="column" gap="1" className="min-w-0">
<Flex align="center" gap="2">
<Heading className="font-bold text-2xl">Loops</Heading>
<Flex
align="center"
className="gap-1.5 rounded-full bg-(--accent-a3) px-2.5 py-1"
>
<CloudIcon
size={12}
weight="fill"
className="text-(--accent-11)"
/>
<Text className="font-medium text-(--accent-11) text-[11px]">
Runs entirely in the cloud
</Text>
</Flex>
</Flex>
<Text color="gray" className="max-w-2xl text-sm">
Put your work on autopilot. Loops run on a schedule, on an API
call, or when something happens on GitHub. You can finally close
the laptop!
</Text>
</Flex>
<Button
variant="soft"
color="gray"
size="2"
onClick={onStartBlank}
disabled={limitReason != null}
disabledReason={limitReason}
>
<PlusIcon size={14} />
Create manually
</Button>
</Flex>
{isLoading ? (
<LoopsSkeleton />
) : error ? (
<LoopsEmptyNotice
title="Couldn't load loops."
hint={
error instanceof Error
? error.message
: "The loops API returned an error."
}
/>
) : loops.length > 0 ? (
<Flex direction="column" gap="5">
{personalLoops.length > 0 ? (
<LoopListSection title="Personal loops" loops={personalLoops} />
) : null}
{teamLoops.length > 0 ? (
<LoopListSection
title="Team loops"
loops={teamLoops}
members={members}
membersLoading={membersLoading}
membersError={membersError}
membersComplete={membersComplete}
/>
) : null}
</Flex>
) : (
<LoopsEmptyState />
)}
<LoopTemplatesSection onSelect={onStartFromTemplate} />
</Flex>
</div>
<div className="shrink-0">
<Flex
direction="column"
gap="2"
className="mx-auto w-full max-w-5xl px-8 pb-6"
>
{builderSessions.map((session) => (
<BuilderSessionRow
key={session.taskId}
session={session}
onResume={onResumeBuilderSession}
onStopped={onBuilderSessionStopped}
/>
))}
<LoopBuilderComposer disabledReason={limitReason} />
</Flex>
</div>
</Flex>
);
}
function BuilderSessionRow({
session,
onResume,
onStopped,
}: {
session: LoopBuilderSession;
onResume?: (taskId: string) => void;
onStopped?: (taskId: string) => void;
}) {
const [confirmStop, setConfirmStop] = useState(false);
return (
<Flex
align="center"
gap="3"
className="rounded-(--radius-2) border border-border bg-(--color-panel-solid) px-3 py-2"
>
<ChatCircleDotsIcon size={16} className="shrink-0 text-(--accent-11)" />
<Flex direction="column" className="min-w-0 flex-1">
<Text className="font-medium text-[12px] text-gray-10 uppercase tracking-wide">
Builder in progress
</Text>
<Text className="truncate text-[13px] text-gray-12">
{session.prompt}
</Text>
</Flex>
<Button
variant="soft"
size="1"
onClick={() => onResume?.(session.taskId)}
>
Resume
</Button>
<Button
variant="soft"
color="red"
size="1"
onClick={() => setConfirmStop(true)}
>
<StopIcon size={12} weight="bold" />
Stop
</Button>
{confirmStop ? (
<StopCloudRunDialog
open={confirmStop}
taskId={session.taskId}
title="Stop loop builder"
buttonLabel="Stop builder"
onOpenChange={setConfirmStop}
onStopped={() => {
toast.success("Builder stopped");
onStopped?.(session.taskId);
}}
/>
) : null}
</Flex>
);
}
function LoopListSection({
title,
loops,
members = EMPTY_MEMBERS,
membersLoading = false,
membersError = false,
membersComplete = true,
}: {
title: string;
loops: LoopSchemas.Loop[];
members?: UserBasic[];
membersLoading?: boolean;
membersError?: boolean;
membersComplete?: boolean;
}) {
const [expanded, setExpanded] = useState(false);
const visibleLoops = expanded ? loops : loops.slice(0, SECTION_PREVIEW_COUNT);
return (
<Flex direction="column" gap="3">
<Text className="font-medium text-[12px] text-gray-10 uppercase tracking-wide">
{title}
</Text>
<Flex direction="column" gap="2">
{visibleLoops.map((loop) => (
<LoopRow
key={loop.id}
loop={loop}
creator={members.find((member) => member.id === loop.created_by_id)}
creatorLoading={membersLoading}
creatorError={membersError}
creatorLookupComplete={membersComplete}
/>
))}
</Flex>
{loops.length > SECTION_PREVIEW_COUNT ? (
<Button
variant="ghost"
color="gray"
size="1"
className="w-fit"
onClick={() => setExpanded(!expanded)}
>
{expanded ? "Show fewer" : `Show all ${loops.length}`}
</Button>
) : null}
</Flex>
);
}