: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
feat(subscriptions): load members in recipient selectors (#119575)
Matt Pua51191b928e9b
diff --git a/products/subscriptions/frontend/components/Subscriptions/SubscriptionWizard.tsx b/products/subscriptions/frontend/components/Subscriptions/SubscriptionWizard.tsxindex 5541ae0a5d67..34399a2f6d15 100644--- a/products/subscriptions/frontend/components/Subscriptions/SubscriptionWizard.tsx+++ b/products/subscriptions/frontend/components/Subscriptions/SubscriptionWizard.tsx@@ -357,7 +357,8 @@ function SubscriptionDeliveryStep({subscription: SubscriptionTypelogicProps: SubscriptionLogicProps}): JSX.Element {- const { meFirstMembers, membersLoading } = useValues(membersLogic)+ const { members, meFirstMembers, membersLoading } = useValues(membersLogic)+ const { ensureAllMembersLoaded } = useActions(membersLogic)const { integrations, slackIntegrations } = useValues(integrationsLogic)const { preflight } = useValues(preflightLogic)const { setSubscriptionValue } = useActions(subscriptionLogic(logicProps))@@ -386,6 +387,11 @@ function SubscriptionDeliveryStep({allowCustomValuesoptions={usersLemonSelectOptions(meFirstMembers.map((member) => member.user))}loading={membersLoading}+ onFocus={() => {+ if (!members) {+ ensureAllMembersLoaded()+ }+ }}placeholder="Enter an email address"/>)}diff --git a/products/subscriptions/frontend/components/Subscriptions/subscriptionCountLogic.test.ts b/products/subscriptions/frontend/components/Subscriptions/subscriptionCountLogic.test.tsindex 1f2cc02fd2ec..4c06f7deb7be 100644--- a/products/subscriptions/frontend/components/Subscriptions/subscriptionCountLogic.test.ts+++ b/products/subscriptions/frontend/components/Subscriptions/subscriptionCountLogic.test.ts@@ -1,6 +1,11 @@+import { MOCK_DEFAULT_ORGANIZATION, MOCK_DEFAULT_USER } from 'lib/api.mock'+import { expectLogic } from 'kea-test-utils'+import { userLogic } from 'scenes/userLogic'+import { initKeaTests } from '~/test/init'+import { AvailableFeature } from '~/types'import { subscriptionsList } from 'products/subscriptions/frontend/generated/api'@@ -18,22 +23,48 @@ describe('subscriptionCountLogic', () => {beforeEach(() => {initKeaTests()mockSubscriptionsList.mockReset()+ userLogic.mount()+ userLogic.actions.loadUserSuccess({+ ...MOCK_DEFAULT_USER,+ organization: { ...MOCK_DEFAULT_ORGANIZATION, available_product_features: [] },+ })})afterEach(() => {logic?.unmount()+ userLogic.unmount()})- it('loads the team-wide count from the response count field on mount', async () => {- // count (full team total) differs from results.length to catch a results.length regression.- mockSubscriptionsList.mockResolvedValue({ count: 3, results: [{ id: 1 }] })- logic = subscriptionCountLogic()- logic.mount()+ it.each([false, true])(+ 'loads the count only without the subscription entitlement (available=%s)',+ async (available) => {+ userLogic.actions.loadUserSuccess({+ ...MOCK_DEFAULT_USER,+ organization: {+ ...MOCK_DEFAULT_ORGANIZATION,+ available_product_features: available+ ? [{ key: AvailableFeature.SUBSCRIPTIONS, name: 'Subscriptions' }]+ : [],+ },+ })+ // count (full team total) differs from results.length to catch a results.length regression.+ mockSubscriptionsList.mockResolvedValue({ count: 3, results: [{ id: 1 }] })+ logic = subscriptionCountLogic()+ logic.mount()- await expectLogic(logic).toFinishAllListeners().toMatchValues({ subscriptionCount: 3 })- // Lock in the limit=1 invariant — we only need the count, not the rows.- expect(subscriptionsList).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ limit: 1 }))- })+ await expectLogic(logic)+ .toFinishAllListeners()+ .toMatchValues({ subscriptionCount: available ? null : 3 })+ if (available) {+ expect(subscriptionsList).not.toHaveBeenCalled()+ } else {+ expect(subscriptionsList).toHaveBeenCalledWith(+ expect.any(String),+ expect.objectContaining({ limit: 1 })+ )+ }+ }+ )it('loads zero for a team with no subscriptions', async () => {mockSubscriptionsList.mockResolvedValue({ count: 0, results: [] })diff --git a/products/subscriptions/frontend/components/Subscriptions/subscriptionCountLogic.ts b/products/subscriptions/frontend/components/Subscriptions/subscriptionCountLogic.tsindex 87eeae7b6045..0affaa8e755c 100644--- a/products/subscriptions/frontend/components/Subscriptions/subscriptionCountLogic.ts+++ b/products/subscriptions/frontend/components/Subscriptions/subscriptionCountLogic.ts@@ -1,12 +1,16 @@-import { MakeLogicType, afterMount, kea, path } from 'kea'+import { MakeLogicType, afterMount, connect, kea, path } from 'kea'import { loaders } from 'kea-loaders'import { getCurrentTeamId } from 'lib/utils/getAppContext'+import { userLogic } from 'scenes/userLogic'++import { AvailableFeature } from '~/types'import { subscriptionsList } from 'products/subscriptions/frontend/generated/api'// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface subscriptionCountLogicValues {+ hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean // userLogicsubscriptionCount: number | nullsubscriptionCountLoading: boolean}@@ -34,6 +38,7 @@ export type subscriptionCountLogicType = MakeLogicType<subscriptionCountLogicValexport const subscriptionCountLogic = kea<subscriptionCountLogicType>([path(['lib', 'components', 'Subscriptions', 'subscriptionCountLogic']),+ connect(() => ({ values: [userLogic, ['hasAvailableFeature']] })),loaders({// null = not loaded yet; consumers treat null as "unknown" and fail open.subscriptionCount: [@@ -47,7 +52,9 @@ export const subscriptionCountLogic = kea<subscriptionCountLogicType>([},],}),- afterMount(({ actions }) => {- actions.loadSubscriptionCount()+ afterMount(({ actions, values }) => {+ if (!values.hasAvailableFeature(AvailableFeature.SUBSCRIPTIONS)) {+ actions.loadSubscriptionCount()+ }}),])diff --git a/products/subscriptions/frontend/components/Subscriptions/views/EditSubscription.tsx b/products/subscriptions/frontend/components/Subscriptions/views/EditSubscription.tsxindex 47bf23485f3b..e20775d99bd5 100644--- a/products/subscriptions/frontend/components/Subscriptions/views/EditSubscription.tsx+++ b/products/subscriptions/frontend/components/Subscriptions/views/EditSubscription.tsx@@ -246,7 +246,8 @@ export function EditSubscription({dashboardId,})- const { meFirstMembers, membersLoading } = useValues(membersLogic)+ const { members, meFirstMembers, membersLoading } = useValues(membersLogic)+ const { ensureAllMembersLoaded } = useActions(membersLogic)const {subscription,subscriptionLoading,@@ -498,6 +499,11 @@ export function EditSubscription({data-attr="subscribed-emails"options={usersLemonSelectOptions(meFirstMembers.map((x) => x.user))}loading={membersLoading}+ onFocus={() => {+ if (!members) {+ ensureAllMembersLoaded()+ }+ }}placeholder="Enter an email address"/>)}diff --git a/products/subscriptions/frontend/components/Subscriptions/views/EditSubscription.unsavedChanges.test.tsx b/products/subscriptions/frontend/components/Subscriptions/views/EditSubscription.unsavedChanges.test.tsxindex 1a3d8840d74d..6852686eb70d 100644--- a/products/subscriptions/frontend/components/Subscriptions/views/EditSubscription.unsavedChanges.test.tsx+++ b/products/subscriptions/frontend/components/Subscriptions/views/EditSubscription.unsavedChanges.test.tsx@@ -9,6 +9,7 @@ import { router } from 'kea-router'import { userLogic } from 'scenes/userLogic'+import preflightJson from '~/mocks/fixtures/_preflight.json'import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'import { DashboardType, SubscriptionType } from '~/types'@@ -112,6 +113,7 @@ describe('EditSubscription unsaved changes', () => {'an email destination',{ target_type: 'email', target_value: '[email protected]', integration_id: null },],+ ['an empty email destination', { target_type: 'email', target_value: '', integration_id: null }],['a Slack destination', { target_type: 'slack', target_value: 'C1|#general', integration_id: 7 }],['a Slack destination with no stored connection',@@ -121,8 +123,21 @@ describe('EditSubscription unsaved changes', () => {'stays clean on open and after saving %s',async (_label, overrides) => {const subscription = subscriptionFixture(overrides as Partial<SubscriptionType>)+ const listMembers = jest.fn(() => ({+ count: 1,+ results: [+ {+ id: 'mem-1',+ level: 8,+ joined_at: '2022-01-01T00:09:00',+ updated_at: '2022-01-01T00:09:00',+ user: { ...MOCK_DEFAULT_USER, email: '[email protected]' },+ },+ ],+ }))useMocks({get: {+ '/_preflight/': { ...preflightJson, email_service_available: true },'/api/environments/:team/subscriptions/1': subscription,'/api/environments/:team/subscriptions': { count: 0, results: [] },'/api/projects/:team/subscriptions/1/deliveries/': { next: null, previous: null, results: [] },@@ -134,18 +149,7 @@ describe('EditSubscription unsaved changes', () => {at_limit: false,},'/api/organizations/@current/': MOCK_DEFAULT_ORGANIZATION,- '/api/organizations/@current/members/': {- count: 1,- results: [- {- id: 'mem-1',- level: 8,- joined_at: '2022-01-01T00:09:00',- updated_at: '2022-01-01T00:09:00',- user: { ...MOCK_DEFAULT_USER, email: '[email protected]' },- },- ],- },+ '/api/organizations/@current/members/': listMembers,},patch: { '/api/environments/:team/subscriptions/1': subscription },})@@ -162,10 +166,24 @@ describe('EditSubscription unsaved changes', () => {const name = await screen.findByPlaceholderText('e.g. Weekly team report', {}, { timeout: 10000 })expect(isClean()).toBe(true)+ expect(listMembers).not.toHaveBeenCalled()+ if (subscription.target_type === 'email') {+ const placeholder = subscription.target_value ? 'Add value' : 'Enter an email address'+ await userEvent.click(screen.getByPlaceholderText(placeholder))+ if (!subscription.target_value) {+ await userEvent.click(await screen.findByText('<[email protected]>'))+ }+ await waitFor(() => expect(listMembers).toHaveBeenCalledTimes(1))+ }await userEvent.type(name, ' renamed')await waitFor(() => expect(isClean()).toBe(false))+ if (subscription.target_type === 'email' && !subscription.target_value) {+ await userEvent.click(screen.getByPlaceholderText('Add value'))+ expect(listMembers).toHaveBeenCalledTimes(1)+ }+await userEvent.click(saveButton())await waitFor(() => expect(isClean()).toBe(true), { timeout: 10000 })},