@pack/react 0.0.8 → 0.0.10
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render-sections.d.ts","sourceRoot":"","sources":["../src/render-sections.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"render-sections.d.ts","sourceRoot":"","sources":["../src/render-sections.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA6B,MAAM,OAAO,CAAC;AAqClD,wBAAgB,cAAc,CAAC,EAAE,OAAO,EAAE,EAAE,GAAG,4BA8B9C"}
|
package/dist/render-sections.js
CHANGED
|
@@ -1,29 +1,31 @@
|
|
|
1
|
-
import React, { useEffect } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
React.createElement(
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import React, { useEffect, useMemo } from "react";
|
|
2
|
+
import { useCustomizerShell } from "./use-customizer-shell";
|
|
3
|
+
import { usePreviewContext } from "./preview/preview-content";
|
|
4
|
+
import { sectionMap } from "./register-section";
|
|
5
|
+
import { storefrontSettingsSchema } from "./register-storefront-settings-schema";
|
|
6
|
+
function Sections({ sections }) {
|
|
7
|
+
const renderedSections = useMemo(() => {
|
|
8
|
+
return sections
|
|
9
|
+
.map((section) => {
|
|
10
|
+
// TODO: Return a consistent data structure from the API and the customizer
|
|
11
|
+
// Normalize section data
|
|
12
|
+
const key = section.id || section.clientId;
|
|
13
|
+
const data = section.data || section;
|
|
14
|
+
const schemaKey = data._template;
|
|
15
|
+
const Component = sectionMap.get(schemaKey);
|
|
16
|
+
if (!Component)
|
|
17
|
+
return null;
|
|
18
|
+
return (React.createElement("section", { key: key, "data-comp": schemaKey, "data-comp-id": key, hidden: data?.sectionVisibility === "hidden" },
|
|
19
|
+
React.createElement(Component, { "comp-name": schemaKey, cms: data })));
|
|
20
|
+
})
|
|
21
|
+
.filter(Boolean);
|
|
22
|
+
}, [sections]);
|
|
21
23
|
return React.createElement(React.Fragment, null, renderedSections);
|
|
22
24
|
}
|
|
23
25
|
export function RenderSections({ content }) {
|
|
24
26
|
const { isPreview, setPreviewStorefrontSettings } = usePreviewContext();
|
|
25
27
|
const { content: liveContent, storefrontSettings } = useCustomizerShell({
|
|
26
|
-
environment:
|
|
28
|
+
environment: "production",
|
|
27
29
|
isPreview,
|
|
28
30
|
sectionComponents: sectionMap,
|
|
29
31
|
data: {
|
|
@@ -40,8 +42,10 @@ export function RenderSections({ content }) {
|
|
|
40
42
|
storefrontSettings &&
|
|
41
43
|
setPreviewStorefrontSettings({ settings: storefrontSettings });
|
|
42
44
|
}, [setPreviewStorefrontSettings, storefrontSettings]);
|
|
43
|
-
const sections =
|
|
45
|
+
const sections = useMemo(() => {
|
|
46
|
+
return liveContent?.sections?.nodes || liveContent?.sections;
|
|
47
|
+
}, [liveContent?.sections]);
|
|
44
48
|
if (!sections)
|
|
45
49
|
return null;
|
|
46
|
-
return React.createElement(Sections, {
|
|
50
|
+
return React.createElement(Sections, { sections: sections });
|
|
47
51
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback, useEffect, useState } from "react";
|
|
2
|
-
import { connectToParent } from "penpal";
|
|
2
|
+
import { connectToParent, ErrorCode } from "penpal";
|
|
3
3
|
export const useCustomizerShell = ({ environment = "production", isPreview, sectionComponents, data = {}, storefrontSettingsSchema, }) => {
|
|
4
4
|
const [location, setLocation] = useState();
|
|
5
5
|
const [content, setContent] = useState(data.content);
|
|
@@ -10,7 +10,7 @@ export const useCustomizerShell = ({ environment = "production", isPreview, sect
|
|
|
10
10
|
};
|
|
11
11
|
useEffect(() => {
|
|
12
12
|
setLocation(window.location);
|
|
13
|
-
}, []);
|
|
13
|
+
}, [data.handle]);
|
|
14
14
|
const refreshSections = useCallback(() => {
|
|
15
15
|
if (!sectionComponents || !parentConnection)
|
|
16
16
|
return [];
|
|
@@ -22,13 +22,15 @@ export const useCustomizerShell = ({ environment = "production", isPreview, sect
|
|
|
22
22
|
})
|
|
23
23
|
.filter(Boolean);
|
|
24
24
|
try {
|
|
25
|
-
parentConnection
|
|
25
|
+
parentConnection?.setSectionsSchemas(JSON.stringify(sectionSchemas));
|
|
26
26
|
}
|
|
27
27
|
catch (error) {
|
|
28
|
-
|
|
28
|
+
if (error.code !== ErrorCode.ConnectionDestroyed) {
|
|
29
|
+
parentConnection?.displayError("Something went wrong parsing sections");
|
|
30
|
+
}
|
|
29
31
|
}
|
|
30
32
|
return;
|
|
31
|
-
}, [data, parentConnection, sectionComponents]);
|
|
33
|
+
}, [data.handle, parentConnection, sectionComponents]);
|
|
32
34
|
const refreshStorefrontSettingsSchema = useCallback(() => {
|
|
33
35
|
if (!storefrontSettingsSchema || !parentConnection || !sectionComponents) {
|
|
34
36
|
return [];
|
|
@@ -36,9 +38,9 @@ export const useCustomizerShell = ({ environment = "production", isPreview, sect
|
|
|
36
38
|
parentConnection.setStorefrontSettingsSchema(JSON.stringify(storefrontSettingsSchema));
|
|
37
39
|
return;
|
|
38
40
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
39
|
-
}, [data, parentConnection, storefrontSettingsSchema]);
|
|
41
|
+
}, [data.handle, parentConnection, storefrontSettingsSchema]);
|
|
40
42
|
useEffect(() => {
|
|
41
|
-
if (!isPreview || !location)
|
|
43
|
+
if (!isPreview || !location?.pathname)
|
|
42
44
|
return;
|
|
43
45
|
const connection = connectToParent({
|
|
44
46
|
methods: {
|
|
@@ -72,17 +74,15 @@ export const useCustomizerShell = ({ environment = "production", isPreview, sect
|
|
|
72
74
|
});
|
|
73
75
|
setParentConnection(parent);
|
|
74
76
|
});
|
|
75
|
-
return () =>
|
|
76
|
-
|
|
77
|
-
};
|
|
78
|
-
}, [location]);
|
|
77
|
+
return () => connection.destroy();
|
|
78
|
+
}, [location?.pathname]);
|
|
79
79
|
useEffect(() => {
|
|
80
80
|
if (!isPreview)
|
|
81
81
|
return;
|
|
82
82
|
refreshSections();
|
|
83
83
|
refreshStorefrontSettingsSchema();
|
|
84
84
|
}, [
|
|
85
|
-
data,
|
|
85
|
+
data.handle,
|
|
86
86
|
isPreview,
|
|
87
87
|
refreshSections,
|
|
88
88
|
refreshStorefrontSettingsSchema,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pack/react",
|
|
3
3
|
"description": "React",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.10",
|
|
5
5
|
"exports": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"engines": {
|
|
@@ -24,7 +24,6 @@
|
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@shopify/remix-oxygen": "^1.1.3",
|
|
26
26
|
"@types/react": "^18.2.20",
|
|
27
|
-
"@types/uuid": "^9.0.2",
|
|
28
27
|
"react": "^18.2.0",
|
|
29
28
|
"react-dom": "^18.2.0"
|
|
30
29
|
},
|
|
@@ -33,7 +32,6 @@
|
|
|
33
32
|
"react-dom": "^18.0.0"
|
|
34
33
|
},
|
|
35
34
|
"dependencies": {
|
|
36
|
-
"penpal": "^6.2.2"
|
|
37
|
-
"uuid": "^9.0.1"
|
|
35
|
+
"penpal": "^6.2.2"
|
|
38
36
|
}
|
|
39
37
|
}
|