Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: fix trash list order and headers past one batch (#10538)

Abdullah Attaadc78ca048be
diff --git a/packages/core/__tests__/trash.test.ts b/packages/core/__tests__/trash.test.ts
index f308ff2d9..b6183d135 100644
--- a/packages/core/__tests__/trash.test.ts
+++ b/packages/core/__tests__/trash.test.ts
@@ -409,3 +409,29 @@ test("permanently deleted note should not have note fields", () =>
"synced"
]);
}));
+
+test("trash list should be sorted globally across batches", () =>
+ databaseTest().then(async (db) => {
+ const ids: string[] = [];
+ for (let i = 0; i < 510; i++)
+ ids.push(await db.notes.add({ title: `${510 - i}` }));
+ await db.trash.add("note", ids);
+ // app-deleted items are hidden from the list & must not affect headers
+ await db.trash.add("note", [await db.notes.add({ title: "zzz" })], "app");
+
+ const grouping = await db.trash.grouped({
+ groupBy: "abc",
+ sortBy: "title",
+ sortDirection: "asc"
+ });
+ const titles: string[] = [];
+ for (let i = 0; i < grouping.length; i++)
+ titles.push((await grouping.item(i)).item!.title);
+ expect(titles).toStrictEqual(
+ Array.from({ length: 510 }, (_, i) => `${i + 1}`)
+ );
+
+ for (const { index, group } of await grouping.groups!()) {
+ expect((await grouping.item(index)).group?.title).toBe(group.title);
+ }
+ }));
diff --git a/packages/core/src/collections/trash.ts b/packages/core/src/collections/trash.ts
index 08cafc618..7ee711d7c 100644
--- a/packages/core/src/collections/trash.ts
+++ b/packages/core/src/collections/trash.ts
@@ -262,45 +262,45 @@ export default class Trash {
}
async grouped(options: GroupOptions) {
- const ids = [
- ...this.userDeletedCache.notes,
- ...this.userDeletedCache.notebooks
- ];
const selector = getSortSelectors(options)[options.sortDirection];
+ const items = (await this.userDeletedItems()).sort(selector);
+
return new VirtualizedGrouping<TrashItem>(
- ids.length,
+ items.length,
this.db.options.batchSize,
- () => Promise.resolve(ids),
+ async () => items.map((item) => item.id),
async (start, end) => {
- const slicedIds = ids.slice(start, end);
- const noteIds = slicedIds.filter((id) =>
- this.userDeletedCache.notes.includes(id)
- );
- const notebookIds = slicedIds.filter((id) =>
- this.userDeletedCache.notebooks.includes(id)
- );
- const items = [
- ...(await this.trashedNotes(noteIds)),
- ...(await this.trashedNotebooks(notebookIds))
- ];
- items.sort(selector);
-
+ const slicedItems = items.slice(start, end);
return {
- ids: slicedIds,
- items
+ ids: slicedItems.map((item) => item.id),
+ items: slicedItems
};
},
(items) => groupArray(items, createKeySelector(options)),
- async () => {
- const items = await this.all();
- items.sort(selector);
- return Array.from(
- groupArray(items, createKeySelector(options)).values()
- );
- }
+ async () =>
+ Array.from(groupArray(items, createKeySelector(options)).values())
);
}
+ private async userDeletedItems() {
+ return [
+ ...(await this.db
+ .sql()
+ .selectFrom("notes")
+ .where("type", "==", "trash")
+ .where("deletedBy", "in", ["user", "expired"])
+ .selectAll()
+ .execute()),
+ ...(await this.db
+ .sql()
+ .selectFrom("notebooks")
+ .where("type", "==", "trash")
+ .where("deletedBy", "==", "user")
+ .selectAll()
+ .execute())
+ ] as TrashItem[];
+ }
+
/**
*
* @param {string} id
diff --git a/packages/core/src/utils/grouping.ts b/packages/core/src/utils/grouping.ts
index ae780f02c..ab55261d2 100644
--- a/packages/core/src/utils/grouping.ts
+++ b/packages/core/src/utils/grouping.ts
@@ -59,16 +59,17 @@ export const getSortValue = (
return item.dateCreated || 0;
};
+const collator = new Intl.Collator(undefined, { numeric: true });
+
export function getSortSelectors<T extends PartialGroupableItem>(
options: SortOptions
) {
- if (options.sortBy === "title")
+ if (options.sortBy === "title") {
return {
- asc: (a: T, b: T) =>
- getTitle(a).localeCompare(getTitle(b), undefined, { numeric: true }),
- desc: (a: T, b: T) =>
- getTitle(b).localeCompare(getTitle(a), undefined, { numeric: true })
+ asc: (a: T, b: T) => collator.compare(getTitle(a), getTitle(b)),
+ desc: (a: T, b: T) => collator.compare(getTitle(b), getTitle(a))
};
+ }
return {
asc: (a: T, b: T) => getSortValue(options, a) - getSortValue(options, b),