generated from pengx17/logseq-plugin-template-react
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
tasks.ts
51 lines (47 loc) · 1.41 KB
/
tasks.ts
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
import { BlockEntity, PageEntity } from '@logseq/libs/dist/LSPlugin';
import { selectorFamily } from 'recoil';
import TaskEntity, { TASK_PRIORITY_WEIGHT } from '../models/TaskEntity';
import { getBlockUUID } from '../utils';
async function getTaskEntitiesByQuery(query: string) {
const collections = await window.logseq.DB.datascriptQuery<BlockEntity[][]>(
query,
);
const tasks = await Promise.all(
(collections ?? []).map(async ([item]) => {
const uuid = getBlockUUID(item);
const block = await window.logseq.Editor.getBlock(uuid, {
includeChildren: true,
});
if (block === undefined) {
return null;
}
const page = await window.logseq.Editor.getPage(
(block?.page as PageEntity).name,
);
return new TaskEntity(block!, page!);
}),
);
return (
tasks
// @ts-ignore
.filter((task) => {
return task && !task.getPageProperty('todoIgnore');
})
.map((task) => task!.toObject())
.sort((a, b) => {
if (a.scheduled === b.scheduled) {
return (
TASK_PRIORITY_WEIGHT[b.priority] - TASK_PRIORITY_WEIGHT[a.priority]
);
}
return b.scheduled - a.scheduled;
})
);
}
export const tasksState = selectorFamily({
key: 'tasks',
get: (query: string) => () => getTaskEntitiesByQuery(query),
cachePolicy_UNSTABLE: {
eviction: 'most-recent',
},
});