import React, {
useState,
useEffect,
useLayoutEffect,
useRef,
type ReactNode,
} 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,
SortOrder,
} from "@jellyfin/sdk/lib/generated-client";
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
import { useQuery } from "@tanstack/react-query";
import LikeButton from "@/components/buttons/likeButton";
import { Card } from "@/components/card/card";
import { Blurhash } from "react-blurhash";
import { ArtistAlbum } from "@/components/layouts/artist/artistAlbum";
import MusicTrack from "@/components/musicTrack";
import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice";
import ShowMoreText from "@/components/showMoreText";
import { useBackdropStore } from "@/utils/store/backdrop";
import "./artist.scss";
import IconLink from "@/components/iconLink";
import { getTypeIcon } from "@/components/utils/iconsCollection";
import getImageUrlsApi from "@/utils/methods/getImageUrlsApi";
import { useCentralStore } from "@/utils/store/central";
import { createFileRoute } from "@tanstack/react-router";
type TabPanelProp = {
children: ReactNode;
index: number;
value: number;
};
function TabPanel(props: TabPanelProp) {
const { children, value, index, ...other } = props;
return (
{value === index && {children}}
);
}
export const Route = createFileRoute("/_api/artist/$id")({
component: ArtistTitlePage,
});
function ArtistTitlePage() {
const { id } = Route.useParams();
const api = Route.useRouteContext().api;
const user = useCentralStore((s) => s.currentUser);
const item = useQuery({
queryKey: ["item", id],
queryFn: async () => {
if (!api) return null;
const result = await getUserLibraryApi(api).getItem({
userId: user?.Id,
itemId: id,
});
return result.data;
},
enabled: !!user?.Id,
networkMode: "always",
refetchOnWindowFocus: true,
});
const artistDiscography = useQuery({
queryKey: ["item", id, "artist", "discography"],
queryFn: async () => {
if (!api) return null;
const result = await getItemsApi(api).getItems({
albumArtistIds: [item.data?.Id ?? ""],
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
sortOrder: [SortOrder.Descending],
recursive: true,
includeItemTypes: [BaseItemKind.MusicAlbum],
userId: user?.Id,
fields: [ItemFields.Overview],
});
return result.data;
},
enabled: item.isSuccess && item.data?.Type === BaseItemKind.MusicArtist,
});
const artistSongs = useQuery({
queryKey: ["item", id, "artist", "songs"],
queryFn: async () => {
if (!api) return null;
const result = await getItemsApi(api).getItems({
artistIds: [item.data?.Id ?? ""],
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
sortOrder: [SortOrder.Ascending],
recursive: true,
includeItemTypes: [BaseItemKind.Audio],
userId: user?.Id,
fields: [ItemFields.Overview],
});
return result.data;
},
enabled: item.isSuccess && item.data?.Type === BaseItemKind.MusicArtist,
});
const artistAppearances = useQuery({
queryKey: ["item", id, "artist", "appearences"],
queryFn: async () => {
if (!api) return null;
const result = await getItemsApi(api).getItems({
contributingArtistIds: [item.data?.Id ?? ""],
excludeItemIds: [item.data?.Id ?? ""],
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
sortOrder: [SortOrder.Descending],
recursive: true,
includeItemTypes: [BaseItemKind.MusicAlbum],
userId: user?.Id,
});
return result.data;
},
enabled: item.isSuccess && item.data?.Type === BaseItemKind.MusicArtist,
});
const artistTabs = [
{ title: "Discography", data: artistDiscography },
{ title: "Songs", data: artistSongs },
{ title: "Appearances", data: artistAppearances },
];
const [activeArtistTab, setActiveArtistTab] = useState(0);
useLayoutEffect(() => {
if (
artistDiscography.isSuccess &&
artistSongs.isSuccess &&
artistAppearances.isSuccess &&
artistDiscography.data &&
artistSongs.data &&
artistAppearances.data
) {
if (artistDiscography.data.TotalRecordCount !== 0) {
setActiveArtistTab(0);
} else if (artistSongs.data.TotalRecordCount !== 0) {
setActiveArtistTab(1);
} else if (artistAppearances.data.TotalRecordCount !== 0) {
setActiveArtistTab(2);
}
}
}, [
artistDiscography.isSuccess,
artistSongs.isSuccess,
artistAppearances.isSuccess,
]);
const [animationDirection, setAnimationDirection] = useState("forward");
const { setBackdrop } = useBackdropStore();
useEffect(() => {
if (item.isSuccess && item.data) {
setBackdrop(
`${api?.basePath}/Items/${item.data.Id}/Images/Backdrop`,
item.data.Id,
);
}
}, [item.isSuccess]);
const pageRef = useRef(null);
const { scrollYProgress } = useScroll({
target: pageRef,
offset: ["start start", "60vh start"],
});
const parallax = useParallax(scrollYProgress, 50);
if (item.isPending) {
return (
);
}
if (item.isSuccess && item.data) {
return (
{
e.currentTarget.style.opacity = "1";
}}
style={{
y: parallax,
}}
/>
{item.data.ImageTags?.Primary ? (

{
e.currentTarget.style.opacity = "1";
}}
className="item-hero-image"
/>
) : (
{getTypeIcon(item.data.Type ?? "MusicArtist")}
)}
{item.data.ImageTags?.Logo ? (

{
e.currentTarget.style.opacity = "1";
}}
className="item-hero-logo"
/>
) : (
{item.data.Name}
)}
{item.data.ExternalUrls?.map((url) => (
))}
{item.data.PremiereDate && (
Born
{new Date(item.data.PremiereDate).toDateString()}
)}
{item.data.EndDate && (
Death
{new Date(item.data.EndDate).toDateString()}
)}
{
if (newVal > activeArtistTab) {
setAnimationDirection("forward");
} else if (newVal < activeArtistTab) {
setAnimationDirection("backwards");
}
setActiveArtistTab(newVal);
}}
>
{artistTabs.map((tab) => {
return (
);
})}
{user &&
artistDiscography.data?.Items?.map((tabitem) => {
return (
);
})}
{artistSongs.isSuccess &&
artistSongs.data?.Items?.map((tabitem) => {
return (
);
})}
{artistAppearances.isSuccess &&
artistAppearances.data?.Items?.map((tabitem) => {
return (
);
})}
);
}
if (item.isError) {
return ;
}
}
export default ArtistTitlePage;