Rewrite the wiki index: filters (query/tag/category/sort) sync to the URL for shareable/bookmarkable views; a moderator-curated 'Start here' featured strip; category grouping with per-category portal pages (/wiki/category/*) and CATEGORY_META blurbs; excerpt previews; case-insensitive, alphabetized tag counts; and a compact WikiRow. Updates the wiki controls tour copy.
278 lines
10 KiB
TypeScript
278 lines
10 KiB
TypeScript
"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<WikiSortKey, string> = {
|
|
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<WikiCategoryFilter>(() => {
|
|
const value = searchParams.get("category");
|
|
return value !== null && isCategoryFilter(value) ? value : "all";
|
|
});
|
|
const [sort, setSort] = useState<WikiSortKey>(() => {
|
|
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 (
|
|
<div className="flex flex-col gap-4">
|
|
{!filtersActive && featuredPages.length > 0 && (
|
|
<section className="flex flex-col gap-1.5" aria-label="Start here">
|
|
<h2 className="flex items-baseline gap-2 px-1 text-xs font-semibold tracking-wider text-muted-foreground uppercase">
|
|
<SparklesIcon className="size-3 self-center" aria-hidden />
|
|
Start here
|
|
</h2>
|
|
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
|
{featuredPages.map((page) => (
|
|
<Link
|
|
key={page.id}
|
|
href={`/wiki/${page.slug}`}
|
|
className="group flex flex-col gap-1 rounded-lg border p-3 transition-colors hover:bg-accent/40"
|
|
>
|
|
<span className="min-w-0 truncate text-sm font-medium group-hover:underline">
|
|
{page.title}
|
|
</span>
|
|
{page.excerpt && (
|
|
<span className="line-clamp-2 text-xs text-muted-foreground">
|
|
{page.excerpt}
|
|
</span>
|
|
)}
|
|
<span className="text-[11px] tracking-wide text-muted-foreground uppercase">
|
|
{page.category}
|
|
</span>
|
|
</Link>
|
|
))}
|
|
</div>
|
|
</section>
|
|
)}
|
|
|
|
<div className="flex flex-wrap items-center gap-2" data-tour="wiki-controls">
|
|
<div className="relative min-w-56 flex-1 lg:max-w-md">
|
|
<SearchIcon className="pointer-events-none absolute left-2.5 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
|
|
<Input
|
|
aria-label="Search wiki pages"
|
|
className="pl-8"
|
|
placeholder="Search title, tags, or content..."
|
|
value={search}
|
|
onChange={(event) => setSearch(event.target.value)}
|
|
/>
|
|
</div>
|
|
|
|
<Select
|
|
value={category}
|
|
onValueChange={(value) => {
|
|
if (isCategoryFilter(value)) setCategory(value);
|
|
}}
|
|
>
|
|
<SelectTrigger aria-label="Filter wiki pages by category" className="w-40 shrink-0">
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="all">All categories</SelectItem>
|
|
{WIKI_CATEGORIES.map((option) => (
|
|
<SelectItem key={option} value={option}>
|
|
{option}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
|
|
<Combobox
|
|
aria-label="Filter wiki pages by tag"
|
|
className="w-44 shrink-0"
|
|
value={tagFilter}
|
|
onValueChange={setTagFilter}
|
|
placeholder="All tags"
|
|
searchPlaceholder="Search tags..."
|
|
emptyMessage="No tags yet."
|
|
options={[
|
|
{ value: "", label: "All tags" },
|
|
...tagCounts.map(({ tag, count }) => ({
|
|
value: tag,
|
|
label: tag,
|
|
hint: `${count} page${count === 1 ? "" : "s"}`,
|
|
})),
|
|
]}
|
|
/>
|
|
|
|
<Select
|
|
value={sort}
|
|
onValueChange={(value) => {
|
|
if (isSortKey(value)) setSort(value);
|
|
}}
|
|
>
|
|
<SelectTrigger aria-label="Sort wiki pages" className="w-40 shrink-0">
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{(Object.keys(SORT_LABELS) as WikiSortKey[]).map((key) => (
|
|
<SelectItem key={key} value={key}>
|
|
{SORT_LABELS[key]}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
|
|
<span className="text-xs tabular-nums text-muted-foreground">
|
|
{filtersActive ? `${visiblePages.length} of ${pages.length}` : `${pages.length}`} pages
|
|
</span>
|
|
|
|
{isModerator && <Badge variant="outline">Intel editor</Badge>}
|
|
|
|
<Button asChild className="ml-auto" size="sm">
|
|
<Link href="/wiki/new">
|
|
<PlusIcon data-icon="inline-start" />
|
|
New page
|
|
</Link>
|
|
</Button>
|
|
</div>
|
|
|
|
{visiblePages.length === 0 ? (
|
|
<Card>
|
|
<CardContent className="flex flex-col items-start gap-3 p-6">
|
|
<div className="flex flex-col gap-1">
|
|
<p className="font-semibold">No pages match those filters.</p>
|
|
<p className="text-sm text-muted-foreground">
|
|
Try a different search term, tag, or category, or start a new field guide entry.
|
|
</p>
|
|
</div>
|
|
<div className="flex gap-2">
|
|
{filtersActive && (
|
|
<Button onClick={clearFilters} size="sm" variant="outline">
|
|
<XIcon data-icon="inline-start" />
|
|
Clear filters
|
|
</Button>
|
|
)}
|
|
<Button asChild size="sm">
|
|
<Link href="/wiki/new">New page</Link>
|
|
</Button>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
) : (
|
|
<div className="flex flex-col gap-5">
|
|
{groups.map((group) => (
|
|
<section key={group.category} className="flex flex-col gap-1">
|
|
{category === "all" && (
|
|
<h2 className="group/cat flex items-baseline gap-2 px-1 text-xs font-semibold tracking-wider text-muted-foreground uppercase">
|
|
<Link
|
|
href={`/wiki/category/${group.category}`}
|
|
className="inline-flex items-center gap-1 transition-colors hover:text-foreground"
|
|
>
|
|
{group.category}
|
|
<ArrowRightIcon
|
|
className="size-3 opacity-0 transition-opacity group-hover/cat:opacity-70"
|
|
aria-hidden
|
|
/>
|
|
</Link>
|
|
<span className="font-normal tabular-nums normal-case">
|
|
{group.pages.length}
|
|
</span>
|
|
</h2>
|
|
)}
|
|
<div className="divide-y overflow-hidden rounded-lg border">
|
|
{group.pages.map((page) => (
|
|
<WikiRow key={page.id} page={page} activeTag={tagFilter} onTagClick={toggleTag} />
|
|
))}
|
|
</div>
|
|
</section>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|