|
1 | 1 | import { ConfigStore } from '@kinvolk/headlamp-plugin/lib'; |
| 2 | +import type { |
| 3 | + EventListEvent, |
| 4 | + ResourceDetailsViewLoadedEvent, |
| 5 | + ResourceListViewLoadedEvent, |
| 6 | +} from '@kinvolk/headlamp-plugin/lib/plugin/registry'; |
2 | 7 | import React from 'react'; |
3 | 8 | import { useBetween } from 'use-between'; |
4 | | -import { StoredProviderConfig } from './utils/ProviderConfigManager'; |
| 9 | +import type { StoredProviderConfig, SavedConfigurations } from './utils/ProviderConfigManager'; |
5 | 10 |
|
6 | 11 | export const PLUGIN_NAME = '@headlamp-k8s/ai-assistant'; |
7 | 12 | export const getSettingsURL = () => `/settings/plugins/${encodeURIComponent(PLUGIN_NAME)}`; |
8 | 13 |
|
9 | | -export const pluginStore = new ConfigStore<{ isUIPanelOpen?: boolean }>(PLUGIN_NAME); |
| 14 | +//@todo: In index.tsx the setEvent uses things from event.data into the root of the event. |
| 15 | +// Why does it do this? Maybe it can just use event.data as is? |
| 16 | +// This would simplify things and avoid copying and casts/type gymnastics. |
| 17 | + |
| 18 | +//@todo: Can we have these names be the same? It's confusing having three names. |
| 19 | +// EventListEvent, OBJECT_EVENTS and headlamp.object-events |
| 20 | + |
| 21 | +// @todo: We can't just do something like this... |
| 22 | +// because these types actually copy from "data" to top level... and add other fields. |
| 23 | +// type HeadlampEventPayload = |
| 24 | +// | { |
| 25 | +// type: 'headlamp.home-page-loaded'; |
| 26 | +// clusters: any; |
| 27 | +// errors: any; |
| 28 | +// } |
| 29 | +// | EventListEvent |
| 30 | +// | ResourceDetailsViewLoadedEvent |
| 31 | +// | ResourceListViewLoadedEvent; |
| 32 | +/** |
| 33 | + * Event fired when a resource is loaded in the details view. |
| 34 | + */ |
| 35 | +// export interface ResourceDetailsViewLoadedEvent { |
| 36 | +// type: HeadlampEventType.DETAILS_VIEW; |
| 37 | +// data: { |
| 38 | +// /** The resource that was loaded. */ |
| 39 | +// resource: KubeObject; |
| 40 | +// /** The error, if an error has occurred */ |
| 41 | +// error?: Error; |
| 42 | +// }; |
| 43 | +// } |
| 44 | +// /** |
| 45 | +// * Event fired when a list view is loaded for a resource. |
| 46 | +// */ |
| 47 | +// export interface ResourceListViewLoadedEvent { |
| 48 | +// type: HeadlampEventType.LIST_VIEW; |
| 49 | +// data: { |
| 50 | +// /** The list of resources that were loaded. */ |
| 51 | +// resources: KubeObject[]; |
| 52 | +// /** The kind of resource that was loaded. */ |
| 53 | +// resourceKind: string; |
| 54 | +// /** The error, if an error has occurred */ |
| 55 | +// error?: Error; |
| 56 | +// }; |
| 57 | +// } |
| 58 | +// /** |
| 59 | +// * Event fired when kubernetes events are loaded (for a resource or not). |
| 60 | +// */ |
| 61 | +// export interface EventListEvent { |
| 62 | +// type: HeadlampEventType.OBJECT_EVENTS; |
| 63 | +// data: { |
| 64 | +// /** The resource for which the events were loaded. */ |
| 65 | +// resource?: KubeObject; |
| 66 | +// /** The list of events that were loaded. */ |
| 67 | +// events: Event[]; |
| 68 | +// }; |
| 69 | +// } |
| 70 | + |
| 71 | +// interface HeadlampEvent { |
| 72 | +// type?: string; |
| 73 | +// title?: string; |
| 74 | +// resource?: any; |
| 75 | +// items?: any[]; |
| 76 | +// resources?: any[]; |
| 77 | +// resourceKind?: string; |
| 78 | +// errors?: any[]; |
| 79 | +// objectEvent?: { |
| 80 | +// events?: any[]; |
| 81 | +// }; |
| 82 | +// } |
| 83 | + |
| 84 | +export type HeadlampEventPayload = |
| 85 | + | { |
| 86 | + type: 'headlamp.home-page-loaded'; |
| 87 | + title?: string; |
| 88 | + items?: any[]; |
| 89 | + clusters: any; |
| 90 | + errors: any; |
| 91 | + resource?: EventListEvent['data']['resource']; |
| 92 | + resources?: any; |
| 93 | + resourceKind?: string; |
| 94 | + objectEvent?: any; |
| 95 | + } |
| 96 | + | { |
| 97 | + type: EventListEvent['type']; |
| 98 | + title?: string; |
| 99 | + items?: any[]; |
| 100 | + objectEvent: EventListEvent['data']; |
| 101 | + resource?: EventListEvent['data']['resource']; |
| 102 | + resources: any; |
| 103 | + // resourceKind: EventListEvent['data']['resourceKind']; |
| 104 | + resourceKind: string; |
| 105 | + } |
| 106 | + | { |
| 107 | + type: ResourceDetailsViewLoadedEvent['type']; |
| 108 | + title: string; |
| 109 | + items?: any[]; |
| 110 | + resource: ResourceDetailsViewLoadedEvent['data']['resource']; |
| 111 | + resources: any; |
| 112 | + resourceKind: string; |
| 113 | + // resourceKind: ResourceDetailsViewLoadedEvent['data']['resourceKind']; |
| 114 | + objectEvent?: ResourceDetailsViewLoadedEvent['data']; |
| 115 | + } |
| 116 | + | { |
| 117 | + type: ResourceListViewLoadedEvent['type']; |
| 118 | + title?: string; |
| 119 | + items?: any[]; |
| 120 | + //@todo: "resource" is not set in index.tsx with setEvent. Is this a bug? |
| 121 | + resource: any; |
| 122 | + resources: ResourceListViewLoadedEvent['data']['resources']; |
| 123 | + resourceKind: ResourceListViewLoadedEvent['data']['resourceKind']; |
| 124 | + objectEvent?: ResourceListViewLoadedEvent['data']; |
| 125 | + }; |
| 126 | + |
| 127 | +interface PluginConfig extends SavedConfigurations { |
| 128 | + /** Is the AI Assistant UI panel open? */ |
| 129 | + isUIPanelOpen?: boolean; |
| 130 | + /** Is the config popover shown? */ |
| 131 | + configPopoverShown?: boolean; |
| 132 | + /** Saved provider configurations */ |
| 133 | + savedProviders?: StoredProviderConfig[]; |
| 134 | + /** Currently active provider configuration */ |
| 135 | + activeProvider?: StoredProviderConfig; |
| 136 | + /** Is the plugin in test mode? */ |
| 137 | + testMode?: boolean; |
| 138 | + /** Latest Headlamp event payload */ |
| 139 | + event?: HeadlampEventPayload | null; //@todo: should this be HeadlampEventPayload? |
| 140 | +} |
| 141 | + |
| 142 | +export const pluginStore = new ConfigStore<PluginConfig>(PLUGIN_NAME); |
10 | 143 | export const usePluginConfig = pluginStore.useConfig(); |
11 | 144 |
|
12 | 145 | function usePluginSettings() { |
13 | | - const [event, setEvent] = React.useState(null); |
| 146 | + const [event, setEvent] = React.useState<HeadlampEventPayload | null>(null); |
14 | 147 | // Add states to track providers and active provider |
15 | 148 | const [savedProviders, setSavedProviders] = React.useState<StoredProviderConfig[]>([]); |
16 | 149 | const [activeProvider, setActiveProvider] = React.useState<StoredProviderConfig | null>(null); |
@@ -41,6 +174,7 @@ function usePluginSettings() { |
41 | 174 | setActiveProvider, |
42 | 175 | isUIPanelOpen, |
43 | 176 | setIsUIPanelOpen, |
| 177 | + // @todo: should testMode setTestMode be added here? |
44 | 178 | }; |
45 | 179 | } |
46 | 180 |
|
|
0 commit comments