-
Notifications
You must be signed in to change notification settings - Fork 103
feat: bookmarks to pin packages #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Flo0806
wants to merge
6
commits into
npmx-dev:main
Choose a base branch
from
Flo0806:feat/bookmark-packages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8dd0db
feat: add bookmark buttons and logic
Flo0806 ae46238
chore: add tests
Flo0806 fd1a816
Merge branch 'main' into feat/bookmark-packages
Flo0806 86b83ee
[autofix.ci] apply automated fixes
autofix-ci[bot] a88334d
fix: unocss syntax
Flo0806 ed824c1
fix: merge conflict
Flo0806 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <script setup lang="ts"> | ||
| const props = defineProps<{ | ||
| packageName: string | ||
| }>() | ||
|
|
||
| const { useIsBookmarked, toggleBookmark } = useBookmarks() | ||
|
|
||
| const isBookmarked = useIsBookmarked(() => props.packageName) | ||
|
|
||
| function handleClick() { | ||
| toggleBookmark(props.packageName) | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <button | ||
| type="button" | ||
| class="p-1.5 rounded transition-colors duration-150 border border-transparent hover:bg-bg hover:border-border focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50" | ||
| :aria-label=" | ||
| isBookmarked | ||
| ? $t('bookmarks.remove', { name: packageName }) | ||
| : $t('bookmarks.add', { name: packageName }) | ||
| " | ||
| :aria-pressed="isBookmarked" | ||
| @click="handleClick" | ||
| > | ||
| <span | ||
| class="block w-4 h-4 transition-colors duration-150" | ||
| :class=" | ||
| isBookmarked | ||
| ? 'i-carbon:bookmark-filled text-accent' | ||
| : 'i-carbon:bookmark text-fg-subtle hover:text-fg' | ||
| " | ||
| aria-hidden="true" | ||
| /> | ||
| </button> | ||
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <script setup lang="ts"> | ||
| const { bookmarks, hasBookmarks, removeBookmark, clearBookmarks } = useBookmarks() | ||
|
|
||
| const isOpen = ref(false) | ||
|
|
||
| function handleMouseEnter() { | ||
| isOpen.value = true | ||
| } | ||
|
|
||
| function handleMouseLeave() { | ||
| isOpen.value = false | ||
| } | ||
|
|
||
| function handleKeydown(event: KeyboardEvent) { | ||
| if (event.key === 'Escape' && isOpen.value) { | ||
| isOpen.value = false | ||
| } | ||
| } | ||
|
|
||
| function handleRemove(packageName: string, event: Event) { | ||
| event.preventDefault() | ||
| event.stopPropagation() | ||
| removeBookmark(packageName) | ||
| } | ||
|
|
||
| function handleClearAll(event: Event) { | ||
| event.preventDefault() | ||
| clearBookmarks() | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <div | ||
| class="relative flex items-center" | ||
| @mouseenter="handleMouseEnter" | ||
| @mouseleave="handleMouseLeave" | ||
| @keydown="handleKeydown" | ||
| > | ||
| <button | ||
| type="button" | ||
| class="link-subtle font-mono text-sm inline-flex items-center gap-1 leading-tight" | ||
| :aria-expanded="isOpen" | ||
| aria-haspopup="true" | ||
| > | ||
| <span | ||
| class="w-[1em] h-[1em] shrink-0" | ||
| :class="hasBookmarks ? 'i-carbon:bookmark-filled' : 'i-carbon:bookmark'" | ||
| aria-hidden="true" | ||
| /> | ||
| <span class="hidden sm:inline">{{ $t('header.bookmarks') }}</span> | ||
| <span | ||
| class="hidden sm:inline i-carbon-chevron-down w-3 h-3 transition-transform duration-200" | ||
| :class="{ 'rotate-180': isOpen }" | ||
| aria-hidden="true" | ||
| /> | ||
| </button> | ||
|
|
||
| <Transition | ||
| enter-active-class="transition-all duration-150" | ||
| leave-active-class="transition-all duration-100" | ||
| enter-from-class="opacity-0 translate-y-1" | ||
| leave-to-class="opacity-0 translate-y-1" | ||
| > | ||
| <div v-if="isOpen" class="absolute inset-ie-0 top-full pt-2 w-72 z-50"> | ||
| <div class="bg-bg-elevated border border-border rounded-lg shadow-lg overflow-hidden"> | ||
| <div class="px-3 py-2 border-b border-border flex items-center justify-between"> | ||
| <span class="font-mono text-xs text-fg-subtle"> | ||
| {{ $t('header.bookmarks_dropdown.title') }} | ||
| </span> | ||
| <button | ||
| v-if="hasBookmarks" | ||
| type="button" | ||
| class="font-mono text-xs text-fg-subtle hover:text-fg transition-colors" | ||
| @click="handleClearAll" | ||
| > | ||
| {{ $t('header.bookmarks_dropdown.clear_all') }} | ||
| </button> | ||
| </div> | ||
|
|
||
| <ul v-if="hasBookmarks" class="py-1 max-h-80 overflow-y-auto"> | ||
| <li v-for="bookmark in bookmarks" :key="bookmark.packageName"> | ||
| <div class="flex items-center gap-1 px-3 hover:bg-bg-subtle transition-colors"> | ||
| <NuxtLink | ||
| :to="`/${bookmark.packageName}`" | ||
| class="flex-1 py-2 font-mono text-sm text-fg truncate" | ||
| > | ||
| {{ bookmark.packageName }} | ||
| </NuxtLink> | ||
| <button | ||
| type="button" | ||
| class="p-1 text-fg-subtle hover:text-fg transition-colors shrink-0" | ||
| :aria-label=" | ||
| $t('header.bookmarks_dropdown.remove', { name: bookmark.packageName }) | ||
| " | ||
| @click="handleRemove(bookmark.packageName, $event)" | ||
| > | ||
| <span class="i-carbon-close w-3 h-3 block" aria-hidden="true" /> | ||
| </button> | ||
| </div> | ||
| </li> | ||
| </ul> | ||
|
|
||
| <div v-else class="px-3 py-4 text-center"> | ||
| <span class="text-fg-muted text-sm"> | ||
| {{ $t('header.bookmarks_dropdown.empty') }} | ||
| </span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </Transition> | ||
| </div> | ||
| </template> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import type { RemovableRef } from '@vueuse/core' | ||
| import { useLocalStorage } from '@vueuse/core' | ||
|
|
||
| /** | ||
| * Bookmark entry with package name and timestamp | ||
| */ | ||
| export interface Bookmark { | ||
| packageName: string | ||
| addedAt: number | ||
| } | ||
|
|
||
| const STORAGE_KEY = 'npmx-bookmarks' | ||
|
|
||
| // Shared bookmarks instance (singleton per app) | ||
| let bookmarksRef: RemovableRef<Bookmark[]> | null = null | ||
|
|
||
| /** | ||
| * Composable for managing package bookmarks with localStorage persistence. | ||
| * Bookmarks are shared across all components that use this composable. | ||
| */ | ||
| export function useBookmarks() { | ||
| if (!bookmarksRef) { | ||
| bookmarksRef = useLocalStorage<Bookmark[]>(STORAGE_KEY, [], { | ||
| mergeDefaults: true, | ||
| }) | ||
| } | ||
|
|
||
| const bookmarks = bookmarksRef | ||
|
|
||
| /** | ||
| * Check if a package is bookmarked | ||
| */ | ||
| function isBookmarked(packageName: string): boolean { | ||
| return bookmarks.value.some(b => b.packageName === packageName) | ||
| } | ||
|
|
||
| /** | ||
| * Reactive computed to check if a specific package is bookmarked | ||
| */ | ||
| function useIsBookmarked(packageName: MaybeRefOrGetter<string>) { | ||
| return computed(() => isBookmarked(toValue(packageName))) | ||
| } | ||
|
|
||
| /** | ||
| * Add a package to bookmarks | ||
| */ | ||
| function addBookmark(packageName: string): void { | ||
| if (!isBookmarked(packageName)) { | ||
| bookmarks.value = [{ packageName, addedAt: Date.now() }, ...bookmarks.value] | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Remove a package from bookmarks | ||
| */ | ||
| function removeBookmark(packageName: string): void { | ||
| bookmarks.value = bookmarks.value.filter(b => b.packageName !== packageName) | ||
| } | ||
|
|
||
| /** | ||
| * Toggle bookmark status for a package | ||
| */ | ||
| function toggleBookmark(packageName: string): void { | ||
| if (isBookmarked(packageName)) { | ||
| removeBookmark(packageName) | ||
| } else { | ||
| addBookmark(packageName) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clear all bookmarks | ||
| */ | ||
| function clearBookmarks(): void { | ||
| bookmarks.value = [] | ||
| } | ||
|
|
||
| const bookmarkCount = computed(() => bookmarks.value.length) | ||
| const hasBookmarks = computed(() => bookmarks.value.length > 0) | ||
|
|
||
| return { | ||
| bookmarks, | ||
| bookmarkCount, | ||
| hasBookmarks, | ||
| isBookmarked, | ||
| useIsBookmarked, | ||
| addBookmark, | ||
| removeBookmark, | ||
| toggleBookmark, | ||
| clearBookmarks, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here