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.

7.8 KB
Raw Blame
import { Meta, StoryObj } from '@storybook/react'
import { waitFor, within } from '@testing-library/dom'
import userEvent from '@testing-library/user-event'
import { useActions, useValues } from 'kea'
import { useEffect, useState } from 'react'

import { LemonTag } from '@posthog/lemon-ui'

import { useStorybookMocks } from '~/mocks/browser'
import { mockIntegration } from '~/test/mocks'
import { SlackChannelType } from '~/types'

import { recordRecentSlackChannel } from './slackChannel'
import { SlackChannelPicker } from './SlackIntegrationHelpers'
import { slackIntegrationLogic } from './slackIntegrationLogic'

const integration = mockIntegration

const channel = (id: string, name: string): SlackChannelType => ({
    id,
    name,
    is_private: false,
    is_ext_shared: false,
    is_member: true,
    is_private_without_access: false,
})

// The channel someone picks before inviting the PostHog app to it.
const notJoinedChannel: SlackChannelType = { ...channel('C7', 'launch-plans'), is_member: false }

const channels: SlackChannelType[] = [
    channel('C1', 'alerts'),
    channel('C2', 'announcements'),
    channel('C3', 'deploys'),
    channel('C4', 'general'),
    channel('C5', 'incidents'),
    channel('C6', 'random'),
]

// `recentlySubscribedChannelIds` read top-to-bottom: index 0 is the most-recently subscribed.
function OrderingScene({ recentlySubscribedChannelIds }: { recentlySubscribedChannelIds: string[] }): JSX.Element {
    const logic = slackIntegrationLogic({ id: integration.id })
    const { slackChannelsForPicker } = useValues(logic)
    const { loadAllSlackChannels } = useActions(logic)

    useEffect(() => {
        window.localStorage.clear()
        // Record oldest first so the most-recent ends up at the front of the store.
        ;[...recentlySubscribedChannelIds].reverse().forEach((id) => recordRecentSlackChannel(integration.id, id))
        loadAllSlackChannels()
        return () => window.localStorage.clear()
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [])

    const recent = new Set(recentlySubscribedChannelIds)

    return (
        <div className="p-4 max-w-md deprecated-space-y-4">
            <div>
                <h4>Picker</h4>
                <SlackChannelPicker integration={integration} onChange={() => {}} />
            </div>
            <div>
                <h4>Order shown in the dropdown</h4>
                <ol className="deprecated-space-y-1 pl-4">
                    {slackChannelsForPicker.map((c) => (
                        <li key={c.id} className="flex items-center justify-between gap-2">
                            <span>#{c.name}</span>
                            {recent.has(c.id) ? <LemonTag type="highlight">recents</LemonTag> : null}
                        </li>
                    ))}
                </ol>
            </div>
        </div>
    )
}

function MultipleSelectionScene(): JSX.Element {
    const [selectedChannels, setSelectedChannels] = useState(['C1|#alerts', 'C5|#incidents'])

    return (
        <div className="p-4 max-w-md">
            <SlackChannelPicker
                integration={integration}
                mode="multiple"
                value={selectedChannels}
                onChange={setSelectedChannels}
            />
        </div>
    )
}

function PastedChannelIdScene(): JSX.Element {
    const [selectedChannel, setSelectedChannel] = useState<string | null>(null)
    const response = { channels: [channel('C1234567890', 'release-updates')] }
    useStorybookMocks({
        get: {
            '/api/projects/:id/integrations/:intId/channels': response,
            '/api/environments/:id/integrations/:intId/channels': response,
        },
    })

    return (
        <div className="p-4 max-w-2xl">
            <SlackChannelPicker
                integration={integration}
                value={selectedChannel ?? undefined}
                onChange={setSelectedChannel}
            />
        </div>
    )
}

type StoryArgs = { recentlySubscribedChannelIds: string[] }

const meta: Meta<StoryArgs> = {
    title: 'Components/Slack channel picker',
    parameters: { layout: 'fullscreen', viewMode: 'story' },
    decorators: [
        function MocksDecorator(Story) {
            useStorybookMocks({
                get: {
                    '/api/projects/:id/integrations/:intId/channels': { channels },
                    '/api/environments/:id/integrations/:intId/channels': { channels },
                },
            })
            return <Story />
        },
    ],
    render: ({ recentlySubscribedChannelIds }) => (
        <OrderingScene recentlySubscribedChannelIds={recentlySubscribedChannelIds} />
    ),
}
export default meta

type Story = StoryObj<StoryArgs>

// No recency recorded yet — channels fall back to plain alphabetical order.
export const Alphabetical: Story = {
    args: { recentlySubscribedChannelIds: [] },
}

// "general" then "alerts" were the last two channels subscribed, so they float to the top
// (most recent first); everything else stays alphabetical below them.
export const RecentlySubscribedFirst: Story = {
    args: { recentlySubscribedChannelIds: ['C4', 'C1'] },
}

export const MultipleSelection: Story = {
    render: () => <MultipleSelectionScene />,
}

export const PastedChannelId: Story = {
    render: () => <PastedChannelIdScene />,
    play: async ({ canvasElement }) => {
        const input = await within(canvasElement).findByRole('textbox')
        await userEvent.click(input)
        await userEvent.paste('C1234567890')
        await within(document.body).findByText('#release-updates')
    },
}

// The app is not in the picked channel yet. "Check again" re-reads that one channel from Slack,
// so the warning clears as soon as someone runs the invite.
export const MissingAppInChannel: Story = {
    decorators: [
        function NotJoinedMocks(Story) {
            useStorybookMocks({
                get: {
                    '/api/projects/:id/integrations/:intId/channels': { channels: [...channels, notJoinedChannel] },
                    '/api/environments/:id/integrations/:intId/channels': {
                        channels: [...channels, notJoinedChannel],
                    },
                },
            })
            return <Story />
        },
    ],
    render: () => (
        <div className="p-4 max-w-md">
            <SlackChannelPicker
                integration={integration}
                value={`${notJoinedChannel.id}|#${notJoinedChannel.name}`}
                onChange={() => {}}
            />
        </div>
    ),
    // The warning renders only once the membership lookup lands, so settle it before the
    // screenshot rather than racing the banner into frame.
    play: async ({ canvasElement }) => {
        await within(canvasElement).findByText('Check again')
    },
}

// Typing a channel name and clicking away drops the search, because the picker takes an option
// rather than free text. The state below is what tells the user their channel is still unset.
export const SearchDroppedOnBlur: Story = {
    render: () => (
        <div className="p-4 max-w-md">
            <SlackChannelPicker integration={integration} onChange={() => {}} />
        </div>
    ),
    play: async ({ canvasElement }) => {
        // WebKit can start the play phase before React commits the first render, so wait for
        // the input instead of reading it straight out of the canvas.
        const input = await waitFor(() => {
            const element = canvasElement.querySelector<HTMLInputElement>('input[data-attr="select-slack-channel"]')
            if (!element) {
                throw new Error('Channel input not yet rendered')
            }
            return element
        })
        await userEvent.type(input, 'general')
        await userEvent.click(canvasElement)
        await within(canvasElement).findByText('No channel selected. Pick one from the list.')
    },
}