logseq-calendar/lib/queries.ts
2023-08-23 21:47:25 -05:00

35 lines
897 B
TypeScript

import { format } from "date-fns";
import { useQuery } from "react-query";
type JournalQueryDate = JournalEntry[];
interface JournalEntry {
"journal-day": number;
"original-name": string;
}
export function useJournals(dateStart: Date, dateEnd: Date) {
const fmtStart = journalFmt(dateStart);
const fmtEnd = journalFmt(dateEnd);
return useQuery(["journals", dateStart, dateEnd], async () => {
const journals: JournalQueryDate[] = await logseq.DB.datascriptQuery(`
[:find (pull ?p [*])
:where
[?b :block/page ?p]
[?p :block/journal? true]
[?p :block/journal-day ?d]
[(>= ?d ${fmtStart})] [(<= ?d ${fmtEnd})]]
`);
return new Map(
journals
.flatMap((dates) => dates)
.map((entry) => [entry["journal-day"].toString(), entry])
);
});
}
export function journalFmt(date: Date) {
return format(date, "YMMdd");
}