Fix abandoned 'New Task' persisting when navigating away without changes (#889)

This commit is contained in:
Chris 2026-03-02 18:28:54 +02:00 committed by GitHub
parent 29aad09591
commit b81abd9bfe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -43,6 +43,8 @@ const TaskDetails: React.FC = () => {
const location = useLocation();
const { t } = useTranslation();
const isNewTask = location.state?.isNew === true;
const isNewTaskRef = useRef(isNewTask);
const taskModifiedRef = useRef(false);
const { showSuccessToast, showErrorToast } = useToast();
// Clear navigation state so refresh/back doesn't re-trigger edit mode
@ -52,6 +54,22 @@ const TaskDetails: React.FC = () => {
}
}, [isNewTask, navigate, location.pathname]);
// Clean up abandoned new tasks: if user navigates away without modifying anything, delete the task
useEffect(() => {
const taskUid = uid;
return () => {
if (isNewTaskRef.current && !taskModifiedRef.current && taskUid) {
deleteTask(taskUid).catch((err) =>
console.error('Error cleaning up abandoned new task:', err)
);
const store = useStore.getState();
store.tasksStore.setTasks(
store.tasksStore.tasks.filter((t: Task) => t.uid !== taskUid)
);
}
};
}, [uid]);
const projectsStore = useStore((state: any) => state.projectsStore);
const tagsStore = useStore((state: any) => state.tagsStore);
const tasksStore = useStore((state: any) => state.tasksStore);
@ -221,6 +239,7 @@ const TaskDetails: React.FC = () => {
}
try {
taskModifiedRef.current = true;
const recurrencePayload: Partial<Task> = {
recurrence_type: recurrenceForm.recurrence_type,
recurrence_interval: recurrenceForm.recurrence_interval || 1,
@ -336,6 +355,7 @@ const TaskDetails: React.FC = () => {
}
try {
taskModifiedRef.current = true;
await updateTask(task.uid, {
...task,
due_date: editedDueDate || null,
@ -409,6 +429,7 @@ const TaskDetails: React.FC = () => {
}
try {
taskModifiedRef.current = true;
await updateTask(task.uid, {
defer_until: editedDeferUntil || null,
});
@ -636,6 +657,7 @@ const TaskDetails: React.FC = () => {
}
try {
taskModifiedRef.current = true;
await updateTask(task.uid, { ...task, subtasks: subtasksToSave });
if (uid) {
@ -666,6 +688,7 @@ const TaskDetails: React.FC = () => {
if (!task?.uid) return;
try {
taskModifiedRef.current = true;
await updateTask(task.uid, { ...task, project_id: project.id });
if (uid) {
@ -697,6 +720,7 @@ const TaskDetails: React.FC = () => {
if (!task?.uid) return;
try {
taskModifiedRef.current = true;
await updateTask(task.uid, { ...task, project_id: null });
if (uid) {
@ -775,6 +799,7 @@ const TaskDetails: React.FC = () => {
}
try {
taskModifiedRef.current = true;
const updatedTaskResponse = await toggleTaskCompletion(
task.uid,
task
@ -813,6 +838,7 @@ const TaskDetails: React.FC = () => {
if (!task?.uid) return;
try {
taskModifiedRef.current = true;
await updateTask(task.uid, {
...task,
status: newStatus,
@ -853,6 +879,7 @@ const TaskDetails: React.FC = () => {
const handleDeleteConfirm = async () => {
if (taskToDelete?.uid) {
try {
taskModifiedRef.current = true;
await deleteTask(taskToDelete.uid);
showSuccessToast(
t('task.deleteSuccess', 'Task deleted successfully')
@ -899,6 +926,7 @@ const TaskDetails: React.FC = () => {
}
try {
taskModifiedRef.current = true;
await updateTask(task.uid, { ...task, name: newTitle.trim() });
if (uid) {
@ -939,6 +967,7 @@ const TaskDetails: React.FC = () => {
}
try {
taskModifiedRef.current = true;
await updateTask(task.uid, { ...task, note: trimmedContent });
if (uid) {
@ -971,6 +1000,7 @@ const TaskDetails: React.FC = () => {
if (!task?.uid || !name.trim()) return;
try {
taskModifiedRef.current = true;
const newProject = await createProject({ name });
projectsStore.setProjects([...projectsStore.projects, newProject]);
@ -1017,6 +1047,7 @@ const TaskDetails: React.FC = () => {
}
try {
taskModifiedRef.current = true;
await updateTask(task.uid, {
...task,
tags: tags.map((name) => ({ name })),
@ -1058,6 +1089,7 @@ const TaskDetails: React.FC = () => {
if (!task?.uid) return;
try {
taskModifiedRef.current = true;
await updateTask(task.uid, {
...task,
priority: priority,