From fe6ad909d4be7998b3d25d5d6b59ad0e87c8fa73 Mon Sep 17 00:00:00 2001 From: ayoung19 Date: Tue, 1 Oct 2024 15:39:44 -0700 Subject: [PATCH] fix: delete associated tags when entries are deleted --- popup/components/EntryRow.tsx | 2 +- utils/entries.ts | 47 +++++++++++++++++++------------- utils/storage.ts | 50 +++++++++++++++++++++++------------ 3 files changed, 62 insertions(+), 37 deletions(-) diff --git a/popup/components/EntryRow.tsx b/popup/components/EntryRow.tsx index 268dd9f..1fdd853 100644 --- a/popup/components/EntryRow.tsx +++ b/popup/components/EntryRow.tsx @@ -115,7 +115,7 @@ export const EntryRow = ({ entry, selectedEntryIds }: Props) => { {entry.content.slice(0, 1000)} - {entryIdToTags[entry.id]?.toSorted().map((tag) => )} + {entryIdToTags[entry.id]?.toSorted().map((tag) => )} diff --git a/utils/entries.ts b/utils/entries.ts index b1980b6..99c76fe 100644 --- a/utils/entries.ts +++ b/utils/entries.ts @@ -1,26 +1,35 @@ import type { Entry } from "~types/entry"; +import type { Settings } from "~types/settings"; export const applyLocalItemLimit = ( entries: Entry[], - favoriteEntryIdSet: Set, - localItemLimit: number, -) => - entries - .reduceRight<[Entry[], number]>( - (acc, curr) => { - if (favoriteEntryIdSet.has(curr.id)) { - acc[0].push(curr); - return acc; - } + { localItemLimit }: Settings, + favoriteEntryIds: string[], +): [Entry[], string[]] => { + if (localItemLimit === null) { + return [entries, []]; + } - if (acc[1] < localItemLimit) { - acc[0].push(curr); - acc[1] += 1; - return acc; - } + const favoriteEntryIdSet = new Set(favoriteEntryIds); + const [newEntries, skippedEntryIds] = entries.reduceRight<[Entry[], string[], number]>( + (acc, curr) => { + if (favoriteEntryIdSet.has(curr.id)) { + acc[0].push(curr); return acc; - }, - [[], 0], - )[0] - .reverse(); + } + + if (acc[2] < localItemLimit) { + acc[0].push(curr); + acc[2] += 1; + return acc; + } + + acc[1].push(curr.id); + return acc; + }, + [[], [], 0], + ); + + return [newEntries.reverse(), skippedEntryIds]; +}; diff --git a/utils/storage.ts b/utils/storage.ts index 52118a4..46b664b 100644 --- a/utils/storage.ts +++ b/utils/storage.ts @@ -2,6 +2,7 @@ import { createHash } from "crypto"; import { Storage } from "@plasmohq/storage"; +import { getEntryIdToTags, setEntryIdToTags } from "~storage/entryIdToTags"; import { getFavoriteEntryIds } from "~storage/favoriteEntryIds"; import { getSettings } from "~storage/settings"; import { Entry } from "~types/entry"; @@ -38,37 +39,52 @@ export const getEntries = async () => { }; export const setEntries = async (entries: Entry[]) => { - const [settings, favoriteEntryIds] = await Promise.all([getSettings(), getFavoriteEntryIds()]); - const newEntries = - settings.localItemLimit === null - ? entries - : applyLocalItemLimit(entries, new Set(favoriteEntryIds), settings.localItemLimit); + const settings = await getSettings(); await Promise.all([ - storage.set(ENTRIES_STORAGE_KEY, newEntries), - settings.totalItemsBadge ? setActionBadgeText(newEntries.length) : removeActionBadgeText(), + storage.set(ENTRIES_STORAGE_KEY, entries), + settings.totalItemsBadge ? setActionBadgeText(entries.length) : removeActionBadgeText(), ]); }; export const createEntry = async (content: string) => { + const [entries, settings, favoriteEntryIds, entryIdToTags] = await Promise.all([ + getEntries(), + getSettings(), + getFavoriteEntryIds(), + getEntryIdToTags(), + ]); + const entryId = createHash("sha256").update(content).digest("hex"); - const entries = await getEntries(); + const [newEntries, skippedEntryIds] = applyLocalItemLimit( + [ + ...entries.filter(({ id }) => id !== entryId), + { + id: entryId, + createdAt: Date.now(), + content, + }, + ], + settings, + favoriteEntryIds, + ); + skippedEntryIds.forEach((entryId) => delete entryIdToTags[entryId]); - await setEntries([ - ...entries.filter(({ id }) => id !== entryId), - { - id: entryId, - createdAt: Date.now(), - content, - }, + await Promise.all([ + setEntries(newEntries), + ...(skippedEntryIds.length > 0 ? [setEntryIdToTags(entryIdToTags)] : []), ]); }; export const deleteEntries = async (entryIds: string[]) => { const entryIdSet = new Set(entryIds); - const entries = await getEntries(); + const [entries, entryIdToTags] = await Promise.all([getEntries(), getEntryIdToTags()]); + entryIds.forEach((entryId) => delete entryIdToTags[entryId]); - await setEntries(entries.filter(({ id }) => !entryIdSet.has(id))); + await Promise.all([ + setEntries(entries.filter(({ id }) => !entryIdSet.has(id))), + setEntryIdToTags(entryIdToTags), + ]); };