From c69b4478f7ab148f79e32003c605d6063d4bd5bf Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Wed, 3 May 2023 23:03:55 +0200 Subject: [PATCH] Display up to depth-7 parent folders --- .../fragments/parentFolderPreview.graphql | 14 ++++ .../operations/getContentsFolder.graphql | 80 ++++++++++++------- src/pages/contents/folder/[slug].tsx | 75 ++++++++++------- 3 files changed, 110 insertions(+), 59 deletions(-) create mode 100644 src/graphql/fragments/parentFolderPreview.graphql diff --git a/src/graphql/fragments/parentFolderPreview.graphql b/src/graphql/fragments/parentFolderPreview.graphql new file mode 100644 index 0000000..0621ee9 --- /dev/null +++ b/src/graphql/fragments/parentFolderPreview.graphql @@ -0,0 +1,14 @@ +fragment parentFolderPreview on ContentsFolder { + slug + titles(pagination: { limit: -1 }) { + id + language { + data { + attributes { + code + } + } + } + title + } +} diff --git a/src/graphql/operations/getContentsFolder.graphql b/src/graphql/operations/getContentsFolder.graphql index 4e06f5a..fdbf776 100644 --- a/src/graphql/operations/getContentsFolder.graphql +++ b/src/graphql/operations/getContentsFolder.graphql @@ -2,36 +2,7 @@ query getContentsFolder($slug: String, $language_code: String) { contentsFolders(filters: { slug: { eq: $slug } }) { data { attributes { - slug - titles(pagination: { limit: -1 }) { - id - language { - data { - attributes { - code - } - } - } - title - } - parent_folder { - data { - attributes { - slug - titles(pagination: { limit: -1 }) { - id - language { - data { - attributes { - code - } - } - } - title - } - } - } - } + ...parentFolderPreview contents(pagination: { limit: -1 }) { data { id @@ -124,6 +95,55 @@ query getContentsFolder($slug: String, $language_code: String) { } } } + parent_folder { + data { + attributes { + ...parentFolderPreview + parent_folder { + data { + attributes { + ...parentFolderPreview + parent_folder { + data { + attributes { + ...parentFolderPreview + parent_folder { + data { + attributes { + ...parentFolderPreview + parent_folder { + data { + attributes { + ...parentFolderPreview + parent_folder { + data { + attributes { + ...parentFolderPreview + parent_folder { + data { + attributes { + ...parentFolderPreview + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } } } } diff --git a/src/pages/contents/folder/[slug].tsx b/src/pages/contents/folder/[slug].tsx index 2851c28..d6a758f 100644 --- a/src/pages/contents/folder/[slug].tsx +++ b/src/pages/contents/folder/[slug].tsx @@ -4,7 +4,7 @@ import { ContentPanel, ContentPanelWidthSizes } from "components/Containers/Cont import { getOpenGraph } from "helpers/openGraph"; import { getReadySdk } from "graphql/sdk"; import { filterHasAttributes } from "helpers/asserts"; -import { GetContentsFolderQuery } from "graphql/generated"; +import { GetContentsFolderQuery, ParentFolderPreviewFragment } from "graphql/generated"; import { getDefaultPreferredLanguages, staticSmartLanguage } from "helpers/locales"; import { prettySlug } from "helpers/formatters"; import { Ico } from "components/Ico"; @@ -20,6 +20,7 @@ import { TranslatedPreviewFolder } from "components/Contents/PreviewFolder"; import { useFormat } from "hooks/useFormat"; import { getFormat } from "helpers/i18n"; import { Chip } from "components/Chip"; +import { useMemo } from "react"; /* * ╭────────╮ @@ -30,13 +31,22 @@ interface Props extends AppLayoutRequired { folder: NonNullable< NonNullable["data"][number]["attributes"] >; + path: ParentFolderPreviewFragment[]; } -const ContentsFolder = ({ openGraph, folder, ...otherProps }: Props): JSX.Element => { +const ContentsFolder = ({ openGraph, folder, path, ...otherProps }: Props): JSX.Element => { const { format } = useFormat(); const setSubPanelOpened = useAtomSetter(atoms.layout.subPanelOpened); const isContentPanelAtLeast4xl = useAtomGetter(atoms.containerQueries.isContentPanelAtLeast4xl); + const filteredPath = useMemo( + () => + path.filter( + (_, index) => isContentPanelAtLeast4xl || index === 0 || index === path.length - 1 + ), + [path, isContentPanelAtLeast4xl] + ); + const subPanel = ( -
- {folder.parent_folder?.data?.attributes && ( +
+ {filteredPath.map((pathFolder, index) => ( <> - {folder.parent_folder.data.attributes.slug === "root" ? ( -
{folder.subfolders?.data && folder.subfolders.data.length > 0 && ( @@ -237,6 +231,7 @@ export const getStaticProps: GetStaticProps = async (context) => { const props: Props = { openGraph: getOpenGraph(format, title), folder, + path: getRecursiveParentFolderPreview(folder), }; return { props: props, @@ -274,9 +269,31 @@ const NoContentNorFolderMessage = () => {
+ border-dark p-8 text-dark opacity-40 mt-12">

{format("empty_folder_message")}

); }; + +/* + * ╭───────────────────╮ + * ─────────────────────────────────────╯ PRIVATE METHODS ╰─────────────────────────────────────── + */ + +type ParentFolderWithParentFolder = ParentFolderPreviewFragment & { + parent_folder?: { + data?: { + attributes?: ParentFolderPreviewFragment | ParentFolderWithParentFolder | null; + } | null; + } | null; +}; + +const getRecursiveParentFolderPreview = ( + parentFolder: ParentFolderWithParentFolder +): ParentFolderPreviewFragment[] => [ + ...(parentFolder.parent_folder?.data?.attributes + ? getRecursiveParentFolderPreview(parentFolder.parent_folder.data.attributes) + : []), + { slug: parentFolder.slug, titles: parentFolder.titles }, +];