-
Notifications
You must be signed in to change notification settings - Fork 190
feat(modules): add nuxt.care health score badges #924
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,9 @@ const terminalId = useCurrentTerminalId() | |
|
|
||
| <template> | ||
| <ModuleItemBase :mod="mod" :info="staticInfo"> | ||
| <template #badge> | ||
| <ModuleScoreBadge class="ml-1" :npm="data.npm" /> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. About the UI styling, I wonder if this would be better as an item in the table instead of the badge? As I consider the "score" is only one of the aspects of the modules, and does not always reflects if users needs to do any action on that.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A short table item inside the module item? Sure. make sense - I will try something |
||
| </template> | ||
| <template #items> | ||
| <div v-if="mod.entryPath" flex="~ gap-2" title="Open on filesystem"> | ||
| <span i-carbon-folder-move-to flex-none text-lg op50 /> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <script setup lang="ts"> | ||
| import { computed } from 'vue' | ||
| import { statusColors, useModuleScores } from '~/composables/state-module-scores' | ||
|
|
||
| const props = defineProps<{ | ||
| npm?: string | ||
| }>() | ||
|
|
||
| const { scores, loading } = useModuleScores() | ||
| const score = computed(() => props.npm ? scores.value.get(props.npm) : undefined) | ||
| const color = computed(() => score.value ? statusColors[score.value.status] : undefined) | ||
| </script> | ||
|
|
||
| <template> | ||
| <span v-if="loading && !score" class="n-badge" op50> | ||
| <span i-carbon-circle-dash animate-spin /> | ||
| </span> | ||
| <a | ||
| v-else-if="score" | ||
| inline-flex items-center | ||
| :href="`https://nuxt.care/?search=npm:${npm}`" | ||
| target="_blank" | ||
| rel="noopener" | ||
| :title="`Nuxt Care Score: ${score.score}/100 (${score.status})`" | ||
| > | ||
| <span class="n-badge" :style="{ color, borderColor: color }"> | ||
| {{ score.status }} | ||
| </span> | ||
| </a> | ||
| </template> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { watchDebounced } from '@vueuse/core' | ||
| import { computed, ref } from 'vue' | ||
| import { useInstalledModules } from './state-modules' | ||
|
|
||
| export type ModuleStatus = 'optimal' | 'stable' | 'degraded' | 'critical' | ||
|
|
||
| export interface ModuleScore { | ||
| name: string | ||
| npm: string | ||
| score: number | ||
| status: ModuleStatus | ||
| lastUpdated: string | null | ||
| } | ||
|
|
||
| // nuxt.care color schema | ||
| export const statusColors: Record<ModuleStatus, string> = { | ||
| optimal: '#22c55e', | ||
| stable: '#84cc16', | ||
| degraded: '#eab308', | ||
| critical: '#ef4444', | ||
| } | ||
|
|
||
| const API_BASE = 'https://nuxt.care/api/v1/modules/' | ||
| const scores = ref<Map<string, ModuleScore>>(new Map()) | ||
| const fetched = new Set<string>() | ||
| const loading = ref(false) | ||
|
|
||
| export function useModuleScores() { | ||
| const installedModules = useInstalledModules() | ||
|
|
||
| const npmNames = computed(() => | ||
| installedModules.value | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohter than the fixed "installedModules", we also reuse the UI for modules to install, I wonder if we also need to show the scores (or actually more interesting/useful?) when users are installing new modules.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally forgot about that. Yes I will work on it! |
||
| .map(m => m.info?.npm || m.name) | ||
| .filter((name): name is string => !!name && !name.startsWith('.')), | ||
| ) | ||
|
|
||
| watchDebounced(npmNames, async (names) => { | ||
| // Only fetch modules we haven't fetched yet | ||
| const missing = names.filter(name => !fetched.has(name)) | ||
| if (!missing.length) | ||
| return | ||
|
|
||
| missing.forEach(name => fetched.add(name)) | ||
| loading.value = true | ||
|
|
||
| try { | ||
| const params = new URLSearchParams([ | ||
| ['slim', 'true'], | ||
| ...missing.map(name => ['package', name]), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be a bit out-of-scope of the current PR, but I wonder if nuxt.care should also count for module versions? In many cases, the users might have an out-dated, no-longer-maintained version installed, where the "score" should be relatively low and ask users to upgrade.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ]) | ||
|
|
||
| const response = await $fetch<ModuleScore[]>(`${API_BASE}?${params}`) | ||
|
|
||
| // Merge with existing scores | ||
| for (const item of response) | ||
| scores.value.set(item.npm, item) | ||
| } | ||
| catch (e: any) { | ||
| // Only block retry for CORS or rate limit, allow retry for other errors | ||
| const isCors = e?.message?.includes('NetworkError') || e?.message?.includes('CORS') | ||
| const isRateLimit = (e?.response?.status ?? e?.status) === 429 | ||
|
|
||
| if (!isCors && !isRateLimit) | ||
| missing.forEach(name => fetched.delete(name)) | ||
|
|
||
| console.warn('[DevTools] Failed to fetch module scores:', e) | ||
| } | ||
| finally { | ||
| loading.value = false | ||
| } | ||
| }, { debounce: 300, immediate: true }) | ||
|
|
||
| return { scores, loading } | ||
| } | ||