Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(dashboards): load the template schema only in the template editor (#117798)

Paul D'Ambra7772642175e2
diff --git a/frontend/src/scenes/dashboard/dashboardTemplateEditorLogic.test.ts b/frontend/src/scenes/dashboard/dashboardTemplateEditorLogic.test.ts
new file mode 100644
index 000000000000..6b72f3ec332d
--- /dev/null
+++ b/frontend/src/scenes/dashboard/dashboardTemplateEditorLogic.test.ts
@@ -0,0 +1,39 @@
+import { expectLogic } from 'kea-test-utils'
+
+import api from 'lib/api'
+
+import { initKeaTests } from '~/test/init'
+
+import { dashboardTemplateEditorLogic } from './dashboardTemplateEditorLogic'
+
+describe('dashboardTemplateEditorLogic', () => {
+ let logic: ReturnType<typeof dashboardTemplateEditorLogic.build>
+ let getSchema: jest.SpyInstance
+
+ beforeEach(() => {
+ initKeaTests()
+ getSchema = jest.spyOn(api.dashboardTemplates, 'getSchema').mockResolvedValue({ type: 'object' })
+ logic = dashboardTemplateEditorLogic()
+ logic.mount()
+ })
+
+ afterEach(() => {
+ logic.unmount()
+ jest.restoreAllMocks()
+ })
+
+ it('loads the template schema once, when the editor first opens', async () => {
+ expect(getSchema).not.toHaveBeenCalled()
+
+ await expectLogic(logic, () => {
+ logic.actions.openDashboardTemplateEditor()
+ })
+ .toDispatchActions(['getTemplateSchemaSuccess'])
+ .toMatchValues({ templateSchema: { type: 'object' } })
+
+ logic.actions.closeDashboardTemplateEditor()
+ logic.actions.openDashboardTemplateEditor()
+
+ expect(getSchema).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/frontend/src/scenes/dashboard/dashboardTemplateEditorLogic.ts b/frontend/src/scenes/dashboard/dashboardTemplateEditorLogic.ts
index 1fb1bfb5ffd8..3dd495f8f613 100644
--- a/frontend/src/scenes/dashboard/dashboardTemplateEditorLogic.ts
+++ b/frontend/src/scenes/dashboard/dashboardTemplateEditorLogic.ts
@@ -1,4 +1,4 @@
-import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers } from 'kea'
+import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import { createElement, Fragment } from 'react'
@@ -407,6 +407,11 @@ export const dashboardTemplateEditorLogic = kea<dashboardTemplateEditorLogicType
deleteDashboardTemplateSuccess: async () => {
refreshDashboardTemplateListsAfterMutation()
},
+ openDashboardTemplateEditor: () => {
+ if (!values.templateSchema && !values.templateSchemaLoading) {
+ actions.getTemplateSchema()
+ }
+ },
closeDashboardTemplateEditor: () => {
actions.clear()
},
@@ -492,7 +497,4 @@ export const dashboardTemplateEditorLogic = kea<dashboardTemplateEditorLogicType
})
},
})),
- afterMount(({ actions }) => {
- actions.getTemplateSchema()
- }),
])