Files
grafana/public/app/features/alerting/unified/components/PluginBridge.tsx
Ashley Harrison 11bc66a0e8 Chore: use React.PropsWithChildren to explicitly define the children prop (#64433)
* use React.PropsWithChildren to explicitly define the children prop

* fix ThemeDemo as well

* provide empty generics
2023-03-08 16:12:54 +00:00

41 lines
1.1 KiB
TypeScript

import React, { ReactElement } from 'react';
import { usePluginBridge } from '../hooks/usePluginBridge';
import { SupportedPlugin } from '../types/pluginBridges';
export type PluginID = SupportedPlugin | string;
export interface PluginBridgeProps {
plugin: PluginID;
// shows an optional component when the plugin is not installed
notInstalledFallback?: ReactElement;
// shows an optional component when we're checking if the plugin is installed
loadingComponent?: ReactElement;
}
export const PluginBridge = ({
children,
plugin,
loadingComponent,
notInstalledFallback,
}: React.PropsWithChildren<PluginBridgeProps>) => {
const { loading, installed } = usePluginBridge(plugin);
if (loading) {
return loadingComponent ?? null;
}
if (!installed) {
return notInstalledFallback ?? null;
}
return <>{children}</>;
};
export function createBridgeURL(plugin: PluginID, path?: string, options?: Record<string, string>) {
const searchParams = new URLSearchParams(options).toString();
const pluginPath = `/a/${plugin}${path}`;
return pluginPath + (searchParams ? '?' + searchParams : '');
}