From 93645a2f5392e8eee98d6a38ba9fec380c594f4a Mon Sep 17 00:00:00 2001 From: DrMint Date: Sat, 13 Aug 2022 13:38:57 +0200 Subject: [PATCH] Contents only have one folder now --- src/components/Translated.tsx | 24 ++++++ src/graphql/operations/getContentText.graphql | 3 +- src/pages/api/revalidate.ts | 9 ++ src/pages/contents/[slug].tsx | 82 ++++++++++--------- 4 files changed, 76 insertions(+), 42 deletions(-) diff --git a/src/components/Translated.tsx b/src/components/Translated.tsx index 7a674d4..8aeb24e 100644 --- a/src/components/Translated.tsx +++ b/src/components/Translated.tsx @@ -5,6 +5,7 @@ import { NavOption } from "./PanelComponents/NavOption"; import { ChroniclePreview } from "./Chronicles/ChroniclePreview"; import { ChroniclesList } from "./Chronicles/ChroniclesList"; import { Button } from "./Inputs/Button"; +import { ReturnButton } from "./PanelComponents/ReturnButton"; import { useSmartLanguage } from "hooks/useSmartLanguage"; import { PreviewFolder } from "pages/contents/folder/[slug]"; @@ -200,3 +201,26 @@ export const TranslatedPreviewFolder = ({ /> ); }; + +// ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ + +export const TranslatedReturnButton = ({ + translations, + fallback, + ...otherProps +}: TranslatedProps< + Parameters[0], + "title" +>): JSX.Element => { + const [selectedTranslation] = useSmartLanguage({ + items: translations, + languageExtractor, + }); + + return ( + + ); +}; diff --git a/src/graphql/operations/getContentText.graphql b/src/graphql/operations/getContentText.graphql index f9258da..9a034a2 100644 --- a/src/graphql/operations/getContentText.graphql +++ b/src/graphql/operations/getContentText.graphql @@ -214,9 +214,8 @@ query getContentText($slug: String, $language_code: String) { } } } - folders(pagination: { limit: -1 }) { + folder { data { - id attributes { slug titles(pagination: { limit: -1 }) { diff --git a/src/pages/api/revalidate.ts b/src/pages/api/revalidate.ts index 2d28d8e..9a5d341 100644 --- a/src/pages/api/revalidate.ts +++ b/src/pages/api/revalidate.ts @@ -35,6 +35,9 @@ type HookContent = { model: "content"; entry: { slug: string; + folder?: { + slug: string; + }; ranged_contents: { slug: string; }[]; @@ -160,9 +163,15 @@ const Revalidate = ( case "content": { paths.push(`/contents`); paths.push(`/contents/${body.entry.slug}`); + if (body.entry.folder?.slug) { + paths.push(`/contents/folder/${body.entry.folder.slug}`); + } serverRuntimeConfig.locales?.forEach((locale: string) => { paths.push(`/${locale}/contents`); paths.push(`/${locale}/contents/${body.entry.slug}`); + if (body.entry.folder?.slug) { + paths.push(`/${locale}/contents/folder/${body.entry.folder.slug}`); + } }); if (body.entry.ranged_contents.length > 0) { body.entry.ranged_contents.forEach((ranged_content) => { diff --git a/src/pages/contents/[slug].tsx b/src/pages/contents/[slug].tsx index 90d90fe..59afd34 100644 --- a/src/pages/contents/[slug].tsx +++ b/src/pages/contents/[slug].tsx @@ -5,10 +5,7 @@ import { Chip } from "components/Chip"; import { HorizontalLine } from "components/HorizontalLine"; import { PreviewCardCTAs } from "components/Library/PreviewCardCTAs"; import { Markdawn, TableOfContents } from "components/Markdown/Markdawn"; -import { - ReturnButton, - ReturnButtonType, -} from "components/PanelComponents/ReturnButton"; +import { ReturnButtonType } from "components/PanelComponents/ReturnButton"; import { ContentPanel } from "components/Panels/ContentPanel"; import { SubPanel } from "components/Panels/SubPanel"; import { PreviewCard } from "components/PreviewCard"; @@ -34,8 +31,8 @@ import { useMediaMobile } from "hooks/useMediaQuery"; import { AnchorIds, useScrollTopOnChange } from "hooks/useScrollTopOnChange"; import { useSmartLanguage } from "hooks/useSmartLanguage"; import { - TranslatedPreviewFolder, TranslatedPreviewLine, + TranslatedReturnButton, } from "components/Translated"; import { getOpenGraph } from "helpers/openGraph"; import { @@ -78,50 +75,56 @@ const Content = ({ const { previousContent, nextContent } = useMemo( () => ({ previousContent: - content.folders?.data[0]?.attributes?.contents && - content.folders.data[0].attributes.sequence + content.folder?.data?.attributes?.contents && + content.folder.data.attributes.sequence ? getPreviousContent( - content.folders.data[0].attributes.contents.data, + content.folder.data.attributes.contents.data, content.slug ) : undefined, nextContent: - content.folders?.data[0]?.attributes?.contents && - content.folders.data[0].attributes.sequence + content.folder?.data?.attributes?.contents && + content.folder.data.attributes.sequence ? getNextContent( - content.folders.data[0].attributes.contents.data, + content.folder.data.attributes.contents.data, content.slug ) : undefined, }), - [content.folders, content.slug] + [content.folder, content.slug] + ); + + const returnButtonProps = useMemo( + () => ({ + href: content.folder?.data?.attributes + ? `/contents/folder/${content.folder.data.attributes.slug}` + : "/contents", + + translations: filterHasAttributes( + content.folder?.data?.attributes?.titles, + ["language.data.attributes.code"] as const + ).map((title) => ({ + language: title.language.data.attributes.code, + title: title.title, + })), + fallback: { + title: content.folder?.data?.attributes + ? prettySlug(content.folder.data.attributes.slug) + : langui.contents, + }, + langui, + }), + [content.folder?.data?.attributes, langui] ); const subPanel = useMemo( () => ( - {content.folders?.data && content.folders.data.length > 0 && ( - <> -

{langui.folders}

- {filterHasAttributes(content.folders.data, [ - "attributes", - "id", - ] as const).map((folder) => ( - ({ - language: title.language.data.attributes.code, - title: title.title, - }))} - fallback={{ title: prettySlug(folder.attributes.slug) }} - /> - ))} - - - )} + {selectedTranslation?.text_set?.source_language?.data?.attributes ?.code !== undefined && ( @@ -330,11 +333,11 @@ const Content = ({
), [ - content.folders?.data, content.ranged_contents?.data, currencies, languages, langui, + returnButtonProps, selectedTranslation, ] ); @@ -342,10 +345,8 @@ const Content = ({ const contentPanel = useMemo( () => ( - @@ -481,6 +482,7 @@ const Content = ({ langui, nextContent?.attributes, previousContent?.attributes, + returnButtonProps, selectedTranslation?.description, selectedTranslation?.pre_title, selectedTranslation?.subtitle, @@ -605,7 +607,7 @@ export const getStaticPaths: GetStaticPaths = async (context) => { type FolderContents = NonNullable< NonNullable< NonNullable< - NonNullable["data"][number] + NonNullable["data"] >["attributes"] >["contents"] >["data"];