-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathbuildSidebarData.ts
More file actions
237 lines (215 loc) · 7.05 KB
/
Copy pathbuildSidebarData.ts
File metadata and controls
237 lines (215 loc) · 7.05 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
import type { Task, TaskRunStatus } from "@posthog/shared/domain-types";
import { getRepositoryInfo } from "./groupTasks";
import type { TaskData } from "./sidebarData.types";
export type SortMode = "updated" | "created";
export type OrganizeMode = "by-project" | "chronological";
export interface FullTask {
id: string;
title: string;
repository?: string | null;
created_at: string;
updated_at: string;
origin_product?: string;
latest_run?: {
status?: TaskRunStatus | null;
environment?: "local" | "cloud" | null;
output?: { pr_url?: unknown } | null;
state?: Record<string, unknown> | null;
} | null;
}
export interface SidebarTask {
id: string;
title: string;
repository?: string | null;
created_at: string;
updated_at: string;
origin_product?: string;
slack_thread_url?: string;
latest_run?: {
status?: TaskRunStatus | null;
environment?: "local" | "cloud" | null;
output?: { pr_url?: unknown } | null;
} | null;
}
// Accepts both the local `FullTask` shape and the canonical `Task` from
// `@posthog/shared` so callers holding a real `Task` can narrow it directly,
// without an `as unknown as FullTask` escape hatch.
export function narrowFullTask(task: FullTask | Task): SidebarTask {
const slackThreadUrl = task.latest_run?.state?.slack_thread_url;
return {
id: task.id,
title: task.title,
repository: task.repository ?? null,
created_at: task.created_at,
updated_at: task.updated_at,
latest_run: task.latest_run
? {
status: task.latest_run.status,
environment: task.latest_run.environment ?? null,
output: task.latest_run.output ?? null,
}
: null,
origin_product: task.origin_product,
slack_thread_url:
typeof slackThreadUrl === "string" ? slackThreadUrl : undefined,
};
}
export interface FilterVisibleOptions {
archivedIds: ReadonlySet<string>;
workspaceIds: ReadonlySet<string>;
provisioningIds: ReadonlySet<string>;
showAllUsers: boolean;
// System-started tasks (signals, support queue, …) run in the cloud and have
// no local workspace, so this view bypasses the workspace/provisioning gate
// that scopes the default view to the tasks present on this machine.
showSystemStarted: boolean;
}
export function filterVisibleTasks(
rawTasks: SidebarTask[],
options: FilterVisibleOptions,
): SidebarTask[] {
return rawTasks.filter(
(task) =>
!options.archivedIds.has(task.id) &&
(options.showAllUsers ||
options.showSystemStarted ||
options.workspaceIds.has(task.id) ||
options.provisioningIds.has(task.id)),
);
}
export interface TaskSession {
isPromptPending?: boolean;
pendingPermissions?: { size: number };
cloudStatus?: TaskRunStatus;
cloudOutput?: { pr_url?: unknown } | null;
}
/**
* A primitive signature of just the session fields the sidebar renders (see
* {@link deriveTaskData}). The sidebar subscribes to this instead of the whole
* sessions record, so it doesn't rebuild on every streamed event — only when a
* field it actually reads changes. It deliberately ignores `events`.
*/
export function computeSidebarSessionSignature(
sessions: Record<string, TaskSession & { taskId?: string }>,
): string {
let signature = "";
for (const session of Object.values(sessions)) {
if (!session.taskId) continue;
const prUrl =
typeof session.cloudOutput?.pr_url === "string"
? session.cloudOutput.pr_url
: "";
signature += `${session.taskId}:${session.isPromptPending ? 1 : 0}:${
session.pendingPermissions?.size ?? 0
}:${session.cloudStatus ?? ""}:${prUrl};`;
}
return signature;
}
export interface TaskWorkspace {
folderId?: string | null;
folderPath?: string | null;
branchName?: string | null;
linkedBranch?: string | null;
}
export interface TaskTimestamp {
lastViewedAt?: number | null;
lastActivityAt?: number | null;
}
export interface DeriveTaskDataContext {
session: TaskSession | undefined;
workspace: TaskWorkspace | undefined;
timestamp: TaskTimestamp | undefined;
pinnedIds: ReadonlySet<string>;
suspendedIds: ReadonlySet<string>;
}
export function deriveTaskData(
task: SidebarTask,
ctx: DeriveTaskDataContext,
): TaskData {
const { session, workspace, timestamp } = ctx;
const apiUpdatedAt = new Date(task.updated_at).getTime();
const localActivity = timestamp?.lastActivityAt;
const lastActivityAt = localActivity
? Math.max(apiUpdatedAt, localActivity)
: apiUpdatedAt;
const createdAt = new Date(task.created_at).getTime();
const taskLastViewedAt = timestamp?.lastViewedAt;
const isUnread =
taskLastViewedAt != null && lastActivityAt > taskLastViewedAt;
const cloudPrUrl =
typeof task.latest_run?.output?.pr_url === "string"
? task.latest_run.output.pr_url
: ((session?.cloudOutput?.pr_url as string | undefined) ?? null);
const originProduct = task.origin_product;
const slackThreadUrl = task.slack_thread_url;
return {
id: task.id,
title: task.title,
createdAt,
lastActivityAt,
isGenerating: session?.isPromptPending ?? false,
isUnread,
isPinned: ctx.pinnedIds.has(task.id),
isSuspended: ctx.suspendedIds.has(task.id),
needsPermission: (session?.pendingPermissions?.size ?? 0) > 0,
repository: getRepositoryInfo(task, workspace?.folderPath ?? undefined),
folderId: workspace?.folderId || undefined,
taskRunStatus: session?.cloudStatus ?? task.latest_run?.status ?? undefined,
taskRunEnvironment: task.latest_run?.environment ?? undefined,
originProduct,
slackThreadUrl,
folderPath: workspace?.folderPath ?? null,
cloudPrUrl,
branchName: workspace?.branchName ?? null,
linkedBranch: workspace?.linkedBranch ?? null,
};
}
function getSortValue(task: TaskData, sortMode: SortMode): number {
return sortMode === "updated" ? task.lastActivityAt : task.createdAt;
}
function sortTasks(tasks: TaskData[], sortMode: SortMode): TaskData[] {
return [...tasks].sort(
(a, b) => getSortValue(b, sortMode) - getSortValue(a, sortMode),
);
}
export interface PartitionedTasks {
pinnedTasks: TaskData[];
sortedUnpinnedTasks: TaskData[];
totalCount: number;
}
export function partitionAndSortTasks(
taskData: TaskData[],
sortMode: SortMode,
): PartitionedTasks {
const pinned: TaskData[] = [];
const unpinned: TaskData[] = [];
for (const task of taskData) {
if (task.isPinned) {
pinned.push(task);
} else {
unpinned.push(task);
}
}
return {
pinnedTasks: sortTasks(pinned, sortMode),
sortedUnpinnedTasks: sortTasks(unpinned, sortMode),
totalCount: unpinned.length,
};
}
export interface ChronologicalSlice {
flatTasks: TaskData[];
hasMore: boolean;
}
export function sliceChronological(
sortedUnpinnedTasks: TaskData[],
organizeMode: OrganizeMode,
historyVisibleCount: number,
): ChronologicalSlice {
if (organizeMode !== "chronological") {
return { flatTasks: sortedUnpinnedTasks, hasMore: false };
}
return {
flatTasks: sortedUnpinnedTasks.slice(0, historyVisibleCount),
hasMore: sortedUnpinnedTasks.length > historyVisibleCount,
};
}