Skip to content

Commit

Permalink
fix: delete associated tags when entries are deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoung19 committed Oct 1, 2024
1 parent eebe728 commit fe6ad90
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 37 deletions.
2 changes: 1 addition & 1 deletion popup/components/EntryRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const EntryRow = ({ entry, selectedEntryIds }: Props) => {
{entry.content.slice(0, 1000)}
</Text>
<Group align="center" spacing={rem(4)} noWrap>
{entryIdToTags[entry.id]?.toSorted().map((tag) => <TagBadge tag={tag} />)}
{entryIdToTags[entry.id]?.toSorted().map((tag) => <TagBadge key={tag} tag={tag} />)}
</Group>
<Group align="center" spacing={0} noWrap ml={rem(4)}>
<TagSelect entryId={entry.id} />
Expand Down
47 changes: 28 additions & 19 deletions utils/entries.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import type { Entry } from "~types/entry";
import type { Settings } from "~types/settings";

export const applyLocalItemLimit = (
entries: Entry[],
favoriteEntryIdSet: Set<string>,
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];
};
50 changes: 33 additions & 17 deletions utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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),
]);
};

0 comments on commit fe6ad90

Please sign in to comment.