refactor: move to tanstack router

This commit is contained in:
Prayag Prajapati committed 2024-05-29 18:10:22 +05:30
1 parent a4ea932153
commit 522621ed69
136 files changed
+7063 -6127

No files matched your search

+518
View File
@@ -0,0 +1,518 @@
import PropTypes from "prop-types";
import React, { useState, useLayoutEffect, useRef } from "react";
import Box from "@mui/material/Box";
import CircularProgress from "@mui/material/CircularProgress";
import Divider from "@mui/material/Divider";
import Tab from "@mui/material/Tab";
import Tabs from "@mui/material/Tabs";
import Typography from "@mui/material/Typography";
import useParallax from "@/utils/hooks/useParallax";
import { AnimatePresence, motion, useScroll } from "framer-motion";
import {
BaseItemKind,
ItemFields,
LocationType,
} from "@jellyfin/sdk/lib/generated-client";
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
import { Card } from "@/components/card/card";
import { useQuery } from "@tanstack/react-query";
import { Blurhash } from "react-blurhash";
import LikeButton from "@/components/buttons/likeButton";
import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice";
import { setBackdrop } from "@/utils/store/backdrop";
import meshBg from "@/assets/herobg.png";
import ShowMoreText from "@/components/showMoreText";
import "./person.scss";
import IconLink from "@/components/iconLink";
import { createFileRoute } from "@tanstack/react-router";
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`full-width-tabpanel-${index}`}
aria-labelledby={`full-width-tab-${index}`}
{...other}
style={{ marginTop: "1em" }}
>
{value === index && <div>{children}</div>}
</div>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.number.isRequired,
value: PropTypes.number.isRequired,
};
function a11yProps(index) {
return {
id: `full-width-tab-${index}`,
"aria-controls": `full-width-tabpanel-${index}`,
};
}
export const Route = createFileRoute("/_api/person/$id")({
component: PersonTitlePage,
});
function PersonTitlePage() {
const { id } = Route.useParams();
const api = Route.useRouteContext().api;
const user = useQuery({
queryKey: ["user"],
queryFn: async () => {
const usr = await getUserApi(api).getCurrentUser();
return usr.data;
},
networkMode: "always",
enabled: Boolean(api),
});
const item = useQuery({
queryKey: ["item", id],
queryFn: async () => {
const result = await getUserLibraryApi(api).getItem({
userId: user.data.Id,
itemId: id,
fields: [ItemFields.Crew],
});
return result.data;
},
enabled: !!user.data,
networkMode: "always",
refetchOnWindowFocus: true,
});
const personMovies = useQuery({
queryKey: ["item", id, "personMovies"],
queryFn: async () => {
const result = await getItemsApi(api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Movie],
recursive: true,
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
sortOrder: ["Descending"],
excludeLocationTypes: [LocationType.Virtual],
});
return result.data;
},
enabled: item.isSuccess && item.data.Type === BaseItemKind.Person,
networkMode: "always",
});
const personShows = useQuery({
queryKey: ["item", id, "personShows"],
queryFn: async () => {
const result = await getItemsApi(api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Series],
recursive: true,
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
sortOrder: ["Descending"],
excludeLocationTypes: [LocationType.Virtual],
});
return result.data;
},
enabled: item.isSuccess && item.data.Type === BaseItemKind.Person,
networkMode: "always",
});
const personBooks = useQuery({
queryKey: ["item", id, "personBooks"],
queryFn: async () => {
const result = await getItemsApi(api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Book],
recursive: true,
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
sortOrder: ["Descending"],
excludeLocationTypes: [LocationType.Virtual],
});
return result.data;
},
enabled: item.isSuccess && item.data.Type === BaseItemKind.Person,
networkMode: "always",
});
const personPhotos = useQuery({
queryKey: ["item", id, "personPhotos"],
queryFn: async () => {
const result = await getItemsApi(api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Photo],
recursive: true,
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
excludeLocationTypes: [LocationType.Virtual],
});
return result.data;
},
enabled: item.isSuccess && item.data.Type === BaseItemKind.Person,
networkMode: "always",
});
const personEpisodes = useQuery({
queryKey: ["item", id, "personEpisodes"],
queryFn: async () => {
const result = await getItemsApi(api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Episode],
recursive: true,
fields: ["SeasonUserData", "Overview"],
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
excludeLocationTypes: [LocationType.Virtual],
limit: 24,
});
return result.data;
},
enabled: item.isSuccess && item.data.Type === BaseItemKind.Person,
networkMode: "always",
});
const [activePersonTab, setActivePersonTab] = useState(0);
const personTabs = [
{
title: "Movies",
data: personMovies,
queryKey: ["item", id, "personMovies"],
},
{
title: "TV Shows",
data: personShows,
queryKey: ["item", id, "personShows"],
},
{
title: "Books",
data: personBooks,
queryKey: ["item", id, "personBooks"],
},
{
title: "Photos",
data: personPhotos,
queryKey: ["item", id, "personPhotos"],
},
{
title: "Episodes",
data: personEpisodes,
queryKey: ["item", id, "personEpisodes"],
},
];
useLayoutEffect(() => {
if (
personMovies.isSuccess &&
personShows.isSuccess &&
personBooks.isSuccess &&
personPhotos.isSuccess &&
personEpisodes.isSuccess
) {
if (personMovies.data.TotalRecordCount !== 0) {
setActivePersonTab(0);
} else if (personShows.data.TotalRecordCount !== 0) {
setActivePersonTab(1);
} else if (personBooks.data.TotalRecordCount !== 0) {
setActivePersonTab(2);
} else if (personPhotos.data.TotalRecordCount !== 0) {
setActivePersonTab(3);
} else if (personEpisodes.data.TotalRecordCount !== 0) {
setActivePersonTab(4);
}
}
setBackdrop("", "");
}, [
personMovies.isSuccess,
personShows.isSuccess,
personBooks.isSuccess,
personPhotos.isSuccess,
personEpisodes.isSuccess,
]);
const [animationDirection, setAnimationDirection] = useState("forward");
const pageRef = useRef(null);
const { scrollYProgress } = useScroll({
target: pageRef,
offset: ["start start", "60vh start"],
});
const parallax = useParallax(scrollYProgress, 50);
if (item.isPending) {
return (
<Box
sx={{
width: "100%",
height: "100vh",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<CircularProgress />
</Box>
);
}
if (item.isSuccess) {
return (
<motion.div
key={id}
initial={{
opacity: 0,
}}
animate={{
opacity: 1,
}}
transition={{
duration: 0.25,
ease: "easeInOut",
}}
className="scrollY padded-top item item-person flex flex-column"
ref={pageRef}
>
<div className="item-hero">
<div className="item-hero-backdrop-container">
<motion.img
alt={item.data.Name}
src={meshBg}
className="item-hero-backdrop"
onLoad={(e) => {
e.currentTarget.style.opacity = 1;
}}
style={{
y: parallax,
}}
/>
</div>
<div
className="item-hero-image-container"
style={{
aspectRatio: item.data.PrimaryImageAspectRatio ?? 1,
}}
>
{Object.keys(item.data.ImageTags).includes("Primary") ? (
<>
<Blurhash
hash={
item.data.ImageBlurHashes.Primary[
item.data.ImageTags.Primary
]
}
className="item-hero-image-blurhash"
/>
<img
alt={item.data.Name}
src={api.getItemImageUrl(item.data.Id, "Primary", {
quality: 90,
tag: item.data.ImageTags.Primary,
})}
onLoad={(e) => {
e.currentTarget.style.opacity = 1;
}}
className="item-hero-image"
/>
</>
) : (
<></>
)}
</div>
<div className="item-hero-detail flex flex-column">
<Typography variant="h2" fontWeight={200} mb={2}>
{item.data.Name}
</Typography>
<LikeButton
itemName={item.data.Name}
itemId={item.data.Id}
queryKey={["item", id]}
isFavorite={item.data.UserData.IsFavorite}
userId={user.data.Id}
/>
</div>
</div>
<div className="item-detail">
<div style={{ width: "100%" }}>
<ShowMoreText
content={item.data.Overview ?? ""}
collapsedLines={4}
/>
<div
style={{
display: "flex",
gap: "0.6em",
alignSelf: "end",
marginTop: "1em",
}}
>
{item.data.ExternalUrls.map((url) => (
<IconLink url={url.Url} name={url.Name} />
))}
</div>
</div>
<div
style={{
width: "100%",
}}
>
{item.data.PremiereDate && (
<>
<Typography variant="h6">Birth</Typography>
<Typography variant="subtitle1" sx={{ opacity: 0.8 }}>
{new Date(item.data.PremiereDate).toDateString()}
</Typography>
</>
)}
{item.data.EndDate && (
<>
<Typography variant="h6" mt={2}>
Death
</Typography>
<Typography variant="subtitle2" sx={{ opacity: 0.8 }}>
{new Date(item.data.EndDate).toDateString()}
</Typography>
</>
)}
</div>
</div>
<div className="item-detail-person-container">
<div className="item-detail-person-header">
<Tabs
variant="scrollable"
value={activePersonTab}
onChange={(e, newVal) => {
if (newVal > activePersonTab) {
setAnimationDirection("forward");
} else if (newVal < activePersonTab) {
setAnimationDirection("backwards");
}
setActivePersonTab(newVal);
}}
>
{personTabs.map((tab) => {
return (
<Tab
key={tab.title}
label={tab.title}
disabled={tab.data.data?.TotalRecordCount === 0}
/>
);
})}
</Tabs>
<Divider />
</div>
<AnimatePresence>
{personTabs.map((tab, index) => {
return (
<TabPanel value={activePersonTab} index={index} key={tab.title}>
<motion.div
className={`item-detail-person-cards ${
tab.title === "Movies" ||
tab.title === "TV Shows" ||
tab.title === "Books"
? "col-7"
: "col-4"
}`}
key={tab.queryKey}
initial={{
opacity: 0,
transform:
animationDirection === "forward"
? "translate(30px)"
: "translate(-30px)",
}}
animate={{
opacity: 1,
transform: "translate(0px)",
}}
transition={{
duration: 0.2,
ease: "anticipate",
}}
>
{tab.data.isSuccess &&
tab.data.data.Items.map((tabitem, index) => {
return (
<Card
key={tabitem.Id}
item={tabitem}
cardTitle={
tabitem.Type === BaseItemKind.Episode
? tabitem.SeriesName
: tabitem.Name
}
imageType={"Primary"}
cardCaption={
tabitem.Type === BaseItemKind.Episode
? `S${tabitem.ParentIndexNumber}:E${tabitem.IndexNumber} - ${tabitem.Name}`
: tabitem.Type === BaseItemKind.Series
? `${tabitem.ProductionYear} - ${
tabitem.EndDate
? new Date(
tabitem.EndDate,
).toLocaleString([], {
year: "numeric",
})
: "Present"
}`
: tabitem.ProductionYear
}
cardType={
tabitem.Type === BaseItemKind.Episode
? "thumb"
: "portrait"
}
queryKey={tab.queryKey}
userId={user.data.Id}
imageBlurhash={
!!tabitem.ImageBlurHashes?.Primary &&
tabitem.ImageBlurHashes?.Primary[
Object.keys(tabitem.ImageBlurHashes.Primary)[0]
]
}
/>
);
})}
</motion.div>
{tab.data.isSuccess &&
tab.data.data.TotalRecordCount > 24 && (
<Typography
variant="h6"
style={{
opacity: 0.8,
}}
>
And more...
</Typography>
)}
</TabPanel>
);
})}
</AnimatePresence>
</div>
</motion.div>
);
}
if (item.isError) {
return <ErrorNotice />;
}
}
export default PersonTitlePage;
+184
View File
@@ -0,0 +1,184 @@
$cardWidth: 18%;
.item-detail-person {
&-container {
position: relative;
width: 100%;
}
&-header {
width: 100%;
}
&-cards {
display: grid;
align-items: start;
}
}
.col-7 {
grid-template-columns: repeat(7, minmax(0, 1fr));
}
.col-4 {
grid-template-columns: repeat(4, minmax(0, 1fr));
}
.item-person.item {
gap: 1em;
.item-hero {
height: 50vh;
gap: 2em;
row-gap: 1.2em;
position: relative;
align-items: flex-end;
margin-bottom: 1em;
display: grid;
grid-template-columns: $cardWidth 1fr;
grid-template-rows: 1fr auto;
&-image {
opacity: 0;
width: 100%;
object-fit: contain;
transition: opacity $transition-time-default;
&-container {
height: fit-content;
max-height: 100%;
overflow: hidden;
border-radius: $border-radius-default;
box-shadow: $shadow-card;
flex-grow: 1;
flex-shrink: 0;
position: relative;
width: 100%;
}
&-blurhash {
position: absolute !important;
width: 100% !important;
height: 100% !important;
z-index: -1;
}
&-icon {
background: linear-gradient(45deg, rgb(255 255 255 / 0.05), rgb(255 255 255 / 0.15));
height: 100%;
display: flex;
align-items: center;
justify-content: center;
.material-symbols-rounded{
font-size: 4em;
}
}
}
&-logo {
max-width: 34rem;
max-height: 10rem;
opacity: 0;
transition: opacity $transition-time-default;
object-position: bottom;
margin-bottom: 2em;
}
&-detail {
width: 100%;
align-items: flex-start;
gap: 0.2em;
justify-content: end;
}
&-buttons {
&-container {
align-items: center;
justify-content: space-between;
width: 100%;
grid-column: 1/3;
display: grid;
grid-template-columns: inherit;
gap: 2em;
}
}
&-backdrop {
width: 100vw;
height: 100vh;
object-fit: cover;
object-position: top;
opacity: 0;
transition: opacity $transition-time-default;
&-container {
filter: brightness(0.8);
position: absolute;
top: -4.4em;
left: -$page-margin;
width: 100vw;
height: calc(44vh + 4.4em);
z-index: -1;
mask-image: linear-gradient(to top, transparent, black);
-webkit-mask-image: linear-gradient(
to top,
transparent,
black
);
}
}
& .play-button {
width: 100% !important;
}
}
.item-detail {
display: grid;
grid-template-columns: 1fr 35%;
justify-items: center;
align-items: start;
gap: 3em;
&-cast {
&-container {
display: flex;
flex-direction: column;
gap: 1em;
}
&-grid {
display: grid;
grid-template-columns: repeat(5, minmax(0, 1fr));
gap: 1em;
margin-bottom: 1em;
}
&-card {
display: grid;
grid-template-columns: 6em 1fr;
gap: 0.5em;
align-items: center;
color: white !important;
text-decoration: none;
padding: 0.5em;
background: rgb(255 255 255 / 0);
transition: background $transition-time-default;
border-radius: $border-radius-default;
&:hover {
background: rgb(255 255 255 / 0.1);
}
&-image,
&-icon {
width: 6em;
height: 6em;
object-fit: cover;
border-radius: 100%;
box-shadow: $shadow-card-image;
}
&-icon {
display: flex;
align-items: center;
justify-content: center;
font-size: 1em;
background: rgb(255 255 255 / 0.1);
.material-symbols-rounded {
font-size: 3em;
}
}
}
}
&-link {
color: white;
text-decoration-color: rgb(255 255 255 / 0.5);
&:hover {
text-decoration-color: white;
}
}
}
}