Skip to content

Commit 81bb0e6

Browse files
fix
1 parent 36025bf commit 81bb0e6

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

public/app/fn-app/fn-dashboard-page/fn-dashboard.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { FC, useMemo } from 'react';
1+
import { FC, useEffect, useMemo } from 'react';
22
// eslint-disable-next-line no-restricted-imports
33
import { Provider, shallowEqual, useSelector } from 'react-redux';
44

55
import { FnPropMappedFromState, FnState, updatePartialFnStates } from 'app/core/reducers/fn-slice';
66
import { FnLoggerService } from 'app/fn_logger';
77
import {
8+
MfeGlobalState,
89
MfeStore,
910
mfeStore,
1011
removeGrafanaStoreAndDashboard,
@@ -35,6 +36,16 @@ export const DashboardPortal: FC<FNDashboardComponentProps> = (p) => {
3536
.map(([uid, props]) => [uid, props] satisfies [string, FnState]);
3637
}, [globalFnProps.dashboards]);
3738

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+
3849
return useMemo(() => {
3950
return dashboards.map(([uid, props]) => {
4051
if (!uid.length) {
@@ -78,3 +89,28 @@ export const DashboardPortal: FC<FNDashboardComponentProps> = (p) => {
7889
// eslint-disable-next-line react-hooks/exhaustive-deps
7990
}, [dashboards, p, globalFnProps.renderingDashboardUID]);
8091
};
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

Comments
 (0)