generated from pengx17/logseq-plugin-template-react
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
TaskSection.tsx
116 lines (106 loc) · 3 KB
/
TaskSection.tsx
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
import React, { useEffect, useMemo, useState } from 'react';
import {
useRecoilCallback,
useRecoilValue,
useRecoilValueLoadable,
} from 'recoil';
import { groupBy } from 'lodash-es';
import { TaskEntityObject } from '../models/TaskEntity';
import { tasksState } from '../state/tasks';
import { themeStyleState } from '../state/theme';
import { visibleState } from '../state/visible';
import TaskItem from './TaskItem';
import { settingsState } from '../state/settings';
import { openTaskPage } from '../api';
import { inputState } from '../state/input';
export enum GroupBy {
Page,
Tag,
Namespace,
}
export interface ITaskSectionProps {
title: string;
query: string;
groupBy?: GroupBy;
filter: string;
}
const TaskSection: React.FC<ITaskSectionProps> = (props) => {
const { title, query } = props;
const [tasks, setTasks] = useState<TaskEntityObject[]>([]);
const visible = useRecoilValue(visibleState);
const tasksLoadable = useRecoilValueLoadable(tasksState(query));
const themeStyle = useRecoilValue(themeStyleState);
const { openInRightSidebar } = useRecoilValue(settingsState);
const input = useRecoilValue(inputState);
const refreshAll = useRecoilCallback(
({ snapshot, refresh }) =>
() => {
for (const node of snapshot.getNodes_UNSTABLE()) {
refresh(node);
}
},
[],
);
useEffect(() => {
switch (tasksLoadable.state) {
case 'hasValue': {
const tasks = tasksLoadable.contents.filter(
(task: TaskEntityObject) => {
return task.content.toLowerCase().includes(input.toLowerCase());
},
);
setTasks(tasks);
break;
}
case 'hasError':
throw tasksLoadable.contents;
}
}, [tasksLoadable.state, tasksLoadable.contents, input]);
useEffect(() => {
if (visible) {
refreshAll();
}
}, [visible, refreshAll]);
const taskGroups = useMemo(() => {
switch (props.groupBy) {
case GroupBy.Page:
return groupBy(tasks, (task: TaskEntityObject) => task.page.name);
default:
return { '': tasks };
}
}, [props.groupBy, tasks]);
if (tasks.length === 0) {
return null;
}
return (
<div className="py-1">
<h2
className="py-1 text-blue-400"
style={{ color: themeStyle.sectionTitleColor }}
>
{title}
</h2>
<div>
{(Object.entries(taskGroups) ?? []).map(([name, tasks]) => {
const [{ page }] = tasks;
return (
<div key={name}>
{name && (
<h3
className="py-1 text-sm text-gray-400 cursor-pointer"
onClick={() => openTaskPage(page, { openInRightSidebar })}
>
{name}
</h3>
)}
{(tasks ?? []).map((task) => (
<TaskItem key={task.uuid} task={task} onChange={refreshAll} />
))}
</div>
);
})}
</div>
</div>
);
};
export default TaskSection;