Skip to content

Commit

Permalink
revert "chore: Make pluginConfigs be keyed over config id not plugin …
Browse files Browse the repository at this point in the history
…id" (#18037)

Revert "chore: Make pluginConfigs be keyed over config id not plugin id (#17999)"

This reverts commit df282f6.
  • Loading branch information
tiina303 authored Oct 17, 2023
1 parent acf30da commit 4fa2d8c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
4 changes: 2 additions & 2 deletions frontend/src/scenes/apps/frontendAppsLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -31,7 +31,7 @@ export const frontendAppsLogic = kea<frontendAppsLogicType>([
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}`)
Expand Down
45 changes: 21 additions & 24 deletions frontend/src/scenes/plugins/pluginsLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ export const pluginsLogic = kea<pluginsLogicType>([
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 }) => {
Expand All @@ -145,16 +156,14 @@ export const pluginsLogic = kea<pluginsLogicType>([
},
],
pluginConfigs: [
{} as Record<number, PluginConfigType>,
{} as Record<string, PluginConfigType>,
{
loadPluginConfigs: async () => {
const pluginConfigs: Record<number, PluginConfigType> = {}
const pluginConfigs: Record<string, PluginConfigType> = {}
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
Expand Down Expand Up @@ -187,7 +196,7 @@ export const pluginsLogic = kea<pluginsLogicType>([
// 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
Expand All @@ -211,7 +220,7 @@ export const pluginsLogic = kea<pluginsLogicType>([
})
const newPluginConfigs: Record<string, PluginConfigType> = { ...pluginConfigs }
for (const pluginConfig of response) {
newPluginConfigs[pluginConfig.id] = pluginConfig
newPluginConfigs[pluginConfig.plugin] = pluginConfig
}
return newPluginConfigs
},
Expand Down Expand Up @@ -316,7 +325,7 @@ export const pluginsLogic = kea<pluginsLogicType>([
const newPluginConfigs: Record<number, PluginConfigType> = {}
Object.values(pluginConfigs).forEach((pluginConfig) => {
if (plugins[pluginConfig.plugin]) {
newPluginConfigs[pluginConfig.id] = pluginConfig
newPluginConfigs[pluginConfig.plugin] = pluginConfig
}
})
return newPluginConfigs
Expand Down Expand Up @@ -411,13 +420,7 @@ export const pluginsLogic = kea<pluginsLogicType>([
(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],
Expand All @@ -432,14 +435,8 @@ export const pluginsLogic = kea<pluginsLogicType>([

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<PluginConfigType, 'id'> & { id?: number }
if (pluginConfigsForPlugin) {
pluginConfig = pluginConfigsForPlugin[0]
} else {
let pluginConfig: PluginConfigType = { ...pluginConfigs[plugin.id] }
if (!pluginConfigs[plugin.id]) {
const config: Record<string, any> = {}
Object.entries(getConfigSchemaObject(plugin.config_schema)).forEach(
([key, { default: def }]) => {
Expand All @@ -448,7 +445,7 @@ export const pluginsLogic = kea<pluginsLogicType>([
)

pluginConfig = {
id: undefined, // TODO: only place where we use undefined for id
id: undefined,
team_id: currentTeam.id,
plugin: plugin.id,
enabled: false,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/scenes/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export enum PluginRepositoryEntryType {
}

export interface PluginTypeWithConfig extends PluginType {
pluginConfig: Omit<PluginConfigType, 'id'> & { id?: number }
pluginConfig: PluginConfigType
updateStatus: PluginUpdateStatusType
hasMoved?: boolean
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ export interface JobSpec {
}

export interface PluginConfigType {
id: number
id?: number
plugin: number
team_id: number
enabled: boolean
Expand Down

0 comments on commit 4fa2d8c

Please sign in to comment.