-
-
Notifications
You must be signed in to change notification settings - Fork 200
feat: add package scores display #914
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
MhemedAbderrahmen
wants to merge
7
commits into
npmx-dev:main
Choose a base branch
from
MhemedAbderrahmen:feat/display-score
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.
+200
−2
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cfa8db0
feat: added npm package scores
AbderrahmenMHEMED 037ffc1
feat: hue shift % based color change for scores
AbderrahmenMHEMED e9dad28
fix: file naming
MhemedAbderrahmen fd94b75
feat: i18n strings and a11y test coverage
MhemedAbderrahmen 9dad200
test: mock usePackageScore in a11y tests
MhemedAbderrahmen 95c04c0
fix: merge conflicts
MhemedAbderrahmen 725531f
refactor: npms api constant
MhemedAbderrahmen 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <script setup lang="ts"> | ||
| const props = defineProps<{ | ||
| packageName: string | ||
| }>() | ||
|
|
||
| const { data: score, status } = usePackageScore(() => props.packageName) | ||
|
|
||
| const scoreMetrics = computed(() => { | ||
| if (!score.value) return [] | ||
| return [ | ||
| { | ||
| key: 'quality', | ||
| value: score.value.detail.quality * 100, | ||
| label: $t('package.scores.quality'), | ||
| }, | ||
| { | ||
| key: 'popularity', | ||
| value: score.value.detail.popularity * 100, | ||
| label: $t('package.scores.popularity'), | ||
| }, | ||
| { | ||
| key: 'maintenance', | ||
| value: score.value.detail.maintenance * 100, | ||
| label: $t('package.scores.maintenance'), | ||
| }, | ||
| ] | ||
| }) | ||
|
|
||
| function getScoreColor(percentage: number): string { | ||
| const hue = 25 + (percentage / 100) * (145 - 25) | ||
| return `oklch(0.55 0.12 ${hue})` | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <CollapsibleSection id="scores" :title="$t('package.scores.title')"> | ||
| <template #actions> | ||
| <TooltipApp :text="$t('package.scores.source')"> | ||
| <span class="i-carbon:information w-3.5 h-3.5 text-fg-subtle" aria-hidden="true" /> | ||
| </TooltipApp> | ||
| </template> | ||
| <div v-if="status === 'pending'" class="flex flex-col gap-2"> | ||
| <div v-for="i in 3" :key="i" class="flex items-center gap-3"> | ||
| <SkeletonInline class="w-24 h-3" /> | ||
| <SkeletonInline class="flex-1 h-3 rounded-full" /> | ||
| <SkeletonInline class="w-8 h-3" /> | ||
| </div> | ||
| </div> | ||
| <div v-else-if="score" class="flex flex-col gap-2"> | ||
| <div v-for="metric in scoreMetrics" :key="metric.key" class="flex items-center gap-3"> | ||
| <span class="w-24 text-xs text-fg-subtle">{{ metric.label }}</span> | ||
| <div class="flex-1 h-1.5 bg-border-subtle rounded-full overflow-hidden"> | ||
| <div | ||
| class="h-full rounded-full" | ||
| :style="{ width: `${metric.value}%`, background: getScoreColor(metric.value) }" | ||
| /> | ||
| </div> | ||
| <span class="w-8 text-xs font-mono text-fg-muted text-right"> | ||
| {{ Math.round(metric.value) }}% | ||
| </span> | ||
| </div> | ||
| </div> | ||
| <p v-else class="text-fg-subtle text-sm">{{ $t('package.scores.unavailable') }}</p> | ||
| </CollapsibleSection> | ||
| </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,5 @@ | ||
| import type { NpmsScore } from '#server/api/registry/score/[...pkg].get' | ||
|
|
||
| export function usePackageScore(name: MaybeRefOrGetter<string>) { | ||
| return useLazyFetch<NpmsScore>(() => `/api/registry/score/${toValue(name)}`) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import * as v from 'valibot' | ||
| import { PackageRouteParamsSchema } from '#shared/schemas/package' | ||
| import { CACHE_MAX_AGE_ONE_HOUR, NPMS_API } from '#shared/utils/constants' | ||
|
|
||
| export interface NpmsScore { | ||
| final: number | ||
| detail: { | ||
| quality: number | ||
| popularity: number | ||
| maintenance: number | ||
| } | ||
| } | ||
|
|
||
| export default defineCachedEventHandler( | ||
| async event => { | ||
| const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? [] | ||
| const { rawPackageName } = parsePackageParams(pkgParamSegments) | ||
|
|
||
| try { | ||
| const { packageName } = v.parse(PackageRouteParamsSchema, { | ||
| packageName: rawPackageName, | ||
| }) | ||
|
|
||
| const response = await fetch(`${NPMS_API}/${encodeURIComponent(packageName)}`) | ||
|
|
||
| if (!response.ok) { | ||
| throw createError({ statusCode: response.status, message: 'Failed to fetch npms score' }) | ||
| } | ||
|
|
||
| const data = await response.json() | ||
| return data.score as NpmsScore | ||
| } catch (error: unknown) { | ||
| handleApiError(error, { | ||
| statusCode: 502, | ||
| message: 'Failed to fetch package score from npms.io', | ||
| }) | ||
| } | ||
| }, | ||
| { | ||
| maxAge: CACHE_MAX_AGE_ONE_HOUR, | ||
| swr: true, | ||
| getKey: event => { | ||
| const pkg = getRouterParam(event, 'pkg') ?? '' | ||
| return `npms-score:${pkg}` | ||
| }, | ||
| }, | ||
| ) |
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.