|
| 1 | +<script setup lang="ts"> |
| 2 | +defineProps<{ |
| 3 | + /** Dependency path from root to vulnerable package (readonly from VulnerabilityTreeResult) */ |
| 4 | + path: readonly string[] |
| 5 | +}>() |
| 6 | +
|
| 7 | +const { t } = useI18n() |
| 8 | +
|
| 9 | +const isOpen = shallowRef(false) |
| 10 | +const popupEl = ref<HTMLElement | null>(null) |
| 11 | +const popupPosition = shallowRef<{ top: number; left: number } | null>(null) |
| 12 | +
|
| 13 | +// Function ref - captures the element when popup mounts |
| 14 | +function setPopupRef(el: unknown) { |
| 15 | + popupEl.value = (el as HTMLElement) || null |
| 16 | +} |
| 17 | +
|
| 18 | +function closePopup() { |
| 19 | + isOpen.value = false |
| 20 | +} |
| 21 | +
|
| 22 | +// Close popup on click outside |
| 23 | +onClickOutside(popupEl, () => { |
| 24 | + if (isOpen.value) closePopup() |
| 25 | +}) |
| 26 | +
|
| 27 | +// Close popup on ESC or scroll |
| 28 | +function handleKeydown(e: KeyboardEvent) { |
| 29 | + if (e.key === 'Escape') closePopup() |
| 30 | +} |
| 31 | +
|
| 32 | +onMounted(() => { |
| 33 | + document.addEventListener('keydown', handleKeydown) |
| 34 | + window.addEventListener('scroll', closePopup, true) |
| 35 | +}) |
| 36 | +
|
| 37 | +onUnmounted(() => { |
| 38 | + document.removeEventListener('keydown', handleKeydown) |
| 39 | + window.removeEventListener('scroll', closePopup, true) |
| 40 | +}) |
| 41 | +
|
| 42 | +function togglePopup(event: MouseEvent) { |
| 43 | + if (isOpen.value) { |
| 44 | + closePopup() |
| 45 | + } else { |
| 46 | + const button = event.currentTarget as HTMLElement |
| 47 | + const rect = button.getBoundingClientRect() |
| 48 | + popupPosition.value = { |
| 49 | + top: rect.bottom + 4, |
| 50 | + left: rect.left, |
| 51 | + } |
| 52 | + isOpen.value = true |
| 53 | + } |
| 54 | +} |
| 55 | +
|
| 56 | +function getPopupStyle(): Record<string, string> { |
| 57 | + if (!popupPosition.value) return {} |
| 58 | + return { |
| 59 | + top: `${popupPosition.value.top}px`, |
| 60 | + left: `${popupPosition.value.left}px`, |
| 61 | + } |
| 62 | +} |
| 63 | +
|
| 64 | +// Parse package string "name@version" into { name, version } |
| 65 | +function parsePackageString(pkg: string): { name: string; version: string } { |
| 66 | + const atIndex = pkg.lastIndexOf('@') |
| 67 | + if (atIndex > 0) { |
| 68 | + return { name: pkg.slice(0, atIndex), version: pkg.slice(atIndex + 1) } |
| 69 | + } |
| 70 | + return { name: pkg, version: '' } |
| 71 | +} |
| 72 | +</script> |
| 73 | + |
| 74 | +<template> |
| 75 | + <div class="relative"> |
| 76 | + <!-- Path badge button --> |
| 77 | + <button |
| 78 | + type="button" |
| 79 | + class="path-badge font-mono text-[10px] px-1.5 py-0.5 rounded bg-amber-500/10 border border-amber-500/30 text-amber-700 dark:text-amber-400 cursor-pointer transition-all duration-200 ease-out whitespace-nowrap flex items-center gap-1 hover:bg-amber-500/20 hover:border-amber-500/50" |
| 80 | + :aria-expanded="isOpen" |
| 81 | + @click.stop="togglePopup" |
| 82 | + > |
| 83 | + <span class="i-carbon-tree-view w-3 h-3" aria-hidden="true" /> |
| 84 | + <span>{{ t('package.vulnerabilities.path') }}</span> |
| 85 | + </button> |
| 86 | + |
| 87 | + <!-- Tree popup --> |
| 88 | + <div |
| 89 | + v-if="isOpen" |
| 90 | + :ref="setPopupRef" |
| 91 | + class="fixed z-[100] bg-bg-elevated border border-border rounded-lg shadow-xl p-3 min-w-64 max-w-sm" |
| 92 | + :style="getPopupStyle()" |
| 93 | + > |
| 94 | + <ul class="list-none m-0 p-0 space-y-0.5"> |
| 95 | + <li |
| 96 | + v-for="(pathItem, idx) in path" |
| 97 | + :key="idx" |
| 98 | + class="font-mono text-xs" |
| 99 | + :style="{ paddingLeft: `${idx * 12}px` }" |
| 100 | + > |
| 101 | + <span v-if="idx > 0" class="text-fg-subtle mr-1">└─</span> |
| 102 | + <NuxtLink |
| 103 | + :to="{ |
| 104 | + name: 'package', |
| 105 | + params: { |
| 106 | + package: [ |
| 107 | + ...parsePackageString(pathItem).name.split('/'), |
| 108 | + 'v', |
| 109 | + parsePackageString(pathItem).version, |
| 110 | + ], |
| 111 | + }, |
| 112 | + }" |
| 113 | + class="hover:underline" |
| 114 | + :class="idx === path.length - 1 ? 'text-fg font-medium' : 'text-fg-muted'" |
| 115 | + @click="closePopup" |
| 116 | + > |
| 117 | + {{ pathItem }} |
| 118 | + </NuxtLink> |
| 119 | + <span v-if="idx === path.length - 1" class="ml-1 text-amber-500">⚠</span> |
| 120 | + </li> |
| 121 | + </ul> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | +</template> |
0 commit comments