From b3f5b3b1542a0c437f0263e6bfe1226faaca80e3 Mon Sep 17 00:00:00 2001 From: Daniel Metcalfe Date: Sun, 14 Jul 2024 16:41:22 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Deleted=20todo=20not=20removed?= =?UTF-8?q?=20from=20important=20list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/db.ts | 10 ++++++++++ components/pages/Home.tsx | 17 +++++++++++++++++ components/todos/TodoActionSheet.tsx | 10 +++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/components/db.ts b/components/db.ts index 0a2cec0..6c4000c 100644 --- a/components/db.ts +++ b/components/db.ts @@ -1,4 +1,5 @@ import Dexie, { Table } from 'dexie' + import dexieCloud from 'dexie-cloud-addon' export interface Todo { @@ -62,3 +63,12 @@ export class DexieStarfocus extends Dexie { } export const db = new DexieStarfocus() + +// Helpful for inspecting the database to debug errors in production +declare global { + interface Window { + db: DexieStarfocus + } +} + +window.db = db diff --git a/components/pages/Home.tsx b/components/pages/Home.tsx index e8db37e..bc58716 100644 --- a/components/pages/Home.tsx +++ b/components/pages/Home.tsx @@ -386,13 +386,30 @@ export const MiscMenu = () => { { + // Remove invalid notes from todos await db.todos.toCollection().modify(todo => { delete todo['uri'] if (todo.note && !todo.note.uri) { delete todo.note } }) + + // Remove empty todos await db.todos.where('title').equals('').delete() + + // Remove todos from important list that don't exist + const important = await db.lists.get('#important') + important?.order.forEach(async id => { + const todo = await db.todos.get(id) + if (todo) { + await db.lists.update('#important', { + order: removeItemFromArray( + important.order, + important.order.indexOf(id), + ), + }) + } + }) }} > Clean database diff --git a/components/todos/TodoActionSheet.tsx b/components/todos/TodoActionSheet.tsx index 55696f0..05c7c8b 100644 --- a/components/todos/TodoActionSheet.tsx +++ b/components/todos/TodoActionSheet.tsx @@ -43,7 +43,15 @@ export function TodoActionSheetProvider({ children }: PropsWithChildren) { action: 'delete', }, handler: async () => { - await db.todos.delete(todo?.id) + db.transaction('rw', db.todos, async () => { + await db.todos.delete(todo!.id) + const important = await db.lists.get('#important') + if (important!.order.includes(todo!.id!)) { + await db.lists.update('#important', list => { + list.order = list.order.filter(id => id !== todo!.id) + }) + } + }) }, }, ]}