diff --git a/src/helpers/contents.ts b/src/helpers/contents.ts new file mode 100644 index 0000000..00967b9 --- /dev/null +++ b/src/helpers/contents.ts @@ -0,0 +1,31 @@ +import { ContentWithTranslations, Immutable } from "./types"; + +type Group = Immutable< + NonNullable< + NonNullable< + NonNullable< + NonNullable["data"] + >["attributes"] + >["contents"] + >["data"] +>; + +export function getPreviousContent(group: Group, currentSlug: string) { + for (let index = 0; index < group.length; index += 1) { + const content = group[index]; + if (content.attributes?.slug === currentSlug && index > 0) { + return group[index - 1]; + } + } + return undefined; +} + +export function getNextContent(group: Group, currentSlug: string) { + for (let index = 0; index < group.length; index += 1) { + const content = group[index]; + if (content.attributes?.slug === currentSlug && index < group.length - 1) { + return group[index + 1]; + } + } + return undefined; +} diff --git a/src/pages/contents/[slug]/index.tsx b/src/pages/contents/[slug]/index.tsx index 8590235..60504b8 100644 --- a/src/pages/contents/[slug]/index.tsx +++ b/src/pages/contents/[slug]/index.tsx @@ -15,6 +15,7 @@ import { ThumbnailHeader } from "components/ThumbnailHeader"; import { ToolTip } from "components/ToolTip"; import { AppStaticProps, getAppStaticProps } from "graphql/getAppStaticProps"; import { getReadySdk } from "graphql/sdk"; +import { getNextContent, getPreviousContent } from "helpers/contents"; import { prettyinlineTitle, prettyLanguage, @@ -34,48 +35,6 @@ interface Props extends AppStaticProps { content: ContentWithTranslations; } -function getPreviousContent( - group: Immutable< - NonNullable< - NonNullable< - NonNullable< - NonNullable["data"] - >["attributes"] - >["contents"] - >["data"] - >, - currentSlug: string -) { - for (let index = 0; index < group.length; index += 1) { - const content = group[index]; - if (content.attributes?.slug === currentSlug && index > 0) { - return group[index - 1]; - } - } - return undefined; -} - -function getNextContent( - group: Immutable< - NonNullable< - NonNullable< - NonNullable< - NonNullable["data"] - >["attributes"] - >["contents"] - >["data"] - >, - currentSlug: string -) { - for (let index = 0; index < group.length; index += 1) { - const content = group[index]; - if (content.attributes?.slug === currentSlug && index < group.length - 1) { - return group[index + 1]; - } - } - return undefined; -} - export default function Content(props: Immutable): JSX.Element { const { langui, content, languages } = props; const isMobile = useMediaMobile();