Add debug logging for scroll position restoration

This commit is contained in:
Yuyao Huang 2026-05-09 14:16:15 +08:00
parent 643ba56768
commit 1f4efcd7b3

View File

@ -41,20 +41,26 @@ async function loadTasks() {
isInitializing = true;
try {
tasks = await get(`/api/tasks?goal_id=${selectedGoalId}`);
console.log("loadTasks: got", tasks.length, "tasks");
console.log("Task order:", tasks.map(t => ({id: t.id, title: t.title, status: t.status})));
renderTasks();
initSortable();
const currentGoal = goals.find(g => g.id === selectedGoalId);
const savedTaskId = currentGoal ? currentGoal.selected_task_id : null;
console.log("loadTasks: savedTaskId from goal =", savedTaskId);
const savedTaskExists = savedTaskId && tasks.some(t => t.id === savedTaskId);
console.log("loadTasks: savedTaskExists =", savedTaskExists);
if (savedTaskExists) {
console.log("loadTasks: using savedTaskId path");
scrollToTask(savedTaskId);
if (isLandscapeMode()) {
selectTask(savedTaskId);
}
} else {
console.log("loadTasks: using fallback path");
const doingTask = tasks.find(t => t.status === "doing");
if (doingTask) {
scrollToTask(doingTask.id);
@ -144,13 +150,19 @@ function initSortable() {
}
function scrollToTask(taskId) {
console.log("scrollToTask called:", taskId);
const taskElement = document.querySelector(`[data-task-id="${taskId}"]`);
if (taskElement) {
const scrollView = document.getElementById("scroll-view");
const taskTop = taskElement.offsetTop;
const scrollViewHeight = scrollView.clientHeight;
const taskHeight = taskElement.offsetHeight;
scrollView.scrollTop = taskTop - (scrollViewHeight / 2) + (taskHeight / 2);
const targetScrollTop = taskTop - (scrollViewHeight / 2) + (taskHeight / 2);
console.log("Scrolling to:", targetScrollTop, "element offsetTop:", taskTop);
scrollView.scrollTop = targetScrollTop;
console.log("scrollTop after set:", scrollView.scrollTop);
} else {
console.log("Task element not found for id:", taskId);
}
}