feat: add disciples list
This commit is contained in:
+210
-40
@@ -1,15 +1,21 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useMemo } from "react";
|
||||
import { useState, useMemo, useRef, useEffect } from "react";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
} from "@/components/ui/card";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Search, ChevronLeft, ChevronRight } from "lucide-react";
|
||||
import {
|
||||
Search,
|
||||
ChevronLeft,
|
||||
ChevronRight,
|
||||
ArrowUpDown,
|
||||
ArrowDownAZ,
|
||||
ArrowUpAZ,
|
||||
Hash,
|
||||
Check,
|
||||
} from "lucide-react";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
@@ -19,6 +25,8 @@ export type YearGroup = { year: number; count: number };
|
||||
export type RecipientGroup = { name: string; count: number };
|
||||
export type LocationGroup = { name: string; count: number };
|
||||
|
||||
type SortMode = "count" | "asc" | "desc";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Diacritic-aware normalize (matches books-grid pattern)
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -46,13 +54,103 @@ function normalize(text: string): string {
|
||||
.replace(/[\u0300-\u036f]/g, "");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sort Popover
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const SORT_OPTIONS: { key: SortMode; label: string; icon: typeof Hash }[] = [
|
||||
{ key: "count", label: "Most letters first", icon: Hash },
|
||||
{ key: "asc", label: "A → Z", icon: ArrowDownAZ },
|
||||
{ key: "desc", label: "Z → A", icon: ArrowUpAZ },
|
||||
];
|
||||
|
||||
const SORT_OPTIONS_YEAR: { key: SortMode; label: string; icon: typeof Hash }[] =
|
||||
[
|
||||
{ key: "count", label: "Most letters first", icon: Hash },
|
||||
{ key: "asc", label: "Oldest first", icon: ArrowDownAZ },
|
||||
{ key: "desc", label: "Newest first", icon: ArrowUpAZ },
|
||||
];
|
||||
|
||||
function SortPopover({
|
||||
sort,
|
||||
onSortChange,
|
||||
isYear,
|
||||
}: {
|
||||
sort: SortMode;
|
||||
onSortChange: (s: SortMode) => void;
|
||||
isYear?: boolean;
|
||||
}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
|
||||
// Close on outside click
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
function handleClick(e: MouseEvent) {
|
||||
if (ref.current && !ref.current.contains(e.target as Node)) {
|
||||
setOpen(false);
|
||||
}
|
||||
}
|
||||
document.addEventListener("mousedown", handleClick);
|
||||
return () => document.removeEventListener("mousedown", handleClick);
|
||||
}, [open]);
|
||||
|
||||
const options = isYear ? SORT_OPTIONS_YEAR : SORT_OPTIONS;
|
||||
|
||||
return (
|
||||
<div ref={ref} className="relative">
|
||||
<button
|
||||
onClick={() => setOpen(!open)}
|
||||
className={cn(
|
||||
"flex items-center justify-center size-10 rounded-lg border transition-colors",
|
||||
open
|
||||
? "bg-primary/10 border-primary/30 text-primary"
|
||||
: "border-border text-muted-foreground hover:bg-muted hover:text-foreground",
|
||||
)}
|
||||
title="Sort options"
|
||||
aria-label="Sort options"
|
||||
>
|
||||
<ArrowUpDown className="size-4" />
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div className="absolute right-0 top-12 z-50 w-52 rounded-lg border border-border bg-card shadow-lg py-1 animate-in fade-in-0 zoom-in-95">
|
||||
{options.map((opt) => {
|
||||
const Icon = opt.icon;
|
||||
const isActive = sort === opt.key;
|
||||
return (
|
||||
<button
|
||||
key={opt.key}
|
||||
onClick={() => {
|
||||
onSortChange(opt.key);
|
||||
setOpen(false);
|
||||
}}
|
||||
className={cn(
|
||||
"flex items-center gap-3 w-full px-3 py-2.5 text-sm transition-colors text-left",
|
||||
isActive
|
||||
? "bg-primary/10 text-primary font-medium"
|
||||
: "text-foreground hover:bg-muted",
|
||||
)}
|
||||
>
|
||||
<Icon className="size-4 shrink-0" />
|
||||
<span className="flex-1">{opt.label}</span>
|
||||
{isActive && <Check className="size-3.5 shrink-0" />}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Pagination
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const PAGE_SIZE_RECIPIENTS = 50;
|
||||
const PAGE_SIZE_LOCATIONS = 60;
|
||||
const PAGE_SIZE_YEARS = 100; // years are small cards, show all usually
|
||||
const PAGE_SIZE_YEARS = 100;
|
||||
|
||||
function Pagination({
|
||||
page,
|
||||
@@ -102,6 +200,49 @@ function Pagination({
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sorting helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function sortYears(data: YearGroup[], mode: SortMode): YearGroup[] {
|
||||
const sorted = [...data];
|
||||
switch (mode) {
|
||||
case "count":
|
||||
return sorted.sort((a, b) => b.count - a.count);
|
||||
case "asc":
|
||||
return sorted.sort((a, b) => a.year - b.year);
|
||||
case "desc":
|
||||
return sorted.sort((a, b) => b.year - a.year);
|
||||
}
|
||||
}
|
||||
|
||||
function sortRecipients(
|
||||
data: RecipientGroup[],
|
||||
mode: SortMode,
|
||||
): RecipientGroup[] {
|
||||
const sorted = [...data];
|
||||
switch (mode) {
|
||||
case "count":
|
||||
return sorted.sort((a, b) => b.count - a.count);
|
||||
case "asc":
|
||||
return sorted.sort((a, b) => a.name.localeCompare(b.name));
|
||||
case "desc":
|
||||
return sorted.sort((a, b) => b.name.localeCompare(a.name));
|
||||
}
|
||||
}
|
||||
|
||||
function sortLocations(data: LocationGroup[], mode: SortMode): LocationGroup[] {
|
||||
const sorted = [...data];
|
||||
switch (mode) {
|
||||
case "count":
|
||||
return sorted.sort((a, b) => b.count - a.count);
|
||||
case "asc":
|
||||
return sorted.sort((a, b) => a.name.localeCompare(b.name));
|
||||
case "desc":
|
||||
return sorted.sort((a, b) => b.name.localeCompare(a.name));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Component
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -119,32 +260,57 @@ export function LettersBrowser({
|
||||
}) {
|
||||
const [query, setQuery] = useState("");
|
||||
const [page, setPage] = useState(1);
|
||||
const [sortYear, setSortYear] = useState<SortMode>("asc");
|
||||
const [sortRecipient, setSortRecipient] = useState<SortMode>("count");
|
||||
const [sortLocation, setSortLocation] = useState<SortMode>("count");
|
||||
|
||||
// Reset page when query or view changes
|
||||
const handleQueryChange = (q: string) => {
|
||||
setQuery(q);
|
||||
setPage(1);
|
||||
};
|
||||
|
||||
// --- Filtered data ---
|
||||
const handleSortChange = (mode: SortMode) => {
|
||||
if (currentView === "year") setSortYear(mode);
|
||||
else if (currentView === "recipient") setSortRecipient(mode);
|
||||
else setSortLocation(mode);
|
||||
setPage(1);
|
||||
};
|
||||
|
||||
const currentSort =
|
||||
currentView === "year"
|
||||
? sortYear
|
||||
: currentView === "recipient"
|
||||
? sortRecipient
|
||||
: sortLocation;
|
||||
|
||||
// --- Filtered + sorted data ---
|
||||
|
||||
const filteredYears = useMemo(() => {
|
||||
if (!query.trim()) return years;
|
||||
const q = normalize(query.trim());
|
||||
return years.filter((yg) => String(yg.year).includes(q));
|
||||
}, [years, query]);
|
||||
let data = years;
|
||||
if (query.trim()) {
|
||||
const q = normalize(query.trim());
|
||||
data = data.filter((yg) => String(yg.year).includes(q));
|
||||
}
|
||||
return sortYears(data, sortYear);
|
||||
}, [years, query, sortYear]);
|
||||
|
||||
const filteredRecipients = useMemo(() => {
|
||||
if (!query.trim()) return recipients;
|
||||
const q = normalize(query.trim());
|
||||
return recipients.filter((rg) => normalize(rg.name).includes(q));
|
||||
}, [recipients, query]);
|
||||
let data = recipients;
|
||||
if (query.trim()) {
|
||||
const q = normalize(query.trim());
|
||||
data = data.filter((rg) => normalize(rg.name).includes(q));
|
||||
}
|
||||
return sortRecipients(data, sortRecipient);
|
||||
}, [recipients, query, sortRecipient]);
|
||||
|
||||
const filteredLocations = useMemo(() => {
|
||||
if (!query.trim()) return locations;
|
||||
const q = normalize(query.trim());
|
||||
return locations.filter((lg) => normalize(lg.name).includes(q));
|
||||
}, [locations, query]);
|
||||
let data = locations;
|
||||
if (query.trim()) {
|
||||
const q = normalize(query.trim());
|
||||
data = data.filter((lg) => normalize(lg.name).includes(q));
|
||||
}
|
||||
return sortLocations(data, sortLocation);
|
||||
}, [locations, query, sortLocation]);
|
||||
|
||||
// --- Paginated data ---
|
||||
|
||||
@@ -184,15 +350,22 @@ export function LettersBrowser({
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{/* Search */}
|
||||
<div className="relative">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 size-4 text-muted-foreground" />
|
||||
<Input
|
||||
type="text"
|
||||
placeholder={placeholder}
|
||||
value={query}
|
||||
onChange={(e) => handleQueryChange(e.target.value)}
|
||||
className="pl-9"
|
||||
{/* Search + Sort */}
|
||||
<div className="flex gap-2">
|
||||
<div className="relative flex-1">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 size-4 text-muted-foreground" />
|
||||
<Input
|
||||
type="text"
|
||||
placeholder={placeholder}
|
||||
value={query}
|
||||
onChange={(e) => handleQueryChange(e.target.value)}
|
||||
className="pl-9"
|
||||
/>
|
||||
</div>
|
||||
<SortPopover
|
||||
sort={currentSort}
|
||||
onSortChange={handleSortChange}
|
||||
isYear={currentView === "year"}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -212,9 +385,7 @@ export function LettersBrowser({
|
||||
<Link key={yg.year} href={`/letters/year/${yg.year}`}>
|
||||
<Card className="transition-all hover:shadow-md hover:ring-foreground/20">
|
||||
<CardContent className="py-4 px-4 text-center">
|
||||
<p className="text-lg font-bold text-foreground">
|
||||
{yg.year}
|
||||
</p>
|
||||
<p className="text-lg font-bold text-foreground">{yg.year}</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{yg.count} {yg.count === 1 ? "letter" : "letters"}
|
||||
</p>
|
||||
@@ -227,22 +398,21 @@ export function LettersBrowser({
|
||||
|
||||
{/* Recipient view */}
|
||||
{currentView === "recipient" && (
|
||||
<div className="space-y-1.5">
|
||||
<div className="grid grid-cols-2 md:grid-cols-3 gap-3">
|
||||
{(filteredRecipients.slice(startIdx, endIdx) as RecipientGroup[]).map(
|
||||
(rg) => (
|
||||
<Link
|
||||
key={rg.name}
|
||||
href={`/letters/recipient/${encodeURIComponent(rg.name)}`}
|
||||
className="block"
|
||||
>
|
||||
<Card className="transition-all hover:shadow-md hover:ring-foreground/20">
|
||||
<CardContent className="py-3 px-5 flex items-center justify-between">
|
||||
<CardContent className="py-4 px-4 text-center">
|
||||
<p className="font-medium text-foreground truncate">
|
||||
{rg.name}
|
||||
</p>
|
||||
<Badge variant="secondary" className="text-xs shrink-0 ml-3">
|
||||
{rg.count}
|
||||
</Badge>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{rg.count} {rg.count === 1 ? "letter" : "letters"}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</Link>
|
||||
@@ -253,7 +423,7 @@ export function LettersBrowser({
|
||||
|
||||
{/* Location view */}
|
||||
{currentView === "location" && (
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-3">
|
||||
<div className="grid grid-cols-2 md:grid-cols-3 gap-3">
|
||||
{(filteredLocations.slice(startIdx, endIdx) as LocationGroup[]).map(
|
||||
(lg) => (
|
||||
<Link
|
||||
|
||||
Reference in New Issue
Block a user