diff --git a/src/components/Library/ScanSet.tsx b/src/components/Library/ScanSet.tsx index e2e14d5..7f0c555 100644 --- a/src/components/Library/ScanSet.tsx +++ b/src/components/Library/ScanSet.tsx @@ -9,13 +9,10 @@ import { getAssetFilename, getAssetURL, ImageQuality } from "helpers/img"; import { isInteger } from "helpers/numbers"; import { getStatusDescription } from "helpers/others"; import { Immutable } from "helpers/types"; -import useSmartLanguage from "hooks/useSmartLanguage"; -import { Dispatch, SetStateAction } from "react"; +import { useSmartLanguage } from "hooks/useSmartLanguage"; interface Props { - setLightboxOpen: Dispatch>; - setLightboxImages: Dispatch>; - setLightboxIndex: Dispatch>; + openLightBox: (images: string[], index?: number) => void; scanSet: Exclude< Exclude< Exclude< @@ -52,17 +49,8 @@ interface Props { } export default function ScanSet(props: Immutable): JSX.Element { - const { - setLightboxOpen, - setLightboxImages, - setLightboxIndex, - scanSet, - slug, - title, - languages, - langui, - content, - } = props; + const { openLightBox, scanSet, slug, title, languages, langui, content } = + props; const [selectedScan, LanguageSwitcher] = useSmartLanguage({ items: scanSet, @@ -219,9 +207,7 @@ export default function ScanSet(props: Immutable): JSX.Element { getAssetURL(image.attributes.url, ImageQuality.Large) ); }); - setLightboxOpen(true); - setLightboxImages(images); - setLightboxIndex(index); + openLightBox(images, index); }} > {page.attributes && ( diff --git a/src/components/Library/ScanSetCover.tsx b/src/components/Library/ScanSetCover.tsx index c75f0fa..e66e3a4 100644 --- a/src/components/Library/ScanSetCover.tsx +++ b/src/components/Library/ScanSetCover.tsx @@ -10,13 +10,10 @@ import { AppStaticProps } from "graphql/getAppStaticProps"; import { getAssetURL, ImageQuality } from "helpers/img"; import { getStatusDescription } from "helpers/others"; import { Immutable } from "helpers/types"; -import useSmartLanguage from "hooks/useSmartLanguage"; -import { Dispatch, SetStateAction } from "react"; +import { useSmartLanguage } from "hooks/useSmartLanguage"; interface Props { - setLightboxOpen: Dispatch>; - setLightboxImages: Dispatch>; - setLightboxIndex: Dispatch>; + openLightBox: (images: string[], index?: number) => void; images: Exclude< Exclude< Exclude< @@ -32,14 +29,7 @@ interface Props { } export default function ScanSetCover(props: Immutable): JSX.Element { - const { - setLightboxOpen, - setLightboxImages, - setLightboxIndex, - images, - languages, - langui, - } = props; + const { openLightBox, images, languages, langui } = props; const [selectedScan, LanguageSwitcher] = useSmartLanguage({ items: images, @@ -170,9 +160,7 @@ export default function ScanSetCover(props: Immutable): JSX.Element { if (img.url) imgs.push(getAssetURL(img.url, ImageQuality.Large)); }); - setLightboxOpen(true); - setLightboxImages(imgs); - setLightboxIndex(index); + openLightBox(imgs, index); }} > diff --git a/src/components/Markdown/Markdawn.tsx b/src/components/Markdown/Markdawn.tsx index e042c5e..dd0f8d1 100644 --- a/src/components/Markdown/Markdawn.tsx +++ b/src/components/Markdown/Markdawn.tsx @@ -1,15 +1,15 @@ import HorizontalLine from "components/HorizontalLine"; import Img from "components/Img"; import InsetBox from "components/InsetBox"; -import LightBox from "components/LightBox"; import ToolTip from "components/ToolTip"; import { useAppLayout } from "contexts/AppLayoutContext"; import { slugify } from "helpers/formatters"; import { getAssetURL, ImageQuality } from "helpers/img"; import { Immutable } from "helpers/types"; +import { useLightBox } from "hooks/useLightBox"; import Markdown from "markdown-to-jsx"; import { useRouter } from "next/router"; -import React, { useState } from "react"; +import React from "react"; import ReactDOMServer from "react-dom/server"; interface Props { @@ -23,20 +23,12 @@ export default function Markdawn(props: Immutable): JSX.Element { const router = useRouter(); - const [lightboxOpen, setLightboxOpen] = useState(false); - const [lightboxImages, setLightboxImages] = useState([""]); - const [lightboxIndex, setLightboxIndex] = useState(0); + const [openLightBox, LightBox] = useLightBox(); if (text) { return ( <> - + ): JSX.Element {
{ - setLightboxOpen(true); - setLightboxImages([ + openLightBox([ compProps.src.startsWith("/uploads/") ? getAssetURL(compProps.src, ImageQuality.Large) : compProps.src, ]); - setLightboxIndex(0); }} > ): JSX.Element { languageSwitcher, } = props; + const [openLightBox, LightBox] = useLightBox(); + return ( <> +
{thumbnail ? ( { + openLightBox([getAssetURL(thumbnail.url, ImageQuality.Large)]); + }} /> ) : (
diff --git a/src/hooks/useLightBox.tsx b/src/hooks/useLightBox.tsx new file mode 100644 index 0000000..4bf9303 --- /dev/null +++ b/src/hooks/useLightBox.tsx @@ -0,0 +1,28 @@ +import LightBox from "components/LightBox"; +import { useState } from "react"; + +export function useLightBox(): [ + (images: string[], index?: number) => void, + () => JSX.Element +] { + const [lightboxOpen, setLightboxOpen] = useState(false); + const [lightboxImages, setLightboxImages] = useState([""]); + const [lightboxIndex, setLightboxIndex] = useState(0); + + return [ + (images: string[], index = 0) => { + setLightboxOpen(true); + setLightboxImages(images); + setLightboxIndex(index); + }, + () => ( + + ), + ]; +} diff --git a/src/hooks/useSmartLanguage.tsx b/src/hooks/useSmartLanguage.tsx index d1bd3e4..735a435 100644 --- a/src/hooks/useSmartLanguage.tsx +++ b/src/hooks/useSmartLanguage.tsx @@ -24,7 +24,7 @@ function getPreferredLanguage( return undefined; } -export default function useSmartLanguage( +export function useSmartLanguage( props: Props ): [Immutable, () => JSX.Element] { const { diff --git a/src/pages/contents/[slug]/index.tsx b/src/pages/contents/[slug]/index.tsx index edf8c0f..564fc23 100644 --- a/src/pages/contents/[slug]/index.tsx +++ b/src/pages/contents/[slug]/index.tsx @@ -23,7 +23,7 @@ import { import { getStatusDescription } from "helpers/others"; import { Immutable } from "helpers/types"; import { useMediaMobile } from "hooks/useMediaQuery"; -import useSmartLanguage from "hooks/useSmartLanguage"; +import { useSmartLanguage } from "hooks/useSmartLanguage"; import { GetStaticPathsContext, GetStaticPathsResult, diff --git a/src/pages/library/[slug]/index.tsx b/src/pages/library/[slug]/index.tsx index 9e17ecc..eb50c12 100644 --- a/src/pages/library/[slug]/index.tsx +++ b/src/pages/library/[slug]/index.tsx @@ -5,7 +5,6 @@ import Button from "components/Inputs/Button"; import Switch from "components/Inputs/Switch"; import InsetBox from "components/InsetBox"; import ContentLine from "components/Library/ContentLine"; -import LightBox from "components/LightBox"; import NavOption from "components/PanelComponents/NavOption"; import ReturnButton, { ReturnButtonType, @@ -35,6 +34,7 @@ import { getAssetURL, ImageQuality } from "helpers/img"; import { convertMmToInch } from "helpers/numbers"; import { sortContent } from "helpers/others"; import { Immutable } from "helpers/types"; +import { useLightBox } from "hooks/useLightBox"; import { GetStaticPathsContext, GetStaticPathsResult, @@ -63,10 +63,7 @@ export default function LibrarySlug(props: Immutable): JSX.Element { sortContent(item?.contents); - const [lightboxOpen, setLightboxOpen] = useState(false); - const [lightboxImages, setLightboxImages] = useState([""]); - const [lightboxIndex, setLightboxIndex] = useState(0); - + const [openLightBox, LightBox] = useLightBox(); const [keepInfoVisible, setKeepInfoVisible] = useState(false); let displayOpenScans = false; @@ -136,13 +133,7 @@ export default function LibrarySlug(props: Immutable): JSX.Element { const contentPanel = ( - + ): JSX.Element { mobile:h-[60vh] desktop:mb-16 relative cursor-pointer" onClick={() => { if (item?.thumbnail?.data?.attributes) { - setLightboxOpen(true); - setLightboxImages([ + openLightBox([ getAssetURL( item.thumbnail.data.attributes.url, ImageQuality.Large ), ]); - setLightboxIndex(0); } }} > @@ -262,9 +251,7 @@ export default function LibrarySlug(props: Immutable): JSX.Element { ) ); }); - setLightboxOpen(true); - setLightboxImages(images); - setLightboxIndex(index); + openLightBox(images, index); } }} > diff --git a/src/pages/library/[slug]/scans.tsx b/src/pages/library/[slug]/scans.tsx index 6ae8277..91e9704 100644 --- a/src/pages/library/[slug]/scans.tsx +++ b/src/pages/library/[slug]/scans.tsx @@ -1,7 +1,6 @@ import AppLayout from "components/AppLayout"; import ScanSet from "components/Library/ScanSet"; import ScanSetCover from "components/Library/ScanSetCover"; -import LightBox from "components/LightBox"; import NavOption from "components/PanelComponents/NavOption"; import ReturnButton, { ReturnButtonType, @@ -17,12 +16,12 @@ import { getReadySdk } from "graphql/sdk"; import { prettyinlineTitle, prettySlug } from "helpers/formatters"; import { sortContent } from "helpers/others"; import { Immutable } from "helpers/types"; +import { useLightBox } from "hooks/useLightBox"; import { GetStaticPathsContext, GetStaticPathsResult, GetStaticPropsContext, } from "next"; -import { useState } from "react"; interface Props extends AppStaticProps { item: Exclude< @@ -41,9 +40,7 @@ export default function LibrarySlug(props: Immutable): JSX.Element { sortContent(item?.contents); - const [lightboxOpen, setLightboxOpen] = useState(false); - const [lightboxImages, setLightboxImages] = useState([""]); - const [lightboxIndex, setLightboxIndex] = useState(0); + const [openLightBox, LightBox] = useLightBox(); const subPanel = ( @@ -77,13 +74,7 @@ export default function LibrarySlug(props: Immutable): JSX.Element { const contentPanel = ( - + ): JSX.Element { {item?.images && ( @@ -110,9 +99,7 @@ export default function LibrarySlug(props: Immutable): JSX.Element {