Skip to content

Commit dac525e

Browse files
committed
fix(forecast): correct task categorization relative to selected date
- Replace fixed today-based categorization with dynamic date-relative logic - Remove incorrect Past Due display condition (selectedTimestamp >= todayTimestamp) - Tasks now properly categorize as past/present/future relative to selected date - Past due tasks display correctly regardless of selected date - Apply proper task sorting to all recategorized task groups - Handle sort configuration for descending date order Resolves issue where Past Due disappeared when selecting past dates and tasks showed incorrect relative positioning to selected date.
1 parent 0e87703 commit dac525e

1 file changed

Lines changed: 89 additions & 34 deletions

File tree

src/components/task-view/forecast.ts

Lines changed: 89 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -981,52 +981,91 @@ export class ForecastComponent extends Component {
981981
private calculateFilteredDateSections() {
982982
this.dateSections = [];
983983

984-
// Use the helper function to get tasks for the selected date based on relevantDate
985-
const selectedTasks = this.getTasksForRelevantDate(this.selectedDate);
986-
987-
const todayTimestamp = new Date(this.currentDate).setHours(0, 0, 0, 0);
988-
const selectedTimestamp = new Date(this.selectedDate).setHours(
989-
0,
990-
0,
991-
0,
992-
0
984+
// 基于选择日期重新分类所有任务
985+
const selectedTimestamp = new Date(this.selectedDate).setHours(0, 0, 0, 0);
986+
987+
// 获取有相关日期的任务
988+
const tasksWithRelevantDate = this.allTasks.filter(
989+
(task) => this.getRelevantDate(task) !== undefined
993990
);
991+
992+
// 相对于选择日期重新分类
993+
const pastTasksRelativeToSelected = tasksWithRelevantDate.filter((task) => {
994+
const relevantTimestamp = this.getRelevantDate(task)!;
995+
return relevantTimestamp < selectedTimestamp;
996+
});
997+
998+
const selectedDateTasks = tasksWithRelevantDate.filter((task) => {
999+
const relevantTimestamp = this.getRelevantDate(task)!;
1000+
return relevantTimestamp === selectedTimestamp;
1001+
});
1002+
1003+
const futureTasksRelativeToSelected = tasksWithRelevantDate.filter((task) => {
1004+
const relevantTimestamp = this.getRelevantDate(task)!;
1005+
return relevantTimestamp > selectedTimestamp;
1006+
});
1007+
1008+
// 获取排序配置
1009+
const sortCriteria = this.plugin.settings.viewConfiguration.find(
1010+
(view) => view.id === "forecast"
1011+
)?.sortCriteria;
1012+
1013+
// 对重新分类的任务进行排序
1014+
let sortedPastTasks: Task[];
1015+
let sortedSelectedDateTasks: Task[];
1016+
let sortedFutureTasks: Task[];
1017+
1018+
if (sortCriteria && sortCriteria.length > 0) {
1019+
sortedPastTasks = sortTasks(
1020+
pastTasksRelativeToSelected,
1021+
sortCriteria,
1022+
this.plugin.settings
1023+
);
1024+
sortedSelectedDateTasks = sortTasks(
1025+
selectedDateTasks,
1026+
sortCriteria,
1027+
this.plugin.settings
1028+
);
1029+
sortedFutureTasks = sortTasks(
1030+
futureTasksRelativeToSelected,
1031+
sortCriteria,
1032+
this.plugin.settings
1033+
);
1034+
} else {
1035+
sortedPastTasks = this.sortTasksByPriorityAndRelevantDate(
1036+
pastTasksRelativeToSelected
1037+
);
1038+
sortedSelectedDateTasks = this.sortTasksByPriorityAndRelevantDate(
1039+
selectedDateTasks
1040+
);
1041+
sortedFutureTasks = this.sortTasksByPriorityAndRelevantDate(
1042+
futureTasksRelativeToSelected
1043+
);
1044+
}
9941045

9951046
// Section for the selected date
996-
if (selectedTasks.length > 0) {
1047+
if (sortedSelectedDateTasks.length > 0) {
9971048
this.dateSections.push({
998-
// Format title based on whether it's today, tomorrow, or other day relative to today
999-
title: this.formatSectionTitleForDate(this.selectedDate), // Use helper
1049+
title: this.formatSectionTitleForDate(this.selectedDate),
10001050
date: new Date(this.selectedDate),
1001-
tasks: selectedTasks,
1051+
tasks: sortedSelectedDateTasks,
10021052
isExpanded: true,
10031053
});
10041054
}
10051055

1006-
// Add overdue/past scheduled section if applicable (selected date is today or future, and past tasks exist)
1007-
if (selectedTimestamp >= todayTimestamp && this.pastTasks.length > 0) {
1056+
// Add Past Due section if applicable
1057+
if (sortedPastTasks.length > 0) {
10081058
this.dateSections.unshift({
1009-
title: t("Past Due"), // Keep title
1059+
title: t("Past Due"),
10101060
date: new Date(0), // Placeholder
1011-
tasks: this.pastTasks, // Use pastTasks
1061+
tasks: sortedPastTasks,
10121062
isExpanded: true,
10131063
});
10141064
}
10151065

1016-
// Add future sections relative to the selected date
1017-
// Filter futureTasks based on their relevantDate being after selectedDate
1018-
const futureTasksAfterSelected = this.futureTasks.filter((task) => {
1019-
const relevantTimestamp = this.getRelevantDate(task);
1020-
// Ensure relevantTimestamp exists and is strictly greater than selectedTimestamp
1021-
return (
1022-
relevantTimestamp !== undefined &&
1023-
relevantTimestamp > selectedTimestamp
1024-
);
1025-
});
1026-
1066+
// Add future sections by date
10271067
const dateMap = new Map<string, Task[]>();
1028-
futureTasksAfterSelected.forEach((task) => {
1029-
// We already know relevantTimestamp is defined from the filter above
1068+
sortedFutureTasks.forEach((task) => {
10301069
const relevantTimestamp = this.getRelevantDate(task)!;
10311070
const date = new Date(relevantTimestamp);
10321071
// Create date key
@@ -1037,7 +1076,7 @@ export class ForecastComponent extends Component {
10371076
if (!dateMap.has(dateKey)) {
10381077
dateMap.set(dateKey, []);
10391078
}
1040-
// Avoid duplicates if somehow a task slipped through category logic (unlikely but safe)
1079+
// Avoid duplicates
10411080
if (!dateMap.get(dateKey)!.some((t) => t.id === task.id)) {
10421081
dateMap.get(dateKey)!.push(task);
10431082
}
@@ -1047,10 +1086,8 @@ export class ForecastComponent extends Component {
10471086
sortedDates.forEach((dateKey) => {
10481087
const [year, month, day] = dateKey.split("-").map(Number);
10491088
const date = new Date(year, month - 1, day);
1050-
const tasks = dateMap.get(dateKey)!; // Tasks within category are sorted by priority
1089+
const tasks = dateMap.get(dateKey)!;
10511090

1052-
// Format title relative to today for consistency, but could be relative to selectedDate
1053-
// Let's stick to formatting relative to today (Today, Tomorrow, DayOfWeek)
10541091
let title = this.formatSectionTitleForDate(date);
10551092

10561093
this.dateSections.push({
@@ -1065,6 +1102,24 @@ export class ForecastComponent extends Component {
10651102
});
10661103
});
10671104

1105+
// 处理排序配置中的降序设置
1106+
if (sortCriteria && sortCriteria.length > 0) {
1107+
const dueDateSortCriterion = sortCriteria.find(
1108+
(t) => t.field === "dueDate"
1109+
);
1110+
const scheduledDateSortCriterion = sortCriteria.find(
1111+
(t) => t.field === "scheduledDate"
1112+
);
1113+
if (dueDateSortCriterion && dueDateSortCriterion.order === "desc") {
1114+
this.dateSections.reverse();
1115+
} else if (
1116+
scheduledDateSortCriterion &&
1117+
scheduledDateSortCriterion.order === "desc"
1118+
) {
1119+
this.dateSections.reverse();
1120+
}
1121+
}
1122+
10681123
// Handle empty state in renderDateSectionsUI
10691124
}
10701125

0 commit comments

Comments
 (0)