'use client' import { useState, useEffect } from 'react' import { formatNumber, Modal } from '@ciphera-net/ui' import { FrameCornersIcon, Copy, Check, CursorClick } from '@phosphor-icons/react' import { toast } from '@ciphera-net/ui' import type { FrustrationElement } from '@/lib/api/stats' import { ListSkeleton } from '@/components/skeletons' const DISPLAY_LIMIT = 7 interface FrustrationTableProps { title: string description: string items: FrustrationElement[] total: number totalSignals: number showAvgClicks?: boolean loading: boolean fetchAll?: () => Promise<{ items: FrustrationElement[]; total: number }> } function SkeletonRows() { return (
{Array.from({ length: DISPLAY_LIMIT }).map((_, i) => (
))}
) } function SelectorCell({ selector }: { selector: string }) { const [copied, setCopied] = useState(false) const handleCopy = (e: React.MouseEvent) => { e.stopPropagation() navigator.clipboard.writeText(selector) setCopied(true) toast.success('Selector copied') setTimeout(() => setCopied(false), 2000) } return ( ) } function Row({ item, showAvgClicks, totalSignals, }: { item: FrustrationElement showAvgClicks?: boolean totalSignals: number }) { const pct = totalSignals > 0 ? `${Math.round((item.count / totalSignals) * 100)}%` : '' return (
{item.page_path}
{/* Percentage badge: slides in on hover */} {pct} {formatNumber(item.count)}
) } export default function FrustrationTable({ title, description, items, total, totalSignals, showAvgClicks, loading, fetchAll, }: FrustrationTableProps) { const [isModalOpen, setIsModalOpen] = useState(false) const [fullData, setFullData] = useState([]) const [isLoadingFull, setIsLoadingFull] = useState(false) const hasData = items.length > 0 const showViewAll = hasData && total > items.length const emptySlots = Math.max(0, DISPLAY_LIMIT - items.length) useEffect(() => { if (isModalOpen && fetchAll) { const load = async () => { setIsLoadingFull(true) try { const result = await fetchAll() setFullData(result.items) } catch { // silent } finally { setIsLoadingFull(false) } } load() } else { setFullData([]) } }, [isModalOpen, fetchAll]) return ( <>

{title}

{showViewAll && ( )}

{description}

{loading ? ( ) : hasData ? ( <> {items.map((item, i) => ( ))} {Array.from({ length: emptySlots }).map((_, i) => (
setIsModalOpen(false)} title={title} className="max-w-2xl" >
{isLoadingFull ? (
) : fullData.length > 0 ? (
{fullData.map((item, i) => ( ))}
) : (

No data available

)}
) }