From 202b9020dfc6aad60b0be8f33eb42aff96038e2d Mon Sep 17 00:00:00 2001 From: KrishJdev Date: Mon, 16 Nov 2026 19:09:31 +0530 Subject: [PATCH 1/4] fix(tasks): fix drag from weekly to daily task disappearance and improve task management discoverability --- HANDOFF.md | 35 +++ .../cockpit/cockpit-tasks-section.tsx | 275 ++++++++++++++++-- .../tasks/data/task-sync.repository.ts | 40 ++- .../web/features/tasks/domain/task-reorder.ts | 4 + 4 files changed, 330 insertions(+), 24 deletions(-) diff --git a/HANDOFF.md b/HANDOFF.md index 5f2fbf3b..630330d1 100644 --- a/HANDOFF.md +++ b/HANDOFF.md @@ -3202,3 +3202,38 @@ Legend: ✅ Done end-to-end · ⚠️ Partial / backend-only / deviates from spe - `npm run test:web`: ✅ 260 test files (2,773 tests) passing 100% green. - `npm run build:web`: ✅ Production build clean (21 routes compiled, 0 errors). - **NEXT STEP:** Merge `afnan-jr` into `dev` via PR once peer-reviewed. + +### Session 210 — 2026-11-16 (fix(tasks): fix drag from weekly to daily task disappearance, challenge scoping, and task management UI discoverability — on `krish`) + +- **Context & Objectives:** + 1. Fix weekly task disappearing when dragged to a day column in the Cockpit while persisting in profile weekly goals. + 2. Ensure daily tasks adhere to Law L6 (Three-Condition Invariant): daily tasks cannot have challengeId links, and dragging a weekly challenge goal to daily unlinks challengeId and assigns the day's dueDate. + 3. Fix task management and edit discoverability: the 3-dots menu button and trash button were hidden behind `opacity-0 sm:opacity-0` and only visible on hover (making them completely inaccessible on touch devices / tablets and undiscoverable on desktop). Add dedicated Pencil edit button, double-click task text to edit, and ensure options triggers are always accessible. +- **Root Causes:** + 1. In `features/tasks/data/task-sync.repository.ts`, `applyTaskMutation` for action `"UPDATE"` or `"MOVE"` did not update `taskType` when moving tasks across categories or when `taskType` was passed in the payload. PostgreSQL retained `taskType = "WEEKLY"` while its `categoryId` pointed to a `DAILY` category. Consequently, `getUserCategorizedTasksFromDatabase` filtered it out of the Daily board (`t.taskType === "DAILY"`), and out of the Weekly board (`c.taskType === "WEEKLY"`), causing the task to disappear on sync while remaining on the user's profile. + 2. In `features/study-logs/presentation/cockpit/cockpit-tasks-section.tsx`, `handleTaskDrop` only enqueued action `"REORDER"` when dropping onto a category with existing tasks, omitting `"MOVE"`. + 3. `features/tasks/domain/task-reorder.ts` did not clear `challengeId` when moving a task or category to daily. + 4. The task row options trigger in `cockpit-tasks-section.tsx` had `opacity-0 group-hover:opacity-100 sm:opacity-0 max-sm:opacity-100`. On tablets and touch devices (>640px), lack of mouse hover rendered the menu permanently invisible. +- **Changes Completed:** + 1. **Domain Logic (`apps/web/features/tasks/domain/task-reorder.ts`):** + - Added `challengeId?: string | null` to `DragTaskItem`. + - Updated `moveTaskBetweenCategories` to clear `challengeId: null` when `targetTaskType === "DAILY"`. + - Updated `moveCategoryBetweenColumns` to clear `challengeId: null` when moving categories to daily. + 2. **Data & Synchronization (`apps/web/features/tasks/data/task-sync.repository.ts`):** + - In `applyTaskMutation`, correctly detect `nextTaskType` (from `payload.taskType` or `targetCategory.taskType`) and update `dataToUpdate.taskType` when it differs from `currentTaskGuard.taskType`. + - Clear `dataToUpdate.challengeId = null` when becoming `DAILY` or when explicitly unlinking. + - Clear `dataToUpdate.dueDate = null` when moving from `DAILY` to `WEEKLY` without an explicit dueDate. + 3. **Presentation & Cockpit UI (`apps/web/features/study-logs/presentation/cockpit/cockpit-tasks-section.tsx`):** + - Created `openEditTask(task, catIdx, taskType)` helper function. + - Added quick `Pencil` edit button to each task row in both Daily and Weekly boards. + - Made the 3-dots `MoreVertical` options trigger and `Trash2` button always visible with gentle tertiary styling (`text-text-tertiary hover:text-white`), removing `opacity-0 sm:opacity-0` hover locks for full touch/tablet/desktop accessibility. + - Added `onDoubleClick` on task text spans to open the Edit Task dialog directly. + - In `handleTaskDrop`, enqueued `MOVE` mutation with `taskId, categoryId, taskType, sortOrder, dueDate, challengeId` on cross-category/type moves regardless of target category length. + - In `hydrateFromIndexedDB`, updated task filtering and added self-healing for IndexedDB tasks with mismatched `taskType`. + 4. **Quality Gates & Verification:** + - `npm run typecheck:web`: ✅ 0 TypeScript compiler errors. + - `npm run test:web -- cockpit-tasks`: ✅ 10 test suites (108 tests) passing 100% green. + - `npm run test:web -- task-sync.repository.test.ts`: ✅ 38 tests passing. + - `npm run test:web -- task-reorder.test.ts`: ✅ 15 tests passing. + - `npm run test:web -- task.repository.test.ts`: ✅ 47 tests passing. +- **NEXT STEP:** Submit PR from `krish` to `dev`. diff --git a/apps/web/features/study-logs/presentation/cockpit/cockpit-tasks-section.tsx b/apps/web/features/study-logs/presentation/cockpit/cockpit-tasks-section.tsx index 4f979d52..70b89529 100644 --- a/apps/web/features/study-logs/presentation/cockpit/cockpit-tasks-section.tsx +++ b/apps/web/features/study-logs/presentation/cockpit/cockpit-tasks-section.tsx @@ -375,6 +375,28 @@ export function CockpitTasksSection({ useState("CHALLENGE"); const [isRolloverDismissed, setIsRolloverDismissed] = useState(false); + const openEditTask = ( + task: TaskItem, + catIdx: number, + taskType: "daily" | "weekly" + ) => { + setEditTaskModal({ + id: task.id, + text: task.text, + completed: task.completed, + status: getTaskStatus(task), + dueDate: task.dueDate ?? null, + createdAt: task.createdAt, + challengeId: task.challengeId ?? null, + catIdx, + taskType, + }); + setEditTaskInputChallengeId(task.challengeId ?? null); + setEditTaskInputText(task.text); + setEditTaskInputStatus(getTaskStatus(task)); + setEditTaskInputDueDate(task.dueDate ?? ""); + }; + // Edit Category Modal State const [editCategoryModal, setEditCategoryModal] = useState<{ id?: string; @@ -1185,10 +1207,11 @@ export function CockpitTasksSection({ activeCollapsed ), tasks: sortedTasks - .filter( - (t) => - t.categoryId === c.id && t.taskType === "DAILY" - ) + .filter((t) => { + if (t.categoryId !== c.id) return false; + if (c.taskType === "DAILY") return true; + return t.taskType === "DAILY"; + }) .map((t) => { const effectiveDueDate = t.dueDate ? typeof t.dueDate === "string" @@ -1201,10 +1224,17 @@ export function CockpitTasksSection({ (t.status as TaskStatus) || (t.isComplete ? "COMPLETED" : "TODO"); - if (!t.status) { + const needsDailyHeal = + !t.status || + t.taskType !== "DAILY" || + t.challengeId !== null; + + if (needsDailyHeal) { tasksToHeal.push({ ...t, - dueDate: effectiveDueDate, + taskType: "DAILY", + challengeId: null, + dueDate: effectiveDueDate || selectedDateKey, status: effectiveStatus, }); } @@ -1214,9 +1244,9 @@ export function CockpitTasksSection({ text: t.title, completed: t.isComplete, status: effectiveStatus, - dueDate: effectiveDueDate, + dueDate: effectiveDueDate || selectedDateKey, createdAt: t.createdAt, - challengeId: t.challengeId ?? null, + challengeId: null, }; }), })); @@ -1235,10 +1265,11 @@ export function CockpitTasksSection({ activeCollapsed ), tasks: sortedTasks - .filter( - (t) => - t.categoryId === c.id && t.taskType === "WEEKLY" - ) + .filter((t) => { + if (t.categoryId !== c.id) return false; + if (c.taskType === "WEEKLY") return true; + return t.taskType === "WEEKLY"; + }) .map((t) => { const effectiveDueDate = t.dueDate ? typeof t.dueDate === "string" @@ -1251,10 +1282,16 @@ export function CockpitTasksSection({ (t.status as TaskStatus) || (t.isComplete ? "COMPLETED" : "TODO"); - if (!t.status) { + const needsWeeklyHeal = + !t.status || + t.taskType !== "WEEKLY" || + t.dueDate !== null; + + if (needsWeeklyHeal) { tasksToHeal.push({ ...t, - dueDate: effectiveDueDate, + taskType: "WEEKLY", + dueDate: null, status: effectiveStatus, }); } @@ -1264,7 +1301,7 @@ export function CockpitTasksSection({ text: t.title, completed: t.isComplete, status: effectiveStatus, - dueDate: effectiveDueDate, + dueDate: null, createdAt: t.createdAt, challengeId: t.challengeId ?? null, }; @@ -3670,14 +3707,45 @@ export function CockpitTasksSection({ .map((t) => t.id) .filter((id) => Boolean(id)); + const isCrossCategoryMove = + draggedItem.sourceColumn !== targetColumn || + draggedItem.sourceCatIdx !== targetCatIdx; + if (isLoggedIn && userId && taskIds.length > 0) { - persistTasks(tasksToUpdate, { + const mutations: Array> = []; + if (isCrossCategoryMove) { + mutations.push({ + id: crypto.randomUUID(), + entityType: "TASK", + action: "MOVE", + payload: { + taskId: res.movedTask.id, + categoryId: res.targetCategoryId, + taskType: res.targetTaskType, + sortOrder: destIdx, + dueDate: res.movedTask.dueDate ?? null, + challengeId: + res.targetTaskType === "DAILY" + ? null + : (res.movedTask.challengeId ?? null), + }, + createdAt: Date.now(), + retryCount: 0, + }); + } + mutations.push({ id: crypto.randomUUID(), entityType: "TASK", action: "REORDER", payload: { taskIds }, createdAt: Date.now() + 1, retryCount: 0, + }); + + commitLocalBatch({ + categories: [], + tasks: tasksToUpdate, + mutations: mutations.map((m) => ({ ...m, userId })), }) .then(() => scheduleSync(userId)) .catch(() => {}); @@ -3720,6 +3788,10 @@ export function CockpitTasksSection({ taskType: res.targetTaskType, sortOrder: destIdx, dueDate: res.movedTask.dueDate ?? null, + challengeId: + res.targetTaskType === "DAILY" + ? null + : (res.movedTask.challengeId ?? null), }, createdAt: Date.now(), retryCount: 0, @@ -4328,6 +4400,117 @@ export function CockpitTasksSection({ .catch(() => {}); } } + } else if ( + targetColumn === "weekly" && + draggedItem.sourceColumn === "daily" + ) { + const sourceCat = dailyCategories[draggedItem.sourceCatIdx]; + if (sourceCat) { + let targetWeeklyIdx = weeklyCategories.findIndex( + (c) => + (sourceCat.id && c.id === sourceCat.id) || + c.name === sourceCat.name + ); + let currentWeeklyCats = weeklyCategories; + if (targetWeeklyIdx === -1) { + const newCatId = crypto.randomUUID(); + const newWeeklyCat: CategoryGroup = { + id: newCatId, + name: sourceCat.name, + isCollapsed: false, + tasks: [], + }; + currentWeeklyCats = [...weeklyCategories, newWeeklyCat]; + targetWeeklyIdx = currentWeeklyCats.length - 1; + + persistCategory( + { + id: newCatId, + userId: userId ?? null, + name: sourceCat.name, + taskType: "WEEKLY", + sortOrder: targetWeeklyIdx, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + syncState: "pending", + }, + { + id: crypto.randomUUID(), + entityType: "CATEGORY", + action: "CREATE", + payload: { + id: newCatId, + name: sourceCat.name, + taskType: "WEEKLY", + sortOrder: targetWeeklyIdx, + }, + createdAt: Date.now(), + retryCount: 0, + } + ).catch(() => {}); + } + + const res = moveTaskBetweenCategories({ + taskId: draggedItem.id, + sourceCategoryIndex: draggedItem.sourceCatIdx, + sourceColumn: "daily", + targetCategoryIndex: targetWeeklyIdx, + targetColumn: "weekly", + targetTaskIndex: + currentWeeklyCats[targetWeeklyIdx].tasks.length, + targetDueDate: null, + dailyCategories, + weeklyCategories: currentWeeklyCats, + }); + + setDailyCategories(res.dailyCategories); + setWeeklyCategories(res.weeklyCategories); + + if (res.movedTask && res.targetCategoryId) { + const nowIso = new Date().toISOString(); + const isMovedComplete = Boolean( + res.movedTask.completed ?? res.movedTask.isComplete + ); + const movedStatus = + (res.movedTask.status as TaskStatus) || + (isMovedComplete ? "COMPLETED" : "TODO"); + const movedLocalTask: LocalTaskRecord = { + id: res.movedTask.id, + userId: userId ?? null, + categoryId: res.targetCategoryId, + title: res.movedTask.text ?? res.movedTask.title ?? "", + taskType: "WEEKLY", + sortOrder: currentWeeklyCats[targetWeeklyIdx].tasks.length, + isComplete: isMovedComplete, + status: movedStatus, + dueDate: null, + createdAt: res.movedTask.createdAt + ? new Date(res.movedTask.createdAt).toISOString() + : nowIso, + updatedAt: nowIso, + completedAt: isMovedComplete ? nowIso : null, + syncState: "pending", + }; + + persistTask(movedLocalTask, { + id: crypto.randomUUID(), + entityType: "TASK", + action: "MOVE", + payload: { + taskId: res.movedTask.id, + categoryId: res.targetCategoryId, + taskType: "WEEKLY", + sortOrder: + currentWeeklyCats[targetWeeklyIdx].tasks.length, + dueDate: null, + }, + createdAt: Date.now(), + retryCount: 0, + }) + .then(() => scheduleSync(userId)) + .catch(() => {}); + } + } } else if (targetCategories.length > 0) { const lastCatIdx = targetCategories.length - 1; const isMovingWeeklyToDaily = @@ -5252,7 +5435,14 @@ export function CockpitTasksSection({ title={ task.text } - className={`text-sm break-words leading-snug ${ + onDoubleClick={() => + openEditTask( + task, + catIdx, + "daily" + ) + } + className={`text-sm break-words leading-snug cursor-pointer ${ expandedTaskIds.has( task.id ) @@ -5446,6 +5636,23 @@ export function CockpitTasksSection({ )} + )} + {Boolean( + linkableChallengeId + ) && + (task.challengeId === + linkableChallengeId ? ( + + + + ) : !task.challengeId ? ( + + + + ) : null)}