|
| 1 | +import { useMemo } from 'react' |
| 2 | +import { TriangleAlert } from 'lucide-react' |
| 3 | +import { useTranslation } from '../../i18n' |
| 4 | +import { Badge, type Tone } from '@/components/ui/Badge' |
| 5 | +import { Button } from '@/components/ui/Button' |
| 6 | +import { |
| 7 | + detectCapabilities, |
| 8 | + extractTriggers, |
| 9 | + type Capability, |
| 10 | + type CapabilityLevel, |
| 11 | +} from '../../lib/skillInsights' |
| 12 | +import type { NormalizedSkillDetail } from '../../types/market' |
| 13 | + |
| 14 | +type Translate = ReturnType<typeof useTranslation> |
| 15 | + |
| 16 | +export const CAPABILITY_LEVEL_TONES: Record<CapabilityLevel, Tone> = { |
| 17 | + high: 'danger', |
| 18 | + medium: 'warning', |
| 19 | + low: 'neutral', |
| 20 | +} |
| 21 | + |
| 22 | +/** One capability, phrased with its evidence. */ |
| 23 | +export function capabilityText(t: Translate, capability: Capability): { title: string; detail: string } { |
| 24 | + const evidence = capability.evidence.join(capability.kind === 'shell' ? ' / ' : '、') |
| 25 | + return { |
| 26 | + title: t(`market.cap.${capability.kind}`), |
| 27 | + detail: t(`market.cap.${capability.kind}.detail`, { evidence }), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** Capabilities and triggers, read once per detail off its own SKILL.md and file list. */ |
| 32 | +export function useSkillInsights(detail: NormalizedSkillDetail | null | undefined): { |
| 33 | + capabilities: Capability[] |
| 34 | + triggers: string[] |
| 35 | +} { |
| 36 | + return useMemo(() => { |
| 37 | + if (!detail) return { capabilities: [], triggers: [] } |
| 38 | + const description = detail.descriptionFrontmatter?.description |
| 39 | + return { |
| 40 | + capabilities: detectCapabilities({ |
| 41 | + markdown: detail.description, |
| 42 | + frontmatter: detail.descriptionFrontmatter, |
| 43 | + files: detail.files, |
| 44 | + }), |
| 45 | + triggers: extractTriggers(typeof description === 'string' ? description : undefined), |
| 46 | + } |
| 47 | + }, [detail]) |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * "Before installing: what this skill does." |
| 52 | + * |
| 53 | + * Every card cites the file, command, variable or host it came from, so the |
| 54 | + * reader can check the claim rather than trust it. Renders nothing when no |
| 55 | + * rule fired: an empty "nothing risky here" panel would be a verdict these |
| 56 | + * rules cannot make. |
| 57 | + */ |
| 58 | +export function CapabilityPanel({ |
| 59 | + capabilities, |
| 60 | + onViewReport, |
| 61 | +}: { |
| 62 | + capabilities: readonly Capability[] |
| 63 | + /** Shown as the header link; omitted where the panel already sits in the report. */ |
| 64 | + onViewReport?: () => void |
| 65 | +}) { |
| 66 | + const t = useTranslation() |
| 67 | + if (capabilities.length === 0) return null |
| 68 | + |
| 69 | + return ( |
| 70 | + <section |
| 71 | + aria-labelledby="market-capabilities-title" |
| 72 | + data-testid="market-capability-panel" |
| 73 | + className="flex-shrink-0 rounded-[var(--radius-xl)] border border-[var(--color-warning)] bg-[var(--color-warning-container)] px-5 py-[18px]" |
| 74 | + > |
| 75 | + <header className="flex flex-wrap items-center justify-between gap-x-4 gap-y-2"> |
| 76 | + <h2 |
| 77 | + id="market-capabilities-title" |
| 78 | + className="flex items-center gap-2 text-[15px] font-semibold text-[var(--color-on-warning-container)]" |
| 79 | + > |
| 80 | + <TriangleAlert className="h-[17px] w-[17px] flex-shrink-0" strokeWidth={1.8} aria-hidden="true" /> |
| 81 | + {t('market.cap.title')} |
| 82 | + </h2> |
| 83 | + {onViewReport && ( |
| 84 | + <Button variant="link" size="sm" data-testid="market-capability-view-report" onClick={onViewReport}> |
| 85 | + {t('market.cap.viewReport')} |
| 86 | + </Button> |
| 87 | + )} |
| 88 | + </header> |
| 89 | + <ul className="mt-3.5 grid gap-2.5 grid-cols-[repeat(auto-fill,minmax(200px,1fr))]"> |
| 90 | + {capabilities.map((capability) => { |
| 91 | + const text = capabilityText(t, capability) |
| 92 | + return ( |
| 93 | + <li |
| 94 | + key={capability.kind} |
| 95 | + data-testid={`market-capability-${capability.kind}`} |
| 96 | + className="flex min-w-0 flex-col gap-1.5 rounded-[var(--radius-lg)] border border-[var(--color-border)] bg-[var(--color-surface)] px-3.5 py-3" |
| 97 | + > |
| 98 | + <span className="flex items-center justify-between gap-2"> |
| 99 | + <strong className="text-[13.5px] font-semibold text-[var(--color-text-primary)]">{text.title}</strong> |
| 100 | + <Badge tone={CAPABILITY_LEVEL_TONES[capability.level]} size="xs" pill={false}> |
| 101 | + {t(`market.cap.level.${capability.level}`)} |
| 102 | + </Badge> |
| 103 | + </span> |
| 104 | + <span className="break-words font-mono text-xs leading-5 text-[var(--color-text-secondary)]"> |
| 105 | + {text.detail} |
| 106 | + </span> |
| 107 | + </li> |
| 108 | + ) |
| 109 | + })} |
| 110 | + </ul> |
| 111 | + <p className="mt-3 text-xs leading-5 text-[var(--color-on-warning-container)]">{t('market.cap.note')}</p> |
| 112 | + </section> |
| 113 | + ) |
| 114 | +} |
0 commit comments