import { Fragment, useCallback, useMemo } from "react"; import { Chip } from "components/Chip"; import { Img } from "components/Img"; import { Button } from "components/Inputs/Button"; import { RecorderChip } from "components/RecorderChip"; import { ToolTip } from "components/ToolTip"; import { GetLibraryItemScansQuery } from "graphql/generated"; import { AppStaticProps } from "graphql/getAppStaticProps"; import { getAssetFilename, getAssetURL, ImageQuality } from "helpers/img"; import { isInteger } from "helpers/numbers"; import { filterHasAttributes, getStatusDescription, isDefined, isDefinedAndNotEmpty, } from "helpers/others"; import { useSmartLanguage } from "hooks/useSmartLanguage"; /* * ╭─────────────╮ * ───────────────────────────────────────╯ COMPONENT ╰─────────────────────────────────────────── */ interface Props { openLightBox: (images: string[], index?: number) => void; scanSet: NonNullable< NonNullable< NonNullable< NonNullable< NonNullable< GetLibraryItemScansQuery["libraryItems"] >["data"][number]["attributes"] >["contents"] >["data"][number]["attributes"] >["scan_set"] >; id: string; title: string; languages: AppStaticProps["languages"]; langui: AppStaticProps["langui"]; content: NonNullable< NonNullable< NonNullable< NonNullable< GetLibraryItemScansQuery["libraryItems"] >["data"][number]["attributes"] >["contents"] >["data"][number]["attributes"] >["content"]; } // ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ export const ScanSet = ({ openLightBox, scanSet, id, title, languages, langui, content, }: Props): JSX.Element => { const [selectedScan, LanguageSwitcher, languageSwitcherProps] = useSmartLanguage({ items: scanSet, languages: languages, languageExtractor: useCallback( (item: NonNullable) => item.language?.data?.attributes?.code, [] ), transform: useCallback((item: NonNullable) => { item.pages?.data.sort((a, b) => { if ( a.attributes && b.attributes && isDefinedAndNotEmpty(a.attributes.url) && isDefinedAndNotEmpty(b.attributes.url) ) { let aName = getAssetFilename(a.attributes.url); let bName = getAssetFilename(b.attributes.url); /* * If the number is a succession of 0s, make the number * incrementally smaller than 0 (i.e: 00 becomes -1) */ if (aName.replaceAll("0", "").length === 0) { aName = (1 - aName.length).toString(10); } if (bName.replaceAll("0", "").length === 0) { bName = (1 - bName.length).toString(10); } if (isInteger(aName) && isInteger(bName)) { return parseInt(aName, 10) - parseInt(bName, 10); } return a.attributes.url.localeCompare(b.attributes.url); } return 0; }); return item; }, []), }); const pages = useMemo( () => filterHasAttributes(selectedScan?.pages?.data, ["attributes"]), [selectedScan] ); return ( <> {selectedScan && isDefined(pages) && (

{title}

{content?.data?.attributes && isDefinedAndNotEmpty(content.data.attributes.slug) && (
{pages.map((page, index) => (
{ const images = pages.map((image) => getAssetURL(image.attributes.url, ImageQuality.Large) ); openLightBox(images, index); }} >
))}
)} ); };