Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(utils): prevent duplicate date group headers in groupItemsByDate (#1999) (#2000)

Henry Sufd157ed00609
diff --git a/src/utils/dateGrouping.ts b/src/utils/dateGrouping.ts
index 833a03c97..6196a3af0 100644
--- a/src/utils/dateGrouping.ts
+++ b/src/utils/dateGrouping.ts
@@ -6,8 +6,11 @@ export interface DateGroup<T> {
}
/**
- * Buckets newest-first items into Today / Yesterday / Previous 7 days / Older
- * groups, preserving item order within each group.
+ * Buckets items into Today / Yesterday / Previous 7 days / Older, in that
+ * order, preserving item order within each group. The input is not assumed to
+ * be sorted: `updated_at` mixes CURRENT_TIMESTAMP and ISO strings, so SQLite's
+ * string ORDER BY can interleave days, and an edited note keeps its old slot
+ * in the store.
*/
export function groupItemsByDate<T>(
items: T[],
@@ -23,30 +26,23 @@ export function groupItemsByDate<T>(
const weekAgo = new Date(today);
weekAgo.setDate(weekAgo.getDate() - 7);
- const groups: Array<DateGroup<T>> = [];
- let current: DateGroup<T> | null = null;
-
+ const labels = [t("chat.today"), t("chat.yesterday"), t("chat.previousWeek"), t("chat.older")];
+ const buckets: T[][] = labels.map(() => []);
for (const item of items) {
const date = normalizeDbDate(getDate(item));
- const target = new Date(date.getFullYear(), date.getMonth(), date.getDate());
-
- let label: string;
- if (target.getTime() >= today.getTime()) {
- label = t("chat.today");
- } else if (target.getTime() >= yesterday.getTime()) {
- label = t("chat.yesterday");
- } else if (target.getTime() >= weekAgo.getTime()) {
- label = t("chat.previousWeek");
- } else {
- label = t("chat.older");
- }
-
- if (!current || current.label !== label) {
- current = { label, items: [] };
- groups.push(current);
- }
- current.items.push(item);
+ const target = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
+ const index =
+ target >= today.getTime()
+ ? 0
+ : target >= yesterday.getTime()
+ ? 1
+ : target >= weekAgo.getTime()
+ ? 2
+ : 3;
+ buckets[index].push(item);
}
- return groups;
+ return labels.flatMap((label, index) =>
+ buckets[index].length > 0 ? [{ label, items: buckets[index] }] : []
+ );
}
diff --git a/test/utils/dateGrouping.test.js b/test/utils/dateGrouping.test.js
index 1f9be818c..924f9b8b3 100644
--- a/test/utils/dateGrouping.test.js
+++ b/test/utils/dateGrouping.test.js
@@ -84,3 +84,26 @@ test("SQLite zoneless timestamps are grouped like their UTC instant", async (t2)
assert.equal(groups.length, 1);
assert.equal(groups[0].label, "Today");
});
+
+test("consolidates non-consecutive items into single canonical groups without duplicate headers", async (t2) => {
+ const { groupItemsByDate } = await load();
+ t2.mock.timers.enable({ apis: ["Date"], now: NOON_JUNE_15 });
+
+ const items = [
+ { id: 1, updated_at: localIsoDate(2024, 5, 15, 10) }, // Today
+ { id: 2, updated_at: localIsoDate(2024, 5, 14, 22) }, // Yesterday
+ { id: 3, updated_at: localIsoDate(2024, 5, 15, 8) }, // Today
+ { id: 4, updated_at: localIsoDate(2024, 0, 1, 12) }, // Older
+ { id: 5, updated_at: localIsoDate(2024, 5, 10, 12) }, // Previous 7 days
+ ];
+ const groups = groupItemsByDate(items, (i) => i.updated_at, t);
+
+ assert.deepEqual(
+ groups.map((g) => g.label),
+ ["Today", "Yesterday", "Previous 7 days", "Older"]
+ );
+ assert.deepEqual(
+ groups.map((g) => g.items.map((i) => i.id)),
+ [[1, 3], [2], [5], [4]]
+ );
+});