@j2inn/app-react
Version:
React implementation of the j2inn-app framework
30 lines (29 loc) • 1.12 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useDynamicResources } from '../../hooks/useDynamicResource';
import { Loading } from '../Loading';
/**
* An inner remote resource component.
*/
const InnerRemote = ({ urls, children }) => {
const { ready, failed } = useDynamicResources(urls);
if (!urls.length) {
return _jsx("h2", { children: "No remote specified" });
}
if (failed) {
return _jsxs("h2", { children: ["Failed to load dynamic resources: ", urls.join(', ')] });
}
if (!ready) {
return _jsx(Loading, {});
}
return _jsx(_Fragment, { children: children });
};
/**
* Loads remote resources dynamically. The child components will only render once
* the script has been successfully loaded.
*/
export const RemoteResources = ({ urls, children, }) => {
// Using a unique key will force the inner remote resource to be recreated whenever the
// URLs changes.
const key = urls.join(',');
return (_jsx(InnerRemote, { urls: urls, children: children }, key));
};