|
1 |
| -import { FC, useMemo } from 'react'; |
| 1 | +import { FC, useEffect, useMemo } from 'react'; |
2 | 2 | // eslint-disable-next-line no-restricted-imports
|
3 | 3 | import { Provider, shallowEqual, useSelector } from 'react-redux';
|
4 | 4 |
|
5 | 5 | import { FnPropMappedFromState, FnState, updatePartialFnStates } from 'app/core/reducers/fn-slice';
|
6 | 6 | import { FnLoggerService } from 'app/fn_logger';
|
7 | 7 | import {
|
| 8 | + MfeGlobalState, |
8 | 9 | MfeStore,
|
9 | 10 | mfeStore,
|
10 | 11 | removeGrafanaStoreAndDashboard,
|
@@ -35,6 +36,16 @@ export const DashboardPortal: FC<FNDashboardComponentProps> = (p) => {
|
35 | 36 | .map(([uid, props]) => [uid, props] satisfies [string, FnState]);
|
36 | 37 | }, [globalFnProps.dashboards]);
|
37 | 38 |
|
| 39 | + useEffect(() => { |
| 40 | + const timer = isDashboardValidPoller(globalFnProps, dashboards); |
| 41 | + if (!globalFnProps.renderingDashboardUID.length && dashboards.length > 0) { |
| 42 | + clearInterval(timer); |
| 43 | + } |
| 44 | + return () => { |
| 45 | + clearInterval(timer); |
| 46 | + }; |
| 47 | + }, [globalFnProps, dashboards]); |
| 48 | + |
38 | 49 | return useMemo(() => {
|
39 | 50 | return dashboards.map(([uid, props]) => {
|
40 | 51 | if (!uid.length) {
|
@@ -78,3 +89,28 @@ export const DashboardPortal: FC<FNDashboardComponentProps> = (p) => {
|
78 | 89 | // eslint-disable-next-line react-hooks/exhaustive-deps
|
79 | 90 | }, [dashboards, p, globalFnProps.renderingDashboardUID]);
|
80 | 91 | };
|
| 92 | + |
| 93 | +/** |
| 94 | + * Checks if the dashboard is valid every 500ms. |
| 95 | + */ |
| 96 | +const POLLING_INTERVAL = 500; |
| 97 | +function isDashboardValidPoller(globalFnProps: MfeGlobalState, dashboards: Array<[string, FnState]>) { |
| 98 | + return setInterval(() => { |
| 99 | + dashboards.forEach(([uid, props]) => { |
| 100 | + if (!document.getElementById(props.portalContainerID)) { |
| 101 | + FnLoggerService.info("[Polling]:: removing dashboard from MfeStore because portalContainerID doesn't exist", { |
| 102 | + portalID: props.portalContainerID, |
| 103 | + uid, |
| 104 | + }); |
| 105 | + mfeStore.dispatch(removeGrafanaStoreAndDashboard(uid)); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + if ( |
| 110 | + globalFnProps.renderingDashboardUID && |
| 111 | + !dashboards.some(([uid]) => uid === globalFnProps.renderingDashboardUID) |
| 112 | + ) { |
| 113 | + mfeStore.dispatch(updateRenderingDashboardUID('')); |
| 114 | + } |
| 115 | + }, POLLING_INTERVAL); |
| 116 | +} |
0 commit comments