Continuous Integration / build_web (push) Failing after 1s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Measure the library grid instead of guessing from the viewport, reuse the card scroller for search and latest rows, mirror item/music page structure, and add skeleton sections to the home placeholder.
508 lines
13 KiB
TypeScript
508 lines
13 KiB
TypeScript
import { BaseItemKind } from "@jellyfin/sdk/lib/generated-client";
|
|
import { getLibraryApi } from "@jellyfin/sdk/lib/utils/api/library-api";
|
|
import { getPersonApi } from "@jellyfin/sdk/lib/utils/api/person-api";
|
|
import { getSearchApi } from "@jellyfin/sdk/lib/utils/api/search-api";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import React, { useEffect, useMemo, useState } from "react";
|
|
import { Card } from "@/components/card/card";
|
|
import CardScroller from "@/components/cardScroller/cardScroller";
|
|
import { CardSkeleton } from "@/components/skeleton/cards";
|
|
import { useBackdropStore } from "@/utils/store/backdrop";
|
|
import "./search.scss";
|
|
import {
|
|
Box,
|
|
Chip,
|
|
Fade,
|
|
FormControl,
|
|
Grow,
|
|
IconButton,
|
|
InputBase,
|
|
MenuItem,
|
|
OutlinedInput,
|
|
Select,
|
|
type SelectChangeEvent,
|
|
Typography,
|
|
} from "@mui/material";
|
|
import { createFileRoute, Outlet, useNavigate } from "@tanstack/react-router";
|
|
import { useShallow } from "zustand/shallow";
|
|
import useDebounce from "@/utils/hooks/useDebounce";
|
|
import { useApiInContext } from "@/utils/store/api";
|
|
import { useCentralStore } from "@/utils/store/central";
|
|
|
|
export const Route = createFileRoute("/_api/search/")({
|
|
component: SearchPage,
|
|
validateSearch: (search: Record<string, unknown>): { query: string } => {
|
|
return { query: search.query as string };
|
|
},
|
|
});
|
|
|
|
const CATEGORIES = [
|
|
{ label: "Фильмы", value: "Movie", kind: BaseItemKind.Movie },
|
|
{ label: "Сериалы", value: "Series", kind: BaseItemKind.Series },
|
|
{ label: "Серии", value: "Episode", kind: "Episode" },
|
|
{ label: "Музыка", value: "MusicAlbum", kind: BaseItemKind.MusicAlbum },
|
|
{
|
|
label: "Исполнители",
|
|
value: "MusicArtist",
|
|
kind: BaseItemKind.MusicArtist,
|
|
},
|
|
{ label: "Люди", value: "Person", kind: "Person" },
|
|
{ label: "Книги", value: "Book", kind: BaseItemKind.Book },
|
|
];
|
|
|
|
function SearchPage() {
|
|
const api = useApiInContext((state) => state.api);
|
|
const navigate = useNavigate();
|
|
const { query } = Route.useSearch();
|
|
const [setBackdrop] = useBackdropStore(useShallow((s) => [s.setBackdrop]));
|
|
const user = useCentralStore((s) => s.currentUser);
|
|
|
|
const [searchTerm, setSearchTerm] = useState(query ?? "");
|
|
const debouncedSearchTerm = useDebounce(searchTerm, 300);
|
|
const [selectedCategories, setSelectedCategories] = useState<string[]>([]);
|
|
|
|
useEffect(() => {
|
|
if (query && query !== searchTerm) {
|
|
setSearchTerm(query);
|
|
}
|
|
}, [query]);
|
|
|
|
useEffect(() => {
|
|
if (debouncedSearchTerm !== query) {
|
|
navigate({
|
|
to: "/search",
|
|
search: { query: debouncedSearchTerm ?? "" },
|
|
replace: true,
|
|
});
|
|
}
|
|
}, [debouncedSearchTerm, navigate]);
|
|
|
|
useEffect(() => {
|
|
setBackdrop("");
|
|
}, []);
|
|
|
|
const handleCategoryChange = (event: SelectChangeEvent<string[]>) => {
|
|
const {
|
|
target: { value },
|
|
} = event;
|
|
setSelectedCategories(typeof value === "string" ? value.split(",") : value);
|
|
};
|
|
|
|
const activeCategories = useMemo(() => {
|
|
if (selectedCategories.length === 0) return CATEGORIES;
|
|
return CATEGORIES.filter((cat) => selectedCategories.includes(cat.value));
|
|
}, [selectedCategories]);
|
|
|
|
return (
|
|
<div className="search-page scrollY">
|
|
<div
|
|
className={`search-page-bg ${debouncedSearchTerm ? "active" : ""}`}
|
|
/>
|
|
|
|
<SearchToolbar
|
|
searchTerm={searchTerm}
|
|
setSearchTerm={setSearchTerm}
|
|
selectedCategories={selectedCategories}
|
|
handleCategoryChange={handleCategoryChange}
|
|
handleClearCategories={() => setSelectedCategories([])}
|
|
/>
|
|
|
|
<div className="search-page-content">
|
|
{debouncedSearchTerm ? (
|
|
<SearchResultsList
|
|
categories={activeCategories}
|
|
query={debouncedSearchTerm}
|
|
api={api}
|
|
user={user}
|
|
/>
|
|
) : (
|
|
<Fade in timeout={800}>
|
|
<Box className="search-empty-state">
|
|
<Box className="icon-stack">
|
|
<span className="material-symbols-rounded icon-lg">movie</span>
|
|
<span className="material-symbols-rounded icon-sm">
|
|
music_note
|
|
</span>
|
|
<span className="material-symbols-rounded icon-md">tv</span>
|
|
</Box>
|
|
<Typography
|
|
variant="h4"
|
|
gutterBottom
|
|
sx={{
|
|
fontWeight: 600,
|
|
}}
|
|
>
|
|
Исследуйте библиотеку
|
|
</Typography>
|
|
<Typography variant="body1" sx={{ opacity: 0.6, maxWidth: 400 }}>
|
|
Ищите фильмы, сериалы, музыку, людей и многое другое.
|
|
</Typography>
|
|
</Box>
|
|
</Fade>
|
|
)}
|
|
</div>
|
|
<Outlet />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
interface SearchToolbarProps {
|
|
searchTerm: string;
|
|
setSearchTerm: (term: string) => void;
|
|
selectedCategories: string[];
|
|
handleCategoryChange: (event: SelectChangeEvent<string[]>) => void;
|
|
handleClearCategories: () => void;
|
|
}
|
|
|
|
function SearchToolbar({
|
|
searchTerm,
|
|
setSearchTerm,
|
|
selectedCategories,
|
|
handleCategoryChange,
|
|
handleClearCategories,
|
|
}: SearchToolbarProps) {
|
|
return (
|
|
<div className="search-toolbar">
|
|
<Box className="search-input-container">
|
|
<span className="material-symbols-rounded search-icon">search</span>
|
|
<InputBase
|
|
className="search-input"
|
|
placeholder="Поиск в библиотеке..."
|
|
fullWidth
|
|
autoFocus
|
|
value={searchTerm}
|
|
onChange={(e) => setSearchTerm(e.target.value)}
|
|
endAdornment={
|
|
searchTerm && (
|
|
<IconButton
|
|
size="small"
|
|
onClick={() => setSearchTerm("")}
|
|
sx={{ color: "white" }}
|
|
>
|
|
<span
|
|
className="material-symbols-rounded"
|
|
style={{ fontSize: "1.2rem" }}
|
|
>
|
|
close
|
|
</span>
|
|
</IconButton>
|
|
)
|
|
}
|
|
size="small"
|
|
/>
|
|
|
|
<FormControl size="small" className="search-category-select">
|
|
<Select
|
|
multiple
|
|
displayEmpty
|
|
value={selectedCategories}
|
|
onChange={handleCategoryChange}
|
|
input={
|
|
<OutlinedInput
|
|
endAdornment={
|
|
selectedCategories.length > 0 && (
|
|
<IconButton
|
|
size="small"
|
|
onMouseDown={(e) => {
|
|
e.stopPropagation();
|
|
}}
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
handleClearCategories();
|
|
}}
|
|
sx={{ p: 0.5 }}
|
|
>
|
|
<span
|
|
className="material-symbols-rounded"
|
|
style={{ fontSize: "1rem" }}
|
|
>
|
|
close
|
|
</span>
|
|
</IconButton>
|
|
)
|
|
}
|
|
/>
|
|
}
|
|
IconComponent={(props) => (
|
|
<span
|
|
{...props}
|
|
className={`material-symbols-rounded ${props.className}`}
|
|
>
|
|
expand_more
|
|
</span>
|
|
)}
|
|
renderValue={(selected) => {
|
|
if (selected.length === 0)
|
|
return (
|
|
<Typography
|
|
variant="body2"
|
|
color="textSecondary"
|
|
sx={{ opacity: 0.7 }}
|
|
>
|
|
Типы
|
|
</Typography>
|
|
);
|
|
return (
|
|
<Box
|
|
sx={{
|
|
display: "flex",
|
|
flexWrap: "nowrap",
|
|
gap: 0.5,
|
|
overflow: "hidden",
|
|
maxWidth: "100%",
|
|
maskImage:
|
|
"linear-gradient(to right, black 85%, transparent 100%)",
|
|
}}
|
|
>
|
|
{selected.map((value) => {
|
|
const label =
|
|
CATEGORIES.find((c) => c.value === value)?.label || value;
|
|
return (
|
|
<Chip
|
|
key={value}
|
|
label={label}
|
|
size="small"
|
|
sx={{ height: 24 }}
|
|
/>
|
|
);
|
|
})}
|
|
</Box>
|
|
);
|
|
}}
|
|
MenuProps={{
|
|
slotProps: { paper: { className: "glass-menu-paper" } },
|
|
disableScrollLock: true,
|
|
}}
|
|
size="small"
|
|
>
|
|
<MenuItem disabled value="">
|
|
<Typography variant="overline" color="textSecondary">
|
|
Фильтр по типу
|
|
</Typography>
|
|
</MenuItem>
|
|
{CATEGORIES.map((cat) => (
|
|
<MenuItem key={cat.value} value={cat.value}>
|
|
{cat.label}
|
|
</MenuItem>
|
|
))}
|
|
</Select>
|
|
</FormControl>
|
|
</Box>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function SearchResultsList({ categories, query, api, user }: any) {
|
|
// Lift state to know if we have ANY results
|
|
const [resultsStatus, setResultsStatus] = useState<Record<string, boolean>>(
|
|
{},
|
|
);
|
|
|
|
const handleResultUpdate = (catValue: string, hasResults: boolean) => {
|
|
setResultsStatus((prev) => {
|
|
if (prev[catValue] === hasResults) return prev;
|
|
return { ...prev, [catValue]: hasResults };
|
|
});
|
|
};
|
|
|
|
// Derived state: are *all* queried categories empty?
|
|
// We only know for sure if all active categories have reported back false.
|
|
// This is tricky with async. A better way implies checking isLoading.
|
|
// For now, we render the sections; if they are empty, they render null.
|
|
// If ALL render null, we show "No Results".
|
|
|
|
// Simpler visual fix: Render "No results found" at the bottom if nothing appears?
|
|
// No, that looks broken.
|
|
|
|
// Let's use a timeout or assume if no results update to true after X time.
|
|
|
|
return (
|
|
<Box sx={{ display: "flex", flexDirection: "column", gap: "1em", pb: 4 }}>
|
|
{categories.map((cat: any) => (
|
|
<CategorySection
|
|
key={cat.value}
|
|
category={cat}
|
|
query={query}
|
|
api={api}
|
|
user={user}
|
|
onResult={(hasItems: boolean) =>
|
|
handleResultUpdate(cat.value, hasItems)
|
|
}
|
|
/>
|
|
))}
|
|
|
|
{/*
|
|
Only show "No results" if we are confident.
|
|
Since we don't have a global loading state here,
|
|
checking `!hasAnyResults` immediately causes flickers.
|
|
Better to handle "No results" inside the sections or leave it blank?
|
|
User asked for "User needs to know search result is empty".
|
|
*/}
|
|
<NoResultsDetector
|
|
categories={categories}
|
|
resultsStatus={resultsStatus}
|
|
/>
|
|
</Box>
|
|
);
|
|
}
|
|
|
|
function NoResultsDetector({ categories, resultsStatus }: any) {
|
|
// Wait for all categories to report status
|
|
const allReported = categories.every(
|
|
(c: any) => resultsStatus[c.value] !== undefined,
|
|
);
|
|
const hasAny = Object.values(resultsStatus).some((v) => v);
|
|
|
|
if (allReported && !hasAny) {
|
|
return (
|
|
<Fade in>
|
|
<Box className="search-no-results">
|
|
<Typography variant="h6">Ничего не найдено</Typography>
|
|
<Typography variant="body2" sx={{ opacity: 0.6 }}>
|
|
Попробуйте изменить запрос или фильтры.
|
|
</Typography>
|
|
</Box>
|
|
</Fade>
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function CategorySection({ category, query, api, user, onResult }: any) {
|
|
const limit = 20;
|
|
|
|
const { data, isLoading } = useQuery({
|
|
queryKey: [
|
|
"search",
|
|
"category",
|
|
category.value,
|
|
query,
|
|
user?.Id,
|
|
api?.basePath,
|
|
],
|
|
queryFn: async () => {
|
|
if (!api || !user?.Id) return { items: [], total: 0 };
|
|
|
|
if (category.value === "Person") {
|
|
const res = await getPersonApi(api).getPersons({
|
|
userId: user.Id,
|
|
searchTerm: query,
|
|
limit,
|
|
});
|
|
return { items: res.data.Items, total: res.data.TotalRecordCount };
|
|
}
|
|
|
|
if (category.value === "Episode") {
|
|
const res = await getSearchApi(api).getSearchHints({
|
|
userId: user.Id,
|
|
searchTerm: query,
|
|
limit,
|
|
includeItemTypes: ["Episode"],
|
|
});
|
|
return {
|
|
items: res.data.SearchHints,
|
|
total: res.data.TotalRecordCount,
|
|
};
|
|
}
|
|
|
|
const res = await getLibraryApi(api).getItems({
|
|
userId: user.Id,
|
|
searchTerm: query,
|
|
includeItemTypes: [category.kind],
|
|
recursive: true,
|
|
limit,
|
|
});
|
|
return { items: res.data.Items, total: res.data.TotalRecordCount };
|
|
},
|
|
enabled: Boolean(api && user?.Id && query),
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (!isLoading && data) {
|
|
onResult((data.items?.length || 0) > 0);
|
|
} else if (!isLoading && !data) {
|
|
onResult(false);
|
|
}
|
|
}, [data, isLoading, onResult]);
|
|
|
|
if (isLoading) return <CategorySkeleton category={category} />;
|
|
if (!data?.items || data.items.length === 0) return null;
|
|
|
|
// ... (Card mapping logic same as before) ...
|
|
return (
|
|
<Grow in>
|
|
<div>
|
|
<CardScroller
|
|
title={category.label}
|
|
displayCards={category.value === "Episode" ? 4 : 7}
|
|
disableDecoration
|
|
>
|
|
{data.items.map((item: any) => {
|
|
let props: any = {
|
|
cardTitle: item?.Name,
|
|
imageType: "Primary",
|
|
cardCaption: item?.ProductionYear,
|
|
cardType: "portrait",
|
|
};
|
|
|
|
if (category.value === "Series") {
|
|
props.cardCaption = `${item.ProductionYear || ""}`;
|
|
} else if (
|
|
category.value === "MusicAlbum" ||
|
|
category.value === "MusicArtist" ||
|
|
category.value === "Person"
|
|
) {
|
|
props.cardType = "square";
|
|
} else if (category.value === "Episode") {
|
|
props = {
|
|
cardTitle: item.Series,
|
|
imageType: "Primary",
|
|
cardCaption: `S${item.ParentIndexNumber}:E${item.IndexNumber} - ${item.Name}`,
|
|
cardType: "thumb",
|
|
};
|
|
}
|
|
|
|
return (
|
|
<Card
|
|
key={item.Id}
|
|
item={item}
|
|
{...props}
|
|
queryKey={["search", "category", category.value, query]}
|
|
userId={user?.Id}
|
|
/>
|
|
);
|
|
})}
|
|
</CardScroller>
|
|
</div>
|
|
</Grow>
|
|
);
|
|
}
|
|
|
|
function CategorySkeleton({ category }: { category: any }) {
|
|
const displayCards = category.value === "Episode" ? 4 : 7;
|
|
const cardType =
|
|
category.value === "Episode"
|
|
? "thumb"
|
|
: ["MusicAlbum", "MusicArtist", "Person"].includes(category.value)
|
|
? "square"
|
|
: "portrait";
|
|
|
|
return (
|
|
<div aria-busy="true">
|
|
<CardScroller
|
|
title={category.label}
|
|
displayCards={displayCards}
|
|
disableDecoration
|
|
>
|
|
{Array.from({ length: displayCards }, (_, index) => (
|
|
<CardSkeleton
|
|
// biome-ignore lint/suspicious/noArrayIndexKey: static skeleton slides
|
|
key={`${category.value}-skeleton-${index}`}
|
|
cardType={cardType}
|
|
/>
|
|
))}
|
|
</CardScroller>
|
|
</div>
|
|
);
|
|
}
|