diff --git a/app/pages/posts/index.vue b/app/pages/posts/index.vue index 426ff1c..14b8c4f 100644 --- a/app/pages/posts/index.vue +++ b/app/pages/posts/index.vue @@ -11,6 +11,41 @@ function calculateReadingTime(text: string): number { const words = text.split(/\s/g).length return Math.ceil(words / wordsPerMinute) } + +const route = useRoute() +const tags = ref(new Set(route.query.tags ? route.query.tags.split(',') : [])) + +function toggleTag(tag: string) { + if (tags.value.has(tag)) { + tags.value.delete(tag) + } + else { + tags.value.add(tag) + } +} + +const sortedPosts = computed(() => { + if (tags.value.size === 0) { + history.replaceState(null, '', '/posts') + return posts.value + } + else { + history.replaceState(null, '', `/posts?tags=${Array.from(tags.value).join(',')}`) + + return [...posts.value].sort((a, b) => { + const aHasTag = a.tags.some(tag => tags.value.has(tag)) + const bHasTag = b.tags.some(tag => tags.value.has(tag)) + + if (aHasTag && !bHasTag) { + return -1 + } + if (!aHasTag && bHasTag) { + return 1 + } + return 0 + }) + } +})