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(frontend): stop refetching group types at boot (#106256)

Paul D'Ambra49ceeeacfa06
diff --git a/frontend/src/models/groupsModel.test.ts b/frontend/src/models/groupsModel.test.ts
new file mode 100644
index 000000000000..1d79982875f1
--- /dev/null
+++ b/frontend/src/models/groupsModel.test.ts
@@ -0,0 +1,64 @@
+import { MOCK_DEFAULT_ORGANIZATION, MOCK_DEFAULT_TEAM, MOCK_DEFAULT_USER } from 'lib/api.mock'
+
+import { expectLogic } from 'kea-test-utils'
+
+import { userLogic } from 'scenes/userLogic'
+
+import { useMocks } from '~/mocks/jest'
+import { groupsModel } from '~/models/groupsModel'
+import { initKeaTests } from '~/test/init'
+import { AvailableFeature, TeamType } from '~/types'
+
+const ORGANIZATION_WITH_GROUPS = {
+ ...MOCK_DEFAULT_ORGANIZATION,
+ available_product_features: [{ key: AvailableFeature.GROUP_ANALYTICS, name: AvailableFeature.GROUP_ANALYTICS }],
+}
+
+describe('groupsModel', () => {
+ let groupTypesRequest: jest.Mock
+
+ beforeEach(() => {
+ groupTypesRequest = jest.fn(() => [200, []])
+ useMocks({ get: { '/api/projects/:team_id/groups_types': groupTypesRequest } })
+ })
+
+ afterEach(() => {
+ groupsModel.unmount()
+ })
+
+ it.each([
+ {
+ name: 'uses app context group types without a request',
+ team: MOCK_DEFAULT_TEAM,
+ expectedRequests: 0,
+ expectedGroupTypeCount: MOCK_DEFAULT_TEAM.group_types.length,
+ },
+ {
+ name: 'loads group types once when the app context has none',
+ team: { ...MOCK_DEFAULT_TEAM, group_types: undefined } as unknown as TeamType,
+ expectedRequests: 1,
+ expectedGroupTypeCount: 0,
+ },
+ ])('on mount with groups enabled, $name', async ({ team, expectedRequests, expectedGroupTypeCount }) => {
+ initKeaTests(true, team, undefined, ORGANIZATION_WITH_GROUPS)
+ groupsModel.mount()
+
+ await expectLogic(groupsModel).toFinishAllListeners()
+
+ expect(groupsModel.values.groupsEnabled).toBe(true)
+ expect(groupTypesRequest).toHaveBeenCalledTimes(expectedRequests)
+ expect(groupsModel.values.groupTypes.size).toBe(expectedGroupTypeCount)
+ })
+
+ it('loads group types when groups become available after mount', async () => {
+ initKeaTests(true, MOCK_DEFAULT_TEAM, undefined, MOCK_DEFAULT_ORGANIZATION)
+ groupsModel.mount()
+ await expectLogic(groupsModel).toFinishAllListeners()
+ expect(groupTypesRequest).not.toHaveBeenCalled()
+
+ userLogic.actions.loadUserSuccess({ ...MOCK_DEFAULT_USER, organization: ORGANIZATION_WITH_GROUPS })
+ await expectLogic(groupsModel).toFinishAllListeners()
+
+ expect(groupTypesRequest).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/frontend/src/models/groupsModel.ts b/frontend/src/models/groupsModel.ts
index a3227be30719..6dc06a3142a7 100644
--- a/frontend/src/models/groupsModel.ts
+++ b/frontend/src/models/groupsModel.ts
@@ -1,14 +1,14 @@
import { MakeLogicType, afterMount, connect, kea, listeners, path, selectors } from 'kea'
import { loaders } from 'kea-loaders'
-import { subscriptions } from 'kea-subscriptions'
import api from 'lib/api'
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
import { GroupsAccessStatus, groupsAccessLogic } from 'lib/introductions/groupsAccessLogic'
import { wordPluralize } from 'lib/utils/strings'
import { projectLogic } from 'scenes/projectLogic'
+import { userLogic } from 'scenes/userLogic'
-import { GroupType, GroupTypeIndex } from '~/types'
+import { GroupType, GroupTypeIndex, UserType } from '~/types'
export interface Noun {
singular: string
@@ -32,6 +32,34 @@ export interface groupsModelValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface groupsModelActions {
+ loadUserSuccess: (
+ user: UserType | null,
+ payload?:
+ | {
+ resetOnFailure: boolean | undefined
+ }
+ | undefined
+ ) => {
+ payload?: {
+ resetOnFailure: boolean | undefined
+ }
+ user: UserType | null
+ } // userLogic
+ updateUserSuccess: (
+ user: UserType,
+ payload?:
+ | {
+ successCallback: (() => void) | undefined
+ user: Partial<UserType>
+ }
+ | undefined
+ ) => {
+ payload?: {
+ successCallback: (() => void) | undefined
+ user: Partial<UserType>
+ }
+ user: UserType
+ } // userLogic
createDetailDashboard: (groupTypeIndex: number) => number
createDetailDashboardFailure: (
error: string,
@@ -157,6 +185,7 @@ export const groupsModel = kea<groupsModelType>([
path(['models', 'groupsModel']),
connect(() => ({
values: [projectLogic, ['currentProjectId'], groupsAccessLogic, ['groupsEnabled', 'groupsAccessStatus']],
+ actions: [userLogic, ['loadUserSuccess', 'updateUserSuccess']],
})),
loaders(({ values }) => ({
groupTypesRaw: [
@@ -281,20 +310,25 @@ export const groupsModel = kea<groupsModelType>([
},
],
}),
- subscriptions(({ values }) => ({
- groupsEnabled: (enabled) => {
- // Load the groups types in the case of groups becoming an available feature after this logic is mounted
- if (!values.groupTypesLoading && enabled) {
- groupsModel.actions.loadAllGroupTypes()
+ listeners(({ actions, values, cache }) => {
+ // groupsEnabled comes from the organization's available features on the user, so it can only
+ // turn on when the user reloads, for example after an upgrade.
+ const loadGroupTypesIfGroupsTurnedOn = (): void => {
+ if (values.groupsEnabled && !cache.groupsWereEnabled && !values.groupTypesLoading) {
+ actions.loadAllGroupTypes()
}
- },
- })),
- listeners(({ actions }) => ({
- deleteGroupTypeSuccess: () => {
- actions.loadAllGroupTypes()
- },
- })),
- afterMount(({ actions }) => {
+ cache.groupsWereEnabled = values.groupsEnabled
+ }
+ return {
+ loadUserSuccess: loadGroupTypesIfGroupsTurnedOn,
+ updateUserSuccess: loadGroupTypesIfGroupsTurnedOn,
+ deleteGroupTypeSuccess: () => {
+ actions.loadAllGroupTypes()
+ },
+ }
+ }),
+ afterMount(({ actions, values, cache }) => {
+ cache.groupsWereEnabled = values.groupsEnabled
if (window.POSTHOG_APP_CONTEXT?.current_team?.group_types) {
actions.loadAllGroupTypesSuccess(window.POSTHOG_APP_CONTEXT.current_team.group_types)
} else {