-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuseSidebarData.ts
More file actions
272 lines (241 loc) · 7.58 KB
/
useSidebarData.ts
File metadata and controls
272 lines (241 loc) · 7.58 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
import { useArchivedTaskIds } from "@features/archive/hooks/useArchivedTaskIds";
import { useSessions } from "@features/sessions/stores/sessionStore";
import { useTasks } from "@features/tasks/hooks/useTasks";
import { useWorkspaces } from "@features/workspace/hooks/useWorkspace";
import { getTaskRepository, parseRepository } from "@renderer/utils/repository";
import type { Task } from "@shared/types";
import { useEffect, useMemo, useRef } from "react";
import { useSidebarStore } from "../stores/sidebarStore";
import type { SortMode } from "../types";
import { usePinnedTasks } from "./usePinnedTasks";
import { useTaskViewed } from "./useTaskViewed";
export interface TaskRepositoryInfo {
fullPath: string;
name: string;
}
export interface TaskData {
id: string;
title: string;
createdAt: number;
lastActivityAt: number;
isGenerating: boolean;
isUnread: boolean;
isPinned: boolean;
needsPermission: boolean;
repository: TaskRepositoryInfo | null;
folderId?: string;
taskRunStatus?:
| "started"
| "in_progress"
| "completed"
| "failed"
| "cancelled";
taskRunEnvironment?: "local" | "cloud";
}
export interface TaskGroup {
id: string;
name: string;
tasks: TaskData[];
}
export interface SidebarData {
isHomeActive: boolean;
isInboxActive: boolean;
isLoading: boolean;
activeTaskId: string | null;
pinnedTasks: TaskData[];
flatTasks: TaskData[];
groupedTasks: TaskGroup[];
totalCount: number;
hasMore: boolean;
}
interface ViewState {
type:
| "task-detail"
| "task-input"
| "settings"
| "folder-settings"
| "inbox"
| "archived";
data?: Task;
}
interface UseSidebarDataProps {
activeView: ViewState;
}
function getRepositoryInfo(
task: Task,
folderPath?: string,
): TaskRepositoryInfo | null {
const repository = getTaskRepository(task);
if (repository) {
const parsed = parseRepository(repository);
return {
fullPath: repository,
name: parsed?.repoName ?? repository,
};
}
if (folderPath) {
const name = folderPath.split("/").pop() ?? folderPath;
return {
fullPath: folderPath,
name,
};
}
return 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),
);
}
function groupByRepository(
tasks: TaskData[],
folderOrder: string[],
): TaskGroup[] {
const groupMap = new Map<string, TaskGroup>();
for (const task of tasks) {
const repository = task.repository;
const groupId = repository?.fullPath ?? "other";
const groupName = repository?.name ?? "Other";
if (!groupMap.has(groupId)) {
groupMap.set(groupId, { id: groupId, name: groupName, tasks: [] });
}
groupMap.get(groupId)?.tasks.push(task);
}
const groups = Array.from(groupMap.values());
if (folderOrder.length === 0) {
return groups.sort((a, b) => a.name.localeCompare(b.name));
}
return groups.sort((a, b) => {
const aIndex = folderOrder.indexOf(a.id);
const bIndex = folderOrder.indexOf(b.id);
if (aIndex === -1 && bIndex === -1) {
return a.name.localeCompare(b.name);
}
if (aIndex === -1) return 1;
if (bIndex === -1) return -1;
return aIndex - bIndex;
});
}
export function useSidebarData({
activeView,
}: UseSidebarDataProps): SidebarData {
const { data: rawTasks = [], isLoading: isLoadingTasks } = useTasks();
const { data: workspaces, isFetched: isWorkspacesFetched } = useWorkspaces();
const archivedTaskIds = useArchivedTaskIds();
const isLoading = isLoadingTasks || !isWorkspacesFetched;
const allTasks = useMemo(
() =>
rawTasks.filter(
(task) => !archivedTaskIds.has(task.id) && workspaces?.[task.id],
),
[rawTasks, archivedTaskIds, workspaces],
);
const sessions = useSessions();
const { timestamps } = useTaskViewed();
const historyVisibleCount = useSidebarStore(
(state) => state.historyVisibleCount,
);
const { pinnedTaskIds } = usePinnedTasks();
const organizeMode = useSidebarStore((state) => state.organizeMode);
const sortMode = useSidebarStore((state) => state.sortMode);
const folderOrder = useSidebarStore((state) => state.folderOrder);
const isHomeActive = activeView.type === "task-input";
const isInboxActive = activeView.type === "inbox";
const activeTaskId =
activeView.type === "task-detail" && activeView.data
? activeView.data.id
: null;
const sessionByTaskId = useMemo(() => {
const map = new Map<string, (typeof sessions)[string]>();
for (const session of Object.values(sessions)) {
if (session.taskId) {
map.set(session.taskId, session);
}
}
return map;
}, [sessions]);
const taskData = useMemo(() => {
return allTasks.map((task) => {
const session = sessionByTaskId.get(task.id);
const workspace = workspaces?.[task.id];
const apiUpdatedAt = new Date(task.updated_at).getTime();
const taskTimestamps = timestamps[task.id];
const localActivity = taskTimestamps?.lastActivityAt;
const lastActivityAt = localActivity
? Math.max(apiUpdatedAt, localActivity)
: apiUpdatedAt;
const createdAt = new Date(task.created_at).getTime();
const taskLastViewedAt = taskTimestamps?.lastViewedAt;
const isUnread =
taskLastViewedAt != null && lastActivityAt > taskLastViewedAt;
return {
id: task.id,
title: task.title,
createdAt,
lastActivityAt,
isGenerating: session?.isPromptPending ?? false,
isUnread,
isPinned: pinnedTaskIds.has(task.id),
needsPermission: (session?.pendingPermissions?.size ?? 0) > 0,
repository: getRepositoryInfo(task, workspace?.folderPath),
folderId: workspace?.folderId || undefined,
taskRunStatus: task.latest_run?.status,
taskRunEnvironment: task.latest_run?.environment,
};
});
}, [allTasks, timestamps, pinnedTaskIds, sessionByTaskId, workspaces]);
const pinnedTasks = useMemo(() => {
const pinned = taskData.filter((task) => task.isPinned);
return sortTasks(pinned, sortMode);
}, [taskData, sortMode]);
const unpinnedTasks = useMemo(
() => taskData.filter((task) => !task.isPinned),
[taskData],
);
const sortedUnpinnedTasks = useMemo(
() => sortTasks([...unpinnedTasks], sortMode),
[unpinnedTasks, sortMode],
);
const totalCount = unpinnedTasks.length;
const hasMore =
organizeMode === "chronological" &&
sortedUnpinnedTasks.length > historyVisibleCount;
const flatTasks = useMemo(() => {
if (organizeMode !== "chronological") {
return sortedUnpinnedTasks;
}
return sortedUnpinnedTasks.slice(0, historyVisibleCount);
}, [organizeMode, sortedUnpinnedTasks, historyVisibleCount]);
const groupedTasks = useMemo(
() => groupByRepository(sortedUnpinnedTasks, folderOrder),
[sortedUnpinnedTasks, folderOrder],
);
const groupIdsRef = useRef<string[]>([]);
useEffect(() => {
if (groupedTasks.length === 0) return;
const groupIds = groupedTasks.map((g) => g.id);
const prev = groupIdsRef.current;
if (
groupIds.length === prev.length &&
groupIds.every((id, i) => id === prev[i])
) {
return;
}
groupIdsRef.current = groupIds;
useSidebarStore.getState().syncFolderOrder(groupIds);
}, [groupedTasks]);
return {
isHomeActive,
isInboxActive,
isLoading,
activeTaskId,
pinnedTasks,
flatTasks,
groupedTasks,
totalCount,
hasMore,
};
}