PluginExtensions: Fixed so we expose the proper types for usePluginComponents (#100949)
* Fixed usage of incorrect type in the 'usePluginComponents' hook. * improved betterer. * Changed import path of usePluginLinks types. * improved betterer result.
This commit is contained in:
+1
-5
@@ -5599,15 +5599,11 @@ exports[`better eslint`] = {
|
||||
[0, 0, 0, "\'@grafana/data/src/types/pluginExtensions\' import is restricted from being used by a pattern. Import from the public export instead.", "0"]
|
||||
],
|
||||
"public/app/features/plugins/extensions/usePluginComponents.tsx:5381": [
|
||||
[0, 0, 0, "\'@grafana/runtime/src/services/pluginExtensions/getPluginExtensions\' import is restricted from being used by a pattern. Import from the public export instead.", "0"],
|
||||
[0, 0, 0, "Do not use any type assertions.", "1"]
|
||||
[0, 0, 0, "Do not use any type assertions.", "0"]
|
||||
],
|
||||
"public/app/features/plugins/extensions/usePluginFunctions.tsx:5381": [
|
||||
[0, 0, 0, "Do not use any type assertions.", "0"]
|
||||
],
|
||||
"public/app/features/plugins/extensions/usePluginLinks.tsx:5381": [
|
||||
[0, 0, 0, "\'@grafana/runtime/src/services/pluginExtensions/getPluginExtensions\' import is restricted from being used by a pattern. Import from the public export instead.", "0"]
|
||||
],
|
||||
"public/app/features/plugins/extensions/validators.ts:5381": [
|
||||
[0, 0, 0, "\'@grafana/data/src/types/pluginExtensions\' import is restricted from being used by a pattern. Import from the public export instead.", "0"]
|
||||
],
|
||||
|
||||
@@ -21,9 +21,6 @@ export {
|
||||
type GetPluginExtensionsResult,
|
||||
type UsePluginExtensions,
|
||||
type UsePluginExtensionsResult,
|
||||
type UsePluginComponentResult,
|
||||
type UsePluginFunctionsOptions,
|
||||
type UsePluginFunctionsResult,
|
||||
} from './pluginExtensions/getPluginExtensions';
|
||||
export {
|
||||
setPluginExtensionsHook,
|
||||
@@ -32,10 +29,29 @@ export {
|
||||
usePluginComponentExtensions,
|
||||
} from './pluginExtensions/usePluginExtensions';
|
||||
|
||||
export { setPluginComponentHook, usePluginComponent } from './pluginExtensions/usePluginComponent';
|
||||
export { setPluginComponentsHook, usePluginComponents } from './pluginExtensions/usePluginComponents';
|
||||
export { setPluginLinksHook, usePluginLinks } from './pluginExtensions/usePluginLinks';
|
||||
export { setPluginFunctionsHook, usePluginFunctions } from './pluginExtensions/usePluginFunctions';
|
||||
export {
|
||||
setPluginComponentHook,
|
||||
usePluginComponent,
|
||||
type UsePluginComponentResult,
|
||||
} from './pluginExtensions/usePluginComponent';
|
||||
export {
|
||||
setPluginComponentsHook,
|
||||
usePluginComponents,
|
||||
type UsePluginComponentsResult,
|
||||
type UsePluginComponentsOptions,
|
||||
} from './pluginExtensions/usePluginComponents';
|
||||
export {
|
||||
setPluginLinksHook,
|
||||
usePluginLinks,
|
||||
type UsePluginLinksOptions,
|
||||
type UsePluginLinksResult,
|
||||
} from './pluginExtensions/usePluginLinks';
|
||||
export {
|
||||
setPluginFunctionsHook,
|
||||
usePluginFunctions,
|
||||
type UsePluginFunctionsOptions,
|
||||
type UsePluginFunctionsResult,
|
||||
} from './pluginExtensions/usePluginFunctions';
|
||||
|
||||
export { isPluginExtensionLink, isPluginExtensionComponent } from './pluginExtensions/utils';
|
||||
export { setCurrentUser } from './user';
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
import type {
|
||||
PluginExtension,
|
||||
PluginExtensionLink,
|
||||
PluginExtensionComponent,
|
||||
PluginExtensionFunction,
|
||||
PluginExtensionComponentMeta,
|
||||
} from '@grafana/data';
|
||||
import type { PluginExtension, PluginExtensionLink, PluginExtensionComponent } from '@grafana/data';
|
||||
|
||||
import { isPluginExtensionComponent, isPluginExtensionLink } from './utils';
|
||||
|
||||
@@ -23,11 +17,6 @@ export type GetPluginExtensionsOptions = {
|
||||
limitPerPlugin?: number;
|
||||
};
|
||||
|
||||
export type UsePluginComponentOptions = {
|
||||
extensionPointId: string;
|
||||
limitPerPlugin?: number;
|
||||
};
|
||||
|
||||
export type GetPluginExtensionsResult<T = PluginExtension> = {
|
||||
extensions: T[];
|
||||
};
|
||||
@@ -37,37 +26,6 @@ export type UsePluginExtensionsResult<T = PluginExtension> = {
|
||||
isLoading: boolean;
|
||||
};
|
||||
|
||||
export type UsePluginComponentResult<Props = {}> = {
|
||||
component: React.ComponentType<Props> | undefined | null;
|
||||
isLoading: boolean;
|
||||
};
|
||||
|
||||
export type UsePluginComponentsResult<Props = {}> = {
|
||||
components: Array<React.ComponentType<Props> & { meta: PluginExtensionComponentMeta }>;
|
||||
isLoading: boolean;
|
||||
};
|
||||
|
||||
export type UsePluginLinksOptions = {
|
||||
extensionPointId: string;
|
||||
context?: object | Record<string | symbol, unknown>;
|
||||
limitPerPlugin?: number;
|
||||
};
|
||||
|
||||
export type UsePluginLinksResult = {
|
||||
isLoading: boolean;
|
||||
links: PluginExtensionLink[];
|
||||
};
|
||||
|
||||
export type UsePluginFunctionsOptions = {
|
||||
extensionPointId: string;
|
||||
limitPerPlugin?: number;
|
||||
};
|
||||
|
||||
export type UsePluginFunctionsResult<Signature> = {
|
||||
isLoading: boolean;
|
||||
functions: Array<PluginExtensionFunction<Signature>>;
|
||||
};
|
||||
|
||||
let singleton: GetPluginExtensions | undefined;
|
||||
|
||||
export function setPluginExtensionGetter(instance: GetPluginExtensions): void {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { UsePluginComponentResult } from './getPluginExtensions';
|
||||
export type UsePluginComponent<Props extends object = {}> = (componentId: string) => UsePluginComponentResult<Props>;
|
||||
|
||||
export type UsePluginComponent<Props extends object = {}> = (id: string) => UsePluginComponentResult<Props>;
|
||||
export type UsePluginComponentResult<Props = {}> = {
|
||||
component: React.ComponentType<Props> | undefined | null;
|
||||
isLoading: boolean;
|
||||
};
|
||||
|
||||
let singleton: UsePluginComponent | undefined;
|
||||
|
||||
@@ -12,9 +15,9 @@ export function setPluginComponentHook(hook: UsePluginComponent): void {
|
||||
singleton = hook;
|
||||
}
|
||||
|
||||
export function usePluginComponent<Props extends object = {}>(id: string): UsePluginComponentResult<Props> {
|
||||
export function usePluginComponent<Props extends object = {}>(componentId: string): UsePluginComponentResult<Props> {
|
||||
if (!singleton) {
|
||||
throw new Error('setPluginComponentHook(options) can only be used after the Grafana instance has started.');
|
||||
}
|
||||
return singleton(id) as UsePluginComponentResult<Props>;
|
||||
return singleton(componentId) as UsePluginComponentResult<Props>;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
import { GetPluginExtensionsOptions, UsePluginComponentsResult } from './getPluginExtensions';
|
||||
import { PluginExtensionComponentMeta } from '@grafana/data';
|
||||
|
||||
export type UsePluginComponentsOptions = {
|
||||
extensionPointId: string;
|
||||
limitPerPlugin?: number;
|
||||
};
|
||||
|
||||
export type UsePluginComponentsResult<Props = {}> = {
|
||||
components: Array<React.ComponentType<Props> & { meta: PluginExtensionComponentMeta }>;
|
||||
isLoading: boolean;
|
||||
};
|
||||
|
||||
export type UsePluginComponents<Props extends object = {}> = (
|
||||
options: GetPluginExtensionsOptions
|
||||
options: UsePluginComponentsOptions
|
||||
) => UsePluginComponentsResult<Props>;
|
||||
|
||||
let singleton: UsePluginComponents | undefined;
|
||||
|
||||
export function setPluginComponentsHook(hook: UsePluginComponents): void {
|
||||
// We allow overriding the registry in tests
|
||||
// We allow overriding the hook in tests
|
||||
if (singleton && process.env.NODE_ENV !== 'test') {
|
||||
throw new Error('setPluginComponentsHook() function should only be called once, when Grafana is starting.');
|
||||
}
|
||||
@@ -15,7 +25,7 @@ export function setPluginComponentsHook(hook: UsePluginComponents): void {
|
||||
}
|
||||
|
||||
export function usePluginComponents<Props extends object = {}>(
|
||||
options: GetPluginExtensionsOptions
|
||||
options: UsePluginComponentsOptions
|
||||
): UsePluginComponentsResult<Props> {
|
||||
if (!singleton) {
|
||||
throw new Error('setPluginComponentsHook(options) can only be used after the Grafana instance has started.');
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
import { UsePluginFunctionsOptions, UsePluginFunctionsResult } from './getPluginExtensions';
|
||||
import { PluginExtensionFunction } from '@grafana/data';
|
||||
|
||||
export type UsePluginFunctionsOptions = {
|
||||
extensionPointId: string;
|
||||
limitPerPlugin?: number;
|
||||
};
|
||||
|
||||
export type UsePluginFunctionsResult<Signature> = {
|
||||
isLoading: boolean;
|
||||
functions: Array<PluginExtensionFunction<Signature>>;
|
||||
};
|
||||
|
||||
export type UsePluginFunctions<T> = (options: UsePluginFunctionsOptions) => UsePluginFunctionsResult<T>;
|
||||
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
import { UsePluginLinksOptions, UsePluginLinksResult } from './getPluginExtensions';
|
||||
import { PluginExtensionLink } from '@grafana/data';
|
||||
|
||||
export type UsePluginLinksOptions = {
|
||||
extensionPointId: string;
|
||||
context?: object | Record<string | symbol, unknown>;
|
||||
limitPerPlugin?: number;
|
||||
};
|
||||
|
||||
export type UsePluginLinksResult = {
|
||||
isLoading: boolean;
|
||||
links: PluginExtensionLink[];
|
||||
};
|
||||
|
||||
export type UsePluginLinks = (options: UsePluginLinksOptions) => UsePluginLinksResult;
|
||||
|
||||
|
||||
@@ -2,10 +2,7 @@ import { useMemo } from 'react';
|
||||
import { useObservable } from 'react-use';
|
||||
|
||||
import { PluginExtensionComponentMeta, PluginExtensionTypes, usePluginContext } from '@grafana/data';
|
||||
import {
|
||||
UsePluginComponentOptions,
|
||||
UsePluginComponentsResult,
|
||||
} from '@grafana/runtime/src/services/pluginExtensions/getPluginExtensions';
|
||||
import { UsePluginComponentsOptions, UsePluginComponentsResult } from '@grafana/runtime';
|
||||
|
||||
import { useAddedComponentsRegistry } from './ExtensionRegistriesContext';
|
||||
import * as errors from './errors';
|
||||
@@ -19,7 +16,7 @@ import { isExtensionPointIdValid, isExtensionPointMetaInfoMissing } from './vali
|
||||
export function usePluginComponents<Props extends object = {}>({
|
||||
limitPerPlugin,
|
||||
extensionPointId,
|
||||
}: UsePluginComponentOptions): UsePluginComponentsResult<Props> {
|
||||
}: UsePluginComponentsOptions): UsePluginComponentsResult<Props> {
|
||||
const registry = useAddedComponentsRegistry();
|
||||
const registryState = useObservable(registry.asObservable());
|
||||
const pluginContext = usePluginContext();
|
||||
|
||||
@@ -3,10 +3,7 @@ import { useMemo } from 'react';
|
||||
import { useObservable } from 'react-use';
|
||||
|
||||
import { PluginExtensionLink, PluginExtensionTypes, usePluginContext } from '@grafana/data';
|
||||
import {
|
||||
UsePluginLinksOptions,
|
||||
UsePluginLinksResult,
|
||||
} from '@grafana/runtime/src/services/pluginExtensions/getPluginExtensions';
|
||||
import { UsePluginLinksOptions, UsePluginLinksResult } from '@grafana/runtime';
|
||||
|
||||
import { useAddedLinksRegistry } from './ExtensionRegistriesContext';
|
||||
import * as errors from './errors';
|
||||
|
||||
Reference in New Issue
Block a user