Skip to content

Commit 1be85da

Browse files
committed
perf: optimize urgency calculations
1 parent 4c9264c commit 1be85da

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/components/SettingsDialog.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ function SettingsDialog(props: Props) {
117117

118118
const clearData = () => {
119119
dispatch(rootActions.setTaskJson(initTaskJson()));
120+
dispatch(rootActions.addNotification({
121+
severity: "success",
122+
text: "Successfully cleared data"
123+
}))
120124
};
121125
const login = () => {
122126
dispatch(asyncActions.login({

src/components/TaskDialog.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function TaskDialog(props: Props) {
122122

123123
dispatch(rootActions.addNotification({
124124
severity: "success",
125-
text: `Successfully ${props.task ? "edit" : "add"} a new task.`
125+
text: `Successfully ${props.task ? "edit" : "add"} a task.`
126126
}));
127127
handleClose();
128128
}

src/components/TaskList.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,32 @@ const Actions = (props: ActionsProps) => {
189189
function TaskList(props: Props) {
190190
const classes = useStyles();
191191
const dispatch = useDispatch();
192-
// sorted tasks
193-
const tasks = useSelector(
192+
193+
const originalTasks = useSelector(
194194
(state: RootState) => (
195195
state.taskJson[props.taskType].map(task => ({
196196
...task,
197197
due: task.due && DateTime.fromISO(task.due).toFormat("yyyy-MM-dd")
198-
})).sort(
199-
(a, b) => taskUrgency(b) - taskUrgency(a)
200-
)
198+
}))
201199
)
202200
);
203201

202+
// sorted tasks
203+
let tasks: Task[];
204+
// Map<id, urgency>
205+
const urgencyMap = new Map<string, number>();
206+
if (props.taskType === "todo") {
207+
for (const task of originalTasks) {
208+
urgencyMap.set(task.id, taskUrgency(task));
209+
}
210+
tasks = originalTasks.sort(
211+
(a, b) => urgencyMap.get(b.id)! - urgencyMap.get(a.id)!
212+
);
213+
}
214+
else {
215+
tasks = originalTasks;
216+
}
217+
204218
const removeTasks = (ids: string[]) => {
205219
if (props.taskType !== "removed") {
206220
dispatch(rootActions.removeTasks({
@@ -224,7 +238,11 @@ function TaskList(props: Props) {
224238
};
225239

226240
const colorTask = (task: Task) => {
227-
const urgency = taskUrgency(task);
241+
// Only color todo tasks
242+
if (props.taskType !== "todo")
243+
return "";
244+
245+
const urgency = urgencyMap.get(task.id)!;
228246
if (urgency >= 1000)
229247
return classes.red;
230248
if (urgency >= 100)

0 commit comments

Comments
 (0)