+
{virtualizer.getVirtualItems().map((virtualRow) => {
const rowManga = rows[virtualRow.index];
return (
@@ -382,7 +391,6 @@ export default function Library() {
cropCovers={settings.libraryCropCovers}
/>
))}
- {/* Ghost cards on last row to fill grid */}
{virtualRow.index === rows.length - 1 &&
Array.from({ length: cols - rowManga.length }).map((_, i) => (
@@ -394,20 +402,10 @@ export default function Library() {
)}
{ctx && (
-
setCtx(null)}
- />
+ setCtx(null)} />
)}
{emptyCtx && (
- setEmptyCtx(null)}
- />
+ setEmptyCtx(null)} />
)}
);
diff --git a/src/components/pages/Search.module.css b/src/components/pages/Search.module.css
index 9afbe99..8b71c6a 100644
--- a/src/components/pages/Search.module.css
+++ b/src/components/pages/Search.module.css
@@ -1,30 +1,79 @@
+/* ── Root ────────────────────────────────────────────────────────────────── */
.root {
display: flex; flex-direction: column; height: 100%;
overflow: hidden; animation: fadeIn 0.14s ease both;
}
+/* ── Header ──────────────────────────────────────────────────────────────── */
.header {
display: flex; align-items: center; gap: var(--sp-4);
- padding: var(--sp-4) var(--sp-6); border-bottom: 1px solid var(--border-dim); flex-shrink: 0;
+ padding: var(--sp-3) var(--sp-6) var(--sp-3) var(--sp-6);
+ border-bottom: 1px solid var(--border-dim); flex-shrink: 0;
}
+
.heading {
font-family: var(--font-ui); font-size: var(--text-xs);
color: var(--text-faint); letter-spacing: var(--tracking-wider);
text-transform: uppercase; flex-shrink: 0;
}
+
+/* ── Tab bar ─────────────────────────────────────────────────────────────── */
+.tabs {
+ display: flex; gap: 2px;
+ background: var(--bg-raised); border: 1px solid var(--border-dim);
+ border-radius: var(--radius-md); padding: 2px;
+}
+.tab {
+ display: flex; align-items: center; gap: 5px;
+ font-family: var(--font-ui); font-size: var(--text-2xs);
+ letter-spacing: var(--tracking-wide); text-transform: uppercase;
+ padding: 4px 10px; border-radius: var(--radius-sm); border: none;
+ background: none; color: var(--text-faint); cursor: pointer;
+ transition: background var(--t-base), color var(--t-base); white-space: nowrap;
+}
+.tab:hover { color: var(--text-muted); }
+.tabActive {
+ background: var(--accent-muted); color: var(--accent-fg);
+ border: 1px solid var(--accent-dim);
+}
+.tabActive:hover { color: var(--accent-fg); }
+
+/* ── Keyword tab bar area ────────────────────────────────────────────────── */
+.keywordBar {
+ flex-shrink: 0; display: flex; flex-direction: column;
+ border-bottom: 1px solid var(--border-dim);
+}
+
+/* ── Shared search bar ───────────────────────────────────────────────────── */
.searchBar {
- flex: 1; display: flex; align-items: center; gap: var(--sp-2);
+ display: flex; align-items: center; gap: var(--sp-2);
background: var(--bg-raised); border: 1px solid var(--border-dim);
border-radius: var(--radius-lg); padding: 0 var(--sp-3) 0 var(--sp-2);
transition: border-color var(--t-base);
+ margin: var(--sp-3) var(--sp-6);
}
.searchBar:focus-within { border-color: var(--border-strong); }
+
.searchIcon { color: var(--text-faint); flex-shrink: 0; }
+
.searchInput {
flex: 1; background: none; border: none; outline: none;
color: var(--text-primary); font-size: var(--text-sm); padding: 8px 0;
}
.searchInput::placeholder { color: var(--text-faint); }
+
+.advancedBtn {
+ display: flex; align-items: center; justify-content: center;
+ width: 26px; height: 26px; border-radius: var(--radius-sm);
+ background: none; border: 1px solid transparent;
+ color: var(--text-faint); cursor: pointer; flex-shrink: 0;
+ transition: color var(--t-base), border-color var(--t-base), background var(--t-base);
+}
+.advancedBtn:hover { color: var(--text-muted); border-color: var(--border-dim); }
+.advancedBtnActive {
+ background: var(--accent-muted); border-color: var(--accent-dim); color: var(--accent-fg);
+}
+
.searchBtn {
font-family: var(--font-ui); font-size: var(--text-xs); letter-spacing: var(--tracking-wide);
padding: 5px 12px; border-radius: var(--radius-md); flex-shrink: 0;
@@ -35,74 +84,108 @@
.searchBtn:hover:not(:disabled) { filter: brightness(1.1); }
.searchBtn:disabled { opacity: 0.4; cursor: default; }
-.langBar {
- display: flex;
- align-items: center;
- flex-wrap: wrap;
- gap: var(--sp-1);
- padding: var(--sp-2) var(--sp-6);
- border-bottom: 1px solid var(--border-dim);
- flex-shrink: 0;
+.clearSearchBtn {
+ display: flex; align-items: center; justify-content: center;
+ width: 20px; height: 20px; border-radius: 50%;
+ font-size: 15px; line-height: 1;
+ color: var(--text-faint); background: var(--bg-overlay); border: none;
+ cursor: pointer; flex-shrink: 0; transition: color var(--t-base);
+}
+.clearSearchBtn:hover { color: var(--text-muted); }
+
+/* ── Advanced filter panel ───────────────────────────────────────────────── */
+.advancedPanel {
+ display: flex; flex-direction: column; gap: var(--sp-3);
+ padding: 0 var(--sp-6) var(--sp-4);
+ animation: fadeIn 0.1s ease both;
}
-.langBtn {
- font-family: var(--font-ui);
- font-size: var(--text-2xs);
- letter-spacing: var(--tracking-wider);
- padding: 3px 8px;
- border-radius: var(--radius-sm);
- border: 1px solid var(--border-dim);
- background: none;
- color: var(--text-faint);
- cursor: pointer;
+.advancedHeader { display: flex; align-items: center; justify-content: space-between; }
+.advancedTitle {
+ font-family: var(--font-ui); font-size: var(--text-2xs);
+ color: var(--text-faint); letter-spacing: var(--tracking-wider); text-transform: uppercase;
+}
+.advancedActions { display: flex; gap: var(--sp-3); }
+.advancedLink {
+ font-family: var(--font-ui); font-size: var(--text-2xs);
+ letter-spacing: var(--tracking-wide); color: var(--accent-fg);
+ background: none; border: none; cursor: pointer; padding: 0;
+ transition: opacity var(--t-base);
+}
+.advancedLink:hover { opacity: 0.7; }
+
+.langGrid { display: flex; flex-wrap: wrap; gap: var(--sp-1); }
+.langFilterRow { display: flex; flex-wrap: wrap; gap: var(--sp-1); padding: var(--sp-3) var(--sp-3) 0; }
+
+.langChip {
+ font-family: var(--font-ui); font-size: var(--text-2xs);
+ letter-spacing: var(--tracking-wider); padding: 3px 8px;
+ border-radius: var(--radius-sm); border: 1px solid var(--border-dim);
+ background: none; color: var(--text-faint); cursor: pointer;
transition: color var(--t-base), border-color var(--t-base), background var(--t-base);
+ white-space: nowrap;
}
-.langBtn:hover { color: var(--text-muted); border-color: var(--border-strong); }
-.langBtnActive {
- background: var(--accent-muted);
- border-color: var(--accent-dim);
- color: var(--accent-fg);
-}
-.langBtnActive:hover { background: var(--accent-muted); color: var(--accent-fg); }
-
-.sourceCount {
- font-family: var(--font-ui);
- font-size: var(--text-2xs);
- color: var(--text-faint);
- letter-spacing: var(--tracking-wide);
- margin-left: auto;
+.langChip:hover { color: var(--text-muted); border-color: var(--border-strong); }
+.langChipActive {
+ background: var(--accent-muted); border-color: var(--accent-dim); color: var(--accent-fg);
}
-.results { flex: 1; overflow-y: auto; padding: var(--sp-5) var(--sp-6); display: flex; flex-direction: column; gap: var(--sp-6); }
+.advancedDivider { height: 1px; background: var(--border-dim); margin: 0 calc(-1 * var(--sp-6)); }
+.advancedCheck {
+ display: flex; align-items: center; gap: var(--sp-2);
+ font-family: var(--font-ui); font-size: var(--text-xs);
+ color: var(--text-muted); cursor: pointer; user-select: none;
+}
+.checkbox { accent-color: var(--accent-fg); width: 13px; height: 13px; cursor: pointer; }
+.advancedFooter {
+ font-family: var(--font-ui); font-size: var(--text-2xs);
+ color: var(--text-faint); letter-spacing: var(--tracking-wide);
+}
+.advancedFooter strong { color: var(--text-muted); font-weight: var(--weight-medium); }
+
+/* ── Keyword results list ────────────────────────────────────────────────── */
+.results {
+ flex: 1; overflow-y: auto; padding: var(--sp-5) var(--sp-6);
+ display: flex; flex-direction: column; gap: var(--sp-6);
+ scrollbar-width: thin;
+}
.sourceSection { display: flex; flex-direction: column; gap: var(--sp-3); }
-.sourceHeader {
- display: flex; align-items: center; gap: var(--sp-2);
-}
+.sourceHeader { display: flex; align-items: center; gap: var(--sp-2); }
.sourceIcon { width: 18px; height: 18px; border-radius: 4px; object-fit: cover; flex-shrink: 0; }
.sourceName { font-size: var(--text-sm); font-weight: var(--weight-medium); color: var(--text-secondary); }
-.resultCount {
+.sourceLang {
font-family: var(--font-ui); font-size: var(--text-2xs); color: var(--text-faint);
- letter-spacing: var(--tracking-wide); margin-left: auto;
+ letter-spacing: var(--tracking-wider); padding: 1px 5px;
+ border: 1px solid var(--border-dim); border-radius: var(--radius-sm);
+}
+.resultCount {
+ font-family: var(--font-ui); font-size: var(--text-2xs);
+ color: var(--text-faint); letter-spacing: var(--tracking-wide); margin-left: auto;
}
.sourceError { font-size: var(--text-xs); color: var(--color-error); padding: 0 var(--sp-1); }
.sourceRow {
display: flex; gap: var(--sp-3); overflow-x: auto;
- padding-bottom: var(--sp-2);
- scrollbar-width: thin;
+ padding-bottom: var(--sp-2); scrollbar-width: thin;
}
+
+/* ── Shared manga card ───────────────────────────────────────────────────── */
.card {
- flex-shrink: 0; width: 110px; background: none; border: none; padding: 0;
+ flex-shrink: 0; width: 110px;
+ background: none; border: none; padding: 0;
cursor: pointer; text-align: left;
}
.card:hover .cover { filter: brightness(1.06); }
+
.coverWrap {
- position: relative; aspect-ratio: 2/3; border-radius: var(--radius-md);
- overflow: hidden; background: var(--bg-raised); border: 1px solid var(--border-dim);
+ position: relative; aspect-ratio: 2/3;
+ border-radius: var(--radius-md); overflow: hidden;
+ background: var(--bg-raised); border: 1px solid var(--border-dim);
}
.cover { width: 100%; height: 100%; object-fit: cover; transition: filter var(--t-base); }
+
.inLibBadge {
position: absolute; bottom: var(--sp-1); left: var(--sp-1);
font-family: var(--font-ui); font-size: var(--text-2xs); letter-spacing: var(--tracking-wide);
@@ -115,14 +198,127 @@
line-height: var(--leading-snug);
}
+/* ── Skeleton cards ──────────────────────────────────────────────────────── */
.skCard { flex-shrink: 0; width: 110px; }
-.skCover { aspect-ratio: 2/3; border-radius: var(--radius-md); }
-.skTitle { height: 11px; margin-top: var(--sp-1); width: 75%; }
+.skCover { aspect-ratio: 2/3; border-radius: var(--radius-md); width: 100%; }
+.skTitle { height: 11px; margin-top: var(--sp-1); width: 75%; border-radius: 3px; }
+/* ── Empty state ─────────────────────────────────────────────────────────── */
.empty {
flex: 1; display: flex; flex-direction: column;
align-items: center; justify-content: center; gap: var(--sp-2);
+ padding: var(--sp-6);
}
.emptyIcon { color: var(--text-faint); }
.emptyText { font-size: var(--text-base); color: var(--text-muted); }
-.emptyHint { font-size: var(--text-sm); color: var(--text-faint); }
\ No newline at end of file
+.emptyHint { font-size: var(--text-sm); color: var(--text-faint); text-align: center; max-width: 280px; }
+
+.advancedLinkStandalone {
+ display: flex; align-items: center; gap: var(--sp-1);
+ font-family: var(--font-ui); font-size: var(--text-xs);
+ color: var(--text-faint); background: none; border: none;
+ cursor: pointer; padding: 0; margin-top: var(--sp-1);
+ transition: color var(--t-base);
+}
+.advancedLinkStandalone:hover { color: var(--accent-fg); }
+
+/* ── Split layout (tag + source tabs) ───────────────────────────────────── */
+.splitRoot { flex: 1; display: flex; overflow: hidden; }
+
+.splitSidebar {
+ width: 192px; flex-shrink: 0;
+ border-right: 1px solid var(--border-dim);
+ display: flex; flex-direction: column; overflow: hidden;
+ background: var(--bg-raised);
+}
+
+.splitSearchWrap {
+ display: flex; align-items: center; gap: var(--sp-2);
+ padding: var(--sp-3);
+ border-bottom: 1px solid var(--border-dim); flex-shrink: 0;
+}
+.splitSearchIcon { color: var(--text-faint); flex-shrink: 0; }
+.splitSearchInput {
+ flex: 1; background: none; border: none; outline: none;
+ color: var(--text-primary); font-size: var(--text-xs);
+ font-family: var(--font-ui); letter-spacing: var(--tracking-wide);
+}
+.splitSearchInput::placeholder { color: var(--text-faint); }
+
+.splitList {
+ flex: 1; overflow-y: auto; padding: var(--sp-2);
+ display: flex; flex-direction: column; gap: 1px; scrollbar-width: thin;
+}
+
+.splitItem {
+ display: flex; align-items: center; width: 100%;
+ padding: 7px var(--sp-2); border-radius: var(--radius-md);
+ border: none; background: none;
+ color: var(--text-muted); font-size: var(--text-sm);
+ cursor: pointer; text-align: left;
+ transition: background var(--t-fast), color var(--t-fast);
+}
+.splitItem:hover { background: var(--bg-overlay); color: var(--text-secondary); }
+.splitItemActive { background: var(--accent-muted); color: var(--accent-fg); }
+.splitItemActive:hover { background: var(--accent-muted); color: var(--accent-fg); }
+
+.splitItemSource { gap: var(--sp-2); }
+.splitItemLabel { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
+.splitSourceIcon { width: 16px; height: 16px; border-radius: 3px; object-fit: cover; flex-shrink: 0; }
+
+.splitEmpty {
+ font-family: var(--font-ui); font-size: var(--text-xs);
+ color: var(--text-faint); padding: var(--sp-3) var(--sp-2);
+}
+.splitLoading {
+ flex: 1; display: flex; align-items: center; justify-content: center;
+}
+
+/* ── Split right content ─────────────────────────────────────────────────── */
+.splitContent { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
+
+.splitContentHeader {
+ display: flex; align-items: center; gap: var(--sp-3);
+ padding: var(--sp-3) var(--sp-5);
+ border-bottom: 1px solid var(--border-dim); flex-shrink: 0;
+ flex-wrap: wrap;
+ min-height: 52px;
+}
+.splitContentTitle {
+ font-size: var(--text-base); font-weight: var(--weight-medium);
+ color: var(--text-secondary); letter-spacing: var(--tracking-tight);
+}
+.splitResultCount {
+ font-family: var(--font-ui); font-size: var(--text-2xs);
+ color: var(--text-faint); letter-spacing: var(--tracking-wide);
+ margin-left: auto;
+}
+.splitSourceTitle {
+ display: flex; align-items: center; gap: var(--sp-2); flex-shrink: 0;
+}
+.sourceBrowseBar {
+ display: flex; align-items: center; gap: var(--sp-2); flex: 1; min-width: 0;
+}
+
+/* ── Grid (tag + source results) ─────────────────────────────────────────── */
+.tagGrid {
+ display: grid;
+ grid-template-columns: repeat(auto-fill, minmax(clamp(90px, 9vw, 120px), 1fr));
+ gap: var(--sp-3);
+ padding: var(--sp-4) var(--sp-5);
+ overflow-y: auto; flex: 1;
+ align-content: start;
+ scrollbar-width: thin;
+}
+/* In the grid, cards stretch to fill the column */
+.tagGrid .card { width: auto; }
+.tagGrid .skCard { width: auto; }
+.tagGrid .skCover { width: 100%; }
+
+/* ── NSFW badge ──────────────────────────────────────────────────────────── */
+.nsfwBadge {
+ font-family: var(--font-ui); font-size: var(--text-2xs);
+ letter-spacing: var(--tracking-wide); padding: 1px 5px;
+ border-radius: var(--radius-sm); border: 1px solid var(--border-dim);
+ color: var(--text-faint); flex-shrink: 0;
+}
\ No newline at end of file
diff --git a/src/components/pages/Search.tsx b/src/components/pages/Search.tsx
index d2165a2..ce0e458 100644
--- a/src/components/pages/Search.tsx
+++ b/src/components/pages/Search.tsx
@@ -1,11 +1,19 @@
-import { useState, useRef, useCallback, useEffect } from "react";
-import { MagnifyingGlass, CircleNotch } from "@phosphor-icons/react";
+import { useState, useRef, useCallback, useEffect, memo, useMemo } from "react";
+import {
+ MagnifyingGlass, CircleNotch, SlidersHorizontal, Hash, List,
+} from "@phosphor-icons/react";
import { gql, thumbUrl } from "../../lib/client";
import { GET_SOURCES, FETCH_SOURCE_MANGA } from "../../lib/queries";
+import { cache, CACHE_KEYS, getTopSources } from "../../lib/cache";
+import { dedupeSources, dedupeMangaByTitle } from "../../lib/sourceUtils";
import { useStore } from "../../store";
import type { Manga, Source } from "../../lib/types";
import s from "./Search.module.css";
+// ── Types ─────────────────────────────────────────────────────────────────────
+
+type SearchTab = "keyword" | "tag" | "source";
+
interface SourceResult {
source: Source;
mangas: Manga[];
@@ -13,15 +21,30 @@ interface SourceResult {
error: string | null;
}
-const CONCURRENCY = 3;
+// ── Constants ─────────────────────────────────────────────────────────────────
+
+const CONCURRENCY = 4;
+const RESULTS_PER_SOURCE = 8;
+
+const COMMON_GENRES = [
+ "Action","Adventure","Comedy","Drama","Fantasy","Romance",
+ "Sci-Fi","Slice of Life","Horror","Mystery","Thriller","Sports",
+ "Supernatural","Mecha","Historical","Psychological","School Life",
+ "Shounen","Seinen","Josei","Shoujo","Isekai","Martial Arts",
+ "Magic","Music","Cooking","Medical","Military","Harem","Ecchi",
+];
+
+// ── Concurrent fetch helper ───────────────────────────────────────────────────
async function runConcurrent
(
items: T[],
- fn: (item: T) => Promise
+ fn: (item: T) => Promise,
+ signal: AbortSignal,
): Promise {
let i = 0;
async function worker() {
while (i < items.length) {
+ if (signal.aborted) return;
const item = items[i++];
await fn(item).catch(() => {});
}
@@ -29,84 +52,280 @@ async function runConcurrent(
await Promise.all(Array.from({ length: Math.min(CONCURRENCY, items.length) }, worker));
}
+// ── Shared card ───────────────────────────────────────────────────────────────
+
+const CoverImg = memo(function CoverImg({
+ src, alt, className,
+}: { src: string; alt: string; className?: string }) {
+ const [loaded, setLoaded] = useState(false);
+ return (
+
setLoaded(true)}
+ style={{ opacity: loaded ? 1 : 0, transition: loaded ? "opacity 0.15s ease" : "none" }}
+ />
+ );
+});
+
+function MangaCard({ manga, onClick }: { manga: Manga; onClick: () => void }) {
+ return (
+
+ );
+}
+
+function GridSkeleton({ count = 18 }: { count?: number }) {
+ return (
+
+ {Array.from({ length: count }).map((_, i) => (
+
+ ))}
+
+ );
+}
+
+function RowSkeleton({ count = 4 }: { count?: number }) {
+ return (
+
+ {Array.from({ length: count }).map((_, i) => (
+
+ ))}
+
+ );
+}
+
+// ── Root ──────────────────────────────────────────────────────────────────────
+
export default function Search() {
- const [query, setQuery] = useState("");
- const [submitted, setSubmitted] = useState("");
- const [results, setResults] = useState([]);
- const [allSources, setAllSources] = useState([]);
+ const [tab, setTab] = useState("keyword");
+
+ const preferredLang = useStore((st) => st.settings.preferredExtensionLang);
+ const searchPrefill = useStore((st) => st.searchPrefill ?? "");
+ const setSearchPrefill = useStore((st) => st.setSearchPrefill);
+ const setPreviewManga = useStore((st) => st.setPreviewManga);
+
+ const [allSources, setAllSources] = useState([]);
const [loadingSources, setLoadingSources] = useState(false);
- const [activeLang, setActiveLang] = useState("preferred");
- const inputRef = useRef(null);
- const setActiveManga = useStore((st) => st.setActiveManga);
- const setNavPage = useStore((st) => st.setNavPage);
- const preferredLang = useStore((st) => st.settings.preferredExtensionLang);
+ const pendingPrefill = useRef("");
+ // Consume searchPrefill → route to keyword tab
+ useEffect(() => {
+ if (!searchPrefill) return;
+ pendingPrefill.current = searchPrefill;
+ setTab("keyword");
+ setSearchPrefill("");
+ }, [searchPrefill, setSearchPrefill]);
+
+ // Load sources once, shared across all tabs
useEffect(() => {
setLoadingSources(true);
- gql<{ sources: { nodes: Source[] } }>(GET_SOURCES)
- .then((d) => setAllSources(d.sources.nodes.filter((s) => s.id !== "0")))
+ cache.get(CACHE_KEYS.SOURCES, () =>
+ gql<{ sources: { nodes: Source[] } }>(GET_SOURCES)
+ .then((d) => d.sources.nodes.filter((src) => src.id !== "0"))
+ )
+ .then(setAllSources)
.catch(console.error)
.finally(() => setLoadingSources(false));
+ // eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
- const langs = ["preferred", ...Array.from(new Set(allSources.map((s) => s.lang))).sort(), "all"];
-
- const visibleSources = allSources.filter((src) => {
- if (activeLang === "all") return true;
- if (activeLang === "preferred") return src.lang === preferredLang;
- return src.lang === activeLang;
- });
-
- const runSearch = useCallback(async () => {
- const q = query.trim();
- if (!q || !visibleSources.length) return;
- setSubmitted(q);
-
- setResults(visibleSources.map((src) => ({ source: src, mangas: [], loading: true, error: null })));
-
- await runConcurrent(visibleSources, async (src) => {
- try {
- const d = await gql<{ fetchSourceManga: { mangas: Manga[] } }>(FETCH_SOURCE_MANGA, {
- source: src.id, type: "SEARCH", page: 1, query: q,
- });
- setResults((prev) => prev.map((r) =>
- r.source.id === src.id ? { ...r, mangas: d.fetchSourceManga.mangas, loading: false } : r
- ));
- } catch (e: any) {
- setResults((prev) => prev.map((r) =>
- r.source.id === src.id ? { ...r, loading: false, error: e.message } : r
- ));
- }
- });
- }, [query, visibleSources]);
-
- function openManga(m: Manga) {
- setActiveManga(m);
- setNavPage("library");
- }
-
- const hasResults = results.some((r) => r.mangas.length > 0);
- const allDone = results.every((r) => !r.loading);
+ const availableLangs = useMemo(() =>
+ Array.from(new Set(allSources.map((s) => s.lang))).sort(), [allSources]);
+ const hasMultipleLangs = availableLangs.length > 1;
return (
Search
+
+
+
+
+
+
+
+ {tab === "keyword" && (
+
+ )}
+ {tab === "tag" && (
+
+ )}
+ {tab === "source" && (
+
+ )}
+
+ );
+}
+
+// ── Keyword tab ───────────────────────────────────────────────────────────────
+
+function KeywordTab({
+ allSources, loadingSources, availableLangs, hasMultipleLangs,
+ preferredLang, pendingPrefill, onMangaClick,
+}: {
+ allSources: Source[];
+ loadingSources: boolean;
+ availableLangs: string[];
+ hasMultipleLangs: boolean;
+ preferredLang: string;
+ pendingPrefill: React.MutableRefObject;
+ onMangaClick: (m: Manga) => void;
+}) {
+ const [query, setQuery] = useState("");
+ const [submitted, setSubmitted] = useState("");
+ const [results, setResults] = useState([]);
+ const [showAdvanced, setShowAdvanced] = useState(false);
+ const [selectedLangs, setSelectedLangs] = useState>(new Set());
+ const [includeNsfw, setIncludeNsfw] = useState(false);
+
+ const abortRef = useRef(null);
+ const inputRef = useRef(null);
+ const allSourcesRef = useRef([]);
+ const selectedLangsRef = useRef>(new Set());
+
+ useEffect(() => { allSourcesRef.current = allSources; }, [allSources]);
+ useEffect(() => { selectedLangsRef.current = selectedLangs; }, [selectedLangs]);
+
+ // Set default lang selection once sources load
+ useEffect(() => {
+ if (!allSources.length) return;
+ const available = new Set(allSources.map((s) => s.lang));
+ setSelectedLangs(available.has(preferredLang)
+ ? new Set([preferredLang])
+ : new Set(availableLangs.slice(0, 1))
+ );
+ }, [allSources]); // eslint-disable-line react-hooks/exhaustive-deps
+
+ // Consume prefill once sources are ready
+ useEffect(() => {
+ if (loadingSources || !pendingPrefill.current || submitted) return;
+ if (!allSourcesRef.current.length) return;
+ const q = pendingPrefill.current;
+ pendingPrefill.current = "";
+ setQuery(q);
+ doSearch(q);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [loadingSources]);
+
+ useEffect(() => () => { abortRef.current?.abort(); }, []);
+
+ const getVisibleSources = useCallback((): Source[] => {
+ let filtered = allSourcesRef.current;
+ if (selectedLangsRef.current.size > 0)
+ filtered = filtered.filter((s) => selectedLangsRef.current.has(s.lang));
+ if (!includeNsfw)
+ filtered = filtered.filter((s) => !s.isNsfw);
+ return filtered;
+ }, [includeNsfw]);
+
+ const doSearch = useCallback(async (q: string) => {
+ const trimmed = q.trim();
+ if (!trimmed) return;
+ const visible = getVisibleSources();
+ if (!visible.length) return;
+
+ abortRef.current?.abort();
+ const ctrl = new AbortController();
+ abortRef.current = ctrl;
+
+ setSubmitted(trimmed);
+ setResults(visible.map((src) => ({ source: src, mangas: [], loading: true, error: null })));
+
+ await runConcurrent(visible, async (src) => {
+ if (ctrl.signal.aborted) return;
+ try {
+ const d = await gql<{ fetchSourceManga: { mangas: Manga[] } }>(
+ FETCH_SOURCE_MANGA,
+ { source: src.id, type: "SEARCH", page: 1, query: trimmed },
+ ctrl.signal,
+ );
+ if (ctrl.signal.aborted) return;
+ setResults((prev) => prev.map((r) =>
+ r.source.id === src.id ? { ...r, mangas: d.fetchSourceManga.mangas, loading: false } : r
+ ));
+ } catch (e: any) {
+ if (ctrl.signal.aborted || e?.name === "AbortError") return;
+ setResults((prev) => prev.map((r) =>
+ r.source.id === src.id ? { ...r, loading: false, error: e.message } : r
+ ));
+ }
+ }, ctrl.signal);
+ }, [getVisibleSources]);
+
+ function toggleLang(lang: string) {
+ setSelectedLangs((prev) => {
+ const next = new Set(prev);
+ if (next.has(lang)) { if (next.size === 1) return prev; next.delete(lang); }
+ else next.add(lang);
+ return next;
+ });
+ }
+
+ const visibleCount = getVisibleSources().length;
+ const hasResults = results.some((r) => r.mangas.length > 0);
+ const allDone = results.every((r) => !r.loading);
+
+ return (
+ <>
+
-
- {langs.map((l) => (
-
- ))}
- {visibleSources.length > 0 && (
-
{visibleSources.length} sources
+ {hasMultipleLangs && showAdvanced && (
+
+
+
Languages
+
+
+
+
+
+
+ {availableLangs.map((lang) => (
+
+ ))}
+
+
+
+
+ Searching {visibleCount} source{visibleCount !== 1 ? "s" : ""}
+
+
)}
@@ -136,8 +371,15 @@ export default function Search() {
Search across sources
- Searching {visibleSources.length} {activeLang === "preferred" ? `${preferredLang.toUpperCase()}` : activeLang === "all" ? "" : activeLang.toUpperCase()} source{visibleSources.length !== 1 ? "s" : ""}.
+ {hasMultipleLangs
+ ? `${visibleCount} source${visibleCount !== 1 ? "s" : ""} · ${selectedLangs.size} language${selectedLangs.size !== 1 ? "s" : ""}`
+ : `${visibleCount} source${visibleCount !== 1 ? "s" : ""}`}
+ {hasMultipleLangs && !showAdvanced && (
+
+ )}
)}
@@ -148,59 +390,321 @@ export default function Search() {
-
})
{ (e.target as HTMLImageElement).style.display = "none"; }}
- />
+
})
{ (e.target as HTMLImageElement).style.display = "none"; }} />
{source.displayName}
- {loading &&
}
- {!loading && mangas.length > 0 && (
-
{mangas.length} results
- )}
+ {hasMultipleLangs &&
{source.lang.toUpperCase()}}
+ {loading &&
}
+ {!loading && mangas.length > 0 &&
{mangas.length} results}
-
{error ? (
{error}
) : loading ? (
-
- {Array.from({ length: 4 }).map((_, i) => (
-
- ))}
-
+
) : mangas.length > 0 ? (
- {mangas.slice(0, 8).map((m) => (
-
+ {mangas.slice(0, RESULTS_PER_SOURCE).map((m) => (
+
onMangaClick(m)} />
))}
) : null}
))}
-
- {allDone && !hasResults && submitted && (
+ {allDone && !hasResults && (