@freelygive/canvas-utils 0.3.0-dev.3a2544d4 → 0.3.3-dev.770641bc
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/package.json
CHANGED
|
@@ -5,20 +5,23 @@ import useSWR from 'swr';
|
|
|
5
5
|
/**
|
|
6
6
|
* Fetch the main entity for the current page using SWR.
|
|
7
7
|
* Uses Canvas mainEntity (when available) with fallback to title-based lookup.
|
|
8
|
-
*
|
|
9
|
-
* @param {
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {
|
|
8
|
+
*
|
|
9
|
+
* @param {string} entityType - The JSON:API entity type (e.g., 'node--news_article').
|
|
10
|
+
* @param {Object} [options] - Fetch options.
|
|
11
|
+
* @param {string[]} [options.includes] - Relationships to include.
|
|
12
|
+
* @param {Object} [options.fields] - Sparse fieldset map (e.g., { 'node--news_article': ['title'] }).
|
|
12
13
|
* @returns {{ data: object|undefined, error: Error|undefined, isLoading: boolean }}
|
|
13
14
|
*/
|
|
14
15
|
export const useMainEntity = (
|
|
15
16
|
entityType,
|
|
16
17
|
{ includes = [], fields = {} } = {},
|
|
17
18
|
) => {
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
// mainEntity will be available in future Canvas versions.
|
|
20
|
+
const pageData =
|
|
21
|
+
/** @type {{ pageTitle: string, mainEntity?: { uuid: string } }} */ (
|
|
22
|
+
getPageData()
|
|
23
|
+
);
|
|
24
|
+
const { pageTitle, mainEntity } = pageData;
|
|
22
25
|
const entityId = mainEntity?.uuid || pageTitle || null;
|
|
23
26
|
|
|
24
27
|
return useSWR(
|
|
@@ -11,8 +11,9 @@ const flattenChildren = (children) => {
|
|
|
11
11
|
React.Children.forEach(children, (child) => {
|
|
12
12
|
if (!child) return;
|
|
13
13
|
// Check if it's a Fragment (type is Symbol(react.fragment))
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const el = /** @type {React.ReactElement} */ (child);
|
|
15
|
+
if (el.type === React.Fragment) {
|
|
16
|
+
result.push(...flattenChildren(el.props.children));
|
|
16
17
|
} else {
|
|
17
18
|
result.push(child);
|
|
18
19
|
}
|