-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproject-structure.ts
More file actions
76 lines (69 loc) · 2.01 KB
/
Copy pathproject-structure.ts
File metadata and controls
76 lines (69 loc) · 2.01 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
import { toTaskView } from "./views.js";
type ItemType = "to-do" | "project" | "heading";
type ItemStatus = "incomplete" | "canceled" | "completed";
type StartBucket = "Inbox" | "Anytime" | "Someday" | "";
type ChecklistItem = {
id: string;
title: string;
status: ItemStatus;
stopDate: string | null;
};
type TaskLike = {
id: string;
type: ItemType;
title: string;
status: ItemStatus;
trashed: boolean;
notes: string;
start: StartBucket;
startDate: string | null;
deadline: string | null;
deadlineSuppressed: boolean;
stopDate: string | null;
created: string | null;
modified: string | null;
areaId: string | null;
areaTitle: string | null;
projectId: string | null;
projectTitle: string | null;
headingId: string | null;
headingTitle: string | null;
tags: string[];
checklist: ChecklistItem[];
index: number;
todayIndex: number;
};
export function buildProjectStructure(tasks: TaskLike[], projectUuid: string) {
const project = tasks.find(
(task) => task.type === "project" && task.id === projectUuid && !task.trashed
);
if (!project) {
throw new Error(`Project not found: ${projectUuid}`);
}
const headings = tasks.filter(
(task) => task.type === "heading" && task.projectId === projectUuid && !task.trashed
);
const todos = tasks.filter(
(task) => task.type === "to-do" && task.projectId === projectUuid && !task.trashed
);
const todosWithoutHeading = todos.filter((task) => !task.headingId);
return {
project,
headings,
todos,
todosWithoutHeading,
compact: {
project: toTaskView(project, "compact"),
headings: headings.map((heading) => ({
...toTaskView(heading, "compact"),
todoCount: todos.filter((task) => task.headingId === heading.id).length,
})),
todosWithoutHeading: todosWithoutHeading.map((task) => toTaskView(task, "compact")),
counts: {
headings: headings.length,
todos: todos.length,
todosWithoutHeading: todosWithoutHeading.length,
},
},
};
}