Skip to content

Commit

Permalink
Refactor viewedPostIds as Set
Browse files Browse the repository at this point in the history
  • Loading branch information
jonamil committed Dec 7, 2024
1 parent e2220e0 commit 1ff6027
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
v-for="(postItem, index) in content.currentCategoryPostItems?.items"
:key="index"
:item="postItem"
:viewed="content.viewedPostIds.includes(postItem.id)"
:viewed="content.viewedPostIds.has(postItem.id)"
:active="route.name === 'post' && postItem.id === content.currentPostItem?.id"
@click="router.push({ name: 'post', params: { postId: postItem.id } })"
/>
Expand Down
6 changes: 2 additions & 4 deletions src/stores/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,13 @@ export const useContentStore = defineStore('content', () => {
}

// IDs of posts that have previously been viewed
const viewedPostIds = useStorage<number[]>('viewedPostIds', []);
const viewedPostIds = useStorage<Set<number>>('viewedPostIds', new Set());

// mark a post as viewed when it becomes the current post item
watch(
() => currentPostItem.value,
() => {
if (!viewedPostIds.value.includes(currentPostItem.value!.id)) {
viewedPostIds.value.push(currentPostItem.value!.id);
}
viewedPostIds.value.add(currentPostItem.value!.id);
}
);

Expand Down

0 comments on commit 1ff6027

Please sign in to comment.