fix: save the in-focus task instead of recalculating top task

handleScrollSave now saves the task with in-focus class (determined by
handleScrollFocus as the centered task) rather than recalculating which
task is closest to the top. This ensures consistency between what's
highlighted and what's saved.
This commit is contained in:
Yuyao Huang 2026-05-09 15:21:13 +08:00
parent 12610d26c0
commit fd92c6fe96

View File

@ -76,19 +76,14 @@ async function loadTasks() {
} }
async function persistSelectedTask(taskId) { async function persistSelectedTask(taskId) {
console.log("persistSelectedTask called:", taskId, "selectedGoalId=", selectedGoalId, "isInitializing=", isInitializing); if (!selectedGoalId || isInitializing) return;
if (!selectedGoalId || isInitializing) {
console.log("persistSelectedTask: SKIPPED due to", !selectedGoalId ? "no goal" : "initializing");
return;
}
try { try {
console.log("persistSelectedTask: PATCH /api/goals/", selectedGoalId, "/selected-task with", taskId);
await patch(`/api/goals/${selectedGoalId}/selected-task`, { task_id: taskId }); await patch(`/api/goals/${selectedGoalId}/selected-task`, { task_id: taskId });
const goal = goals.find(g => g.id === selectedGoalId); const goal = goals.find(g => g.id === selectedGoalId);
if (goal) { if (goal) {
goal.selected_task_id = taskId; goal.selected_task_id = taskId;
} }
console.log("persistSelectedTask: SUCCESS, saved task", taskId); console.log("Saved selected task:", taskId);
} catch (error) { } catch (error) {
console.error("Failed to persist selected task:", error); console.error("Failed to persist selected task:", error);
} }
@ -151,16 +146,11 @@ function initSortable() {
} }
function scrollToTask(taskId) { function scrollToTask(taskId) {
console.log("scrollToTask called:", taskId);
const taskElement = document.querySelector(`[data-task-id="${taskId}"]`); const taskElement = document.querySelector(`[data-task-id="${taskId}"]`);
if (taskElement) { if (taskElement) {
const scrollView = document.getElementById("scroll-view"); const scrollView = document.getElementById("scroll-view");
const taskTop = taskElement.offsetTop; const taskTop = taskElement.offsetTop;
// Align task to top of scroll view instead of center
scrollView.scrollTop = taskTop; scrollView.scrollTop = taskTop;
console.log("scrollTop set to taskTop:", taskTop);
} else {
console.log("Task element not found for id:", taskId);
} }
} }
@ -213,31 +203,12 @@ function handleScrollFocus() {
} }
function handleScrollSave() { function handleScrollSave() {
const scrollView = document.getElementById("scroll-view"); // Save the task that has in-focus class (determined by handleScrollFocus)
const taskItems = document.querySelectorAll(".task-item"); const inFocusTask = document.querySelector(".task-item.in-focus");
const scrollViewRect = scrollView.getBoundingClientRect(); console.log("handleScrollSave: inFocusTask=", inFocusTask?.dataset.taskId, "isInitializing=", isInitializing);
console.log("handleScrollSave: isInitializing=", isInitializing, "task count=", taskItems.length); if (inFocusTask && !isInitializing) {
const taskId = parseInt(inFocusTask.dataset.taskId);
// Find the task closest to the top of the scroll view
let topTask = null;
let minTopDistance = Infinity;
taskItems.forEach(item => {
const itemRect = item.getBoundingClientRect();
const distanceFromTop = Math.abs(itemRect.top - scrollViewRect.top);
console.log(`Task ${item.dataset.taskId}: rect.top=${itemRect.top}, distance=${distanceFromTop}`);
if (distanceFromTop < minTopDistance) {
minTopDistance = distanceFromTop;
topTask = item;
}
});
console.log("handleScrollSave: selected topTask=", topTask?.dataset.taskId, "saving=", topTask && !isInitializing);
if (topTask && !isInitializing) {
const taskId = parseInt(topTask.dataset.taskId);
clearTimeout(persistTimer); clearTimeout(persistTimer);
persistTimer = setTimeout(() => persistSelectedTask(taskId), 400); persistTimer = setTimeout(() => persistSelectedTask(taskId), 400);
} }