|
| 1 | +--- |
| 2 | +import type { Note } from '../../../types/NoteType'; |
| 3 | +
|
| 4 | +interface Props { |
| 5 | + notes: Note[]; |
| 6 | +} |
| 7 | +
|
| 8 | +const { notes } = Astro.props; |
| 9 | +
|
| 10 | +// Extract unique types and tags from notes |
| 11 | +const types = Array.from(new Set(notes.map(n => n.data.type).filter(Boolean))) as string[]; |
| 12 | +const tags = Array.from(new Set(notes.flatMap(n => n.data.tags || []))); |
| 13 | +--- |
| 14 | + |
| 15 | +<section class="py-12 bg-background"> |
| 16 | + <div class="container mx-auto px-4 max-w-4xl"> |
| 17 | + <h1 class="text-center text-3xl mb-10">Notes</h1> |
| 18 | + |
| 19 | + <div class="mb-8 space-y-4"> |
| 20 | + <div> |
| 21 | + <label class="block text-sm font-medium mb-2">Filter by Type</label> |
| 22 | + <div class="flex flex-wrap gap-2"> |
| 23 | + <button |
| 24 | + class="filter-btn px-4 py-2 rounded border border-primary text-primary hover:bg-primary hover:text-white transition-colors active" |
| 25 | + data-filter="all" |
| 26 | + > |
| 27 | + All |
| 28 | + </button> |
| 29 | + {types.map(type => ( |
| 30 | + <button |
| 31 | + class="filter-btn px-4 py-2 rounded border border-gray-300 hover:border-primary hover:text-primary transition-colors" |
| 32 | + data-filter={`type-${type}`} |
| 33 | + data-type={type} |
| 34 | + > |
| 35 | + {type.charAt(0).toUpperCase() + type.slice(1)} |
| 36 | + </button> |
| 37 | + ))} |
| 38 | + </div> |
| 39 | + </div> |
| 40 | + |
| 41 | + <div> |
| 42 | + <label class="block text-sm font-medium mb-2">Filter by Tag</label> |
| 43 | + <div class="flex flex-wrap gap-2"> |
| 44 | + {tags.map(tag => ( |
| 45 | + <button |
| 46 | + class="tag-filter px-3 py-1 rounded-full text-sm border border-gray-300 hover:border-primary hover:text-primary transition-colors" |
| 47 | + data-tag={tag} |
| 48 | + > |
| 49 | + #{tag} |
| 50 | + </button> |
| 51 | + ))} |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + |
| 56 | + <div id="notes-container" class="space-y-6"> |
| 57 | + {notes.map((note: Note, index: number) => ( |
| 58 | + <div |
| 59 | + class="note-item transition-all duration-300" |
| 60 | + data-type={note.data.type || 'all'} |
| 61 | + data-tags={JSON.stringify(note.data.tags || [])} |
| 62 | + > |
| 63 | + <a href={`/notes/${note.slug}`} class="block hover:opacity-80"> |
| 64 | + <h3 class="text-xl font-semibold mb-2">{note.data.title}</h3> |
| 65 | + <p class="text-gray-600 dark:text-gray-400 mb-3">{note.data.description}</p> |
| 66 | + {(note.data.tags && note.data.tags.length > 0) && ( |
| 67 | + <div class="flex flex-wrap gap-2 mb-2"> |
| 68 | + {note.data.tags.map(tag => ( |
| 69 | + <span class="inline-block px-2 py-1 text-xs rounded-full bg-primary/10 text-primary"> |
| 70 | + #{tag} |
| 71 | + </span> |
| 72 | + ))} |
| 73 | + </div> |
| 74 | + )} |
| 75 | + <span class="text-sm text-gray-500"> |
| 76 | + {new Date(note.data.datetime).toLocaleDateString()} |
| 77 | + {note.data.type && ` • ${note.data.type}`} |
| 78 | + </span> |
| 79 | + </a> |
| 80 | + </div> |
| 81 | + ))} |
| 82 | + </div> |
| 83 | + </div> |
| 84 | +</section> |
| 85 | + |
| 86 | +<script> |
| 87 | + document.addEventListener('DOMContentLoaded', () => { |
| 88 | + const filterBtns = document.querySelectorAll('.filter-btn'); |
| 89 | + const tagBtns = document.querySelectorAll('.tag-filter'); |
| 90 | + const noteItems = document.querySelectorAll('.note-item'); |
| 91 | + |
| 92 | + let activeType = 'all'; |
| 93 | + let activeTags = new Set<string>(); |
| 94 | + |
| 95 | + const updateDisplay = () => { |
| 96 | + noteItems.forEach(item => { |
| 97 | + const itemType = item.getAttribute('data-type'); |
| 98 | + const itemTags = JSON.parse(item.getAttribute('data-tags') || '[]'); |
| 99 | + |
| 100 | + const typeMatch = activeType === 'all' || itemType === activeType; |
| 101 | + const tagMatch = activeTags.size === 0 || itemTags.some(tag => activeTags.has(tag)); |
| 102 | + |
| 103 | + if (typeMatch && tagMatch) { |
| 104 | + item.classList.remove('hidden'); |
| 105 | + item.classList.add('opacity-100'); |
| 106 | + } else { |
| 107 | + item.classList.add('hidden'); |
| 108 | + item.classList.remove('opacity-100'); |
| 109 | + } |
| 110 | + }); |
| 111 | + }; |
| 112 | + |
| 113 | + filterBtns.forEach(btn => { |
| 114 | + btn.addEventListener('click', () => { |
| 115 | + filterBtns.forEach(b => b.classList.remove('active', 'bg-primary', 'text-white')); |
| 116 | + btn.classList.add('active', 'bg-primary', 'text-white'); |
| 117 | + activeType = btn.getAttribute('data-filter')?.replace('type-', '') || 'all'; |
| 118 | + updateDisplay(); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + tagBtns.forEach(btn => { |
| 123 | + btn.addEventListener('click', () => { |
| 124 | + const tag = btn.getAttribute('data-tag'); |
| 125 | + if (tag) { |
| 126 | + if (activeTags.has(tag)) { |
| 127 | + activeTags.delete(tag); |
| 128 | + btn.classList.remove('active', 'border-primary', 'text-primary', 'bg-primary/10'); |
| 129 | + } else { |
| 130 | + activeTags.add(tag); |
| 131 | + btn.classList.add('active', 'border-primary', 'text-primary', 'bg-primary/10'); |
| 132 | + } |
| 133 | + updateDisplay(); |
| 134 | + } |
| 135 | + }); |
| 136 | + }); |
| 137 | + }); |
| 138 | +</script> |
| 139 | + |
| 140 | +<style> |
| 141 | + .active { |
| 142 | + @apply bg-primary text-white border-primary; |
| 143 | + } |
| 144 | +</style> |
0 commit comments