: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(integrations): suppress slack picker visual flashes on mount, selection, and blur (#61093)
Daniel Marchuk39d548d0eb30
diff --git a/frontend/src/lib/integrations/SlackChannelPicker.test.tsx b/frontend/src/lib/integrations/SlackChannelPicker.test.tsxindex 12e847ce926d..b7332cc87dec 100644--- a/frontend/src/lib/integrations/SlackChannelPicker.test.tsx+++ b/frontend/src/lib/integrations/SlackChannelPicker.test.tsx@@ -174,6 +174,70 @@ describe('SlackChannelPicker', () => {expect(channelIdLookups).toEqual([])})+ it('does not reload the bulk list on blur when no search has clobbered the cache', async () => {+ // LemonInputSelect calls setInputValue('') on blur (LemonInputSelect.tsx:_onBlur), which+ // lands in onInputChange(''). Without the hasActiveSearchRef guard the empty-val branch+ // would unconditionally fire loadAllSlackChannels() and briefly toggle+ // allSlackChannelsLoading true → false, flickering the "Only the first page" hint off and+ // back on for every focus → blur cycle.+ const { container } = render(+ <Provider>+ <SlackChannelPicker+ integration={INTEGRATION}+ value="C0B6HUH9FUH|#test-slack-notifications"+ onChange={jest.fn()}+ />+ </Provider>+ )++ // Wait for the initial useEffect-driven bulk load.+ await waitFor(() => {+ expect(channelsRequestSearchQueries).toEqual([''])+ })++ // Focus and blur the picker without typing — the user clicks into the input and then+ // clicks somewhere else. Clicking outside triggers LemonDropdown's onClickOutside, which+ // clears popoverFocusRef and blurs the input, so _onBlur reaches setInputValue('').+ const input = container.querySelector<HTMLInputElement>('input[data-attr="select-slack-channel"]')!+ await userEvent.click(input)+ await userEvent.click(document.body)++ // The empty-search load has no breakpoint, so a missed guard fires its request during the+ // awaited click above — verified by reverting the guard. 100ms is margin for CI scheduling.+ await new Promise((resolve) => setTimeout(resolve, 100))+ expect(channelsRequestSearchQueries).toEqual([''])+ })++ it('does reload the bulk list on blur after a search clobbered it', async () => {+ // Sanity check that the guard still allows the original "user typed a search, cleared+ // it, wants the full list back" recovery — otherwise the dropdown would stay stuck on+ // search results after the user gave up.+ const { container } = render(+ <Provider>+ <SlackChannelPicker integration={INTEGRATION} onChange={jest.fn()} />+ </Provider>+ )+ await waitFor(() => {+ expect(channelsRequestSearchQueries).toEqual([''])+ })++ const input = container.querySelector<HTMLInputElement>('input[data-attr="select-slack-channel"]')!+ await userEvent.click(input)+ await userEvent.type(input, 'general')+ await waitFor(() => expect(channelsRequestSearchQueries).toContain('general'), { timeout: 5000 })++ // Now clear the input — should trigger the recovery reload (empty search) to refresh the+ // bulk list back to the full first page.+ await userEvent.clear(input)+ await waitFor(+ () => {+ // The recovery is observable as a second empty-search request beyond the initial one.+ expect(channelsRequestSearchQueries.filter((q) => q === '').length).toBeGreaterThanOrEqual(2)+ },+ { timeout: 2000 }+ )+ })+it('still searches when the user actually types a different value', async () => {// Render without an initial value so the input starts empty and typing isn't appended// to LemonInputSelect's auto-fill of an existing selection.diff --git a/frontend/src/lib/integrations/SlackIntegrationHelpers.tsx b/frontend/src/lib/integrations/SlackIntegrationHelpers.tsxindex e6304dd890aa..d5ac21e53759 100644--- a/frontend/src/lib/integrations/SlackIntegrationHelpers.tsx+++ b/frontend/src/lib/integrations/SlackIntegrationHelpers.tsx@@ -1,5 +1,5 @@import { useActions, useValues } from 'kea'-import { useEffect, useMemo, useState } from 'react'+import { useEffect, useMemo, useRef, useState } from 'react'import {LemonBanner,@@ -88,8 +88,13 @@ export function SlackChannelPicker({ onChange, value, integration, disabled }: SisPrivateChannelWithoutAccess,getChannelRefreshButtonDisabledReason,} = useValues(slackIntegrationLogic({ id: integration.id }))- const { loadAllSlackChannels, loadSlackChannelById } = useActions(slackIntegrationLogic({ id: integration.id }))+ const { loadAllSlackChannels, loadSlackChannelById, loadSlackChannelByIdSuccess } = useActions(+ slackIntegrationLogic({ id: integration.id })+ )const [localValue, setLocalValue] = useState<string | null>(null)+ // Gates the empty-val recovery reload: LemonInputSelect's setInputValue('') on blur and+ // after-select would otherwise flicker the "first page of channels" hint on every focus cycle.+ const hasActiveSearchRef = useRef(false)const channelRefreshButtonDisabledReason = getChannelRefreshButtonDisabledReason()// 1s tick while the cooldown is active so the countdown updates; otherwise idle the rerender (60s, picker is short-lived).@@ -150,7 +155,19 @@ export function SlackChannelPicker({ onChange, value, integration, disabled }: Sreturn (<><LemonInputSelect- onChange={(val) => onChange?.(val[0] ?? null)}+ onChange={(val) => {+ const key = val[0] ?? null+ if (key) {+ // Pin into the by-id slot so the post-select bulk reload can't drop the+ // channel from slackChannels and unresolve the label.+ const [channelId] = key.split('|')+ const channel = slackChannels.find((c: SlackChannelType) => c.id === channelId)+ if (channel) {+ loadSlackChannelByIdSuccess(channel)+ }+ }+ onChange?.(key)+ }}onInputChange={(val) => {if (val) {// Slack channel IDs are uppercase; normalize so pasted lowercase IDs still@@ -167,10 +184,12 @@ export function SlackChannelPicker({ onChange, value, integration, disabled }: S// would overwrite the cached list with [], so the bare ID could no longer// resolve to a name after blur.loadAllSlackChannels(false, val)+ hasActiveSearchRef.current = true}setLocalValue(val)- } else {+ } else if (hasActiveSearchRef.current) {loadAllSlackChannels()+ hasActiveSearchRef.current = false}}}value={modifiedValue ? [modifiedValue] : []}diff --git a/frontend/src/lib/integrations/slackIntegrationLogic.test.ts b/frontend/src/lib/integrations/slackIntegrationLogic.test.tsindex 0314427f60ac..d237ca1a1d35 100644--- a/frontend/src/lib/integrations/slackIntegrationLogic.test.ts+++ b/frontend/src/lib/integrations/slackIntegrationLogic.test.ts@@ -84,6 +84,31 @@ describe('slackIntegrationLogic — loadAllSlackChannels search & pagination', (expect(lastChannelsQuery.limit).toBeUndefined()})+ it('loadSlackChannelByIdSuccess pins a channel into slackChannels so a subsequent bulk reload cannot drop it', async () => {+ // First, load a bulk page that does NOT include the channel we're about to pin.+ nextChannelsResponse = [{ id: 'C_BULK', name: 'bulk-channel' }]+ await expectLogic(logic, () => {+ logic.actions.loadAllSlackChannels()+ }).toFinishAllListeners()+ expect(logic.values.slackChannels.map((c) => c.id)).toEqual(['C_BULK'])++ // Now pin an off-page channel via the loader's auto-generated success action — this is+ // exactly what the picker does on selection to keep the channel resolvable through any+ // subsequent bulk reload triggered by LemonInputSelect's setInputValue('') side-effect.+ logic.actions.loadSlackChannelByIdSuccess(buildChannel('C_OFFPAGE', 'off-page-channel'))+ expect(logic.values.slackChannels.map((c) => c.id).sort()).toEqual(['C_BULK', 'C_OFFPAGE'])++ // Trigger a bulk reload that returns a fresh page still not including the pinned channel.+ nextChannelsResponse = [{ id: 'C_BULK2', name: 'bulk-channel-2' }]+ await expectLogic(logic, () => {+ logic.actions.loadAllSlackChannels()+ }).toFinishAllListeners()++ // Without pinning, slackChannels would be ['C_BULK2']. With the pin held in+ // _fetchedSlackChannelById, the channel survives the reload.+ expect(logic.values.slackChannels.map((c) => c.id).sort()).toEqual(['C_BULK2', 'C_OFFPAGE'])+ })+it('reloads the full list when a search-then-clear sequence runs', async () => {// First, narrow the cache with a search — server returns only the matching subset.nextChannelsResponse = [{ id: 'C2', name: 'engineering' }]@@ -106,6 +131,28 @@ describe('slackIntegrationLogic — loadAllSlackChannels search & pagination', (expect(lastChannelsQuery.search).toBe('')expect(logic.values.slackChannels.map((c) => c.id)).toEqual(['C1', 'C2'])})++ it('isMemberOfSlackChannel returns null when the channel has not been loaded yet', () => {+ // Default selector state: no channels fetched, no by-id lookup landed. The picker uses+ // === false strict comparison to decide whether to show the "not in channel" warning, so+ // returning null here is what keeps that warning hidden until membership is actually known.+ expect(logic.values.isMemberOfSlackChannel('C_UNKNOWN')).toBeNull()+ })++ it('isMemberOfSlackChannel returns true/false once the channel is in slackChannels', async () => {+ nextChannelsResponse = [+ { id: 'CMEMBER', name: 'i-am-a-member' },+ // Override the helper's default by patching is_member after the fact.+ ]+ await expectLogic(logic, () => {+ logic.actions.loadAllSlackChannels()+ }).toFinishAllListeners()++ // The fixture builds channels with is_member: true by default.+ expect(logic.values.isMemberOfSlackChannel('CMEMBER')).toBe(true)+ // Unrelated id still reads as not-yet-loaded.+ expect(logic.values.isMemberOfSlackChannel('CMISSING')).toBeNull()+ })})describe('slackIntegrationLogic — getChannelRefreshButtonDisabledReason', () => {diff --git a/frontend/src/lib/integrations/slackIntegrationLogic.ts b/frontend/src/lib/integrations/slackIntegrationLogic.tsindex c3d0183c68d2..74920395ee56 100644--- a/frontend/src/lib/integrations/slackIntegrationLogic.ts+++ b/frontend/src/lib/integrations/slackIntegrationLogic.ts@@ -104,8 +104,11 @@ export const slackIntegrationLogic = kea<slackIntegrationLogicType>([isMemberOfSlackChannel: [(s) => [s.slackChannels],(slackChannels: SlackChannelType[]) => {- return (channel: string) =>- slackChannels.find((x) => x.id === slackChannelId(channel))?.is_member ?? false+ return (channel: string): boolean | null => {+ const found = slackChannels.find((x) => x.id === slackChannelId(channel))+ // Null when unknown so the picker's strict `=== false` membership gate doesn't fire spuriously.+ return found ? (found.is_member ?? false) : null+ }},],isPrivateChannelWithoutAccess: [