diff --git a/frontend/src/scenes/apps/frontendAppsLogic.tsx b/frontend/src/scenes/apps/frontendAppsLogic.tsx index e0376749c61bb..1405c620eed87 100644 --- a/frontend/src/scenes/apps/frontendAppsLogic.tsx +++ b/frontend/src/scenes/apps/frontendAppsLogic.tsx @@ -2,7 +2,7 @@ import { actions, afterMount, connect, defaults, kea, path, reducers } from 'kea import type { frontendAppsLogicType } from './frontendAppsLogicType' import { getAppContext } from 'lib/utils/getAppContext' import { loaders } from 'kea-loaders' -import { FrontendApp, FrontendAppConfig, PluginConfigType } from '~/types' +import { FrontendApp, FrontendAppConfig } from '~/types' import { frontendAppRequire } from './frontendAppRequire' import { lemonToast } from 'lib/lemon-ui/lemonToast' import { pluginsLogic } from 'scenes/plugins/pluginsLogic' @@ -31,7 +31,7 @@ export const frontendAppsLogic = kea([ loadFrontendApp: async ({ id, pluginId, reload, attempt }) => { if (!values.appConfigs[id]) { if (pluginsLogic.findMounted()) { - const pluginConfig: PluginConfigType | undefined = pluginsLogic.values.getPluginConfig(id) + const pluginConfig = Object.values(pluginsLogic.values.pluginConfigs).find((c) => c.id === id) const plugin = pluginConfig ? pluginsLogic.values.plugins[pluginConfig.plugin] : undefined if (!plugin && !pluginConfig) { throw Error(`Could not load metadata for app with ID ${id}`) diff --git a/frontend/src/scenes/plugins/pluginsLogic.ts b/frontend/src/scenes/plugins/pluginsLogic.ts index ce98a2080f1b7..dd03bed0a3f6b 100644 --- a/frontend/src/scenes/plugins/pluginsLogic.ts +++ b/frontend/src/scenes/plugins/pluginsLogic.ts @@ -136,6 +136,17 @@ export const pluginsLogic = kea([ const response = await api.create(`api/organizations/@current/plugins/${id}/upgrade`) capturePluginEvent(`plugin updated`, response) actions.pluginUpdated(id) + // Check if we need to update the config (e.g. new required field) and if so, open the drawer. + const schema = getConfigSchemaObject(response.config_schema) + const pluginConfig = Object.values(values.pluginConfigs).filter((c) => c.plugin === id)[0] + if (pluginConfig?.enabled) { + if ( + Object.entries(schema).find(([key, { required }]) => required && !pluginConfig.config[key]) + ) { + actions.editPlugin(id) + } + } + return { ...values.plugins, [id]: response } }, patchPlugin: async ({ id, pluginChanges }) => { @@ -145,16 +156,14 @@ export const pluginsLogic = kea([ }, ], pluginConfigs: [ - {} as Record, + {} as Record, { loadPluginConfigs: async () => { - const pluginConfigs: Record = {} + const pluginConfigs: Record = {} const results: PluginConfigType[] = await loadPaginatedResults('api/plugin_config') for (const pluginConfig of results) { - if (pluginConfig.id) { - pluginConfigs[pluginConfig.id] = { ...pluginConfig } - } + pluginConfigs[pluginConfig.plugin] = { ...pluginConfig } } return pluginConfigs @@ -187,7 +196,7 @@ export const pluginsLogic = kea([ // Run the sync after we return from the loader, and save its data window.setTimeout(() => response.id && actions.syncFrontendAppState(response.id), 0) } - return { ...pluginConfigs, [response.id]: response } + return { ...pluginConfigs, [response.plugin]: response } }, toggleEnabled: async ({ id, enabled }) => { const { pluginConfigs, plugins } = values @@ -211,7 +220,7 @@ export const pluginsLogic = kea([ }) const newPluginConfigs: Record = { ...pluginConfigs } for (const pluginConfig of response) { - newPluginConfigs[pluginConfig.id] = pluginConfig + newPluginConfigs[pluginConfig.plugin] = pluginConfig } return newPluginConfigs }, @@ -316,7 +325,7 @@ export const pluginsLogic = kea([ const newPluginConfigs: Record = {} Object.values(pluginConfigs).forEach((pluginConfig) => { if (plugins[pluginConfig.plugin]) { - newPluginConfigs[pluginConfig.id] = pluginConfig + newPluginConfigs[pluginConfig.plugin] = pluginConfig } }) return newPluginConfigs @@ -411,13 +420,7 @@ export const pluginsLogic = kea([ (s) => [s.pluginConfigs], (pluginConfigs): ((id: number) => PluginConfigType | undefined) => (id: number) => - pluginConfigs[id], - ], - getPluginConfigsForPlugin: [ - (s) => [s.pluginConfigs], - (pluginConfigs): ((pluginId: number) => PluginConfigType[]) => - (pluginId: number) => - Object.values(pluginConfigs).filter((config) => config.plugin === pluginId), + Object.values(pluginConfigs).find(({ id: _id }) => id === _id), ], installedPlugins: [ (s) => [s.plugins, s.pluginConfigs, s.updateStatus], @@ -432,14 +435,8 @@ export const pluginsLogic = kea([ return pluginValues .map((plugin, index) => { - // TODO: Currently just returning the first pluginConfig if exists or creating an empty one - const pluginConfigsForPlugin = Object.values(pluginConfigs).filter( - (config) => config.plugin === plugin.id - ) - let pluginConfig: Omit & { id?: number } - if (pluginConfigsForPlugin) { - pluginConfig = pluginConfigsForPlugin[0] - } else { + let pluginConfig: PluginConfigType = { ...pluginConfigs[plugin.id] } + if (!pluginConfigs[plugin.id]) { const config: Record = {} Object.entries(getConfigSchemaObject(plugin.config_schema)).forEach( ([key, { default: def }]) => { @@ -448,7 +445,7 @@ export const pluginsLogic = kea([ ) pluginConfig = { - id: undefined, // TODO: only place where we use undefined for id + id: undefined, team_id: currentTeam.id, plugin: plugin.id, enabled: false, diff --git a/frontend/src/scenes/plugins/types.ts b/frontend/src/scenes/plugins/types.ts index c67dc927aac0b..f662a93708dd5 100644 --- a/frontend/src/scenes/plugins/types.ts +++ b/frontend/src/scenes/plugins/types.ts @@ -17,7 +17,7 @@ export enum PluginRepositoryEntryType { } export interface PluginTypeWithConfig extends PluginType { - pluginConfig: Omit & { id?: number } + pluginConfig: PluginConfigType updateStatus: PluginUpdateStatusType hasMoved?: boolean } diff --git a/frontend/src/types.ts b/frontend/src/types.ts index eef5a4a5fe1a5..6322a3fb6c4d0 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -1459,7 +1459,7 @@ export interface JobSpec { } export interface PluginConfigType { - id: number + id?: number plugin: number team_id: number enabled: boolean