'use client' import { useCallback, useEffect, useRef, useState } from 'react' import { useRouter } from 'next/navigation' import { useRealtime } from '@/hooks/useRealtime' import type { Notification, Message } from '@/services/realtimeService' // ─── Types ────────────────────────────────────────────────────────────────── type ToastKind = 'notification' | 'message' interface ToastItem { /** Unique key for React + dismissal bookkeeping. */ id: string kind: ToastKind /** Headline (e.g. the actor's username). */ title: string /** Body text describing what happened. */ body: string /** Initial(s) shown in the avatar circle. */ avatarText: string /** Optional avatar image URL. */ avatarUrl: string | null /** Route to push to when the toast is clicked. */ href: string } // ─── Config ───────────────────────────────────────────────────────────────── const AUTO_DISMISS_MS = 5_000 const MAX_VISIBLE = 3 // ─── Helpers ──────────────────────────────────────────────────────────────── function initialsFrom(value: string): string { const trimmed = value.trim() if (trimmed.length === 0) return '•' return trimmed.slice(0, 2).toUpperCase() } function truncate(value: string, max: number): string { const trimmed = value.trim() return trimmed.length > max ? `${trimmed.slice(0, max)}…` : trimmed } function notificationToToast(n: Notification): ToastItem { const username = n.actorUsername ?? 'Someone' const href = n.postId ? `/post/${n.postId}` : `/notifications` return { id: `notif-${n.id}`, kind: 'notification', title: username, body: truncate(n.message ?? 'New notification', 80), avatarText: initialsFrom(username), avatarUrl: null, href, } } function messageToToast(m: Message): ToastItem { return { id: `msg-${m.id}`, kind: 'message', title: 'New message', body: truncate(m.content ?? '', 80), avatarText: initialsFrom(m.senderId), avatarUrl: null, href: `/messages?chat=${encodeURIComponent(m.chatId)}`, } } // ─── Single toast ───────────────────────────────────────────────────────────── interface ToastRowProps { item: ToastItem onDismiss: (id: string) => void onClick: (item: ToastItem) => void } function ToastRow({ item, onDismiss, onClick }: ToastRowProps) { return (