accords-library.com/src/pages/news/[slug].tsx

104 lines
4.0 KiB
TypeScript
Raw Normal View History

2022-07-07 23:42:38 +00:00
import { GetStaticPaths, GetStaticPathsResult, GetStaticProps } from "next";
2022-09-05 15:02:22 +00:00
import { NextRouter, useRouter } from "next/router";
import { PostPage } from "components/PostPage";
import { getPostStaticProps, PostStaticProps } from "graphql/getPostStaticProps";
import { getReadySdk } from "graphql/sdk";
import { filterHasAttributes, isDefined, isDefinedAndNotEmpty } from "helpers/asserts";
2022-09-05 15:02:22 +00:00
import { Terminal } from "components/Cli/Terminal";
import { PostWithTranslations } from "types/types";
import { getDefaultPreferredLanguages, staticSmartLanguage } from "helpers/locales";
import { prettyTerminalBoxedTitle } from "helpers/terminal";
import { prettyMarkdown } from "helpers/description";
import { atoms } from "contexts/atoms";
import { useAtomGetter } from "helpers/atoms";
/*
*
* PAGE
*/
2022-03-17 16:05:34 +00:00
2022-08-27 15:03:05 +00:00
interface Props extends PostStaticProps {}
2022-03-17 16:05:34 +00:00
2022-08-27 15:03:05 +00:00
const LibrarySlug = (props: Props): JSX.Element => {
const langui = useAtomGetter(atoms.localData.langui);
const isTerminalMode = useAtomGetter(atoms.layout.terminalMode);
2022-09-05 15:02:22 +00:00
const router = useRouter();
if (isTerminalMode) {
return (
<Terminal
parentPath={"/news"}
childrenPaths={[]}
content={terminalPostPage(props.post, router)}
/>
);
}
2022-08-27 15:03:05 +00:00
return (
<PostPage
returnHref="/news"
returnTitle={langui.news}
displayCredits
displayThumbnailHeader
displayToc
{...props}
/>
);
};
export default LibrarySlug;
/*
*
* NEXT DATA FETCHING
*/
export const getStaticProps: GetStaticProps = async (context) => {
const slug =
context.params && isDefined(context.params.slug) ? context.params.slug.toString() : "";
2022-05-08 19:37:41 +00:00
return await getPostStaticProps(slug)(context);
};
// ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
2022-03-17 16:05:34 +00:00
export const getStaticPaths: GetStaticPaths = async (context) => {
const sdk = getReadySdk();
const posts = await sdk.getPostsSlugs();
const paths: GetStaticPathsResult["paths"] = [];
2022-06-18 19:53:23 +00:00
filterHasAttributes(posts.posts?.data, ["attributes"] as const).map((item) => {
context.locales?.map((local) =>
paths.push({ params: { slug: item.attributes.slug }, locale: local })
);
});
2022-03-17 16:05:34 +00:00
return {
paths,
fallback: "blocking",
2022-03-17 16:05:34 +00:00
};
};
2022-09-05 15:02:22 +00:00
const terminalPostPage = (post: PostWithTranslations, router: NextRouter): string => {
let result = "";
if (router.locales && router.locale) {
const selectedTranslation = staticSmartLanguage({
items: filterHasAttributes(post.translations, ["language.data.attributes.code"] as const),
languageExtractor: (item) => item.language.data.attributes.code,
preferredLanguages: getDefaultPreferredLanguages(router.locale, router.locales),
});
if (selectedTranslation) {
result += prettyTerminalBoxedTitle(selectedTranslation.title);
if (isDefinedAndNotEmpty(selectedTranslation.excerpt)) {
result += "\n\n";
result += prettyMarkdown(selectedTranslation.excerpt);
}
if (isDefinedAndNotEmpty(selectedTranslation.body)) {
result += "\n\n";
result += prettyMarkdown(selectedTranslation.body);
}
}
}
result += "\n\n";
return result;
};