AppPlugin: add types for jsonData (#17177)

* add types for App jsonData

* add types for App jsonData

* add value as possible type

* make it extend {}
pull/17193/head
Ryan McKinley 7 years ago committed by Torkel Ödegaard
parent f98095d629
commit 744682e648
  1. 16
      packages/grafana-ui/src/types/app.ts
  2. 10
      packages/grafana-ui/src/types/plugin.ts
  3. 5
      public/app/plugins/app/example-app/ExampleRootPage.tsx
  4. 3
      public/app/plugins/app/example-app/module.ts
  5. 4
      public/app/plugins/app/example-app/types.ts

@ -1,12 +1,12 @@
import { ComponentClass } from 'react'; import { ComponentClass } from 'react';
import { NavModel } from './navModel'; import { NavModel } from './navModel';
import { PluginMeta, PluginIncludeType, GrafanaPlugin } from './plugin'; import { PluginMeta, PluginIncludeType, GrafanaPlugin, KeyValue } from './plugin';
export interface AppRootProps { export interface AppRootProps<T = KeyValue> {
meta: AppPluginMeta; meta: AppPluginMeta<T>;
path: string; // The URL path to this page path: string; // The URL path to this page
query: { [s: string]: any }; // The URL query parameters query: KeyValue; // The URL query parameters
/** /**
* Pass the nav model to the container... is there a better way? * Pass the nav model to the container... is there a better way?
@ -14,13 +14,13 @@ export interface AppRootProps {
onNavChanged: (nav: NavModel) => void; onNavChanged: (nav: NavModel) => void;
} }
export interface AppPluginMeta extends PluginMeta { export interface AppPluginMeta<T = KeyValue> extends PluginMeta<T> {
// TODO anything specific to apps? // TODO anything specific to apps?
} }
export class AppPlugin extends GrafanaPlugin<AppPluginMeta> { export class AppPlugin<T = KeyValue> extends GrafanaPlugin<AppPluginMeta<T>> {
// Content under: /a/${plugin-id}/* // Content under: /a/${plugin-id}/*
root?: ComponentClass<AppRootProps>; root?: ComponentClass<AppRootProps<T>>;
rootNav?: NavModel; // Initial navigation model rootNav?: NavModel; // Initial navigation model
// Old style pages // Old style pages
@ -37,7 +37,7 @@ export class AppPlugin extends GrafanaPlugin<AppPluginMeta> {
* Set the component displayed under: * Set the component displayed under:
* /a/${plugin-id}/* * /a/${plugin-id}/*
*/ */
setRootPage(root: ComponentClass<AppRootProps>, rootNav?: NavModel) { setRootPage(root: ComponentClass<AppRootProps<T>>, rootNav?: NavModel) {
this.root = root; this.root = root;
this.rootNav = rootNav; this.rootNav = rootNav;
return this; return this;

@ -11,7 +11,9 @@ export enum PluginType {
app = 'app', app = 'app',
} }
export interface PluginMeta { export type KeyValue<T = any> = { [s: string]: T };
export interface PluginMeta<T extends {} = KeyValue> {
id: string; id: string;
name: string; name: string;
type: PluginType; type: PluginType;
@ -27,8 +29,8 @@ export interface PluginMeta {
dependencies?: PluginDependencies; dependencies?: PluginDependencies;
// Filled in by the backend // Filled in by the backend
jsonData?: { [str: string]: any }; jsonData?: T;
secureJsonData?: { [str: string]: any }; secureJsonData?: KeyValue;
enabled?: boolean; enabled?: boolean;
defaultNavUrl?: string; defaultNavUrl?: string;
hasUpdate?: boolean; hasUpdate?: boolean;
@ -93,7 +95,7 @@ export interface PluginMetaInfo {
export interface PluginConfigPageProps<T extends GrafanaPlugin> { export interface PluginConfigPageProps<T extends GrafanaPlugin> {
plugin: T; plugin: T;
query: { [s: string]: any }; // The URL query parameters query: KeyValue; // The URL query parameters
} }
export interface PluginConfigPage<T extends GrafanaPlugin> { export interface PluginConfigPage<T extends GrafanaPlugin> {

@ -10,7 +10,7 @@ const TAB_ID_A = 'A';
const TAB_ID_B = 'B'; const TAB_ID_B = 'B';
const TAB_ID_C = 'C'; const TAB_ID_C = 'C';
export class ExampleRootPage extends PureComponent<Props> { export class ExampleRootPage<ExampleAppSettings> extends PureComponent<Props> {
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
} }
@ -79,7 +79,7 @@ export class ExampleRootPage extends PureComponent<Props> {
} }
render() { render() {
const { path, query } = this.props; const { path, query, meta } = this.props;
return ( return (
<div> <div>
@ -96,6 +96,7 @@ export class ExampleRootPage extends PureComponent<Props> {
<a href={path + '?x=1&y=2&y=3'}>ZZZ</a> <a href={path + '?x=1&y=2&y=3'}>ZZZ</a>
</li> </li>
</ul> </ul>
<pre>{JSON.stringify(meta.jsonData)}</pre>
</div> </div>
); );
} }

@ -5,6 +5,7 @@ import { AppPlugin } from '@grafana/ui';
import { ExamplePage1 } from './config/ExamplePage1'; import { ExamplePage1 } from './config/ExamplePage1';
import { ExamplePage2 } from './config/ExamplePage2'; import { ExamplePage2 } from './config/ExamplePage2';
import { ExampleRootPage } from './ExampleRootPage'; import { ExampleRootPage } from './ExampleRootPage';
import { ExampleAppSettings } from './types';
// Legacy exports just for testing // Legacy exports just for testing
export { export {
@ -12,7 +13,7 @@ export {
AngularExamplePageCtrl, // Must match `pages.component` in plugin.json AngularExamplePageCtrl, // Must match `pages.component` in plugin.json
}; };
export const plugin = new AppPlugin() export const plugin = new AppPlugin<ExampleAppSettings>()
.setRootPage(ExampleRootPage) .setRootPage(ExampleRootPage)
.addConfigPage({ .addConfigPage({
title: 'Page 1', title: 'Page 1',

@ -0,0 +1,4 @@
export interface ExampleAppSettings {
customText?: string;
customCheckbox?: boolean;
}
Loading…
Cancel
Save