Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ui/apps/pmm-compat/src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, SettingsUpdatedEvent } from 'lib/events';

export const initialize = () => {
// If Grafana is opened outside of iframe (or on login), redirect to PMM UI
Expand Down Expand Up @@ -144,4 +145,16 @@ export const initialize = () => {
});
},
});

getAppEvents().subscribe(SettingsUpdatedEvent, () => {
messenger.sendMessage({
type: 'SETTINGS_CHANGED',
});
});

getAppEvents().subscribe(ServiceAddedEvent, () => {
messenger.sendMessage({
type: 'SERVICE_ADDED',
});
});
};
14 changes: 14 additions & 0 deletions ui/apps/pmm-compat/src/lib/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @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';
}
21 changes: 21 additions & 0 deletions ui/apps/pmm/src/contexts/grafana/grafana.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () =>
Expand All @@ -34,6 +36,13 @@ export const GrafanaProvider: FC<PropsWithChildren> = ({ 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);

Expand Down Expand Up @@ -95,10 +104,22 @@ export const GrafanaProvider: FC<PropsWithChildren> = ({ children }) => {
},
});

messenger.addListener({
type: 'SETTINGS_CHANGED',
onMessage: () => refetchSettings(),
});

messenger.addListener({
type: 'SERVICE_ADDED',
onMessage: () => refetchServiceTypes(),
});

// Cleanup once provider unmounts
return () => {
messenger.unregister();
};

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoaded, setFromGrafana, navigate]);

// -------- OUTGOING TO GRAFANA --------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useLocalStorage } from 'hooks/utils/useLocalStorage';
export const NavigationProvider: FC<PropsWithChildren> = ({ children }) => {
const { user } = useUser();
const { data: serviceTypes } = useServiceTypes({
enabled: !!user,
refetchInterval: INTERVALS_MS.SERVICE_TYPES,
});
const { settings } = useSettings();
Expand Down
8 changes: 7 additions & 1 deletion ui/packages/shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export type MessageType =
| 'GRAFANA_READY'
| 'DOCUMENT_TITLE_CHANGE'
| 'GRAFANA_THEME_CHANGED'
| 'CHANGE_THEME';
| 'CHANGE_THEME'
| 'SETTINGS_CHANGED'
| 'SERVICE_ADDED';

export type LocationState = { fromGrafana?: boolean } | null;

Expand Down Expand Up @@ -55,3 +57,7 @@ export type ChangeThemeMessage = Message<
theme: ColorMode;
}
>;

export type SettingsChangedMessage = Message<'SETTINGS_CHANGED'>;

export type ServiceAddedMessage = Message<'SERVICE_ADDED'>;
Loading