Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

note-list.ts

36 lines1.1 KB

1import type { Note } from "@/db/schema";
2​
3import { groupByDate } from "./dates";
4import type { DateGroup } from "./dates";
5​
6export interface NoteListSections<T extends Note> {
7 pinned: T[];
8 /** Date groups ("Today", "Yesterday"…), or one group without a label. */
9 groups: DateGroup<T>[];
11​
12export interface NoteListOptions {
13 /** The date the list is sorted by, or `null` when it's sorted by title. */
14 dateField: "updatedAt" | "createdAt" | null;
15 groupByDate: boolean;
17​
18/**
19 * Splits a folder's notes (already sorted, pinned first) into the Pinned
20 * section and date groups, the way Notes shows them.
21 */
22export function noteListSections<T extends Note>(
23 notes: T[],
24 now: Date,
25 { dateField, groupByDate: grouped }: NoteListOptions,
26): NoteListSections<T> {
27 const pinned = notes.filter((note) => note.pinned);
28 const others = notes.filter((note) => !note.pinned);
29 if (!grouped || dateField === null) {
30 return { pinned, groups: others.length > 0 ? [{ label: "", items: others }] : [] };
31 }
32 return {
33 pinned,
34 groups: groupByDate(others, (note) => new Date(note[dateField]), now),
35 };