"use client"; import Link from "next/link"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { useEffect, useMemo, useRef, useState } from "react"; import { ArrowRightIcon, PlusIcon, SearchIcon, SparklesIcon, XIcon } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Combobox } from "@/components/ui/combobox"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { WIKI_CATEGORIES } from "@/lib/wiki/categories"; import { WikiRow } from "./WikiRow"; import { collectTagCounts, filterWikiPages, groupWikiPagesByCategory, isCategoryFilter, isSortKey, selectFeatured, sortWikiPages, type WikiCategoryFilter, type WikiIndexEntry, type WikiSortKey, } from "@/lib/wiki/indexView"; interface WikiIndexProps { readonly pages: readonly WikiIndexEntry[]; readonly isModerator: boolean; } const SORT_LABELS: Record = { recent: "Recently edited", oldest: "Oldest edit first", title: "Title A-Z", }; export function WikiIndex({ pages, isModerator }: WikiIndexProps) { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); const [search, setSearch] = useState(() => searchParams.get("q") ?? ""); const [tagFilter, setTagFilter] = useState(() => searchParams.get("tag") ?? ""); const [category, setCategory] = useState(() => { const value = searchParams.get("category"); return value !== null && isCategoryFilter(value) ? value : "all"; }); const [sort, setSort] = useState(() => { const value = searchParams.get("sort"); return value !== null && isSortKey(value) ? value : "recent"; }); // The URL carries the view so filtered lists can be shared or bookmarked. // Local state drives rendering; the replace is debounced to keep typing // snappy and skipped when nothing actually changed. Crucially the effect // keys ONLY off local filter state: useSearchParams identity churns while a // client navigation is in flight, and re-running (or replacing) then aborts // that navigation (e.g. clicking a category header). Latest params are read // through a ref purely for the no-op comparison. const searchParamsRef = useRef(searchParams); searchParamsRef.current = searchParams; const pathnameRef = useRef(pathname); pathnameRef.current = pathname; useEffect(() => { const timer = setTimeout(() => { const params = new URLSearchParams(); if (search.trim() !== "") params.set("q", search.trim()); if (tagFilter.trim() !== "") params.set("tag", tagFilter.trim()); if (category !== "all") params.set("category", category); if (sort !== "recent") params.set("sort", sort); const query = params.toString(); if (query === searchParamsRef.current.toString()) return; const currentPath = pathnameRef.current; router.replace(query ? `${currentPath}?${query}` : currentPath, { scroll: false }); }, 300); return () => clearTimeout(timer); }, [category, router, search, sort, tagFilter]); const visiblePages = useMemo( () => sortWikiPages(filterWikiPages(pages, { query: search, tag: tagFilter, category }), sort), [category, pages, search, sort, tagFilter], ); const groups = useMemo(() => groupWikiPagesByCategory(visiblePages), [visiblePages]); const tagCounts = useMemo(() => collectTagCounts(pages), [pages]); const featuredPages = useMemo(() => selectFeatured(pages), [pages]); const filtersActive = search.trim() !== "" || tagFilter.trim() !== "" || category !== "all"; const clearFilters = () => { setSearch(""); setTagFilter(""); setCategory("all"); }; const toggleTag = (tag: string) => { setTagFilter((current) => current.toLowerCase() === tag.toLowerCase() ? "" : tag, ); }; return (
{!filtersActive && featuredPages.length > 0 && (

Start here

{featuredPages.map((page) => ( {page.title} {page.excerpt && ( {page.excerpt} )} {page.category} ))}
)}
setSearch(event.target.value)} />
({ value: tag, label: tag, hint: `${count} page${count === 1 ? "" : "s"}`, })), ]} /> {filtersActive ? `${visiblePages.length} of ${pages.length}` : `${pages.length}`} pages {isModerator && Intel editor}
{visiblePages.length === 0 ? (

No pages match those filters.

Try a different search term, tag, or category, or start a new field guide entry.

{filtersActive && ( )}
) : (
{groups.map((group) => (
{category === "all" && (

{group.category} {group.pages.length}

)}
{group.pages.map((page) => ( ))}
))}
)}
); }