generated from pengx17/logseq-plugin-template-react
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
TaskSection.tsx
87 lines (78 loc) · 2.2 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
import React, { useEffect, useMemo, useState } from 'react';
import {
useRecoilRefresher_UNSTABLE,
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';
export enum GroupBy {
Page,
Tag,
Namespace,
}
export interface ITaskSectionProps {
title: string;
query: string;
groupBy?: GroupBy;
}
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 refresh = useRecoilRefresher_UNSTABLE(tasksState(query));
const themeStyle = useRecoilValue(themeStyleState);
useEffect(() => {
switch (tasksLoadable.state) {
case 'hasValue':
setTasks(tasksLoadable.contents);
break;
case 'hasError':
throw tasksLoadable.contents;
}
}, [tasksLoadable.state, tasksLoadable.contents]);
useEffect(() => {
if (visible) {
refresh();
}
}, [visible, refresh]);
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]) => {
return (
<div key={name}>
{name && <h3 className="py-1 text-sm text-gray-400">{name}</h3>}
{(tasks ?? []).map((task) => (
<TaskItem key={task.uuid} task={task} onChange={refresh} />
))}
</div>
);
})}
</div>
</div>
);
};
export default TaskSection;