: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(teams): keep each tab's project when reloading the team or project (#119284)
posthog[bot]e2edd6b60577
diff --git a/frontend/src/mocks/jest.ts b/frontend/src/mocks/jest.tsindex f16f7d1ab347..93265cbbd994 100644--- a/frontend/src/mocks/jest.ts+++ b/frontend/src/mocks/jest.ts@@ -1,3 +1,5 @@+import { MOCK_DEFAULT_TEAM } from 'lib/api.mock'+import { setupServer } from 'msw/node'import { useAvailableFeatures } from '~/mocks/features'@@ -12,6 +14,8 @@ import { Mocks, mocksToHandlers } from '~/mocks/utils'// still overrides them per test.const jestOnlyDefaultHandlers = mocksToHandlers({get: {+ // teamLogic and projectLogic reload the tab project by id, and the paginated floor is not a project.+ '/api/projects/:team_id/': MOCK_DEFAULT_TEAM,'/api/projects/:team_id/product_tours/': EMPTY_PAGINATED_RESPONSE,'/api/projects/:team_id/integrations/': EMPTY_PAGINATED_RESPONSE,'/api/environments/:team_id/llm_analytics/evaluation_config/': { active_provider_key: null },diff --git a/frontend/src/scenes/projectLogic.test.ts b/frontend/src/scenes/projectLogic.test.tsindex 7387aaa66ac7..cf1cc0f04ec7 100644--- a/frontend/src/scenes/projectLogic.test.ts+++ b/frontend/src/scenes/projectLogic.test.ts@@ -64,6 +64,29 @@ describe('projectLogic', () => {expect(requestedOrganizationIds).toEqual([MOCK_ORGANIZATION_ID])})+ it('loadCurrentProject reloads the tab project after another tab changes the current project', async () => {+ initKeaTests()+ const logic = projectLogic()+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadCurrentProjectSuccess'])+ useMocks({+ get: {+ '/api/projects/@current': () => [200, { ...MOCK_DEFAULT_PROJECT, id: MOCK_DEFAULT_PROJECT.id + 1 }],+ '/api/projects/:id': ({ params }) =>+ params.id === String(MOCK_DEFAULT_PROJECT.id)+ ? [200, { ...MOCK_DEFAULT_PROJECT, name: 'Reloaded project' }]+ : [404, {}],+ },+ })++ await expectLogic(logic, () => {+ logic.actions.loadCurrentProject()+ }).toDispatchActions(['loadCurrentProjectSuccess'])++ expect(logic.values.currentProject?.id).toBe(MOCK_DEFAULT_PROJECT.id)+ expect(logic.values.currentProject?.name).toBe('Reloaded project')+ })+it.each([['/project/997/insights', false],['/project/997/my-tickets', true],diff --git a/frontend/src/scenes/projectLogic.ts b/frontend/src/scenes/projectLogic.tsindex f5f8b3eca0c9..75f76c543d8b 100644--- a/frontend/src/scenes/projectLogic.ts+++ b/frontend/src/scenes/projectLogic.ts@@ -230,8 +230,11 @@ export const projectLogic = kea<projectLogicType>([return null}try {+ // @current is the user's server-side current project, which another tab can change.+ // Once this tab has a project, reload it by id so its API calls stay in it.+ const projectId = values.currentProject?.id ?? '@current'// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.- return await api.get('api/projects/@current')+ return await api.get(`api/projects/${projectId}`)} catch {return values.currentProject}diff --git a/frontend/src/scenes/teamLogic.test.ts b/frontend/src/scenes/teamLogic.test.tsindex 67233997654a..4c9927489913 100644--- a/frontend/src/scenes/teamLogic.test.ts+++ b/frontend/src/scenes/teamLogic.test.ts@@ -31,32 +31,6 @@ describe('teamLogic', () => {expect(logic.values.currentProjectId).toBe(MOCK_DEFAULT_TEAM.project_id)})- const refreshedFilters = [{ key: 'email', type: 'person', value: 'example.com', operator: 'not_icontains' }]- it.each([- ['the same team', MOCK_TEAM_ID, refreshedFilters],- ['a different team', MOCK_TEAM_ID + 1, MOCK_DEFAULT_TEAM.test_account_filters],- ])('refreshCurrentTeam takes a response for %s without a team reload', async (_, id, expectedFilters) => {- await expectLogic(logic).toDispatchActions(['loadCurrentTeamSuccess'])- useMocks({- get: {- '/api/environments/@current': () => [- 200,- { ...MOCK_DEFAULT_TEAM, id, test_account_filters: refreshedFilters },- ],- },- })-- await expectLogic(logic, () => {- logic.actions.refreshCurrentTeam()- })- .toFinishAllListeners()- .toNotHaveDispatchedActions(['loadCurrentTeamSuccess'])- .toDispatchActions(['refreshCurrentTeamSuccess'])-- expect(logic.values.currentTeam?.id).toBe(MOCK_TEAM_ID)- expect(logic.values.currentTeam?.test_account_filters).toEqual(expectedFilters)- })-it.each([['a rename', { name: 'Renamed project' }, 'Renamed project'],['a logo-only change', { logo_media_id: 'media-id' }, MOCK_DEFAULT_TEAM.name],@@ -85,6 +59,54 @@ describe('teamLogic', () => {})})+ describe('when the team id differs from its project id', () => {+ const refreshedFilters = [{ key: 'email', type: 'person', value: 'example.com', operator: 'not_icontains' }]+ const tabTeam = { ...MOCK_DEFAULT_TEAM, id: MOCK_TEAM_ID + 10 }++ beforeEach(() => {+ initKeaTests(true, tabTeam)+ useMocks({+ get: {+ '/api/projects/@current': () => [+ 200,+ { ...MOCK_DEFAULT_TEAM, id: MOCK_TEAM_ID + 1, project_id: MOCK_TEAM_ID + 1 },+ ],+ '/api/projects/:id': ({ params }) =>+ params.id === String(MOCK_TEAM_ID)+ ? [200, { ...tabTeam, test_account_filters: refreshedFilters }]+ : [404, {}],+ },+ })+ logic = teamLogic()+ logic.mount()+ })++ it('loadCurrentTeam reloads the tab project after another tab changes the current project', async () => {+ await expectLogic(logic).toDispatchActions(['loadCurrentTeamSuccess'])++ await expectLogic(logic, () => {+ logic.actions.loadCurrentTeam()+ }).toDispatchActions(['loadCurrentTeamSuccess'])++ expect(logic.values.currentTeam?.project_id).toBe(MOCK_TEAM_ID)+ expect(logic.values.currentTeam?.test_account_filters).toEqual(refreshedFilters)+ })++ it('refreshCurrentTeam reloads the tab project without a team reload', async () => {+ await expectLogic(logic).toDispatchActions(['loadCurrentTeamSuccess'])++ await expectLogic(logic, () => {+ logic.actions.refreshCurrentTeam()+ })+ .toFinishAllListeners()+ .toNotHaveDispatchedActions(['loadCurrentTeamSuccess'])+ .toDispatchActions(['refreshCurrentTeamSuccess'])++ expect(logic.values.currentTeam?.project_id).toBe(MOCK_TEAM_ID)+ expect(logic.values.currentTeam?.test_account_filters).toEqual(refreshedFilters)+ })+ })+describe('updateCurrentTeam with a name-only payload', () => {beforeEach(() => {initKeaTests(false)diff --git a/frontend/src/scenes/teamLogic.tsx b/frontend/src/scenes/teamLogic.tsxindex 0cde1e53fc88..3552b42f1515 100644--- a/frontend/src/scenes/teamLogic.tsx+++ b/frontend/src/scenes/teamLogic.tsx@@ -384,17 +384,25 @@ export const teamLogic = kea<teamLogicType>([}try {+ // @current is the user's server-side current project, which another tab can change.+ // Once this tab has a team, reload its project by id so its API calls stay in it.+ // The endpoint looks up project ids, and a team id can differ from its project id.+ const projectId = values.currentTeam?.project_id ?? '@current'// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.- return await api.get('api/projects/@current')+ return await api.get(`api/projects/${projectId}`)} catch {return values.currentTeam}},refreshCurrentTeam: async () => {+ const projectId = values.currentTeam?.project_id+ if (!projectId) {+ return values.currentTeam+ }+try {// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.- const team = await api.get('api/projects/@current')- return team?.id === values.currentTeam?.id ? team : values.currentTeam+ return await api.get(`api/projects/${projectId}`)} catch {return values.currentTeam}diff --git a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.ts b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.tsindex 87a288dd276d..341dbf97fd68 100644--- a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.ts+++ b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.ts@@ -2095,7 +2095,7 @@ describe('replayScannerLogic', () => {beforeEach(() => {teamSpy = jest.fn(() => [200, MOCK_DEFAULT_TEAM])- useMocks({ get: { '/api/environments/@current': teamSpy } })+ useMocks({ get: { '/api/projects/:id': teamSpy } })teamLogic.mount()})diff --git a/products/signals/frontend/inbox/components/onboarding/InboxSetupFlow.stories.tsx b/products/signals/frontend/inbox/components/onboarding/InboxSetupFlow.stories.tsxindex 1eab1f4fa174..a7968d059c6d 100644--- a/products/signals/frontend/inbox/components/onboarding/InboxSetupFlow.stories.tsx+++ b/products/signals/frontend/inbox/components/onboarding/InboxSetupFlow.stories.tsx@@ -47,7 +47,7 @@ function projectMocks(}return mswDecorator({get: {- '/api/projects/@current': () => [200, team],+ '/api/projects/:id': () => [200, team],'/api/projects/:id/error_tracking/issues/exists/': { exists: hasData },},post: {diff --git a/products/signals/frontend/inbox/logics/inboxSetupLogic.test.ts b/products/signals/frontend/inbox/logics/inboxSetupLogic.test.tsindex 17f26cd74c70..6584944f86be 100644--- a/products/signals/frontend/inbox/logics/inboxSetupLogic.test.ts+++ b/products/signals/frontend/inbox/logics/inboxSetupLogic.test.ts@@ -68,7 +68,7 @@ describe('focused Inbox setup', () => {scanners = []useMocks({get: {- '/api/projects/@current': () => [200, { ...MOCK_DEFAULT_TEAM, ingested_event: hasEvents }],+ '/api/projects/:id': () => [200, { ...MOCK_DEFAULT_TEAM, ingested_event: hasEvents }],'/api/projects/:id/signals/reports/available_reviewers/': {},'/api/projects/:id/wizard/sessions/latest/': () => [204, null],'/api/environments/:id/external_data_sources/': () => ({