UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

120 lines (119 loc) 3.5 kB
"use client"; import React, { useRef } from 'react'; import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js"; import useMountEffect from "../../shared/helpers/useMountEffect.js"; import GlobalStatusProvider from "./GlobalStatusProvider.js"; import { removeUndefinedProps } from "../../shared/component-helper.js"; export class GlobalStatusInterceptor { constructor(props) { let GSP = null; try { GSP = GlobalStatusProvider; } catch (e) {} if (!GSP && typeof window !== 'undefined') { GSP = window.GlobalStatusProvider; } this.provider = GSP.init(props.id || 'main', provider => { const { statusId } = provider.add(props); this.statusId = statusId; }); return this; } add(props) { return this.provider.add({ statusId: this.statusId, ...props }); } update(props) { return this.provider.update(this.statusId, props); } remove() { return this.provider.remove(this.statusId); } } function initProvider(id) { let GSP = null; try { GSP = GlobalStatusProvider; } catch (e) {} if (!GSP && typeof window !== 'undefined') { GSP = window.GlobalStatusProvider; } return GSP.init(id); } const globalStatusControllerDefaultProps = { id: 'main', statusId: null, removeOnUnmount: false }; function GlobalStatusController(ownProps) { const props = { ...globalStatusControllerDefaultProps, ...removeUndefinedProps({ ...ownProps }) }; const providerRef = useRef(undefined); const statusIdRef = useRef(undefined); const prevPropsRef = useRef(undefined); const removeOnUnmountRef = useRef(props.removeOnUnmount); removeOnUnmountRef.current = props.removeOnUnmount; if (!providerRef.current) { providerRef.current = initProvider(props.id); prevPropsRef.current = ownProps; } if (prevPropsRef.current !== ownProps) { providerRef.current?.update(statusIdRef.current, props); prevPropsRef.current = ownProps; } useMountEffect(() => { const { statusId } = providerRef.current.add(props); statusIdRef.current = statusId; return () => { if (providerRef.current && removeOnUnmountRef.current) { providerRef.current.remove(statusIdRef.current); } }; }); return null; } const MemoizedGlobalStatusController = React.memo(GlobalStatusController); const globalStatusRemoveDefaultProps = { id: 'main' }; function GlobalStatusRemove(ownProps) { const props = { ...globalStatusRemoveDefaultProps, ...removeUndefinedProps({ ...ownProps }) }; const providerRef = useRef(undefined); const prevPropsRef = useRef(undefined); if (!providerRef.current) { providerRef.current = initProvider(props.id); prevPropsRef.current = ownProps; } if (prevPropsRef.current !== ownProps) { providerRef.current?.update(props.statusId, props); prevPropsRef.current = ownProps; } useMountEffect(() => { providerRef.current.remove(props.statusId, props); }); return null; } const MemoizedGlobalStatusRemove = React.memo(GlobalStatusRemove); MemoizedGlobalStatusController.Remove = MemoizedGlobalStatusRemove; MemoizedGlobalStatusController.Update = MemoizedGlobalStatusController; withComponentMarkers(MemoizedGlobalStatusController, { _supportsSpacingProps: true }); export default MemoizedGlobalStatusController; export { MemoizedGlobalStatusRemove as GlobalStatusRemove }; //# sourceMappingURL=GlobalStatusController.js.map