"use client"; /** * MentionAutocomplete — a search input with a suggestions popover for tagging * users. Suggestions come from the backend prioritised (following → followers * → others), 20 per page, and the popover infinite-scrolls (≈5 rows visible). * Already-selected users are filtered out. Calls onSelect with the chosen * user; the caller manages the selected set. */ import { useEffect, useRef, useState } from "react"; import Image from "next/image"; import { Loader2, Search } from "lucide-react"; import { posts, type UserSummary } from "@/lib/social"; const PAGE = 20; export function MentionAutocomplete({ excludeIds = [], onSelect, placeholder = "Search people to mention…", autoFocus = false, }: { excludeIds?: string[]; onSelect: (user: UserSummary) => void; placeholder?: string; autoFocus?: boolean; }) { const [q, setQ] = useState(""); const [open, setOpen] = useState(false); const [items, setItems] = useState([]); const [loading, setLoading] = useState(false); const [hasMore, setHasMore] = useState(true); const reqRef = useRef(0); const busyRef = useRef(false); const rootRef = useRef(null); // (Re)load page 0 on open / query change (debounced — timer callback, so // setState isn't synchronous-in-effect). useEffect(() => { if (!open) return; const t = window.setTimeout(() => { const token = ++reqRef.current; busyRef.current = true; setLoading(true); posts .mentionSuggest(q, { limit: PAGE, offset: 0 }) .then((res) => { if (reqRef.current !== token) return; setItems(res); setHasMore(res.length === PAGE); }) .catch(() => { if (reqRef.current === token) { setItems([]); setHasMore(false); } }) .finally(() => { if (reqRef.current === token) { setLoading(false); busyRef.current = false; } }); }, 200); return () => window.clearTimeout(t); }, [q, open]); function loadMore() { if (busyRef.current || !hasMore) return; const token = reqRef.current; busyRef.current = true; setLoading(true); posts .mentionSuggest(q, { limit: PAGE, offset: items.length }) .then((res) => { if (reqRef.current !== token) return; setItems((prev) => [...prev, ...res]); setHasMore(res.length === PAGE); }) .catch(() => {}) .finally(() => { if (reqRef.current === token) { setLoading(false); busyRef.current = false; } }); } // Close on outside click / Escape. useEffect(() => { if (!open) return; function onDown(e: PointerEvent) { if (rootRef.current && !rootRef.current.contains(e.target as Node)) { setOpen(false); } } function onKey(e: KeyboardEvent) { if (e.key === "Escape") setOpen(false); } document.addEventListener("pointerdown", onDown); document.addEventListener("keydown", onKey); return () => { document.removeEventListener("pointerdown", onDown); document.removeEventListener("keydown", onKey); }; }, [open]); const excluded = new Set(excludeIds); const visible = items.filter((u) => !u.id || !excluded.has(u.id)); return (
setOpen(true)} onChange={(e) => { setQ(e.target.value); setOpen(true); }} placeholder={placeholder} autoComplete="off" // text-base (16px) on mobile prevents iOS Safari from auto-zooming // on focus (it zooms any input < 16px); compact 14px on desktop. className="w-full rounded-lg border border-border bg-background py-2 pl-9 pr-3 text-base sm:text-sm text-foreground outline-none placeholder:text-muted-foreground focus-visible:ring-2 focus-visible:ring-ring" />
{open ? (
{ const el = e.currentTarget; if (el.scrollHeight - el.scrollTop - el.clientHeight < 80) { loadMore(); } }} className="absolute z-50 mt-1 max-h-60 w-full overflow-y-auto overscroll-contain rounded-lg border border-border bg-card shadow-lg" > {visible.length === 0 && !loading ? (

{q.trim() ? "No people found." : "No suggestions."}

) : ( visible.map((u) => ( )) )} {loading ? (
) : null}
) : null}
); } function RowAvatar({ src, name }: { src: string | null; name: string }) { if (src) { return ( {name} ); } const initials = name .split(/\s+/) .map((p) => p[0]) .slice(0, 2) .join("") .toUpperCase() || "?"; return ( {initials} ); }