diff --git a/ui/apps/pmm-compat/src/compat.ts b/ui/apps/pmm-compat/src/compat.ts index 2216e60bd60..3021157cbbd 100644 --- a/ui/apps/pmm-compat/src/compat.ts +++ b/ui/apps/pmm-compat/src/compat.ts @@ -20,6 +20,7 @@ import { adjustToolbar } from 'compat/toolbar'; import { isWithinIframe, getLinkWithVariables } from 'lib/utils'; import { documentTitleObserver } from 'lib/utils/document'; import { isFirstLogin, updateIsFirstLogin } from 'lib/utils/login'; +import { ServiceAddedEvent, ServiceDeletedEvent, SettingsUpdatedEvent } from 'lib/events'; export const initialize = () => { // If Grafana is opened outside of iframe (or on login), redirect to PMM UI @@ -144,4 +145,22 @@ export const initialize = () => { }); }, }); + + getAppEvents().subscribe(SettingsUpdatedEvent, () => { + messenger.sendMessage({ + type: 'SETTINGS_CHANGED', + }); + }); + + getAppEvents().subscribe(ServiceAddedEvent, () => { + messenger.sendMessage({ + type: 'SERVICE_ADDED', + }); + }); + + getAppEvents().subscribe(ServiceDeletedEvent, () => { + messenger.sendMessage({ + type: 'SERVICE_DELETED', + }); + }); }; diff --git a/ui/apps/pmm-compat/src/lib/events.ts b/ui/apps/pmm-compat/src/lib/events.ts new file mode 100644 index 00000000000..cf9d25658dc --- /dev/null +++ b/ui/apps/pmm-compat/src/lib/events.ts @@ -0,0 +1,18 @@ +/** + * @fileoverview + * Same events as in public/app/percona/shared/core/events.ts in grafana repository + */ + +import { BusEventBase } from '@grafana/data'; + +export class SettingsUpdatedEvent extends BusEventBase { + static type = 'settings-updated-event'; +} + +export class ServiceAddedEvent extends BusEventBase { + static type = 'service-added-event'; +} + +export class ServiceDeletedEvent extends BusEventBase { + static type = 'service-deleted-event'; +} diff --git a/ui/apps/pmm/src/contexts/grafana/grafana.provider.tsx b/ui/apps/pmm/src/contexts/grafana/grafana.provider.tsx index f2ccd920ca2..085176891dc 100644 --- a/ui/apps/pmm/src/contexts/grafana/grafana.provider.tsx +++ b/ui/apps/pmm/src/contexts/grafana/grafana.provider.tsx @@ -17,6 +17,8 @@ import { useKioskMode } from 'hooks/utils/useKioskMode'; import { useColorMode } from 'hooks/theme'; import { getLocationUrl } from './grafana.utils'; import messenger from 'lib/messenger'; +import { useSettings } from 'hooks/api/useSettings'; +import { useServiceTypes } from 'hooks/api/useServices'; /** Guard DOM usage. */ const isBrowser = () => @@ -34,6 +36,13 @@ export const GrafanaProvider: FC = ({ children }) => { const location = useLocation(); const navigate = useNavigate(); + const { refetch: refetchSettings } = useSettings({ + enabled: false, + }); + const { refetch: refetchServiceTypes } = useServiceTypes({ + enabled: false, + }); + const src = location.pathname.replace(PMM_NEW_NAV_PATH, ''); const isGrafanaPage = src.startsWith(GRAFANA_SUB_PATH); @@ -95,10 +104,27 @@ export const GrafanaProvider: FC = ({ children }) => { }, }); + messenger.addListener({ + type: 'SETTINGS_CHANGED', + onMessage: () => refetchSettings(), + }); + + messenger.addListener({ + type: 'SERVICE_ADDED', + onMessage: () => refetchServiceTypes(), + }); + + messenger.addListener({ + type: 'SERVICE_DELETED', + onMessage: () => refetchServiceTypes(), + }); + // Cleanup once provider unmounts return () => { messenger.unregister(); }; + + // eslint-disable-next-line react-hooks/exhaustive-deps }, [isLoaded, setFromGrafana, navigate]); // -------- OUTGOING TO GRAFANA -------- diff --git a/ui/apps/pmm/src/contexts/navigation/navigation.provider.tsx b/ui/apps/pmm/src/contexts/navigation/navigation.provider.tsx index 7e764deff0e..24bd73d9844 100644 --- a/ui/apps/pmm/src/contexts/navigation/navigation.provider.tsx +++ b/ui/apps/pmm/src/contexts/navigation/navigation.provider.tsx @@ -34,6 +34,7 @@ import { useHaInfo } from 'hooks/api/useHA'; export const NavigationProvider: FC = ({ children }) => { const { user } = useUser(); const { data: serviceTypes } = useServiceTypes({ + enabled: !!user, refetchInterval: INTERVALS_MS.SERVICE_TYPES, }); const { settings } = useSettings(); diff --git a/ui/packages/shared/src/messenger.ts b/ui/packages/shared/src/messenger.ts index 1a526e8b5b9..57c4b7afd82 100644 --- a/ui/packages/shared/src/messenger.ts +++ b/ui/packages/shared/src/messenger.ts @@ -38,6 +38,7 @@ export class CrossFrameMessenger { } unregister() { + this.listeners = []; this.window.removeEventListener('message', this.eventListener); } diff --git a/ui/packages/shared/src/types.ts b/ui/packages/shared/src/types.ts index 17f5b1a03c9..a792896d742 100644 --- a/ui/packages/shared/src/types.ts +++ b/ui/packages/shared/src/types.ts @@ -9,7 +9,10 @@ export type MessageType = | 'GRAFANA_READY' | 'DOCUMENT_TITLE_CHANGE' | 'GRAFANA_THEME_CHANGED' - | 'CHANGE_THEME'; + | 'CHANGE_THEME' + | 'SETTINGS_CHANGED' + | 'SERVICE_ADDED' + | 'SERVICE_DELETED'; export type LocationState = { fromGrafana?: boolean } | null; @@ -55,3 +58,7 @@ export type ChangeThemeMessage = Message< theme: ColorMode; } >; + +export type SettingsChangedMessage = Message<'SETTINGS_CHANGED'>; + +export type ServiceAddedMessage = Message<'SERVICE_ADDED'>;