-
-
Notifications
You must be signed in to change notification settings - Fork 375
feat: add OG image for compare pages #2277
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
Adebesin-Cell
wants to merge
16
commits into
npmx-dev:main
Choose a base branch
from
Adebesin-Cell:feat/compare-og-image
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.
+185
−0
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
806e120
feat: add OG image for compare pages
Adebesin-Cell 663f2d4
chore: remove screenshot assets from repo
Adebesin-Cell fdd8983
[autofix.ci] apply automated fixes
autofix-ci[bot] 2758f20
feat: show live download bars and versions in compare OG image
Adebesin-Cell 9adfedc
[autofix.ci] apply automated fixes
autofix-ci[bot] 75f2bf5
fix: mark packages prop as optional
Adebesin-Cell e3bdf73
fix: handle empty state in compare OG image
Adebesin-Cell ddbeefa
fix: use translated description for empty compare OG image
Adebesin-Cell 8dad8ae
fix: skip a11y test for OgImage/Compare (server-rendered image)
Adebesin-Cell 9fcab62
fix(ui): increase visual prominence of download numbers in compare OG…
Adebesin-Cell 7770052
fix: add fetch timeouts and boost download number prominence
Adebesin-Cell 28ca4d3
[autofix.ci] apply automated fixes
autofix-ci[bot] f518e4a
fix: normalise array package input and fix zero-download bar width
Adebesin-Cell 838cf32
[autofix.ci] apply automated fixes
autofix-ci[bot] eac610a
fix: truncate long package names in compare OG image
Adebesin-Cell 8d77d56
Merge branch 'main' into feat/compare-og-image
Adebesin-Cell 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| <script setup lang="ts"> | ||
| import { computed, ref } from 'vue' | ||
| import { encodePackageName } from '#shared/utils/npm' | ||
|
|
||
| const props = withDefaults( | ||
| defineProps<{ | ||
| packages?: string | string[] | ||
| emptyDescription?: string | ||
| primaryColor?: string | ||
| }>(), | ||
| { | ||
| packages: () => [], | ||
| emptyDescription: 'Compare npm packages side-by-side', | ||
| primaryColor: '#60a5fa', | ||
| }, | ||
| ) | ||
|
|
||
| const ACCENT_COLORS = ['#60a5fa', '#f472b6', '#34d399', '#fbbf24'] | ||
|
|
||
| const displayPackages = computed(() => { | ||
| const raw = props.packages | ||
| const list = (typeof raw === 'string' ? raw.split(',') : raw).map(p => p.trim()).filter(Boolean) | ||
| return list.slice(0, 4) | ||
| }) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| interface PkgStats { | ||
| name: string | ||
| downloads: number | ||
| version: string | ||
| color: string | ||
| } | ||
|
|
||
| const stats = ref<PkgStats[]>([]) | ||
|
|
||
| const FETCH_TIMEOUT_MS = 2500 | ||
|
|
||
| try { | ||
| const results = await Promise.all( | ||
| displayPackages.value.map(async (name, index) => { | ||
| const encoded = encodePackageName(name) | ||
| const [dlData, pkgData] = await Promise.all([ | ||
| $fetch<{ downloads: number }>( | ||
| `https://api.npmjs.org/downloads/point/last-week/${encoded}`, | ||
| { timeout: FETCH_TIMEOUT_MS }, | ||
| ).catch(() => null), | ||
| $fetch<{ 'dist-tags'?: { latest?: string } }>(`https://registry.npmjs.org/${encoded}`, { | ||
| timeout: FETCH_TIMEOUT_MS, | ||
| headers: { Accept: 'application/vnd.npm.install-v1+json' }, | ||
| }).catch(() => null), | ||
| ]) | ||
| return { | ||
| name, | ||
| downloads: dlData?.downloads ?? 0, | ||
| version: pkgData?.['dist-tags']?.latest ?? '', | ||
| color: ACCENT_COLORS[index % ACCENT_COLORS.length]!, | ||
| } | ||
| }), | ||
| ) | ||
| stats.value = results | ||
| } catch { | ||
| stats.value = displayPackages.value.map((name, index) => ({ | ||
| name, | ||
| downloads: 0, | ||
| version: '', | ||
| color: ACCENT_COLORS[index % ACCENT_COLORS.length]!, | ||
| })) | ||
| } | ||
|
|
||
| const maxDownloads = computed(() => Math.max(...stats.value.map(s => s.downloads), 1)) | ||
|
|
||
| function formatDownloads(n: number): string { | ||
| if (n === 0) return '—' | ||
| return Intl.NumberFormat('en', { | ||
| notation: 'compact', | ||
| maximumFractionDigits: 1, | ||
| }).format(n) | ||
| } | ||
|
|
||
| // Bar width as percentage string (max 100%) | ||
| function barPct(downloads: number): string { | ||
| if (downloads <= 0) return '0%' | ||
| const pct = (downloads / maxDownloads.value) * 100 | ||
| return `${Math.min(100, Math.max(pct, 5))}%` | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </script> | ||
|
|
||
| <template> | ||
| <div | ||
| class="h-full w-full flex flex-col justify-center px-20 bg-[#050505] text-[#fafafa] relative overflow-hidden" | ||
| style="font-family: 'Geist Mono', sans-serif" | ||
| > | ||
| <div class="relative z-10 flex flex-col gap-5"> | ||
| <!-- Icon + title row --> | ||
| <div class="flex items-start gap-4"> | ||
| <div | ||
| class="flex items-center justify-center w-14 h-14 p-3 rounded-xl shadow-lg bg-gradient-to-tr from-[#3b82f6]" | ||
| :style="{ backgroundColor: primaryColor }" | ||
| > | ||
| <svg | ||
| width="32" | ||
| height="32" | ||
| viewBox="0 0 24 24" | ||
| fill="none" | ||
| stroke="white" | ||
| stroke-width="2.5" | ||
| stroke-linecap="round" | ||
| stroke-linejoin="round" | ||
| > | ||
| <circle cx="18" cy="18" r="3" /> | ||
| <circle cx="6" cy="6" r="3" /> | ||
| <path d="M13 6h3a2 2 0 0 1 2 2v7" /> | ||
| <path d="M11 18H8a2 2 0 0 1-2-2V9" /> | ||
| </svg> | ||
| </div> | ||
|
|
||
| <h1 class="text-7xl font-bold tracking-tight"> | ||
| <span | ||
| class="opacity-80 tracking-[-0.1em]" | ||
| :style="{ color: primaryColor }" | ||
| style="margin-right: 0.25rem" | ||
| >./</span | ||
| >compare | ||
| </h1> | ||
| </div> | ||
|
|
||
| <!-- Empty state --> | ||
| <div | ||
| v-if="stats.length === 0" | ||
| class="text-4xl text-[#a3a3a3]" | ||
| style="font-family: 'Geist', sans-serif" | ||
| > | ||
| {{ emptyDescription }} | ||
| </div> | ||
|
|
||
| <!-- Bar chart rows --> | ||
| <div v-else class="flex flex-col gap-2"> | ||
| <div v-for="pkg in stats" :key="pkg.name" class="flex flex-col gap-1"> | ||
| <!-- Label row: name + downloads + version --> | ||
| <div class="flex items-center gap-3" style="font-family: 'Geist', sans-serif"> | ||
| <span | ||
| class="text-2xl font-semibold tracking-tight truncate max-w-[400px]" | ||
| :style="{ color: pkg.color }" | ||
| > | ||
| {{ pkg.name }} | ||
| </span> | ||
| <span class="text-3xl font-bold text-[#fafafa]"> | ||
| {{ formatDownloads(pkg.downloads) }}/wk | ||
| </span> | ||
| <span | ||
| v-if="pkg.version" | ||
| class="text-lg px-2 py-0.5 rounded-md border" | ||
| :style="{ | ||
| color: pkg.color, | ||
| backgroundColor: pkg.color + '10', | ||
| borderColor: pkg.color + '30', | ||
| }" | ||
| > | ||
| {{ pkg.version }} | ||
| </span> | ||
| </div> | ||
|
|
||
| <!-- Bar --> | ||
| <div | ||
| class="h-6 rounded-md" | ||
| :style="{ | ||
| width: barPct(pkg.downloads), | ||
| background: `linear-gradient(90deg, ${pkg.color}50, ${pkg.color}20)`, | ||
| }" | ||
| /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div | ||
| class="absolute -top-32 -inset-ie-32 w-[550px] h-[550px] rounded-full blur-3xl" | ||
| :style="{ backgroundColor: primaryColor + '10' }" | ||
| /> | ||
| </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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.