Activity Feed
FreeA live workspace feed grouped by day with sticky headers, a sliding type filter, events that blur in at the top, a “new events” pill when you've scrolled away, and pause/resume.
"use client";
import { useEffect, useMemo, useRef, useState } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { ArrowUp, CreditCard, GitMerge, MessageSquare, Pause, Play, Rocket, UserPlus } from "lucide-react";
import { cn } from "@/lib/utils";
type Kind = "deploy" | "billing" | "team" | "comment" | "merge";
type Event = { id: number; kind: Kind; who: string; text: string; target: string; at: number; body?: string };
const meta: Record<Kind, { icon: typeof Rocket; label: string; tone: string }> = {
deploy: { icon: Rocket, label: "Deploys", tone: "text-violet-600 dark:text-violet-400 bg-violet-500/10" },
billing: { icon: CreditCard, label: "Billing", tone: "text-emerald-600 dark:text-emerald-400 bg-emerald-500/10" },
team: { icon: UserPlus, label: "Team", tone: "text-sky-600 dark:text-sky-400 bg-sky-500/10" },
comment: { icon: MessageSquare, label: "Comments", tone: "text-amber-600 dark:text-amber-400 bg-amber-500/10" },
merge: { icon: GitMerge, label: "Merges", tone: "text-fuchsia-600 dark:text-fuchsia-400 bg-fuchsia-500/10" },
};
const MIN = 60_000;
const DAY = 24 * 60 * MIN;
const NOW = Date.UTC(2026, 10, 24, 17, 0);
const seed: Event[] = [
{ id: 1, kind: "deploy", who: "Maya", text: "deployed", target: "web@a41f9c to production", at: NOW - 4 * MIN },
{ id: 2, kind: "comment", who: "Jon", text: "commented on", target: "Pricing page v3", at: NOW - 22 * MIN, body: "Can we test the annual toggle defaulting to yearly? Last quarter 62% picked yearly when it was preselected." },
{ id: 3, kind: "billing", who: "Stripe", text: "received", target: "$1,240 from Globex", at: NOW - 70 * MIN },
{ id: 4, kind: "merge", who: "Lee", text: "merged", target: "#482 Usage-based billing", at: NOW - 3 * 60 * MIN },
{ id: 5, kind: "team", who: "Priya", text: "invited", target: "sam@northwind.dev as Editor", at: NOW - DAY - 2 * 60 * MIN },
{ id: 6, kind: "deploy", who: "Maya", text: "rolled back", target: "api@7c02e1", at: NOW - DAY - 5 * 60 * MIN, body: "p95 latency jumped from 180ms to 1.4s after the queue change. Rolled back while we profile." },
{ id: 7, kind: "billing", who: "Stripe", text: "failed to charge", target: "Initech ($320)", at: NOW - 2 * DAY },
{ id: 8, kind: "comment", who: "Ana", text: "commented on", target: "Onboarding emails", at: NOW - 3 * DAY, body: "Day-3 email has the old logo." },
];
const incoming: Omit<Event, "id" | "at">[] = [
{ kind: "billing", who: "Stripe", text: "received", target: "$89 from Hooli" },
{ kind: "team", who: "Sam", text: "joined", target: "the workspace" },
{ kind: "merge", who: "Jon", text: "merged", target: "#486 Annual toggle default" },
{ kind: "deploy", who: "Lee", text: "deployed", target: "web@d93b20 to production" },
{ kind: "comment", who: "Maya", text: "commented on", target: "Q1 roadmap", body: "Moving SSO up; three Team trials asked this week." },
];
const ring = "outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background";
function ago(at: number, now: number) {
const m = Math.max(0, Math.round((now - at) / MIN));
if (m < 1) return "just now";
if (m < 60) return `${m}m ago`;
if (m < 24 * 60) return `${Math.round(m / 60)}h ago`;
return new Date(at).toLocaleDateString("en-US", { month: "short", day: "numeric", timeZone: "UTC" });
}
function day(at: number) {
const since = NOW - at;
const today = new Date(NOW).getUTCHours() * 60 * MIN;
if (since < today) return "Today";
if (since < today + DAY) return "Yesterday";
return "Earlier";
}
export default function ActivityFeedBlock() {
const reduce = useReducedMotion();
const [events, setEvents] = useState(seed);
const [pending, setPending] = useState<Event[]>([]);
const [filter, setFilter] = useState<Kind | "all">("all");
const [live, setLive] = useState(true);
const [atTop, setAtTop] = useState(true);
const list = useRef<HTMLDivElement>(null);
const n = useRef(0);
const nextId = useRef(100);
useEffect(() => {
if (!live) return;
const t = setInterval(() => {
const src = incoming[n.current++ % incoming.length];
const ev = { ...src, id: nextId.current++, at: NOW };
if (atTop) setEvents((e) => [ev, ...e]);
else setPending((p) => [ev, ...p]);
}, 3800);
return () => clearInterval(t);
}, [live, atTop]);
const shown = useMemo(() => (filter === "all" ? events : events.filter((e) => e.kind === filter)), [events, filter]);
const groups = useMemo(() => {
const g: { label: string; items: Event[] }[] = [];
for (const e of shown) {
const label = day(e.at);
const last = g[g.length - 1];
if (last?.label === label) last.items.push(e);
else g.push({ label, items: [e] });
}
return g;
}, [shown]);
function showPending() {
setEvents((e) => [...pending, ...e]);
setPending([]);
list.current?.scrollTo({ top: 0, behavior: reduce ? "auto" : "smooth" });
}
return (
<section className="max-w-[100vw] flex min-h-full w-full items-start justify-center bg-muted/40 px-4 py-10">
<div className="w-full min-w-0 max-w-lg overflow-hidden rounded-xl border border-border bg-card shadow-[inset_0_1px_0_rgb(255_255_255/0.04),0_1px_2px_rgb(0_0_0/0.05),0_24px_48px_-28px_rgb(0_0_0/0.4)]">
<div className="flex items-center justify-between gap-3 border-b border-border px-4 py-3">
<div className="flex items-center gap-2">
<h2 className="text-sm font-semibold">Activity</h2>
<span className={cn("inline-flex items-center gap-1.5 rounded px-1.5 py-0.5 text-[11px] font-medium", live ? "bg-emerald-500/10 text-emerald-700 dark:text-emerald-400" : "bg-muted text-muted-foreground")}>
<span className="relative flex size-1.5">
{live && !reduce ? <span className="absolute inset-0 animate-ping rounded-full bg-emerald-500 opacity-60" /> : null}
<span className={cn("relative size-1.5 rounded-full", live ? "bg-emerald-500" : "bg-muted-foreground")} />
</span>
{live ? "Live" : "Paused"}
</span>
</div>
<button type="button" onClick={() => setLive((l) => !l)} aria-pressed={!live} className={cn("inline-flex h-7 items-center gap-1.5 rounded-md border border-border px-2 text-xs font-medium shadow-[inset_0_1px_0_rgb(255_255_255/0.05)] hover:bg-muted", ring)}>
{live ? <Pause className="size-3" aria-hidden /> : <Play className="size-3" aria-hidden />}
{live ? "Pause" : "Resume"}
</button>
</div>
<div role="tablist" aria-label="Filter activity" className="flex gap-1 overflow-x-auto border-b border-border px-3 py-2 [scrollbar-width:none]">
{(["all", ...Object.keys(meta)] as (Kind | "all")[]).map((k) => (
<button
key={k}
type="button"
role="tab"
aria-selected={filter === k}
onClick={() => setFilter(k)}
className={cn("relative h-7 shrink-0 rounded-md px-2.5 text-xs font-medium transition-colors", filter === k ? "text-foreground" : "text-muted-foreground hover:text-foreground", ring)}
>
{filter === k ? <motion.span layoutId="af-tab" transition={{ type: "spring", stiffness: 500, damping: 38 }} className="absolute inset-0 rounded-md bg-muted shadow-[inset_0_0_0_1px_var(--border)]" /> : null}
<span className="relative">{k === "all" ? "All" : meta[k].label}</span>
</button>
))}
</div>
<div className="relative">
<AnimatePresence>
{pending.length ? (
<motion.button
type="button"
onClick={showPending}
initial={{ opacity: 0, y: -10, x: "-50%" }}
animate={{ opacity: 1, y: 0, x: "-50%" }}
exit={{ opacity: 0, y: -10, x: "-50%" }}
className={cn("absolute left-1/2 top-3 z-10 inline-flex h-7 items-center gap-1 rounded-full bg-foreground px-3 text-xs font-medium text-background shadow-[0_4px_12px_-2px_rgb(0_0_0/0.35)]", ring)}
>
<ArrowUp className="size-3" aria-hidden />
{pending.length} new
</motion.button>
) : null}
</AnimatePresence>
<div
ref={list}
onScroll={(e) => {
const top = e.currentTarget.scrollTop < 8;
if (top !== atTop) setAtTop(top);
if (top && pending.length) showPending();
}}
className="relative h-[440px] overflow-y-auto"
aria-live="polite"
aria-relevant="additions"
>
{groups.length ? (
groups.map((g) => (
<div key={g.label}>
<p className="sticky top-0 z-[1] border-b border-border/60 bg-card/90 px-4 py-1.5 text-[11px] font-medium uppercase tracking-wider text-muted-foreground backdrop-blur">{g.label}</p>
<ol className="relative px-4 py-2">
<span aria-hidden className="absolute bottom-4 left-[31px] top-4 w-px bg-border" />
<AnimatePresence initial={false}>
{g.items.map((e) => {
const M = meta[e.kind];
return (
<motion.li
key={e.id}
layout={!reduce}
initial={reduce ? { opacity: 0 } : { opacity: 0, y: -12, filter: "blur(4px)" }}
animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
transition={{ type: "spring", stiffness: 360, damping: 30 }}
className="relative flex gap-3 py-2"
>
<span className={cn("relative z-[1] grid size-7 shrink-0 place-items-center rounded-md ring-4 ring-card", M.tone)}>
<M.icon className="size-3.5" aria-hidden />
</span>
<div className="min-w-0 flex-1 pt-1">
<p className="text-[13px] leading-snug">
<span className="font-medium">{e.who}</span> <span className="text-muted-foreground">{e.text}</span> <span className="font-medium">{e.target}</span>
</p>
{e.body ? <p className="mt-1.5 rounded-md border border-border bg-muted/40 px-2.5 py-2 text-[13px] text-foreground/85">{e.body}</p> : null}
</div>
<time className="shrink-0 pt-1 text-xs tabular-nums text-muted-foreground">{ago(e.at, NOW)}</time>
</motion.li>
);
})}
</AnimatePresence>
</ol>
</div>
))
) : (
<div className="grid h-full place-items-center px-6 text-center">
<div>
<p className="text-sm font-medium">No {filter === "all" ? "" : meta[filter].label.toLowerCase()} activity yet</p>
<p className="mt-1 text-xs text-muted-foreground">New events show up here as they happen.</p>
</div>
</div>
)}
</div>
</div>
</div>
</section>
);
}