@c-rex/templates 0.1.28 → 0.1.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/layout.tsx CHANGED
@@ -1,71 +1,71 @@
1
- import React from "react";
2
- import { NextIntlClientProvider } from 'next-intl';
3
- import { getLocale } from "next-intl/server";
4
- import { Toaster } from "@c-rex/ui/sonner"
5
- import { AppConfigProvider } from "@c-rex/contexts/config-provider";
6
- import { NuqsAdapter } from 'nuqs/adapters/next/app'
7
- import { cookies } from "next/headers";
8
- import { CrexSDK } from "@c-rex/core/sdk";
9
- import { getIssuerMetadata } from "@c-rex/core/OIDC";
10
- import { resolveUILanguage, resolveContentLanguage } from "./utils";
11
- import { informationUnitsLanguagesServer } from "@c-rex/services/server-requests";
12
- import { UI_LANG_KEY } from "@c-rex/constants";
13
- import { transformLanguageData } from "@c-rex/utils";
14
- import { getSearchSettingsFromCookie } from "@c-rex/components/search-settings-store";
15
-
16
- interface DefaultRootLayoutProps {
17
- children: React.ReactNode;
18
- }
19
-
20
- export const DefaultRootLayout = async ({ children }: DefaultRootLayoutProps) => {
21
- const sdk = new CrexSDK()
22
- const locale = await getLocale();
23
- const metadata = await getIssuerMetadata();
24
- const cookieStore = cookies();
25
- const uiLangCookie = cookieStore.get(UI_LANG_KEY)?.value ?? null;
26
- const clientConfigs = sdk.getClientConfig();
27
- const serverConfigs = sdk.getServerConfig();
28
- const searchSettings = getSearchSettingsFromCookie(cookieStore.get("c-rex-search-settings")?.value);
29
-
30
- sdk.updateConfigProp('OIDC', {
31
- ...serverConfigs.OIDC,
32
- issuerMetadata: metadata
33
- });
34
-
35
- const data = await informationUnitsLanguagesServer();
36
- const availableLanguages = transformLanguageData(data as { value: string; score: number; }[]);
37
-
38
- const uiLang = resolveUILanguage({
39
- uiLangCookie,
40
- defaultLang: clientConfigs.languageSwitcher.default
41
- });
42
-
43
- const contentLang = resolveContentLanguage({
44
- contentLangCookie: searchSettings.language,
45
- availableLanguages,
46
- defaultLang: clientConfigs.languageSwitcher.default
47
- });
48
-
49
- return (
50
- <NuqsAdapter>
51
- <html lang={locale}>
52
- <head>
53
- <link rel="icon" type="image/x-icon" href="/img/favicon.png" />
54
- </head>
55
- <body className="min-h-screen bg-background font-sans antialiased">
56
- <NextIntlClientProvider>
57
- <AppConfigProvider
58
- uiLang={uiLang}
59
- contentLang={contentLang}
60
- availableLanguages={availableLanguages}
61
- initialConfig={clientConfigs}
62
- >
63
- <Toaster />
64
- {children}
65
- </AppConfigProvider>
66
- </NextIntlClientProvider>
67
- </body>
68
- </html>
69
- </NuqsAdapter>
70
- );
1
+ import React from "react";
2
+ import { NextIntlClientProvider } from 'next-intl';
3
+ import { getLocale } from "next-intl/server";
4
+ import { Toaster } from "@c-rex/ui/sonner"
5
+ import { AppConfigProvider } from "@c-rex/contexts/config-provider";
6
+ import { NuqsAdapter } from 'nuqs/adapters/next/app'
7
+ import { cookies } from "next/headers";
8
+ import { CrexSDK } from "@c-rex/core/sdk";
9
+ import { getIssuerMetadata } from "@c-rex/core/OIDC";
10
+ import { resolveUILanguage, resolveContentLanguage } from "./utils";
11
+ import { informationUnitsLanguagesServer } from "@c-rex/services/server-requests";
12
+ import { UI_LANG_KEY } from "@c-rex/constants";
13
+ import { transformLanguageData } from "@c-rex/utils";
14
+ import { getSearchSettingsFromCookie } from "@c-rex/components/search-settings-store";
15
+
16
+ interface DefaultRootLayoutProps {
17
+ children: React.ReactNode;
18
+ }
19
+
20
+ export const DefaultRootLayout = async ({ children }: DefaultRootLayoutProps) => {
21
+ const sdk = new CrexSDK()
22
+ const locale = await getLocale();
23
+ const metadata = await getIssuerMetadata();
24
+ const cookieStore = cookies();
25
+ const uiLangCookie = cookieStore.get(UI_LANG_KEY)?.value ?? null;
26
+ const clientConfigs = sdk.getClientConfig();
27
+ const serverConfigs = sdk.getServerConfig();
28
+ const searchSettings = getSearchSettingsFromCookie(cookieStore.get("c-rex-search-settings")?.value);
29
+
30
+ sdk.updateConfigProp('OIDC', {
31
+ ...serverConfigs.OIDC,
32
+ issuerMetadata: metadata
33
+ });
34
+
35
+ const data = await informationUnitsLanguagesServer();
36
+ const availableLanguages = transformLanguageData(data as { value: string; score: number; }[]);
37
+
38
+ const uiLang = resolveUILanguage({
39
+ uiLangCookie,
40
+ defaultLang: clientConfigs.languageSwitcher.default
41
+ });
42
+
43
+ const contentLang = resolveContentLanguage({
44
+ contentLangCookie: searchSettings.language,
45
+ availableLanguages,
46
+ defaultLang: clientConfigs.languageSwitcher.default
47
+ });
48
+
49
+ return (
50
+ <NuqsAdapter>
51
+ <html lang={locale}>
52
+ <head>
53
+ <link rel="icon" type="image/x-icon" href="/img/favicon.png" />
54
+ </head>
55
+ <body className="min-h-screen bg-background font-sans antialiased">
56
+ <NextIntlClientProvider>
57
+ <AppConfigProvider
58
+ uiLang={uiLang}
59
+ contentLang={contentLang}
60
+ availableLanguages={availableLanguages}
61
+ initialConfig={clientConfigs}
62
+ >
63
+ <Toaster />
64
+ {children}
65
+ </AppConfigProvider>
66
+ </NextIntlClientProvider>
67
+ </body>
68
+ </html>
69
+ </NuqsAdapter>
70
+ );
71
71
  };
package/src/utils.ts CHANGED
@@ -1,49 +1,49 @@
1
- import { UI_LANG_OPTIONS } from "@c-rex/constants";
2
- import { headers } from "next/headers";
3
-
4
- const getBrowserLangFromHeader = (): string => {
5
- const h = headers();
6
- const accept = h.get("accept-language"); // ex: "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7"
7
-
8
- if (!accept) return "";
9
-
10
- return accept.split(",")[0]?.toLowerCase() || "";
11
- }
12
-
13
-
14
- export const resolveUILanguage = ({
15
- uiLangCookie,
16
- defaultLang,
17
- }: {
18
- uiLangCookie?: string | null;
19
- defaultLang: string;
20
- }): string => {
21
- const browserLang = getBrowserLangFromHeader();
22
-
23
- if (uiLangCookie) return uiLangCookie;
24
- if (browserLang && UI_LANG_OPTIONS.includes(browserLang)) return browserLang;
25
-
26
- return defaultLang;
27
- }
28
-
29
- export const resolveContentLanguage = ({
30
- contentLangCookie,
31
- availableLanguages,
32
- defaultLang,
33
- }: {
34
- contentLangCookie?: string | null;
35
- availableLanguages: { value: string }[];
36
- defaultLang: string;
37
- }): string => {
38
- const browserLang = getBrowserLangFromHeader();
39
-
40
- if (contentLangCookie) return contentLangCookie;
41
-
42
- const hasLang = browserLang
43
- ? availableLanguages.some((l) => l.value === browserLang)
44
- : false;
45
-
46
- if (hasLang) return browserLang;
47
-
48
- return defaultLang;
1
+ import { UI_LANG_OPTIONS } from "@c-rex/constants";
2
+ import { headers } from "next/headers";
3
+
4
+ const getBrowserLangFromHeader = (): string => {
5
+ const h = headers();
6
+ const accept = h.get("accept-language"); // ex: "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7"
7
+
8
+ if (!accept) return "";
9
+
10
+ return accept.split(",")[0]?.toLowerCase() || "";
11
+ }
12
+
13
+
14
+ export const resolveUILanguage = ({
15
+ uiLangCookie,
16
+ defaultLang,
17
+ }: {
18
+ uiLangCookie?: string | null;
19
+ defaultLang: string;
20
+ }): string => {
21
+ const browserLang = getBrowserLangFromHeader();
22
+
23
+ if (uiLangCookie) return uiLangCookie;
24
+ if (browserLang && UI_LANG_OPTIONS.includes(browserLang)) return browserLang;
25
+
26
+ return defaultLang;
27
+ }
28
+
29
+ export const resolveContentLanguage = ({
30
+ contentLangCookie,
31
+ availableLanguages,
32
+ defaultLang,
33
+ }: {
34
+ contentLangCookie?: string | null;
35
+ availableLanguages: { value: string }[];
36
+ defaultLang: string;
37
+ }): string => {
38
+ const browserLang = getBrowserLangFromHeader();
39
+
40
+ if (contentLangCookie) return contentLangCookie;
41
+
42
+ const hasLang = browserLang
43
+ ? availableLanguages.some((l) => l.value === browserLang)
44
+ : false;
45
+
46
+ if (hasLang) return browserLang;
47
+
48
+ return defaultLang;
49
49
  }
@@ -1,74 +0,0 @@
1
- import { cache } from "react";
2
-
3
- import { Metadata } from "next";
4
- import { DocumentsPage } from "./page";
5
- import { PageWrapper } from "@c-rex/components/page-wrapper";
6
- import { DocumentArticleCollector } from "../../collectors/DocumentArticleCollector";
7
- import { CrexSDK } from "@c-rex/core/sdk";
8
-
9
- type Props = {
10
- params: { id: string }
11
- }
12
-
13
- const docCollector = new DocumentArticleCollector();
14
-
15
- const getCachedDocumentData = cache(async (id: string) => {
16
- return await docCollector.collect(id);
17
- });
18
-
19
-
20
- export const generateMetadata = async ({ params }: Props): Promise<Metadata> => {
21
- const { metaTags, rootNode } = await getCachedDocumentData(params.id);
22
- const sdk = new CrexSDK()
23
- const config = sdk.getServerConfig()
24
-
25
- const pageTitle = rootNode?.informationUnits[0]?.labels[0]?.value as string;
26
- const title = config.titles.sub
27
- .replace("{{projectName}}", config.projectName)
28
- .replace("{{pageTitle}}", pageTitle);
29
-
30
-
31
- const other: Record<string, string[]> = {}
32
-
33
- for (const { name, content } of metaTags) {
34
- if (Object.keys(other).includes(name)) {
35
- other[name]?.push(content)
36
- } else {
37
- other[name] = [content]
38
- }
39
- }
40
-
41
- return { ...other, title }
42
- }
43
-
44
- export const DocumentsLayout = async ({ params }: Props) => {
45
- const {
46
- articleHtml,
47
- document,
48
- article,
49
- rootNode,
50
- articleAvailableVersions,
51
- documentAvailableVersions,
52
- attachments,
53
- articleInfo,
54
- documentInfo
55
- } = await getCachedDocumentData(params.id);
56
- const title = rootNode?.informationUnits[0]?.labels[0]?.value as string;
57
-
58
- return (
59
- <PageWrapper title={title} pageType="DOC">
60
- <DocumentsPage
61
- articleHtml={articleHtml}
62
- document={document}
63
- article={article}
64
- rootNode={rootNode}
65
- articleAvailableVersions={articleAvailableVersions}
66
- documentAvailableVersions={documentAvailableVersions}
67
- attachments={attachments!}
68
- articleInfo={articleInfo!}
69
- documentInfo={documentInfo!}
70
- id={params.id}
71
- />
72
- </PageWrapper>
73
- );
74
- };
@@ -1,79 +0,0 @@
1
- "use client"
2
-
3
- import React, { FC, useEffect, useState } from "react";
4
- import { ArticleWrapper } from "../wrapper";
5
- import { CollectionResult, TreeOfContent } from "@c-rex/interfaces";
6
- import { createAvailableVersionList, generateTreeOfContent } from "@c-rex/utils";
7
- import { DOCUMENTS_TYPE_AND_LINK, RESULT_TYPES, TOPICS_TYPE_AND_LINK } from "@c-rex/constants";
8
- import { useAppConfig } from "@c-rex/contexts/config-provider";
9
- import { HighlightProvider } from "@c-rex/contexts/highlight-provider";
10
-
11
- interface DocumentsPageProps extends Omit<CollectionResult, "metaTags"> {
12
- id: string;
13
- }
14
-
15
- export const DocumentsPage: FC<DocumentsPageProps> = ({
16
- articleHtml,
17
- document,
18
- article,
19
- rootNode,
20
- articleAvailableVersions,
21
- documentAvailableVersions,
22
- attachments,
23
- articleInfo,
24
- documentInfo,
25
- id
26
- }) => {
27
- const { setPackageID } = useAppConfig();
28
- const breadcrumbItems = [{
29
- link: "/",
30
- label: rootNode?.informationUnits[0]?.labels[0]?.value as string,
31
- id: "title",
32
- active: false,
33
- children: [],
34
- }]
35
-
36
- const [loading, setLoading] = useState<boolean>(true);
37
- const [tree, setTreeOfContent] = useState<TreeOfContent[]>([]);
38
-
39
- useEffect(() => {
40
- const fetchData = async () => {
41
- const aux = await generateTreeOfContent(article.directoryNodes);
42
- if (aux && aux[0]) aux[0].active = true;
43
-
44
- setTreeOfContent(aux);
45
- setLoading(false);
46
- };
47
-
48
- fetchData();
49
-
50
- if (document && document.packages.length > 0 && document.packages[0]) {
51
- setPackageID(document.packages[0].shortId)
52
- }
53
- }, []);
54
-
55
- return (
56
- <HighlightProvider>
57
- <ArticleWrapper
58
- htmlContent={articleHtml}
59
- documentLang={document.languages[0]!}
60
- articleLang={article.languages[0]!}
61
- articleAvailableVersions={
62
- createAvailableVersionList(articleAvailableVersions, article.languages[0]!, TOPICS_TYPE_AND_LINK)
63
- }
64
- documentAvailableVersions={
65
- createAvailableVersionList(documentAvailableVersions, document.languages[0]!, DOCUMENTS_TYPE_AND_LINK)
66
- }
67
- loading={loading}
68
- documents={attachments!}
69
- articleInfo={articleInfo!}
70
- documentInfo={documentInfo!}
71
- breadcrumbItems={breadcrumbItems}
72
- treeOfContent={tree}
73
- id={id}
74
- type={RESULT_TYPES.DOCUMENT}
75
- documentId={document.shortId}
76
- />
77
- </HighlightProvider>
78
- );
79
- };
@@ -1,70 +0,0 @@
1
- import { cache } from "react";
2
- import { TopicArticleCollector } from "../../collectors/TopicArticleCollector";
3
- import { Metadata } from "next";
4
- import { PageWrapper } from "@c-rex/components/page-wrapper";
5
- import { TopicsPage } from "./page";
6
- import { CrexSDK } from "@c-rex/core/sdk";
7
-
8
- type Props = {
9
- params: { id: string }
10
- }
11
-
12
- const topicCollector = new TopicArticleCollector();
13
-
14
- const getCachedTopicData = cache(async (id: string) => {
15
- return await topicCollector.collect(id);
16
- });
17
-
18
- export const generateMetadata = async ({ params }: Props): Promise<Metadata> => {
19
- const { metaTags, rootNode } = await getCachedTopicData(params.id);
20
- const sdk = new CrexSDK()
21
- const config = sdk.getServerConfig()
22
- const pageTitle = rootNode?.informationUnits[0]?.labels[0]?.value as string;
23
- const title = config.titles.sub
24
- .replace("{{projectName}}", config.projectName)
25
- .replace("{{pageTitle}}", pageTitle);
26
-
27
- const other: Record<string, string[]> = {}
28
-
29
- for (const { name, content } of metaTags) {
30
- if (Object.keys(other).includes(name)) {
31
- other[name]?.push(content)
32
- } else {
33
- other[name] = [content]
34
- }
35
- }
36
-
37
- return { ...other, title }
38
- }
39
-
40
- export const TopicsLayout = async ({ params }: { params: { id: string } }) => {
41
- const {
42
- articleHtml,
43
- document,
44
- article,
45
- rootNode,
46
- articleAvailableVersions,
47
- documentAvailableVersions,
48
- attachments,
49
- articleInfo,
50
- documentInfo
51
- } = await getCachedTopicData(params.id);
52
- const title = rootNode?.informationUnits[0]?.labels[0]?.value as string;
53
-
54
- return (
55
- <PageWrapper title={title} pageType="DOC">
56
- <TopicsPage
57
- articleHtml={articleHtml}
58
- document={document}
59
- article={article}
60
- rootNode={rootNode}
61
- articleAvailableVersions={articleAvailableVersions}
62
- documentAvailableVersions={documentAvailableVersions}
63
- attachments={attachments!}
64
- articleInfo={articleInfo!}
65
- documentInfo={documentInfo!}
66
- id={params.id}
67
- />
68
- </PageWrapper>
69
- );
70
- };
@@ -1,71 +0,0 @@
1
- "use client"
2
-
3
- import React, { FC, useEffect, useState } from "react";
4
- import { ArticleWrapper } from "../wrapper";
5
- import { CollectionResult, TreeOfContent } from "@c-rex/interfaces";
6
- import { createAvailableVersionList, generateBreadcrumbItems, generateTreeOfContent } from "@c-rex/utils";
7
- import { DOCUMENTS_TYPE_AND_LINK, RESULT_TYPES, TOPICS_TYPE_AND_LINK } from "@c-rex/constants";
8
- import { useAppConfig } from "@c-rex/contexts/config-provider";
9
- import { HighlightProvider } from "@c-rex/contexts/highlight-provider";
10
-
11
- interface TopicPage extends Omit<CollectionResult, "metaTags"> {
12
- id: string;
13
- }
14
-
15
- export const TopicsPage: FC<TopicPage> = ({
16
- articleHtml,
17
- document,
18
- article,
19
- articleAvailableVersions,
20
- documentAvailableVersions,
21
- attachments,
22
- articleInfo,
23
- documentInfo,
24
- id,
25
- }) => {
26
- const { setPackageID } = useAppConfig();
27
- const [loading, setLoading] = useState<boolean>(true);
28
- const [tree, setTreeOfContent] = useState<TreeOfContent[]>([]);
29
- const [breadcrumbItems, setBreadcrumbItems] = useState<TreeOfContent[]>([]);
30
-
31
- useEffect(() => {
32
- const fetchData = async () => {
33
- const aux = await generateTreeOfContent(article.directoryNodes);
34
- const items = generateBreadcrumbItems(aux);
35
-
36
- setTreeOfContent(aux);
37
- setBreadcrumbItems(items);
38
- setLoading(false);
39
- };
40
-
41
- fetchData();
42
- if (document && document.packages.length > 0 && document.packages[0]) {
43
- setPackageID(document.packages[0].shortId)
44
- }
45
- }, []);
46
-
47
- return (
48
- <HighlightProvider>
49
- <ArticleWrapper
50
- htmlContent={articleHtml}
51
- documentLang={document.languages[0]!}
52
- articleLang={article.languages[0]!}
53
- articleAvailableVersions={
54
- createAvailableVersionList(articleAvailableVersions, article.languages[0]!, TOPICS_TYPE_AND_LINK)
55
- }
56
- documentAvailableVersions={
57
- createAvailableVersionList(documentAvailableVersions, document.languages[0]!, DOCUMENTS_TYPE_AND_LINK)
58
- }
59
- loading={loading}
60
- documents={attachments!}
61
- articleInfo={articleInfo!}
62
- documentInfo={documentInfo!}
63
- breadcrumbItems={breadcrumbItems}
64
- treeOfContent={tree}
65
- id={id}
66
- documentId={document.shortId}
67
- type={RESULT_TYPES.TOPIC}
68
- />
69
- </HighlightProvider>
70
- );
71
- };