'use client'; import { useState, useCallback, useId } from 'react'; import { CheckCircle, Lightning, X } from '@phosphor-icons/react'; import { Badge } from '@/components/ui/badge'; import type { ProfileSkill } from '@/lib/types'; export interface SkillChipProps { skill: ProfileSkill; showCategory?: boolean; editable?: boolean; onEdit?: () => void; onDelete?: () => void; } type VisualState = 'self-declared' | 'endorsed' | 'activity-backed'; function getVisualState(skill: ProfileSkill): VisualState { if (skill.activityBacked) return 'activity-backed'; if (skill.endorsed) return 'endorsed'; return 'self-declared'; } const TOOLTIP_TEXT: Record, string> = { endorsed: "Confirmed by people who've worked with you", 'activity-backed': 'Backed by verified activity', }; export function SkillChip({ skill, showCategory, editable, onEdit, onDelete }: SkillChipProps) { const [tooltipVisible, setTooltipVisible] = useState(false); const tooltipId = useId(); const state = getVisualState(skill); const hasTooltip = state !== 'self-declared'; const showTooltip = useCallback(() => { if (hasTooltip) setTooltipVisible(true); }, [hasTooltip]); const hideTooltip = useCallback(() => { setTooltipVisible(false); }, []); const isClickable = !!editable; const handleClick = isClickable ? onEdit : undefined; const handleKeyDown = isClickable ? (e: React.KeyboardEvent) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); onEdit?.(); } } : undefined; return ( /* eslint-disable-next-line jsx-a11y/no-static-element-interactions -- mouse hover for tooltip; keyboard access is on the focusable Badge */ {showCategory && skill.category && ( {skill.category}: )} {skill.name} {state === 'endorsed' && ( {hasTooltip && tooltipVisible && ( ]} className="pointer-events-none absolute bottom-full left-1/2 z-50 mb-2 -translate-x-1/2 whitespace-nowrap rounded bg-popover px-2 py-1 text-xs text-popover-foreground shadow-md" > {TOOLTIP_TEXT[state as Exclude]} )} ); }