diff --git a/src/components/LanguageSwitcher.tsx b/src/components/LanguageSwitcher.tsx index 42ad095..6f853c7 100644 --- a/src/components/LanguageSwitcher.tsx +++ b/src/components/LanguageSwitcher.tsx @@ -13,12 +13,12 @@ interface Props { } export default function LanguageSwitcher(props: Props): JSX.Element { - const { locales, localesIndex, setLocalesIndex } = props; + const { locales, className, localesIndex, setLocalesIndex } = props; return ( +
{[...locales].map(([locale, value], index) => ( <> {locale && ( @@ -35,7 +35,7 @@ export default function LanguageSwitcher(props: Props): JSX.Element {
} > -
diff --git a/src/components/Library/ScanSet.tsx b/src/components/Library/ScanSet.tsx new file mode 100644 index 0000000..4bf255d --- /dev/null +++ b/src/components/Library/ScanSet.tsx @@ -0,0 +1,118 @@ +import Img, { getAssetURL, ImageQuality } from "components/Img"; +import LanguageSwitcher from "components/LanguageSwitcher"; +import { useAppLayout } from "contexts/AppLayoutContext"; +import { GetLibraryItemScansQuery } from "graphql/generated"; +import { useRouter } from "next/router"; +import { AppStaticProps } from "queries/getAppStaticProps"; +import { getPreferredLanguage } from "queries/helpers"; +import { Dispatch, SetStateAction, useEffect, useMemo, useState } from "react"; + +interface Props { + setLightboxOpen: Dispatch>; + setLightboxImages: Dispatch>; + setLightboxIndex: Dispatch>; + scanSet: Exclude< + Exclude< + Exclude< + Exclude< + Exclude< + GetLibraryItemScansQuery["libraryItems"], + null | undefined + >["data"][number]["attributes"], + null | undefined + >["contents"], + null | undefined + >["data"][number]["attributes"], + null | undefined + >["scan_set"], + null | undefined + >; + slug: string; + title: string; + languages: AppStaticProps["languages"]; +} + +export default function ScanSet(props: Props): JSX.Element { + const { + setLightboxOpen, + setLightboxImages, + setLightboxIndex, + scanSet, + slug, + title, + languages, + } = props; + const appLayout = useAppLayout(); + const router = useRouter(); + + const [selectedScan, setSelectedScan] = useState(); + const scanLocales: Map = new Map(); + + const [selectedScanIndex, setSelectedScanIndex] = useState< + number | undefined + >(); + + scanSet.map((scan, index) => { + if (scan?.language?.data?.attributes?.code) { + scanLocales.set(scan.language.data.attributes.code, index); + } + }); + + useMemo(() => { + setSelectedScanIndex( + getPreferredLanguage( + appLayout.preferredLanguages ?? [router.locale], + scanLocales + ) + ); + }, [appLayout.preferredLanguages]); + + useEffect(() => { + if (selectedScanIndex !== undefined) + setSelectedScan(scanSet[selectedScanIndex]); + }, [selectedScanIndex]); + + return ( +
+
+

+ {title} +

+ +
+ +
+ {selectedScan?.pages?.data.map((page, index) => ( +
{ + const images: string[] = []; + selectedScan.pages?.data.map((image) => { + if (image.attributes?.url) + images.push( + getAssetURL(image.attributes.url, ImageQuality.Large) + ); + }); + setLightboxOpen(true); + setLightboxImages(images); + setLightboxIndex(index); + }} + > + {page.attributes && ( + + )} +
+ ))} +
+
+ ); +} diff --git a/src/pages/library/[slug]/scans.tsx b/src/pages/library/[slug]/scans.tsx index b2a88f1..08c02e9 100644 --- a/src/pages/library/[slug]/scans.tsx +++ b/src/pages/library/[slug]/scans.tsx @@ -1,6 +1,5 @@ import AppLayout from "components/AppLayout"; -import Img, { getAssetURL, ImageQuality } from "components/Img"; -import LanguageSwitcher from "components/LanguageSwitcher"; +import ScanSet from "components/Library/ScanSet"; import LightBox from "components/LightBox"; import NavOption from "components/PanelComponents/NavOption"; import ReturnButton, { @@ -34,7 +33,7 @@ interface Props extends AppStaticProps { } export default function LibrarySlug(props: Props): JSX.Element { - const { item, langui } = props; + const { item, langui, languages } = props; const appLayout = useAppLayout(); sortContent(item?.contents); @@ -90,61 +89,16 @@ export default function LibrarySlug(props: Props): JSX.Element { /> {item?.contents?.data.map((content) => ( <> -

- {prettySlug(content.attributes?.slug, item.slug)} -

- - {content.attributes?.scan_set?.[0] ? ( -
- {content.attributes.scan_set[0].pages?.data.map((page, index) => ( -
{ - setLightboxOpen(true); - if (content.attributes?.scan_set?.[0]?.pages) { - const images: string[] = []; - content.attributes.scan_set[0].pages.data.map((image) => { - if (image.attributes?.url) - images.push( - getAssetURL( - image.attributes.url, - ImageQuality.Large - ) - ); - }); - setLightboxImages(images); - } - - setLightboxIndex(index); - }} - > - {page.attributes && ( - - )} -
- ))} -
- ) : ( -
- {content.attributes?.scan_set_languages && ( - language?.language?.data?.attributes?.code - )} - languages={props.languages} - langui={props.langui} - href={`#${content.attributes.slug}`} - /> - )} -
+ {content.attributes?.scan_set?.[0] && ( + )} ))}