import { useEffect, useState } from 'react' import { Check, ExternalLink, Link2, SearchX, ThumbsDown, ThumbsUp } from 'lucide-react' import type { Faq } from './types' import { load, save } from './storage' type Votes = Record function safeUrl(url: string): string | null { try { const u = new URL(url) return u.protocol === 'https:' || u.protocol === 'http:' ? u.href : null } catch { return null } } function FaqCard({ f, forceOpen, vote, onVote }: { f: Faq; forceOpen: boolean; vote?: 'up' | 'down'; onVote: (v: 'up' | 'down') => void }) { const [copied, setCopied] = useState(false) const href = f.link ? safeUrl(f.link) : null const copyLink = async () => { const url = `${location.origin}${location.pathname}#faq-${f.id}` try { await navigator.clipboard.writeText(url) setCopied(true) window.setTimeout(() => setCopied(false), 2000) } catch { location.hash = `faq-${f.id}` } } const voteBtn = (v: 'up' | 'down', label: string, Icon: typeof ThumbsUp) => ( ) return (
  • {f.category} {f.question} +

    {f.answer}

    {href && ( {f.linkLabel || 'Official guide'} )}
    Was this helpful? {voteBtn('up', 'Yes', ThumbsUp)} {voteBtn('down', 'No', ThumbsDown)}
    {vote === 'down' && (

    Sorry about that. Contact IT and we'll help directly.

    )}
  • ) } export default function FaqList({ items }: { items: Faq[] }) { const [votes, setVotes] = useState(() => load('faq-votes', {})) const [hashId, setHashId] = useState(null) useEffect(() => { const read = () => { const m = /^#faq-(\d+)$/.exec(location.hash) setHashId(m ? Number(m[1]) : null) if (m) window.setTimeout(() => document.getElementById(`faq-${m[1]}`)?.scrollIntoView({ behavior: 'smooth' }), 50) } read() window.addEventListener('hashchange', read) return () => window.removeEventListener('hashchange', read) }, []) const onVote = (id: number, v: 'up' | 'down') => { const next = { ...votes, [id]: v } setVotes(next) save('faq-votes', next) } if (items.length === 0) { return (

    No answers found. Try different words, or contact IT.

    ) } return (
      {items.map((f) => ( onVote(f.id, v)} /> ))}
    ) }