"use client";
import React from "react";
import Link from "next/link";
import { Eye, FolderOpen, Loader2, SlidersHorizontal } from "lucide-react";
import { SEARCH_SCOPE } from "@/lib/frontend/search/urlState";
import { dayPath } from "@/lib/frontend/routes";
import { buildPdfUrl } from "@/lib/frontend/explorer/pdfUrl";
import ExplorerLoading from "@/components/explorer/states/ExplorerLoading";
import ExplorerEmpty from "@/components/explorer/states/ExplorerEmpty";
import ExplorerError from "@/components/explorer/states/ExplorerError";
import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
Table,
TableBody,
TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
const SORT = Object.freeze({
RELEVANCE: "relevance",
DATE_DESC: "date_desc",
FILENAME_ASC: "filename_asc",
});
function toDateKey(it) {
const y = String(it?.year || "");
const m = String(it?.month || "").padStart(2, "0");
const d = String(it?.day || "").padStart(2, "0");
return `${y}-${m}-${d}`;
}
function formatDateDe(it) {
const y = String(it?.year || "");
const m = String(it?.month || "").padStart(2, "0");
const d = String(it?.day || "").padStart(2, "0");
return `${d}.${m}.${y}`;
}
export default function SearchResults({
branch,
scope,
status,
items,
error,
onRetry,
nextCursor,
onLoadMore,
isLoadingMore,
loadMoreError,
}) {
const showBranchColumn =
scope === SEARCH_SCOPE.ALL || scope === SEARCH_SCOPE.MULTI;
const [sortMode, setSortMode] = React.useState(SORT.RELEVANCE);
const sortedItems = React.useMemo(() => {
const arr = Array.isArray(items) ? [...items] : [];
if (sortMode === SORT.RELEVANCE) return arr;
if (sortMode === SORT.DATE_DESC) {
return arr.sort((a, b) => {
const da = toDateKey(a);
const db = toDateKey(b);
if (da !== db) return da < db ? 1 : -1;
const fa = String(a?.filename || "");
const fb = String(b?.filename || "");
return fa.localeCompare(fb, "de");
});
}
if (sortMode === SORT.FILENAME_ASC) {
return arr.sort((a, b) =>
String(a?.filename || "").localeCompare(String(b?.filename || ""), "de")
);
}
return arr;
}, [items, sortMode]);
if (status === "idle") {
return (