wacrm/src/components/dashboard/activity-feed.tsx
Arnas Donauskas 96debd193b
fix(dashboard): paginate recent activity feed with a size picker
With real data the feed showed 20 rows by default and filled most of
the viewport. Switched to a 5-row default with an inline size picker
(5 / 10 / 20 / 50) so users choose how much history is visible.

- Dashboard page bumps the fetch from 20 → 50 so every size option is
  already in memory. Switching sizes is a pure client-side slice, no
  round trip.
- ActivityFeed owns the pageSize state, renders "Showing N of M"
  plus a button group in the footer. The smallest option is always
  enabled; larger options unlock only once we have enough rows to
  meaningfully reveal more (e.g. "10" enables once loaded > 5).
- Existing "View all" header link left alone.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 22:35:42 +03:00

167 lines
6.1 KiB
TypeScript

"use client"
import Link from 'next/link'
import { useState } from 'react'
import {
MessageSquare,
UserPlus,
Briefcase,
Radio,
Zap,
Inbox,
} from 'lucide-react'
import type { ComponentType } from 'react'
import type { ActivityItem, ActivityKind } from '@/lib/dashboard/types'
import { cn } from '@/lib/utils'
import { EmptyState } from './empty-state'
import { Skeleton } from './skeleton'
interface ActivityFeedProps {
items: ActivityItem[] | null
loading: boolean
}
const PAGE_SIZES = [5, 10, 20, 50] as const
type PageSize = (typeof PAGE_SIZES)[number]
interface KindTheme {
icon: ComponentType<{ className?: string }>
/** Tailwind classes for the round icon badge + label color. */
badge: string
}
const KIND_THEME: Record<ActivityKind, KindTheme> = {
message: { icon: MessageSquare, badge: 'bg-blue-500/10 text-blue-400' },
contact: { icon: UserPlus, badge: 'bg-emerald-500/10 text-emerald-400' },
deal: { icon: Briefcase, badge: 'bg-violet-500/10 text-violet-400' },
broadcast: { icon: Radio, badge: 'bg-amber-500/10 text-amber-400' },
automation: { icon: Zap, badge: 'bg-rose-500/10 text-rose-400' },
}
export function ActivityFeed({ items, loading }: ActivityFeedProps) {
// Start at 5 — a quick scan of the most recent events without
// dominating vertical real estate. User expands explicitly via the
// footer control when they want deeper history.
const [pageSize, setPageSize] = useState<PageSize>(5)
const totalLoaded = items?.length ?? 0
const visible = items?.slice(0, pageSize) ?? []
// A size option is "useful" if picking it would reveal rows the
// smaller option doesn't already show. With PAGE_SIZES=[5,10,20,50]:
// "10" is useful only once we've loaded ≥6 items, "20" once ≥11, etc.
// The smallest option is always enabled.
const isSizeUseful = (size: PageSize, i: number) =>
i === 0 || totalLoaded > PAGE_SIZES[i - 1]
return (
<section className="rounded-xl border border-slate-800 bg-slate-900">
<header className="flex items-center justify-between border-b border-slate-800 px-5 py-4">
<h2 className="text-sm font-semibold text-white">Recent Activity</h2>
<Link
href="/inbox"
className="text-xs font-medium text-emerald-400 hover:text-emerald-300"
>
View all →
</Link>
</header>
{loading || !items ? (
<div className="space-y-2 p-5">
{Array.from({ length: 5 }).map((_, i) => (
<Skeleton key={i} className="h-10 w-full" />
))}
</div>
) : items.length === 0 ? (
<div className="p-5">
<EmptyState
icon={Inbox}
title="No activity yet"
hint="Activity from messages, deals, broadcasts, and automations will appear here."
/>
</div>
) : (
<>
<ul className="divide-y divide-slate-800">
{visible.map((it, i) => {
const theme = KIND_THEME[it.kind]
const Icon = theme.icon
// Alternating row background for scanability — dark-theme
// translation of the spec's white / #f9fafb stripes.
const stripe = i % 2 === 0 ? 'bg-transparent' : 'bg-slate-900/40'
const row = (
<div className="flex items-center gap-3 px-5 py-2.5">
<span
className={cn(
'flex h-7 w-7 flex-shrink-0 items-center justify-center rounded-full',
theme.badge,
)}
>
<Icon className="h-3.5 w-3.5" />
</span>
<span className="min-w-0 flex-1 truncate text-sm text-slate-200">
{it.text}
</span>
<span className="flex-shrink-0 text-xs text-slate-500 tabular-nums">
{relativeTime(it.at)}
</span>
</div>
)
return (
<li key={it.id} className={cn(stripe, 'transition-colors hover:bg-slate-800/40')}>
{it.href ? (
<Link href={it.href} className="block">
{row}
</Link>
) : (
row
)}
</li>
)
})}
</ul>
<footer className="flex items-center justify-between border-t border-slate-800 px-5 py-3 text-xs">
<span className="text-slate-500 tabular-nums">
Showing {visible.length} of {totalLoaded}
{totalLoaded === 50 ? '+' : ''}
</span>
<div className="flex items-center gap-1">
<span className="mr-1 text-slate-500">Show</span>
{PAGE_SIZES.map((size, i) => {
const disabled = !isSizeUseful(size, i)
return (
<button
key={size}
type="button"
onClick={() => setPageSize(size)}
disabled={disabled}
className={cn(
'rounded-md px-2 py-1 font-medium tabular-nums transition-colors',
pageSize === size
? 'bg-slate-700 text-white'
: 'text-slate-400 hover:bg-slate-800 hover:text-white',
disabled && 'cursor-not-allowed opacity-40 hover:bg-transparent hover:text-slate-400',
)}
>
{size}
</button>
)
})}
</div>
</footer>
</>
)}
</section>
)
}
function relativeTime(iso: string): string {
const then = new Date(iso).getTime()
if (Number.isNaN(then)) return ''
const diffSec = Math.round((Date.now() - then) / 1000)
if (diffSec < 60) return `${Math.max(1, diffSec)}s ago`
if (diffSec < 3600) return `${Math.floor(diffSec / 60)}m ago`
if (diffSec < 86400) return `${Math.floor(diffSec / 3600)}h ago`
if (diffSec < 2_592_000) return `${Math.floor(diffSec / 86400)}d ago`
return new Date(iso).toLocaleDateString()
}