Skip to content

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

core: fix items appearing under wrong group headers (#10515)

Abdullah Attae1bf280a75bb
diff --git a/apps/mobile/app/stores/use-tag-store.ts b/apps/mobile/app/stores/use-tag-store.ts
index 010d2c39e..189f63e17 100644
--- a/apps/mobile/app/stores/use-tag-store.ts
+++ b/apps/mobile/app/stores/use-tag-store.ts
@@ -23,7 +23,7 @@ import createDBCollectionStore from "./create-db-collection-store";
const { useStore: useTagStore, useCollection: useTags } =
createDBCollectionStore({
getCollection: () =>
- db.tags.all.grouped(db.settings.getGroupOptions("tags")),
+ db.tags.all.sorted(db.settings.getGroupOptions("tags")),
eagerlyFetchFirstBatch: true
});
diff --git a/apps/web/src/stores/tag-store.ts b/apps/web/src/stores/tag-store.ts
index 195cf4fae..48e98671f 100644
--- a/apps/web/src/stores/tag-store.ts
+++ b/apps/web/src/stores/tag-store.ts
@@ -28,7 +28,7 @@ class TagStore extends BaseStore<TagStore> {
refresh = async () => {
this.set({
- tags: await db.tags.all.grouped(db.settings.getGroupOptions("tags"))
+ tags: await db.tags.all.sorted(db.settings.getGroupOptions("tags"))
});
};
}
diff --git a/packages/core/__tests__/notes.test.ts b/packages/core/__tests__/notes.test.ts
index 480330140..a299785da 100644
--- a/packages/core/__tests__/notes.test.ts
+++ b/packages/core/__tests__/notes.test.ts
@@ -820,6 +820,53 @@ test("natural sorting of numbered note titles", () =>
]);
}));
+for (const groupBy of ["default", "week", "month", "year", "abc"] as const) {
+ for (const sortBy of ["title", "dateCreated", "dateEdited"] as const) {
+ for (const sortDirection of ["asc", "desc"] as const) {
+ const options: GroupOptions = { groupBy, sortBy, sortDirection };
+ test(`items should be under their own group when grouped by ${groupBy}, sorted by ${sortBy} (${sortDirection})`, () =>
+ databaseTest().then(async (db) => {
+ // ages cross the recent/last week/older thresholds and calendar
+ // weeks; first letters alternate when ordered by date
+ const titles = [
+ "alpha",
+ "Bravo",
+ "apple",
+ "banana",
+ "Avocado",
+ "berry"
+ ];
+ const ages = [0, 6, 8, 13, 15, 400];
+ for (let i = 0; i < titles.length; i++) {
+ const date = dayjs().subtract(ages[i], "days").valueOf();
+ await db.notes.add({
+ title: titles[i],
+ dateCreated: date,
+ dateEdited: date
+ });
+ }
+
+ const keySelector = createKeySelector(options);
+ const grouping = await db.notes.all.grouped(options);
+ const headers: string[] = [];
+ let currentHeader = "";
+ for (let i = 0; i < grouping.length; i++) {
+ const { item, group } = await grouping.item(i);
+ if (group) {
+ headers.push(group.title);
+ currentHeader = group.title;
+ }
+ expect(keySelector(item!)).toBe(currentHeader);
+ }
+ expect(new Set(headers).size).toBe(headers.length);
+
+ const groups = (await grouping.groups!()).map((g) => g.group.title);
+ expect(groups).toStrictEqual(headers);
+ }));
+ }
+ }
+}
+
test("get archived notes", () =>
noteTest().then(async ({ db, id }) => {
await db.notes.archive(true, id);
diff --git a/packages/core/src/database/sql-collection.ts b/packages/core/src/database/sql-collection.ts
index f6221531e..e83ed3577 100644
--- a/packages/core/src/database/sql-collection.ts
+++ b/packages/core/src/database/sql-collection.ts
@@ -42,6 +42,7 @@ import {
AnyColumnWithTable,
ExpressionOrFactory,
Kysely,
+ RawBuilder,
SelectQueryBuilder,
SqlBool,
sql
@@ -55,14 +56,8 @@ import {
createUpcomingReminderTimeQuery
} from "../collections/reminders.js";
-const formats = {
- month: "%Y-%m",
- year: "%Y",
- week: "%Y-%W",
- abc: null,
- default: "%Y-%W",
- none: null
-} satisfies Record<GroupOptions["groupBy"], string | null>;
+const formats = { month: "%Y-%m", year: "%Y", week: "%Y-%W" };
+const WEEK_MS = 7 * 24 * 60 * 60 * 1000;
export const MAX_SQL_PARAMETERS = 200;
export class SQLCollection<
@@ -539,7 +534,7 @@ export class FilteredSelector<T extends Item> {
}
if (options.groupBy === "abc") fields.push("title");
- else if (options.sortBy === "title" && options.groupBy !== "none")
+ else if (!isSortByDate(options) && options.groupBy !== "none")
fields.push("dateCreated");
else if (options.sortBy !== "dueDate" && options.sortBy !== "relevance")
fields.push(options.sortBy);
@@ -619,11 +614,8 @@ export class FilteredSelector<T extends Item> {
private buildSortExpression(options: GroupOptions, hasDueDate?: boolean) {
sanitizeSortOptions(this.type, options);
- const sortBy: Set<SortOptions["sortBy"]> = new Set();
- sortBy.add(options.sortBy);
- if (options.groupBy === "abc") sortBy.add("title");
- else if (options.sortBy === "title" && options.groupBy !== "none")
- sortBy.add("dateCreated");
+ const groupKey =
+ this.type !== "reminders" ? buildGroupKey(options) : undefined;
return <T>(
qb: SelectQueryBuilder<DatabaseSchema, keyof DatabaseSchema, T>
@@ -638,49 +630,58 @@ export class FilteredSelector<T extends Item> {
"desc"
);
- for (const item of sortBy) {
- if (item === "title") {
- qb = qb.orderBy(
- options.sortBy !== "title"
- ? sql`substring(ltrim(title, ' \u00a0\r\n\t\v'), 1, 1) COLLATE NOCASE`
- : sql`natural_key(ltrim(title, ' \u00a0\r\n\t\v')) COLLATE NOCASE`,
- options.sortDirection
- );
- } else {
- const timeFormat = isGroupOptions(options)
- ? formats[options.groupBy]
- : null;
- if (!timeFormat || isSortByDate(options)) {
- if (item === "dueDate") {
- if (hasDueDate)
- qb = qb.orderBy(item as any, options.sortDirection);
- else
- qb = qb.orderBy(
- (qb) => qb.parens(createUpcomingReminderTimeQuery()),
- options.sortDirection
- );
- } else if (item !== "relevance")
- qb = qb.orderBy(item, options.sortDirection);
- continue;
- }
+ // order by group first so items of the same group stay together
+ if (groupKey) qb = qb.orderBy(...groupKey);
- qb = qb.orderBy(
- sql`strftime('${sql.raw(timeFormat)}', ${sql.raw(
- item
- )} / 1000, 'unixepoch', 'localtime')`,
- options.sortDirection
- );
- }
- }
+ if (options.sortBy === "title")
+ qb = qb.orderBy(
+ sql`natural_key(ltrim(title, ' \u00a0\r\n\t\v')) COLLATE NOCASE`,
+ options.sortDirection
+ );
+ else if (options.sortBy === "dueDate" && hasDueDate)
+ qb = qb.orderBy("dueDate" as any, options.sortDirection);
+ else if (options.sortBy === "dueDate")
+ qb = qb.orderBy(
+ (qb) => qb.parens(createUpcomingReminderTimeQuery()),
+ options.sortDirection
+ );
+ else if (options.sortBy !== "relevance")
+ qb = qb.orderBy(options.sortBy, options.sortDirection);
return qb;
};
}
}
-function isGroupOptions(
- options: SortOptions | GroupOptions
-): options is GroupOptions {
- return "groupBy" in options;
+// must match the group keys in `createKeySelector`
+function buildGroupKey(
+ options: GroupOptions
+): [RawBuilder<unknown>, "asc" | "desc"] | undefined {
+ const { groupBy, sortBy } = options;
+ if (groupBy === "abc")
+ return sortBy === "title"
+ ? undefined
+ : [
+ sql`substring(ltrim(title, ' \u00a0\r\n\t\v'), 1, 1) COLLATE NOCASE`,
+ "asc"
+ ];
+
+ // date sorts already keep date groups together
+ if (groupBy === "none" || sortBy === "relevance" || isSortByDate(options))
+ return;
+
+ if (groupBy === "default") {
+ const lastWeek = Date.now() - WEEK_MS;
+ return [
+ sql`CASE WHEN dateCreated > ${lastWeek} THEN 2 WHEN dateCreated > ${
+ lastWeek - WEEK_MS
+ } THEN 1 ELSE 0 END`,
+ "desc"
+ ];
+ }
+ return [
+ sql`strftime(${formats[groupBy]}, dateCreated / 1000, 'unixepoch', 'localtime')`,
+ "desc"
+ ];
}
function isSortByDate(options: SortOptions | GroupOptions) {