diff --git a/package.json b/package.json index 938c61d..d5bc881 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "dependencies": { "chart.js": "^4.5.0", "chroma-js": "^3.1.2", + "uuid": "^11.1.0", "yaml": "^2.8.0", "zod": "^3.25.67" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b71b8ad..1333920 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: chroma-js: specifier: ^3.1.2 version: 3.1.2 + uuid: + specifier: ^11.1.0 + version: 11.1.0 yaml: specifier: ^2.8.0 version: 2.8.0 @@ -1592,6 +1595,10 @@ packages: uri-js@4.4.1: resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==} + uuid@11.1.0: + resolution: {integrity: sha512-0/A9rDy9P7cJ+8w1c9WD9V//9Wj15Ce2MPz8Ri6032usz+NfePxx5AcN3bN+r6ZL6jEo066/yNYB3tn4pQEx+A==} + hasBin: true + validate-npm-package-license@3.0.4: resolution: {integrity: sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==} @@ -3189,6 +3196,8 @@ snapshots: dependencies: punycode: 2.3.1 + uuid@11.1.0: {} + validate-npm-package-license@3.0.4: dependencies: spdx-correct: 3.2.0 diff --git a/src/commands/LogReadingFinishedCommand.ts b/src/commands/LogReadingFinishedCommand.ts index 07162f2..394b3dd 100644 --- a/src/commands/LogReadingFinishedCommand.ts +++ b/src/commands/LogReadingFinishedCommand.ts @@ -58,7 +58,7 @@ export class LogReadingFinishedCommand extends EditorCheckCommand { ); try { - await this.readingLog.addEntry(fileName, pageCount, pageCount); + await this.readingLog.createEntry(fileName, pageCount, pageCount); } catch (error) { new Notice( `Failed to log reading progress for ${fileName}: ${error}` diff --git a/src/commands/LogReadingProgressCommand.ts b/src/commands/LogReadingProgressCommand.ts index f6103ea..459bbd8 100644 --- a/src/commands/LogReadingProgressCommand.ts +++ b/src/commands/LogReadingProgressCommand.ts @@ -59,7 +59,7 @@ export class LogReadingProgressCommand extends EditorCheckCommand { } try { - await this.readingLog.addEntry(fileName, pageNumber, pageCount); + await this.readingLog.createEntry(fileName, pageNumber, pageCount); } catch (error) { new Notice( `Failed to log reading progress for ${fileName}: ${error}` diff --git a/src/commands/ResetReadingStatusCommand.ts b/src/commands/ResetReadingStatusCommand.ts index 1d4b383..27ae0b8 100644 --- a/src/commands/ResetReadingStatusCommand.ts +++ b/src/commands/ResetReadingStatusCommand.ts @@ -43,7 +43,7 @@ export class ResetReadingStatusCommand extends EditorCheckCommand { frontMatter[this.settings.endDateProperty] = ""; }); - this.readingLog.removeEntries(file.basename); + this.readingLog.removeEntriesForBook(file.basename); new Notice("Reading status reset for " + file.basename); } diff --git a/src/ui/code-blocks/ReadingLogCodeBlockView.svelte b/src/ui/code-blocks/ReadingLogCodeBlockView.svelte index 04d120f..e4d7a6a 100644 --- a/src/ui/code-blocks/ReadingLogCodeBlockView.svelte +++ b/src/ui/code-blocks/ReadingLogCodeBlockView.svelte @@ -3,8 +3,9 @@ import { Edit, Trash, Plus } from "lucide-svelte"; import { ReadingLogEntryEditModal } from "@ui/modals"; import type BookTrackerPlugin from "@src/main"; - - const ALL_TIME = "ALL_TIME"; + import { createReadingLog } from "@ui/stores/reading-log.svelte"; + import { ALL_TIME } from "@ui/stores/date-filter.svelte"; + import { onDestroy } from "svelte"; interface Props { plugin: BookTrackerPlugin; @@ -18,112 +19,60 @@ return `obsidian://open?vault=${v}&file=${f}`; } - let entries = $state( - plugin.readingLog.getEntries().map((entry, id) => ({ - ...entry, - id, - })), - ); - - function reload() { - entries = plugin.readingLog.getEntries().map((entry, id) => ({ - ...entry, - id, - })); - } - - const years = $derived([ - ...new Set(entries.map((entry) => entry.createdAt.year())), - ]); - - // @ts-expect-error Moment is provided by Obsidian - let selectedYear = $state(moment().year().toString()); - const filterYear = $derived( - selectedYear === ALL_TIME ? ALL_TIME : parseInt(selectedYear, 10), - ); - - // @ts-expect-error Moment is provided by Obsidian - let selectedMonth = $state(moment().format("MM")); - const filterMonth = $derived( - selectedMonth === "" ? undefined : parseInt(selectedMonth, 10), - ); - - const filteredEntries = $derived.by(() => { - if (filterYear === ALL_TIME) { - return entries; - } - - // @ts-expect-error Moment is provided by Obsidian - let startDate = moment().year(filterYear).startOf("year"); - - // @ts-expect-error Moment is provided by Obsidian - let endDate = moment().year(filterYear).endOf("year"); - - if (filterMonth !== undefined) { - startDate = startDate.month(filterMonth - 1).startOf("month"); - endDate = endDate.month(filterMonth - 1).endOf("month"); - } - - return entries.filter((entry) => { - return ( - entry.createdAt.isSameOrAfter(startDate) && - entry.createdAt.isSameOrBefore(endDate) - ); - }); - }); + const store = createReadingLog(plugin.readingLog); + onDestroy(() => store.destroy()); function createEntry() { const modal = new ReadingLogEntryEditModal(plugin, async (entry) => { modal.close(); - await plugin.readingLog.addRawEntry(entry); - reload(); + await store.addEntry(entry); }); modal.open(); } - function editEntry(i: number, entry: ReadingLogEntry) { + function editEntry(entry: ReadingLogEntry) { const modal = new ReadingLogEntryEditModal( plugin, async (entry) => { modal.close(); - await plugin.readingLog.updateEntry(i, entry); - reload(); + await store.updateEntry(entry); }, entry, ); modal.open(); } - async function deleteEntry(i: number) { - await plugin.readingLog.spliceEntry(i); - reload(); + async function removeEntry(entry: ReadingLogEntry) { + await store.removeEntry(entry); }