Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions app/composables/useRecentlyViewed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { RemovableRef } from '@vueuse/core'
import { useLocalStorage } from '@vueuse/core'
import { computed } from 'vue'

const MAX_RECENT_ITEMS = 5
const STORAGE_KEY = 'npmx-recent'

export type RecentItemType = 'package' | 'org' | 'user'

export interface RecentItem {
type: RecentItemType
/** Canonical identifier: package name, org name (without @), or username */
name: string
/** Display label shown on homepage (e.g. "@nuxt", "~sindresorhus") */
label: string
/** Unix timestamp (ms) of most recent view */
viewedAt: number
}

let recentRef: RemovableRef<RecentItem[]> | null = null

function getRecentRef() {
if (!recentRef) {
recentRef = useLocalStorage<RecentItem[]>(STORAGE_KEY, [])
}
return recentRef
}

export function useRecentlyViewed() {
const items = getRecentRef()
return { items: computed(() => items.value) }
}

export function trackRecentView(item: Omit<RecentItem, 'viewedAt'>) {
if (import.meta.server) return
const items = getRecentRef()
const filtered = items.value.filter(
existing => !(existing.type === item.type && existing.name === item.name),
)
filtered.unshift({ ...item, viewedAt: Date.now() })
items.value = filtered.slice(0, MAX_RECENT_ITEMS)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Guard against corrupted localStorage payloads before filtering.

npmx-recent is user‑controlled; if it contains a non‑array value (or an older schema), .filter() will throw and break tracking. Normalise to a safe array before mutation.

Suggested fix
 export function trackRecentView(item: Omit<RecentItem, 'viewedAt'>) {
   if (import.meta.server) return
   const items = getRecentRef()
-  const filtered = items.value.filter(
+  const current = Array.isArray(items.value) ? items.value : []
+  if (current !== items.value) items.value = current
+  const filtered = current.filter(
     existing => !(existing.type === item.type && existing.name === item.name),
   )
   filtered.unshift({ ...item, viewedAt: Date.now() })
   items.value = filtered.slice(0, MAX_RECENT_ITEMS)
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function trackRecentView(item: Omit<RecentItem, 'viewedAt'>) {
if (import.meta.server) return
const items = getRecentRef()
const filtered = items.value.filter(
existing => !(existing.type === item.type && existing.name === item.name),
)
filtered.unshift({ ...item, viewedAt: Date.now() })
items.value = filtered.slice(0, MAX_RECENT_ITEMS)
export function trackRecentView(item: Omit<RecentItem, 'viewedAt'>) {
if (import.meta.server) return
const items = getRecentRef()
const current = Array.isArray(items.value) ? items.value : []
if (current !== items.value) items.value = current
const filtered = current.filter(
existing => !(existing.type === item.type && existing.name === item.name),
)
filtered.unshift({ ...item, viewedAt: Date.now() })
items.value = filtered.slice(0, MAX_RECENT_ITEMS)
}

}
Copy link
Copy Markdown
Contributor

@OrbisK OrbisK Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for separating trackReventView and items?

If we move trackRecentView into useRecentlyViewed we dont need recentRef as a shared state. useLocalStorage should sync across all refs. If we want to share the exact same ref we could use https://vueuse.org/shared/createSharedComposable/#createsharedcomposable

Suggested change
export function useRecentlyViewed() {
const items = getRecentRef()
return { items: computed(() => items.value) }
}
export function trackRecentView(item: Omit<RecentItem, 'viewedAt'>) {
if (import.meta.server) return
const items = getRecentRef()
const filtered = items.value.filter(
existing => !(existing.type === item.type && existing.name === item.name),
)
filtered.unshift({ ...item, viewedAt: Date.now() })
items.value = filtered.slice(0, MAX_RECENT_ITEMS)
}
export function useRecentlyViewed() {
const items = useLocalStorage<RecentItem[]>(STORAGE_KEY, [])
function trackRecentView(item: Omit<RecentItem, 'viewedAt'>) {
if (import.meta.server) return
const filtered = items.value.filter(
existing => !(existing.type === item.type && existing.name === item.name),
)
filtered.unshift({ ...item, viewedAt: Date.now() })
items.value = filtered.slice(0, MAX_RECENT_ITEMS)
}
return { items: computed(() => items.value), trackRecentView }
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great feedback, thank you! I'm not a vue/nuxt expert so this is helpful 😃.

67 changes: 40 additions & 27 deletions app/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { SHOWCASED_FRAMEWORKS } from '~/utils/frameworks'
import type { RecentItem } from '~/composables/useRecentlyViewed'
import type { RouteLocationRaw } from 'vue-router'

const { model: searchQuery, flushUpdateUrlQuery } = useGlobalSearch()
const isSearchFocused = shallowRef(false)
Expand All @@ -24,6 +25,19 @@ defineOgImageComponent('Default', {
title: 'npmx',
description: 'a fast, modern browser for the **npm registry**',
})

const { items: recentItems } = useRecentlyViewed()

function recentItemRoute(item: RecentItem): RouteLocationRaw {
switch (item.type) {
case 'package':
return packageRoute(item.name)
case 'org':
return { name: 'org', params: { org: item.name } }
case 'user':
return { name: '~username', params: { username: item.name } }
}
}
</script>

<template>
Expand Down Expand Up @@ -107,22 +121,31 @@ defineOgImageComponent('Default', {
<BuildEnvironment class="mt-4" />
</header>

<nav
:aria-label="$t('nav.popular_packages')"
class="pt-4 pb-36 sm:pb-40 text-center motion-safe:animate-fade-in motion-safe:animate-fill-both max-w-xl mx-auto"
style="animation-delay: 0.3s"
>
<ul class="flex flex-wrap items-center justify-center gap-x-6 gap-y-3 list-none m-0 p-0">
<li v-for="framework in SHOWCASED_FRAMEWORKS" :key="framework.name">
<LinkBase :to="packageRoute(framework.package)" class="gap-2 text-sm">
<span
class="home-tag-dot w-1 h-1 rounded-full bg-accent group-hover:bg-fg transition-colors duration-200"
/>
{{ framework.name }}
</LinkBase>
</li>
</ul>
</nav>
<div class="pt-4 pb-36 sm:pb-40 max-w-xl mx-auto">
<ClientOnly>
<nav
v-if="recentItems.length > 0"
aria-labelledby="recently-viewed-label"
class="text-center motion-safe:animate-fade-in motion-safe:animate-fill-both"
style="animation-delay: 0.3s"
>
<div class="flex flex-wrap items-center justify-center gap-x-2 gap-y-3">
<span id="recently-viewed-label" class="text-xs text-fg-subtle tracking-wider">
{{ $t('nav.recently_viewed') }}:
</span>
<ul
class="flex flex-wrap items-center justify-center gap-x-4 gap-y-3 list-none m-0 p-0"
>
<li v-for="item in recentItems" :key="`${item.type}-${item.name}`">
<LinkBase :to="recentItemRoute(item)" class="text-sm">
{{ item.label }}
</LinkBase>
</li>
</ul>
</div>
</nav>
</ClientOnly>
</div>
</section>

<section class="border-t border-border py-24 bg-bg-subtle/10">
Expand All @@ -132,13 +155,3 @@ defineOgImageComponent('Default', {
</section>
</main>
</template>

<style scoped>
/* Windows High Contrast Mode support */
@media (forced-colors: active) {
.home-tag-dot {
forced-color-adjust: none;
background-color: CanvasText;
}
}
</style>
12 changes: 12 additions & 0 deletions app/pages/org/[org].vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ watch(orgName, () => {
currentPage.value = 1
})

if (import.meta.client) {
watch(
() => [status.value, orgName.value] as const,
([s, name]) => {
if (s === 'success') {
trackRecentView({ type: 'org', name, label: `@${name}` })
}
},
{ immediate: true },
)
}

// Handle filter chip removal
function handleClearFilter(chip: FilterChip) {
clearFilter(chip)
Expand Down
12 changes: 12 additions & 0 deletions app/pages/package/[[org]]/[name].vue
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,18 @@ onKeyStroke(
)

const showSkeleton = shallowRef(false)

if (import.meta.client) {
watch(
() => [status.value, packageName.value] as const,
([s, name]) => {
if (s === 'success') {
trackRecentView({ type: 'package', name, label: name })
}
},
{ immediate: true },
)
}
</script>

<template>
Expand Down
12 changes: 12 additions & 0 deletions app/pages/~[username]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ watch(username, () => {
sortOption.value = 'downloads'
})

if (import.meta.client) {
watch(
() => [status.value, username.value] as const,
([s, name]) => {
if (s === 'success') {
trackRecentView({ type: 'user', name, label: `~${name}` })
}
},
{ immediate: true },
)
}

useSeoMeta({
title: () => `~${username.value} - npmx`,
ogTitle: () => `~${username.value} - npmx`,
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "الصفحة الرئيسية",
"popular_packages": "الحزم الشائعة",
"settings": "الإعدادات",
"compare": "مقارنة",
"back": "عودة",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/az-AZ.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
},
"nav": {
"main_navigation": "Əsas",
"popular_packages": "Populyar paketlər",
"settings": "tənzimləmələr",
"back": "geri"
},
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/bg-BG.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "Главна",
"popular_packages": "Популярни пакети",
"settings": "настройки",
"compare": "сравняване",
"back": "назад",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/bn-IN.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
},
"nav": {
"main_navigation": "প্রধান",
"popular_packages": "জনপ্রিয় প্যাকেজগুলি",
"settings": "সেটিংস",
"compare": "তুলনা করুন",
"back": "পিছনে",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/cs-CZ.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "Hlavní",
"popular_packages": "Populární balíčky",
"settings": "nastavení",
"compare": "porovnat",
"back": "zpět",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/de-DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "Hauptnavigation",
"popular_packages": "Beliebte Pakete",
"settings": "Einstellungen",
"compare": "Vergleichen",
"back": "Zurück",
Expand Down
2 changes: 1 addition & 1 deletion i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
"nav": {
"main_navigation": "Main",
"popular_packages": "Popular packages",
"recently_viewed": "recently viewed",
"settings": "settings",
"compare": "compare",
"back": "back",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "Principal",
"popular_packages": "Paquetes populares",
"settings": "configuración",
"compare": "comparar",
"back": "atrás",
Expand Down
2 changes: 1 addition & 1 deletion i18n/locales/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
"nav": {
"main_navigation": "Barre de navigation",
"popular_packages": "Paquets populaires",
"recently_viewed": "consultés récemment",
"settings": "paramètres",
"compare": "comparer",
"back": "Retour",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/hi-IN.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
},
"nav": {
"main_navigation": "मुख्य",
"popular_packages": "लोकप्रिय पैकेज",
"settings": "सेटिंग्स",
"compare": "तुलना करें",
"back": "वापस",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/hu-HU.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
},
"nav": {
"main_navigation": "Főmenü",
"popular_packages": "Népszerű csomagok",
"settings": "beállítások",
"back": "vissza"
},
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/id-ID.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
},
"nav": {
"main_navigation": "Utama",
"popular_packages": "Paket populer",
"settings": "pengaturan",
"compare": "bandingkan",
"back": "kembali",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/it-IT.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
},
"nav": {
"main_navigation": "Principale",
"popular_packages": "Pacchetti popolari",
"settings": "impostazioni",
"compare": "confronta",
"back": "indietro",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "メイン",
"popular_packages": "人気のパッケージ",
"settings": "設定",
"compare": "比較",
"back": "戻る",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/mr-IN.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
},
"nav": {
"main_navigation": "मुख्य",
"popular_packages": "लोकप्रिय पॅकेजेस",
"settings": "सेटिंग्ज",
"compare": "तुलना करा",
"back": "मागे",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/nb-NO.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "Hovedmeny",
"popular_packages": "Populære pakker",
"settings": "innstillinger",
"compare": "sammenlign",
"back": "tilbake",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/ne-NP.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
},
"nav": {
"main_navigation": "मुख्य",
"popular_packages": "लोकप्रिय प्याकेजहरू",
"settings": "सेटिङ्स",
"compare": "तुलना",
"back": "पछाडि",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/pl-PL.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "Główne",
"popular_packages": "Popularne pakiety",
"settings": "ustawienia",
"compare": "porównaj",
"back": "wstecz",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
},
"nav": {
"main_navigation": "Principal",
"popular_packages": "Pacotes populares",
"settings": "configurações",
"compare": "comparar",
"back": "voltar",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
},
"nav": {
"main_navigation": "Главное",
"popular_packages": "Популярные пакеты",
"settings": "настройки",
"back": "назад"
},
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/ta-IN.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "முதன்மை",
"popular_packages": "பிரபலமான தொகுப்புகள்",
"settings": "அமைப்புகள்",
"compare": "ஒப்பிடு",
"back": "பின்செல்",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/te-IN.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
},
"nav": {
"main_navigation": "ప్రధాన",
"popular_packages": "జనాదరణ ప్యాకేజ్‌లు",
"settings": "సెట్టింగ్‌లు",
"compare": "పోల్చండి",
"back": "వెనక్కి",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/uk-UA.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "Головна",
"popular_packages": "Популярні пакети",
"settings": "параметри",
"compare": "порівняти",
"back": "назад",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "主页",
"popular_packages": "热门软件包",
"settings": "设置",
"compare": "比较包",
"back": "返回",
Expand Down
1 change: 0 additions & 1 deletion i18n/locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"nav": {
"main_navigation": "首頁",
"popular_packages": "熱門套件",
"settings": "設定",
"compare": "比較",
"back": "返回",
Expand Down
Loading
Loading