@remkoj/optimizely-cms-react 2.1.0 → 3.0.0
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/README.md +36 -2
- package/dist/components/client.d.ts +42 -0
- package/dist/components/client.js +52 -0
- package/dist/components/client.js.map +1 -0
- package/dist/components/cms-content/client.d.ts +11 -0
- package/dist/components/cms-content/client.js +72 -0
- package/dist/components/cms-content/client.js.map +1 -0
- package/dist/components/cms-content/get-content-type.d.ts +13 -0
- package/dist/components/cms-content/get-content-type.js +70 -0
- package/dist/components/cms-content/get-content-type.js.map +1 -0
- package/dist/components/cms-content/get-content.d.ts +4 -0
- package/dist/components/cms-content/get-content.js +91 -0
- package/dist/components/cms-content/get-content.js.map +1 -0
- package/dist/components/cms-content/resolve-component.d.ts +18 -0
- package/dist/components/cms-content/resolve-component.js +53 -0
- package/dist/components/cms-content/resolve-component.js.map +1 -0
- package/dist/components/cms-content/resolve-content-type.d.ts +15 -0
- package/dist/components/cms-content/resolve-content-type.js +29 -0
- package/dist/components/cms-content/resolve-content-type.js.map +1 -0
- package/dist/components/cms-content/rsc.d.ts +11 -0
- package/dist/components/cms-content/rsc.js +50 -0
- package/dist/components/cms-content/rsc.js.map +1 -0
- package/dist/components/cms-content/types.d.ts +54 -0
- package/dist/components/cms-content/types.js +2 -0
- package/dist/components/cms-content/types.js.map +1 -0
- package/dist/components/cms-content/utils.d.ts +3 -0
- package/dist/components/cms-content/utils.js +6 -0
- package/dist/components/cms-content/utils.js.map +1 -0
- package/dist/components/cms-content-area/index.d.ts +17 -0
- package/dist/{server/components/cms-content-area.js → components/cms-content-area/index.js} +27 -36
- package/dist/components/cms-content-area/index.js.map +1 -0
- package/dist/{server/components → components/cms-content-area}/types.d.ts +7 -94
- package/dist/components/cms-content-area/types.js.map +1 -0
- package/dist/{server/components/cms-editable.d.ts → components/cms-editable/index.d.ts} +5 -3
- package/dist/components/cms-editable/index.js +15 -0
- package/dist/components/cms-editable/index.js.map +1 -0
- package/dist/components/{cms-styles.js → cms-styles/index.js} +1 -1
- package/dist/components/cms-styles/index.js.map +1 -0
- package/dist/components/rich-text/components.d.ts +14 -0
- package/dist/components/rich-text/components.js +63 -0
- package/dist/components/rich-text/components.js.map +1 -0
- package/dist/components/rich-text/index.d.ts +4 -0
- package/dist/components/rich-text/index.js +47 -0
- package/dist/components/rich-text/index.js.map +1 -0
- package/dist/components/{rich-text.d.ts → rich-text/types.d.ts} +13 -18
- package/dist/components/rich-text/types.js +2 -0
- package/dist/components/rich-text/types.js.map +1 -0
- package/dist/components/rich-text/utils.d.ts +5 -0
- package/dist/components/rich-text/utils.js +24 -0
- package/dist/components/rich-text/utils.js.map +1 -0
- package/dist/components/rsc.d.ts +47 -0
- package/dist/components/rsc.js +55 -0
- package/dist/components/rsc.js.map +1 -0
- package/dist/{server → components}/type-utils.js.map +1 -1
- package/dist/{server/components → components}/visual-builder/functions.d.ts +7 -2
- package/dist/{server/components → components}/visual-builder/functions.js +8 -3
- package/dist/components/visual-builder/functions.js.map +1 -0
- package/dist/{server/components/visual-builder/Composition.d.ts → components/visual-builder/index.d.ts} +4 -3
- package/dist/{server/components/visual-builder/Composition.js → components/visual-builder/index.js} +9 -18
- package/dist/components/visual-builder/index.js.map +1 -0
- package/dist/{server/components → components}/visual-builder/types.d.ts +6 -1
- package/dist/components/visual-builder/types.js.map +1 -0
- package/dist/components.d.ts +27 -0
- package/dist/components.js +20 -0
- package/dist/components.js.map +1 -0
- package/dist/context/client.d.ts +70 -0
- package/dist/context/client.js +78 -0
- package/dist/context/client.js.map +1 -0
- package/dist/context/rsc.d.ts +33 -0
- package/dist/context/rsc.js +75 -0
- package/dist/context/rsc.js.map +1 -0
- package/dist/context/types.d.ts +15 -0
- package/dist/context/types.js +2 -0
- package/dist/{server/components → context}/types.js.map +1 -1
- package/dist/{server/components → data}/queries.js +121 -121
- package/dist/data/queries.js.map +1 -0
- package/dist/{factory.d.ts → factory/default.d.ts} +0 -18
- package/dist/{factory.js → factory/default.js} +1 -33
- package/dist/factory/default.js.map +1 -0
- package/dist/factory/index.d.ts +12 -0
- package/dist/factory/index.js +16 -0
- package/dist/factory/index.js.map +1 -0
- package/dist/factory/types.d.ts +51 -0
- package/dist/factory/types.js +2 -0
- package/dist/factory/types.js.map +1 -0
- package/dist/factory/undef.d.ts +9 -0
- package/dist/factory/undef.js +19 -0
- package/dist/factory/undef.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/{server/is-debug.d.ts → rsc-utilities.d.ts} +0 -1
- package/dist/{server/is-debug.js → rsc-utilities.js} +1 -2
- package/dist/{server/is-debug.js.map → rsc-utilities.js.map} +1 -1
- package/dist/rsc.d.ts +11 -0
- package/dist/rsc.js +14 -0
- package/dist/rsc.js.map +1 -0
- package/dist/types.d.ts +10 -56
- package/package.json +12 -24
- package/dist/browser/context.d.ts +0 -6
- package/dist/browser/context.js +0 -15
- package/dist/browser/context.js.map +0 -1
- package/dist/browser/index.d.ts +0 -1
- package/dist/browser/index.js +0 -2
- package/dist/browser/index.js.map +0 -1
- package/dist/components/cms-styles.js.map +0 -1
- package/dist/components/index.d.ts +0 -2
- package/dist/components/index.js +0 -3
- package/dist/components/index.js.map +0 -1
- package/dist/components/rich-text.js +0 -138
- package/dist/components/rich-text.js.map +0 -1
- package/dist/factory.js.map +0 -1
- package/dist/server/components/cms-content-area.d.ts +0 -13
- package/dist/server/components/cms-content-area.js.map +0 -1
- package/dist/server/components/cms-content.d.ts +0 -11
- package/dist/server/components/cms-content.js +0 -154
- package/dist/server/components/cms-content.js.map +0 -1
- package/dist/server/components/cms-editable.js +0 -18
- package/dist/server/components/cms-editable.js.map +0 -1
- package/dist/server/components/get-content-type.d.ts +0 -12
- package/dist/server/components/get-content-type.js +0 -51
- package/dist/server/components/get-content-type.js.map +0 -1
- package/dist/server/components/index.d.ts +0 -4
- package/dist/server/components/index.js +0 -5
- package/dist/server/components/index.js.map +0 -1
- package/dist/server/components/queries.js.map +0 -1
- package/dist/server/components/visual-builder/Composition.js.map +0 -1
- package/dist/server/components/visual-builder/functions.js.map +0 -1
- package/dist/server/components/visual-builder/index.d.ts +0 -4
- package/dist/server/components/visual-builder/index.js +0 -4
- package/dist/server/components/visual-builder/index.js.map +0 -1
- package/dist/server/components/visual-builder/types.js.map +0 -1
- package/dist/server/context.d.ts +0 -22
- package/dist/server/context.js +0 -81
- package/dist/server/context.js.map +0 -1
- package/dist/server/factory.d.ts +0 -10
- package/dist/server/factory.js +0 -18
- package/dist/server/factory.js.map +0 -1
- package/dist/server/index.d.ts +0 -6
- package/dist/server/index.js +0 -6
- package/dist/server/index.js.map +0 -1
- /package/dist/{server/components → components/cms-content-area}/types.js +0 -0
- /package/dist/components/{cms-styles.d.ts → cms-styles/index.d.ts} +0 -0
- /package/dist/{server → components}/type-utils.d.ts +0 -0
- /package/dist/{server → components}/type-utils.js +0 -0
- /package/dist/{server/components → components}/visual-builder/types.js +0 -0
- /package/dist/{server/components → data}/queries.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,2 +1,36 @@
|
|
|
1
|
-
# Optimizely SaaS CMS React Components
|
|
2
|
-
|
|
1
|
+
# Optimizely SaaS CMS React Components
|
|
2
|
+
> [!WARNING]
|
|
3
|
+
> This documentation is for V3 and up. This version introduced a number of breaking changes - most notably it significantly simplified the exports.
|
|
4
|
+
>
|
|
5
|
+
> For some of the commonly used old exports it still contains placeholders, however those are deprecated and will be removed in a future version
|
|
6
|
+
|
|
7
|
+
This package provides two main entry points, depending on your build environment:
|
|
8
|
+
- `@remkoj/optimizely-cms-react` This export contains the library, whith React components that use a client side context
|
|
9
|
+
- `@remkoj/optimizely-cms-react/rsc` This export contains the library, whith React components that use a server side context
|
|
10
|
+
|
|
11
|
+
## Components & Functions
|
|
12
|
+
### Shared components & functions
|
|
13
|
+
Regardless of which export you're using, the following components are available. The properties of each component are document using JSDoc annotations in the package.
|
|
14
|
+
|
|
15
|
+
| Component/Function | Description |
|
|
16
|
+
| --- | --- |
|
|
17
|
+
| `<CmsContent {...props} />` | This component renders a content item from the Optimizely CMS.<br/>It leverages the factory provided by the context to resolve the Optimizely CMS type to a React Component.<br/>When no, or invalid, initial data is provided, this component will use the connection to Optimizely Graph from the context to fetch the data for the component |
|
|
18
|
+
| `<CmsContentArea {...props} />` | A helper component to quickly and easily render the contents of a Content Area type property in Optimizely CMS.<br /><br />The Content Area leverages the `<CmsContent />` internally to render each item contained within the area. It ensures that the prefix is `component`, forcing a different component to be used when a "non-component" (e.g. a page) is added to the content-area |
|
|
19
|
+
| `<CmsEditable {...props} />` | This is a basic wrapper that performs the logic needed to inject the appropriate attributes into the HTML. <br/>It will use a `div` element by default for that, but you can set any React component to be used on the `as` property. The only requirement is that the property set for this accepts the `data-*` attributes injected and outputs these into the page. Any additional attributes will be passed to the output component. |
|
|
20
|
+
| `<RichText {...props} />` | This is a renderer for the structured HTML output of Optimizely CMS, leveraging the `ComponentFactory` to resolve the actual React components used for the output. It prefixes all types with `RichText`<br/>The library exports a `DefaultComponents` constant, which can be used to populate the ComponentFactory with the basic HTML elements |
|
|
21
|
+
| `<OptimizelyComposition {...props} />` | This is the renderer for a Visual Builder Experience. This will render the composition using the `ComponentFactory` from the context and `<CmsContent />` component.<br/><br/>The style definitions of Visual Builder will be provided to the `layoutProps` property of your CmsComponent |
|
|
22
|
+
|
|
23
|
+
### Server Side components & functions
|
|
24
|
+
In addition to the components above, the following methods are available in the `@remkoj/optimizely-cms-react/rsc` export only:
|
|
25
|
+
|
|
26
|
+
| Component / Function | Description |
|
|
27
|
+
| --- | --- |
|
|
28
|
+
| `getServerContext()` | Retrieve the current server context in use, this leverages `React.cache` to ensure a single context is used within the cache scope defined by your React Server implementation |
|
|
29
|
+
|
|
30
|
+
### Client Side components & functions
|
|
31
|
+
In addition to the components above, the following methods are available in the `@remkoj/optimizely-cms-react` export only:
|
|
32
|
+
|
|
33
|
+
| Component / Function | Description |
|
|
34
|
+
| --- | --- |
|
|
35
|
+
| `<OptimizelyCms {...props} />` | React context to be used client side, without defaults. This needs to be initialized completely by your code. |
|
|
36
|
+
| `useOptimizelyCms()` | React Hook to retrieve the current Optimizely CMS Context in your client-side component. |
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import { type PropsWithContext } from '../context/types.js';
|
|
3
|
+
import { type CmsContentAreaComponent } from './cms-content-area/index.js';
|
|
4
|
+
import { type CmsEditableComponent } from './cms-editable/index.js';
|
|
5
|
+
import { type CmsContentComponent } from './cms-content/client.js';
|
|
6
|
+
import { type OptimizelyCompositionComponent } from './visual-builder/index.js';
|
|
7
|
+
import { type RichTextComponent } from './rich-text/index.js';
|
|
8
|
+
export type { BaseStyleDefinition, ElementStyleDefinition, LayoutProps, LayoutPropsSetting, LayoutPropsSettingChoices, LayoutPropsSettingKeys, LayoutPropsSettingValues, NodeStyleDefinition, StyleDefinition, StyleSetting } from "./cms-styles/index.js";
|
|
9
|
+
export { extractSettings, readSetting } from "./cms-styles/index.js";
|
|
10
|
+
export { DefaultComponents as RichTextComponentDictionary } from './rich-text/components.js';
|
|
11
|
+
/**
|
|
12
|
+
* Wrapper function to turn context dependant components into easy to use
|
|
13
|
+
* client components
|
|
14
|
+
*
|
|
15
|
+
* @param component The component where the `ctx` parameter must be fulfilled
|
|
16
|
+
* @returns The component, without CTX parameter
|
|
17
|
+
*/
|
|
18
|
+
export declare function clientContextAware<P = any>(component: ComponentType<PropsWithContext<P>>): ComponentType<P>;
|
|
19
|
+
/**
|
|
20
|
+
* Client side Optimizely CMS Editable
|
|
21
|
+
*/
|
|
22
|
+
export declare const CmsEditable: CmsEditableComponent;
|
|
23
|
+
/**
|
|
24
|
+
* Client side Optimizely CMS Content, leveraging the CMS Context to load the
|
|
25
|
+
* content type and content data when needed
|
|
26
|
+
*/
|
|
27
|
+
export declare const CmsContent: CmsContentComponent;
|
|
28
|
+
/**
|
|
29
|
+
* Client side Optimizely CMS Content Area, leveraging the CMS Context to infer
|
|
30
|
+
* the connection to Optimizely Graph and component dictionary.
|
|
31
|
+
*/
|
|
32
|
+
export declare const CmsContentArea: CmsContentAreaComponent;
|
|
33
|
+
/**
|
|
34
|
+
* Client side Optimizely Composition (e.g. Visual Builder), leveraging the CMS
|
|
35
|
+
* Context to infer the connection to Optimizely Graph and component
|
|
36
|
+
* dictionary.
|
|
37
|
+
*/
|
|
38
|
+
export declare const OptimizelyComposition: OptimizelyCompositionComponent;
|
|
39
|
+
/**
|
|
40
|
+
* Client side renderer for Rich Text
|
|
41
|
+
*/
|
|
42
|
+
export declare const RichText: RichTextComponent;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useOptimizelyCms } from '../context/client.js';
|
|
4
|
+
import { cmsContentAware } from './cms-content/utils.js';
|
|
5
|
+
import { CmsContentArea as BaseContentArea } from './cms-content-area/index.js'; // Both RSC & Client capable
|
|
6
|
+
import { CmsEditable as BaseEditable } from './cms-editable/index.js'; // Both RSC & Client capable
|
|
7
|
+
import { CmsContent as BaseCmsContent } from './cms-content/client.js'; // Different components for RSC & Client
|
|
8
|
+
import { OptimizelyComposition as BaseOptimizelyComposition } from './visual-builder/index.js'; // Both RSC & Client capable
|
|
9
|
+
import { RichText as BaseRichText } from './rich-text/index.js';
|
|
10
|
+
export { extractSettings, readSetting } from "./cms-styles/index.js";
|
|
11
|
+
// Export dictionary
|
|
12
|
+
export { DefaultComponents as RichTextComponentDictionary } from './rich-text/components.js';
|
|
13
|
+
/**
|
|
14
|
+
* Wrapper function to turn context dependant components into easy to use
|
|
15
|
+
* client components
|
|
16
|
+
*
|
|
17
|
+
* @param component The component where the `ctx` parameter must be fulfilled
|
|
18
|
+
* @returns The component, without CTX parameter
|
|
19
|
+
*/
|
|
20
|
+
export function clientContextAware(component) {
|
|
21
|
+
const BaseComponent = component;
|
|
22
|
+
const ClientContextInjector = (props) => {
|
|
23
|
+
const ctx = useOptimizelyCms();
|
|
24
|
+
return _jsx(BaseComponent, { ctx: ctx, ...props });
|
|
25
|
+
};
|
|
26
|
+
return ClientContextInjector;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Client side Optimizely CMS Editable
|
|
30
|
+
*/
|
|
31
|
+
export const CmsEditable = clientContextAware(BaseEditable);
|
|
32
|
+
/**
|
|
33
|
+
* Client side Optimizely CMS Content, leveraging the CMS Context to load the
|
|
34
|
+
* content type and content data when needed
|
|
35
|
+
*/
|
|
36
|
+
export const CmsContent = clientContextAware(BaseCmsContent);
|
|
37
|
+
/**
|
|
38
|
+
* Client side Optimizely CMS Content Area, leveraging the CMS Context to infer
|
|
39
|
+
* the connection to Optimizely Graph and component dictionary.
|
|
40
|
+
*/
|
|
41
|
+
export const CmsContentArea = cmsContentAware(clientContextAware(BaseContentArea), CmsContent);
|
|
42
|
+
/**
|
|
43
|
+
* Client side Optimizely Composition (e.g. Visual Builder), leveraging the CMS
|
|
44
|
+
* Context to infer the connection to Optimizely Graph and component
|
|
45
|
+
* dictionary.
|
|
46
|
+
*/
|
|
47
|
+
export const OptimizelyComposition = cmsContentAware(clientContextAware(BaseOptimizelyComposition), CmsContent);
|
|
48
|
+
/**
|
|
49
|
+
* Client side renderer for Rich Text
|
|
50
|
+
*/
|
|
51
|
+
export const RichText = BaseRichText;
|
|
52
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/components/client.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAGZ,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAEvD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAExD,OAAO,EAAE,cAAc,IAAI,eAAe,EAAgC,MAAM,6BAA6B,CAAA,CAAC,4BAA4B;AAC1I,OAAO,EAAE,WAAW,IAAI,YAAY,EAA6B,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+
import { type ReactNode } from 'react';
|
|
2
|
+
import type { BaseCmsContentProps } from './types.js';
|
|
3
|
+
export type * from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* React Server Side component for the CmsContent
|
|
6
|
+
*
|
|
7
|
+
* @param param0
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function CmsContent<LocalesType = string>({ contentType, contentTypePrefix, contentLink: rawContentLink, children, fragmentData, layoutProps, noDataLoad, ctx }: BaseCmsContentProps<LocalesType>): ReactNode;
|
|
11
|
+
export default CmsContent;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import getContentType from './get-content-type.js';
|
|
4
|
+
import getContent from './get-content.js';
|
|
5
|
+
import { AuthMode, normalizeContentLink, contentLinkToString } from '@remkoj/optimizely-graph-client';
|
|
6
|
+
import resolveComponent, { isComponentMissingComponent } from './resolve-component.js';
|
|
7
|
+
import resolveContentType from './resolve-content-type.js';
|
|
8
|
+
import { useState, useMemo, useEffect } from 'react';
|
|
9
|
+
/**
|
|
10
|
+
* React Server Side component for the CmsContent
|
|
11
|
+
*
|
|
12
|
+
* @param param0
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
export function CmsContent({
|
|
16
|
+
// Own properties
|
|
17
|
+
contentType, contentTypePrefix, contentLink: rawContentLink, children, fragmentData, layoutProps, noDataLoad, ctx }) {
|
|
18
|
+
// Prepare context
|
|
19
|
+
const graphClient = ctx.client;
|
|
20
|
+
const [myContentType, setMyContentType] = useState(resolveContentType(contentType, fragmentData));
|
|
21
|
+
const contentLink = useMemo(() => normalizeContentLink(rawContentLink), [rawContentLink]);
|
|
22
|
+
const Component = useMemo(() => resolveComponent(myContentType, contentTypePrefix, ctx), [myContentType, contentTypePrefix, ctx]);
|
|
23
|
+
const [data, setData] = useState(getContent(graphClient, contentLink, Component, fragmentData, true));
|
|
24
|
+
// Provide a bit of debugging context
|
|
25
|
+
if (graphClient?.debug) {
|
|
26
|
+
const mode = ctx.inEditMode ? "Edit" : ctx.inPreviewMode ? "Preview" : "Public";
|
|
27
|
+
console.log(`👔 [CmsContent] ${mode} mode active for content with id: ${contentLinkToString(contentLink)} of type ${myContentType?.join("/") ?? "unknown"}`);
|
|
28
|
+
if ((ctx.inEditMode || ctx.inPreviewMode) && graphClient.currentAuthMode == AuthMode.Public)
|
|
29
|
+
console.warn(`🟠 [CmsContent] ${mode} mode active without an authenticated graphClient, this will cause problems. Make sure the context has an authenticated client.`);
|
|
30
|
+
}
|
|
31
|
+
// Ensure we have a content type to work with
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
let isCancelled = false;
|
|
34
|
+
if (myContentType || !contentLink)
|
|
35
|
+
return;
|
|
36
|
+
if (!graphClient) {
|
|
37
|
+
console.warn(`🔴 [CmsContent] No Optimizely Graph Client present unable to load content type`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (graphClient.debug)
|
|
41
|
+
console.warn(`🟠 [CmsContent] No content type provided for content ${contentLinkToString(contentLink)}, this causes an additional GraphQL query to resolve the myContentType`);
|
|
42
|
+
getContentType(contentLink, graphClient).then(ct => {
|
|
43
|
+
if (!isCancelled)
|
|
44
|
+
setMyContentType(ct);
|
|
45
|
+
});
|
|
46
|
+
return () => {
|
|
47
|
+
isCancelled = true;
|
|
48
|
+
};
|
|
49
|
+
}, [myContentType, contentLink, graphClient]);
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
let isCancelled = false;
|
|
52
|
+
// Only load if load hasn't been disabled, and we have both the contentLink and Component
|
|
53
|
+
if (noDataLoad || !contentLink || !Component)
|
|
54
|
+
return;
|
|
55
|
+
if (!graphClient) {
|
|
56
|
+
console.warn(`🔴 [CmsContent] No Optimizely Graph Client present unable to load content type`);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
getContent(graphClient, contentLink, Component, fragmentData).then(newData => {
|
|
60
|
+
if (!isCancelled)
|
|
61
|
+
setData(newData);
|
|
62
|
+
});
|
|
63
|
+
return () => {
|
|
64
|
+
isCancelled = true;
|
|
65
|
+
};
|
|
66
|
+
}, [graphClient, contentLink, Component, fragmentData, noDataLoad]);
|
|
67
|
+
if (isComponentMissingComponent(Component))
|
|
68
|
+
return _jsx(Component, {});
|
|
69
|
+
return _jsx(Component, { contentLink: contentLink, data: data, inEditMode: ctx.inEditMode, layoutProps: layoutProps, children: children });
|
|
70
|
+
}
|
|
71
|
+
export default CmsContent;
|
|
72
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/components/cms-content/client.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAIZ,OAAO,cAAc,MAAM,uBAAuB,CAAA;AAClD,OAAO,UAAU,MAAM,kBAAkB,CAAA;AACzC,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,mBAAmB,EAAoB,MAAM,iCAAiC,CAAA;AACvH,OAAO,gBAAgB,EAAE,EAAE,2BAA2B,EAAE,MAAM,wBAAwB,CAAA;AACtF,OAAO,kBAAkB,MAAM,2BAA2B,CAAA;AAE1D,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,MAAO,OAAO,CAAA;AAIrD;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAuB;AAC7C,iBAAiB;AACjB,WAAW,EACX,iBAAiB,EACjB,WAAW,EAAE,cAAc,EAC3B,QAAQ,EACR,YAAY,EACZ,WAAW,EACX,UAAU,EACV,GAAG,EAC6B;IAEhC,kBAAkB;IAClB,MAAM,WAAW,GAAG,GAAG,CAAC,MAAM,CAAA;IAC9B,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,GAAG,QAAQ,CAA0B,kBAAkB,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC,CAAA;IAC1H,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC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ED,eAAe,UAAU,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type IOptiGraphClient, type ContentLink, type InlineContentLink } from "@remkoj/optimizely-graph-client";
|
|
2
|
+
import type { ContentType } from "../../types.js";
|
|
3
|
+
export type ContentTypeResolver = (type: ContentType | null | undefined, link: ContentLink, gqlClient: IOptiGraphClient) => PromiseLike<ContentType | undefined>;
|
|
4
|
+
export declare function valueToPromiseLike<T>(value: T): PromiseLike<T>;
|
|
5
|
+
/**
|
|
6
|
+
* Resolve the ContentType of an Optimizely CMS Component, identified by its content link
|
|
7
|
+
*
|
|
8
|
+
* @param link The ContentLink of the content item
|
|
9
|
+
* @param gqlClient The GraphQL client to use
|
|
10
|
+
* @returns The ContentType, or undefined if it cannot be resolved
|
|
11
|
+
*/
|
|
12
|
+
export declare function getContentType(link: ContentLink | InlineContentLink, gqlClient: IOptiGraphClient): Promise<ContentType | undefined>;
|
|
13
|
+
export default getContentType;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { isContentLink, isInlineContentLink, contentLinkToString } from "@remkoj/optimizely-graph-client";
|
|
2
|
+
import { gql } from 'graphql-request';
|
|
3
|
+
import { contentLinkToRequestVariables, normalizeContentType } from "../../utilities.js";
|
|
4
|
+
export function valueToPromiseLike(value) {
|
|
5
|
+
const pl = {
|
|
6
|
+
then: (onfulfilled) => {
|
|
7
|
+
if (!onfulfilled)
|
|
8
|
+
return value;
|
|
9
|
+
return onfulfilled(value);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
return pl;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Resolve the ContentType of an Optimizely CMS Component, identified by its content link
|
|
16
|
+
*
|
|
17
|
+
* @param link The ContentLink of the content item
|
|
18
|
+
* @param gqlClient The GraphQL client to use
|
|
19
|
+
* @returns The ContentType, or undefined if it cannot be resolved
|
|
20
|
+
*/
|
|
21
|
+
export async function getContentType(link, gqlClient) {
|
|
22
|
+
if (isInlineContentLink(link)) {
|
|
23
|
+
console.error(`🔴 [CmsContent][getContentType] Unable to dynamically resolve the content type for inline content items: ${contentLinkToString(link)}`);
|
|
24
|
+
throw new Error(`Unable to dynamically resolve the content type for inline content items: ${contentLinkToString(link)}`);
|
|
25
|
+
}
|
|
26
|
+
if (!isContentLink(link)) {
|
|
27
|
+
console.error(`🔴 [CmsContent][getContentType] The provided link is not a valid content link: ${contentLinkToString(link)}`);
|
|
28
|
+
throw new Error(`The provided link is not a valid content link: ${contentLinkToString(link)}`);
|
|
29
|
+
}
|
|
30
|
+
const gqlQueryVars = contentLinkToRequestVariables(link);
|
|
31
|
+
const gqlResponse = await gqlClient.request(getContentTypeQuery, gqlQueryVars);
|
|
32
|
+
if (gqlResponse.Content?.total != 1) {
|
|
33
|
+
if (gqlClient.debug)
|
|
34
|
+
console.error(`🔴 [CmsContent][getContentType] Expected exactly one type, received ${gqlResponse.Content?.total ?? 0} types for`, gqlQueryVars);
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
const items = gqlResponse.Content?.items;
|
|
38
|
+
if (!items || items.length == 0) {
|
|
39
|
+
console.error(`🔴 [CmsContent][getContentType] The content item could not be found! (${contentLinkToString(link)})`);
|
|
40
|
+
throw new Error("The content item could not be found!");
|
|
41
|
+
}
|
|
42
|
+
const contentType = normalizeContentType(items[0]?._metadata?.types);
|
|
43
|
+
if (!contentType) {
|
|
44
|
+
console.error(`🔴 [CmsContent][getContentType] The item did not contain type information! (${contentLinkToString(link)})`);
|
|
45
|
+
throw new Error("The item did not contain type information");
|
|
46
|
+
}
|
|
47
|
+
return contentType;
|
|
48
|
+
}
|
|
49
|
+
export default getContentType;
|
|
50
|
+
const getContentTypeQuery = gql `query getContentType($key: String!, $version: String, $locale: [Locales])
|
|
51
|
+
{
|
|
52
|
+
_Content(
|
|
53
|
+
where: {
|
|
54
|
+
_metadata: {
|
|
55
|
+
key: { eq: $key }
|
|
56
|
+
version: { eq: $version }
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
locale: $locale
|
|
60
|
+
limit: 1
|
|
61
|
+
) {
|
|
62
|
+
items {
|
|
63
|
+
_metadata {
|
|
64
|
+
types
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
total
|
|
68
|
+
}
|
|
69
|
+
}`;
|
|
70
|
+
//# sourceMappingURL=get-content-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-content-type.js","sourceRoot":"","sources":["../../../src/components/cms-content/get-content-type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,mBAAmB,EAAmE,MAAM,iCAAiC,CAAA;AAE1K,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACrC,OAAO,EAAE,6BAA6B,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAIxF,MAAM,UAAU,kBAAkB,CAAI,KAAQ;IAE5C,MAAM,EAAE,GAAoB;QAC1B,IAAI,EAAE,CAAe,WAAiF,EAAE,EAAE;YACxG,IAAI,CAAC,WAAW;gBACd,OAAO,KAAyC,CAAA;YAClD,OAAO,WAAW,CAAC,KAAK,CAA0B,CAAA;QACpD,CAAC;KACF,CAAA;IACD,OAAO,EAAE,CAAA;AACX,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,IAAqC,EAAE,SAA2B;IAEnG,IAAI,mBAAmB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5B,OAAO,CAAC,KAAK,CAAC,4GAA6G,mBAAmB,CAAC,IAAI,CAAE,EAAE,CAAC,CAAA;QACxJ,MAAM,IAAI,KAAK,CAAC,4EAA6E,mBAAmB,CAAC,IAAI,CAAE,EAAE,CAAC,CAAA;IAC9H,CAAC;IAED,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;QACvB,OAAO,CAAC,KAAK,CAAC,kFAAmF,mBAAmB,CAAC,IAAI,CAAE,EAAE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ContentLink, type InlineContentLink, type IOptiGraphClient } from "@remkoj/optimizely-graph-client";
|
|
2
|
+
import { CmsComponent } from "../../types.js";
|
|
3
|
+
export declare function getContent<NDL extends boolean = false>(client: IOptiGraphClient | undefined, contentLink: InlineContentLink | ContentLink | undefined, Component: CmsComponent<any> | undefined, fragmentData: Record<string, any> | undefined | null, noDataLoad?: NDL): NDL extends true ? Record<string, any> : Promise<Record<string, any>>;
|
|
4
|
+
export default getContent;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { print } from 'graphql';
|
|
2
|
+
import { isContentLink, isInlineContentLink, contentLinkToString, OptiCmsSchema } from "@remkoj/optimizely-graph-client";
|
|
3
|
+
import { CmsContentFragments } from "../../data/queries.js";
|
|
4
|
+
import { validatesFragment, contentLinkToRequestVariables, isCmsComponentWithFragment, isCmsComponentWithDataQuery } from "../../utilities.js";
|
|
5
|
+
export function getContent(client, contentLink, Component, fragmentData, noDataLoad) {
|
|
6
|
+
const debug = client?.debug ?? false;
|
|
7
|
+
// Handle provided fragment
|
|
8
|
+
const componentLabel = Component?.displayName ?? Component?.toString() ?? 'undefined';
|
|
9
|
+
const fragmentProps = fragmentData ? Object.getOwnPropertyNames(fragmentData).filter(x => !CmsContentFragments.IContentDataProps.includes(x)) : [];
|
|
10
|
+
if (fragmentData && fragmentProps.length > 0) {
|
|
11
|
+
if (debug)
|
|
12
|
+
console.log("⚪ [CmsContent][getContent] Rendering CMS Component using fragment information", fragmentProps);
|
|
13
|
+
if (validatesFragment(Component) && !Component.validateFragment(fragmentData))
|
|
14
|
+
console.warn("🔴 [CmsContent][getContent] Invalid fragment data received, falling back to loading for ", componentLabel);
|
|
15
|
+
else
|
|
16
|
+
return (noDataLoad ? fragmentData : Promise.resolve(fragmentData));
|
|
17
|
+
}
|
|
18
|
+
if (isInlineContentLink(contentLink)) {
|
|
19
|
+
console.error(`🔴 [CmsContent][getContent] No data for content ${contentLinkToString(contentLink)}, data cannot be resolved for inline content`);
|
|
20
|
+
throw new Error(`Unable to render Inline CMS Content without data. (Content Type: ${componentLabel}; Content Link: ${contentLinkToString(contentLink)}; Data keys: ${Object.getOwnPropertyNames(fragmentData ?? {}).join(", ")})`);
|
|
21
|
+
}
|
|
22
|
+
if (noDataLoad) {
|
|
23
|
+
if (debug)
|
|
24
|
+
console.log(`⚪ [CmsContent][getContent] Component of type "${componentLabel}" was prohibited to load data`);
|
|
25
|
+
return (noDataLoad ? {} : Promise.resolve({}));
|
|
26
|
+
}
|
|
27
|
+
if (!isContentLink(contentLink)) {
|
|
28
|
+
if (debug)
|
|
29
|
+
console.log(`🔴 [CmsContent][getContent] Unable to load data for "${componentLabel}" without a valid content link`);
|
|
30
|
+
return Promise.resolve({});
|
|
31
|
+
}
|
|
32
|
+
// Return immediately when there's no client
|
|
33
|
+
if (!client) {
|
|
34
|
+
console.log(`🔴 [CmsContent][getContent] Data loading for "${componentLabel}" requires a GraphQL Client`);
|
|
35
|
+
throw new Error(`Data loading for "${componentLabel}" requires a GraphQL Client`);
|
|
36
|
+
}
|
|
37
|
+
if (isCmsComponentWithFragment(Component))
|
|
38
|
+
return getComponentDataFromFragment(Component, contentLink, client).then(data => (data || {}));
|
|
39
|
+
if (isCmsComponentWithDataQuery(Component)) {
|
|
40
|
+
const gqlQuery = Component.getDataQuery();
|
|
41
|
+
const gqlVariables = contentLinkToRequestVariables(contentLink);
|
|
42
|
+
if (client.debug)
|
|
43
|
+
console.log("⚪ [CmsContent] Component data fetching variables:", gqlVariables);
|
|
44
|
+
return client.request(gqlQuery, gqlVariables).then(gqlResponse => {
|
|
45
|
+
if (client.debug)
|
|
46
|
+
console.log("⚪ [CmsContent] Component request the following data:", gqlResponse);
|
|
47
|
+
return gqlResponse;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
// Assume there's no data load required for the component
|
|
51
|
+
if (client.debug)
|
|
52
|
+
console.log(`⚪ [CmsContent] Component of type "${componentLabel}" did not request loading of data`);
|
|
53
|
+
return Promise.resolve({});
|
|
54
|
+
}
|
|
55
|
+
export default getContent;
|
|
56
|
+
async function getComponentDataFromFragment(Component, contentLink, client) {
|
|
57
|
+
const [name, fragment] = Component.getDataFragment();
|
|
58
|
+
if (client.debug)
|
|
59
|
+
console.log(`⚪ [CmsContent] Component data fetching using fragment: ${name}`);
|
|
60
|
+
const fragmentQuery = client.currentOptiCmsSchema == OptiCmsSchema.CMS12 ?
|
|
61
|
+
`query getContentFragmentById($key: String!, $version: Int, $locale: [Locales!]) { contentById: Content(where: { ContentLink: { GuidValue: { eq: $key }, WorkId: { eq: $version } } }, locale: $locale) { total, items { _type: __typename, _metadata: ContentLink { key: GuidValue, version: WorkId }, _locale: Language { name: Name } ...${name} }}}\n ${print(fragment)}` :
|
|
62
|
+
`query getContentFragmentById($key: String!, $version: String, $locale: [Locales!]) {contentById: _Content(where: {_metadata: {key: { eq: $key }, version: { eq: $version }}} locale: $locale) { total, items { _type: __typename, _metadata { key, version, locale } ...${name} }}}\n ${print(fragment)}`;
|
|
63
|
+
const fragmentVariables = contentLinkToRequestVariables(contentLink);
|
|
64
|
+
if (client.currentOptiCmsSchema == OptiCmsSchema.CMS12) {
|
|
65
|
+
try {
|
|
66
|
+
const versionNr = fragmentVariables.version ? Number.parseInt(fragmentVariables.version) : 0;
|
|
67
|
+
if (versionNr > 0)
|
|
68
|
+
fragmentVariables.version = versionNr;
|
|
69
|
+
else
|
|
70
|
+
fragmentVariables.version = undefined;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
fragmentVariables.version = undefined;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (client.debug)
|
|
77
|
+
console.log(`⚪ [CmsContent] Component data fetching using variables: ${JSON.stringify(fragmentVariables)}`);
|
|
78
|
+
const fragmentResponse = await client.request(fragmentQuery, fragmentVariables);
|
|
79
|
+
const totalItems = fragmentResponse.contentById.total || 0;
|
|
80
|
+
if (totalItems < 1)
|
|
81
|
+
throw new Error(`CmsContent expected to load exactly one content item of type ${name}, received ${totalItems} from Optimizely Graph. Content Item: ${JSON.stringify(fragmentVariables)}`);
|
|
82
|
+
if (totalItems > 1 && client.debug)
|
|
83
|
+
console.warn(`🟠 [CmsContent] Resolved ${totalItems} content items, expected only 1. Picked the first one`);
|
|
84
|
+
if (client.currentOptiCmsSchema == OptiCmsSchema.CMS12) {
|
|
85
|
+
fragmentResponse.contentById.items[0]._metadata.locale = fragmentResponse.contentById.items[0]._metadata.locale ?? fragmentResponse.contentById.items[0]._locale?.name;
|
|
86
|
+
if (fragmentResponse.contentById.items[0]._locale)
|
|
87
|
+
delete fragmentResponse.contentById.items[0]._locale;
|
|
88
|
+
}
|
|
89
|
+
return fragmentResponse.contentById.items[0];
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=get-content.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-content.js","sourceRoot":"","sources":["../../../src/components/cms-content/get-content.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAC/B,OAAO,EAAmE,aAAa,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAE1L,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,6BAA6B,EAAE,0BAA0B,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AAE/I,MAAM,UAAU,UAAU,CAA8B,MAAoC,EAAE,WAAwD,EAAE,SAAwC,EAAE,YAAoD,EAAE,UAAgB;IAEpQ,MAAM,KAAK,GAAG,MAAM,EAAE,KAAK,IAAI,KAAK,CAAA;IAEpC,2BAA2B;IAC3B,MAAM,cAAc,GAAY,SAAS,EAAE,WAAW,IAAI,SAAS,EAAE,QAAQ,EAAE,IAAI,WAAW,CAAA;IAC9F,MAAM,aAAa,GAAG,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,mBAAmB,CAAC,YAAY,CAA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AC1D,IAAI,UAAU,GAAG,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,gEAAiE,IAAK,cAAe,UAAW,yCAA0C,IAAI,CAAC,SAAS,CAAE,iBAAiB,CAAE,EAAE,CAAC,CAAA;IACpM,IAAI,UAAU,GAAG,CAAC,IAAI,MAAM,CAAC,KAAK;QAAE,OAAO,CAAC,IAAI,CAAC,4BAA6B,UAAW,uDAAuD,CAAC,CAAA;IACjJ,IAAI,MAAM,CAAC,oBAAoB,IAAI,aAAa,CAAC,KAAK,EAAE,CAAC;QACrD,gBAAgB,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,GAAG,gBAAgB,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,IAAI,gBAAgB,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,IAAI,CAAA;QACtK,IAAI,gBAAgB,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO;YAC7C,OAAO,gBAAgB,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAA;IAC5D,CAAC;IACD,OAAO,gBAAgB,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AAChD,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type ComponentTypeHandle } from "../../factory/types.js";
|
|
2
|
+
import { type EnhancedCmsComponent } from "./types.js";
|
|
3
|
+
import { type GenericContext } from "../../context/types.js";
|
|
4
|
+
import { type FunctionComponent, type PropsWithChildren, type ComponentType } from "react";
|
|
5
|
+
/**
|
|
6
|
+
* Helper function to safely resolve a component from the ContentType information to
|
|
7
|
+
*
|
|
8
|
+
* @param contentType The content type to get the component for
|
|
9
|
+
* @param prefix The prefix/context of the component (typically "component", "element", etc..)
|
|
10
|
+
* @param ctx The context in which to operate
|
|
11
|
+
* @returns The component required for rendering
|
|
12
|
+
*/
|
|
13
|
+
export declare function resolveComponent(contentType: ComponentTypeHandle | null | undefined, prefix: string | null | undefined, ctx: GenericContext): EnhancedCmsComponent | ComponentMissingComponent;
|
|
14
|
+
export default resolveComponent;
|
|
15
|
+
export type ComponentMissingComponent = FunctionComponent<PropsWithChildren> & {
|
|
16
|
+
displayName: 'Opti::ComponentMissing';
|
|
17
|
+
};
|
|
18
|
+
export declare function isComponentMissingComponent(toTest: ComponentType<any> | null | undefined): toTest is ComponentMissingComponent;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import * as Utils from "../../utilities.js";
|
|
3
|
+
/**
|
|
4
|
+
* Helper function to safely resolve a component from the ContentType information to
|
|
5
|
+
*
|
|
6
|
+
* @param contentType The content type to get the component for
|
|
7
|
+
* @param prefix The prefix/context of the component (typically "component", "element", etc..)
|
|
8
|
+
* @param ctx The context in which to operate
|
|
9
|
+
* @returns The component required for rendering
|
|
10
|
+
*/
|
|
11
|
+
export function resolveComponent(contentType, prefix, ctx) {
|
|
12
|
+
const { factory, isDebug, inEditMode } = ctx;
|
|
13
|
+
// Ensure we have a factory - we should, but lets' help by providing an explicit error
|
|
14
|
+
if (!factory) {
|
|
15
|
+
console.error(`🔴 [CmsContent] No content type factory registered in the context`);
|
|
16
|
+
throw new Error("Empty factory on the context");
|
|
17
|
+
}
|
|
18
|
+
// Optimizely Graph stores the type in Most Significant first order, we need least significant first, also we're stripping out the common "Content" item from it
|
|
19
|
+
const myContentType = Array.isArray(contentType) ? (prefix ?
|
|
20
|
+
Utils.normalizeAndPrefixContentType(contentType.reverse(), prefix) :
|
|
21
|
+
Utils.normalizeContentType(contentType.reverse(), true)) : contentType;
|
|
22
|
+
// Validate that we have a value to ask the factory a component for
|
|
23
|
+
if (!myContentType || myContentType.length == 0) {
|
|
24
|
+
if (isDebug)
|
|
25
|
+
console.error(`🔴 [CmsContent] The content type ${JSON.stringify(contentType)}, with prefix ${JSON.stringify(prefix)} yielded an empty normalized type`);
|
|
26
|
+
throw new Error(`The content type ${JSON.stringify(contentType)}, with prefix ${JSON.stringify(prefix)} yielded an empty normalized type`);
|
|
27
|
+
}
|
|
28
|
+
// Resolve component
|
|
29
|
+
const Component = factory.resolve(myContentType);
|
|
30
|
+
// Handle component not found in factory
|
|
31
|
+
if (!Component) {
|
|
32
|
+
const contentTypeDisplay = Array.isArray(myContentType) ? myContentType.join('/') : myContentType;
|
|
33
|
+
if (isDebug)
|
|
34
|
+
console.warn(`🟠 [CmsContent] Component of type "${contentTypeDisplay}" not resolved by factory`);
|
|
35
|
+
if (isDebug || inEditMode) {
|
|
36
|
+
const ErrorComponent = props => _jsxs(_Fragment, { children: [_jsxs("div", { className: 'opti-error', children: ["Component of type \"", contentTypeDisplay, "\" not resolved by factory"] }), props.children] });
|
|
37
|
+
ErrorComponent.displayName = 'Opti::ComponentMissing';
|
|
38
|
+
return ErrorComponent;
|
|
39
|
+
}
|
|
40
|
+
const ErrorComponent = props => props.children;
|
|
41
|
+
ErrorComponent.displayName = 'Opti::ComponentMissing';
|
|
42
|
+
return ErrorComponent;
|
|
43
|
+
}
|
|
44
|
+
// Return the component
|
|
45
|
+
if (isDebug)
|
|
46
|
+
console.log("⚪ [CmsContent] Rendering item using component:", Component?.displayName ?? Component);
|
|
47
|
+
return Component;
|
|
48
|
+
}
|
|
49
|
+
export default resolveComponent;
|
|
50
|
+
export function isComponentMissingComponent(toTest) {
|
|
51
|
+
return toTest?.displayName == 'Opti::ComponentMissing';
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=resolve-component.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-component.js","sourceRoot":"","sources":["../../../src/components/cms-content/resolve-component.tsx"],"names":[],"mappings":";AAEA,OAAO,KAAK,KAAK,MAAM,oBAAoB,CAAA;AAI3C;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB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AC,CAAC,CAAC,CAAC,aAAa,CAAA;QACjG,IAAI,OAAO;YACP,OAAO,CAAC,IAAI,CAAC,sCAAuC,kBAAmB,2BAA2B,CAAC,CAAA;QACvG,IAAI,OAAO,IAAI,UAAU,EAAE,CAAC;YACxB,MAAM,cAAc,GAA+B,KAAK,CAAC,EAAE,CAAC,8BACxD,eAAK,SAAS,EAAC,YAAY,qCAAsB,kBAAkB,kCAAiC,EAClG,KAAK,CAAC,QAAQ,IACjB,CAAA;YACH,cAAc,CAAC,WAAW,GAAG,wBAAwB,CAAA;YACrD,OAAO,cAAc,CAAA;QACzB,CAAC;QACD,MAAM,cAAc,GAA+B,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAA;QAC1E,cAAc,CAAC,WAAW,GAAG,wBAAwB,CAAA;QACrD,OAAO,cAAc,CAAA;IACzB,CAAC;IAED,uBAAuB;IACvB,IAAI,OAAO;QACP,OAAO,CAAC,GAAG,CAAC,gDAAgD,EAAE,SAAS,EAAE,WAAW,IAAI,SAAS,CAAC,CAAA;IACtG,OAAO,SAAS,CAAA;AACpB,CAAC;AAED,eAAe,gBAAgB,CAAA;AAM/B,MAAM,UAAU,2BAA2B,CAAC,MAA6C;IAErF,OAAO,MAAM,EAAE,WAAW,IAAI,wBAAwB,CAAA;AAC1D,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve and normalize the content type for a call to a CmsContent component.
|
|
3
|
+
*
|
|
4
|
+
* When the contentType provided is invalid, it will try to resolve the
|
|
5
|
+
* contentType from the fragmentData, if provided.
|
|
6
|
+
*
|
|
7
|
+
* @param contentType The content type received by the CmsContent
|
|
8
|
+
* component
|
|
9
|
+
* @param fragmentData The data received to render the CmsContent
|
|
10
|
+
* @returns The content type
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolveContentType(contentType: string | (string | null)[] | null | undefined, fragmentData?: {
|
|
13
|
+
[fieldname: string]: any;
|
|
14
|
+
}): string[] | undefined;
|
|
15
|
+
export default resolveContentType;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isNonEmptyString } from "../../utilities.js";
|
|
2
|
+
/**
|
|
3
|
+
* Resolve and normalize the content type for a call to a CmsContent component.
|
|
4
|
+
*
|
|
5
|
+
* When the contentType provided is invalid, it will try to resolve the
|
|
6
|
+
* contentType from the fragmentData, if provided.
|
|
7
|
+
*
|
|
8
|
+
* @param contentType The content type received by the CmsContent
|
|
9
|
+
* component
|
|
10
|
+
* @param fragmentData The data received to render the CmsContent
|
|
11
|
+
* @returns The content type
|
|
12
|
+
*/
|
|
13
|
+
export function resolveContentType(contentType, fragmentData) {
|
|
14
|
+
if (Array.isArray(contentType)) {
|
|
15
|
+
const filtered = contentType.filter(isNonEmptyString);
|
|
16
|
+
if (filtered.length > 0)
|
|
17
|
+
return filtered;
|
|
18
|
+
}
|
|
19
|
+
else if (typeof (contentType) == 'string' && contentType.length > 0)
|
|
20
|
+
return contentType.split("/");
|
|
21
|
+
if (fragmentData) {
|
|
22
|
+
const metaTypes = fragmentData._metadata?.types;
|
|
23
|
+
if (metaTypes && Array.isArray(metaTypes))
|
|
24
|
+
return resolveContentType(metaTypes);
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
export default resolveContentType;
|
|
29
|
+
//# sourceMappingURL=resolve-content-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-content-type.js","sourceRoot":"","sources":["../../../src/components/cms-content/resolve-content-type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAErD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAAC,WAA0D,EAAE,YAAyC;IAEpI,IAAI,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,CAAC;QAC7B,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAA;QACrD,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC;YACnB,OAAO,QAAQ,CAAA;IACvB,CAAC;SAAM,IAAI,OAAM,CAAC,WAAW,CAAC,IAAI,QAAQ,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC;QAChE,OAAO,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEjC,IAAI,YAAY,EAAE,CAAC;QACf,MAAM,SAAS,GAAG,YAAY,CAAC,SAAS,EAAE,KAAK,CAAA;QAC/C,IAAI,SAAS,IAAI,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC;YACrC,OAAO,kBAAkB,CAAC,SAAS,CAAC,CAAA;IAC5C,CAAC;IAED,OAAO,SAAS,CAAA;AACpB,CAAC;AAED,eAAe,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import type { BaseCmsContentProps } from './types.js';
|
|
3
|
+
export type * from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* React Server Side component for the CmsContent
|
|
6
|
+
*
|
|
7
|
+
* @param param0
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function CmsContent<LocalesType = string>({ contentType, contentTypePrefix, contentLink: rawContentLink, children, fragmentData, layoutProps, noDataLoad, ctx }: BaseCmsContentProps<LocalesType>): Promise<ReactNode>;
|
|
11
|
+
export default CmsContent;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import getContentType from './get-content-type.js';
|
|
3
|
+
import getContent from './get-content.js';
|
|
4
|
+
import { AuthMode, normalizeContentLink, contentLinkToString } from '@remkoj/optimizely-graph-client';
|
|
5
|
+
import resolveComponent, { isComponentMissingComponent } from './resolve-component.js';
|
|
6
|
+
import resolveContentType from './resolve-content-type.js';
|
|
7
|
+
/**
|
|
8
|
+
* React Server Side component for the CmsContent
|
|
9
|
+
*
|
|
10
|
+
* @param param0
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
export async function CmsContent({
|
|
14
|
+
// Own properties
|
|
15
|
+
contentType, contentTypePrefix, contentLink: rawContentLink, children, fragmentData, layoutProps, noDataLoad, ctx }) {
|
|
16
|
+
// Prepare context
|
|
17
|
+
const graphClient = ctx.client;
|
|
18
|
+
// Normalize the ContentLink
|
|
19
|
+
const contentLink = normalizeContentLink(rawContentLink);
|
|
20
|
+
// Ensure we have the contentType
|
|
21
|
+
let myContentType = resolveContentType(contentType, fragmentData);
|
|
22
|
+
if (!myContentType) {
|
|
23
|
+
if (!graphClient) {
|
|
24
|
+
console.error(`🔴 [CmsContent] Unable to load content type - no Optimizely Graph client present`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (!contentLink) {
|
|
28
|
+
console.error(`🔴 [CmsContent] Unable to load content type - no content link present`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
myContentType = await getContentType(contentLink, graphClient);
|
|
32
|
+
}
|
|
33
|
+
// Provide a bit of debugging context
|
|
34
|
+
if (graphClient?.debug) {
|
|
35
|
+
const mode = ctx.inEditMode ? "Edit" : ctx.inPreviewMode ? "Preview" : "Public";
|
|
36
|
+
console.log(`👔 [CmsContent] ${mode} mode active for content with id: ${contentLinkToString(contentLink)} of type ${myContentType?.join("/") ?? "unknown"}`);
|
|
37
|
+
if ((ctx.inEditMode || ctx.inPreviewMode) && graphClient.currentAuthMode == AuthMode.Public)
|
|
38
|
+
console.warn(`🟠 [CmsContent] ${mode} mode active without an authenticated graphClient, this will cause problems. Make sure the context has an authenticated client.`);
|
|
39
|
+
}
|
|
40
|
+
// Retrieve the Component used to render this item
|
|
41
|
+
const Component = resolveComponent(myContentType, contentTypePrefix, ctx);
|
|
42
|
+
if (isComponentMissingComponent(Component))
|
|
43
|
+
return _jsx(Component, {});
|
|
44
|
+
// Ensure we have the data the Component requested
|
|
45
|
+
const data = await getContent(graphClient, contentLink, Component, fragmentData, noDataLoad);
|
|
46
|
+
// Output the actual component
|
|
47
|
+
return _jsx(Component, { contentLink: contentLink, data: data, inEditMode: ctx.inEditMode, layoutProps: layoutProps, children: children });
|
|
48
|
+
}
|
|
49
|
+
export default CmsContent;
|
|
50
|
+
//# sourceMappingURL=rsc.js.map
|