From 0467813c2c14b0e57eef4e9a489872d2f9c3de7e Mon Sep 17 00:00:00 2001 From: DrMint Date: Sun, 6 Mar 2022 03:44:53 +0100 Subject: [PATCH] Use state instead of anchors for Content TOC in Library Item --- src/components/Library/ContentTOCLine.tsx | 108 ++++++++++++++++++++++ src/pages/library/[slug].tsx | 89 ++---------------- 2 files changed, 114 insertions(+), 83 deletions(-) create mode 100644 src/components/Library/ContentTOCLine.tsx diff --git a/src/components/Library/ContentTOCLine.tsx b/src/components/Library/ContentTOCLine.tsx new file mode 100644 index 0000000..9001184 --- /dev/null +++ b/src/components/Library/ContentTOCLine.tsx @@ -0,0 +1,108 @@ +import Button from "components/Button"; +import Chip from "components/Chip"; +import { + GetLibraryItemQuery, + GetWebsiteInterfaceQuery, +} from "graphql/operations-types"; +import { prettyinlineTitle, prettySlug } from "queries/helpers"; +import { useState } from "react"; + +type ContentTOCLineProps = { + content: GetLibraryItemQuery["libraryItems"]["data"][number]["attributes"]["contents"]["data"][number]; + parentSlug: string; + langui: GetWebsiteInterfaceQuery["websiteInterfaces"]["data"][number]["attributes"]; +}; + +export default function ContentTOCLine( + props: ContentTOCLineProps +): JSX.Element { + const content = props.content; + const langui = props.langui; + + const [opened, setOpened] = useState(false); + + return ( +
+
+ +

setOpened(!opened)}> + {content.attributes.content.data && + content.attributes.content.data.attributes.titles.length > 0 + ? prettyinlineTitle( + content.attributes.content.data.attributes.titles[0] + .pre_title, + content.attributes.content.data.attributes.titles[0].title, + content.attributes.content.data.attributes.titles[0].subtitle + ) + : prettySlug(content.attributes.slug, props.parentSlug)} +

+
+
+ {content.attributes.content.data?.attributes.categories.data.map( + (category) => ( + {category.attributes.short} + ) + )} +
+

+

+ {content.attributes.range[0].__typename === "ComponentRangePageRange" + ? content.attributes.range[0].starting_page + : ""} +

+ {content.attributes.content.data ? ( + + {content.attributes.content.data.attributes.type.data.attributes + .titles.length > 0 + ? content.attributes.content.data.attributes.type.data.attributes + .titles[0].title + : prettySlug( + content.attributes.content.data.attributes.type.data + .attributes.slug + )} + + ) : ( + "" + )} +
+
+ + subdirectory_arrow_right + + + {content.attributes.scan_set.length > 0 ? ( + + ) : ( + "" + )} + + {content.attributes.content.data ? ( + + ) : ( + "" + )} + + {content.attributes.scan_set.length === 0 && + !content.attributes.content.data + ? "The content is not available" + : ""} +
+
+ ); +} diff --git a/src/pages/library/[slug].tsx b/src/pages/library/[slug].tsx index 8261f4d..01f1c0c 100644 --- a/src/pages/library/[slug].tsx +++ b/src/pages/library/[slug].tsx @@ -37,6 +37,7 @@ import InsetBox from "components/InsetBox"; import Img, { ImageQuality } from "components/Img"; import { useAppLayout } from "contexts/AppLayoutContext"; import { useRouter } from "next/router"; +import ContentTOCLine from "components/Library/ContentTOCLine"; interface LibrarySlugProps { libraryItem: GetLibraryItemQuery; @@ -347,90 +348,12 @@ export default function LibrarySlug(props: LibrarySlugProps): JSX.Element {

{langui.contents}

{item.contents.data.map((content) => ( -
-
- -

- {content.attributes.content.data && - content.attributes.content.data.attributes.titles - .length > 0 - ? prettyinlineTitle( - content.attributes.content.data.attributes - .titles[0].pre_title, - content.attributes.content.data.attributes - .titles[0].title, - content.attributes.content.data.attributes - .titles[0].subtitle - ) - : prettySlug(content.attributes.slug, item.slug)} -

-
-
- {content.attributes.content.data?.attributes.categories.data.map( - (category) => ( - - {category.attributes.short} - - ) - )} -
-

-

- {content.attributes.range[0].__typename === - "ComponentRangePageRange" - ? content.attributes.range[0].starting_page - : ""} -

- {content.attributes.content.data ? ( - - {content.attributes.content.data.attributes.type.data - .attributes.titles.length > 0 - ? content.attributes.content.data.attributes.type.data - .attributes.titles[0].title - : prettySlug( - content.attributes.content.data.attributes.type - .data.attributes.slug - )} - - ) : ( - "" - )} -
-
- - subdirectory_arrow_right - - - {content.attributes.scan_set.length > 0 ? ( - - ) : ( - "" - )} - - {content.attributes.content.data ? ( - - ) : ( - "" - )} - - {content.attributes.scan_set.length === 0 && - !content.attributes.content.data - ? "The content is not available" - : ""} -
-
+ /> ))}