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.

5.6 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { IconPlusSmall, IconX } from '@posthog/icons'
import { LemonButton, LemonInput } from '@posthog/lemon-ui'

import { urls } from 'scenes/urls'

import { AssigneeIconDisplay, AssigneeLabelDisplay } from './AssigneeDisplay'
import { assigneeSelectLogic } from './assigneeSelectLogic'
import { Assignee, TicketAssignee, toTicketAssignee } from './types'

export interface AssigneeDropdownProps {
    assignee: TicketAssignee
    onChange: (assignee: TicketAssignee) => void
}

export function AssigneeDropdown({ assignee, onChange }: AssigneeDropdownProps): JSX.Element {
    const { search, filteredRoles, otherFilteredMembers, currentUserMember, rolesLoading, membersLoading } =
        useValues(assigneeSelectLogic)
    const { setSearch } = useActions(assigneeSelectLogic)

    return (
        <div className="max-w-100 deprecated-space-y-2">
            <LemonInput type="search" placeholder="Search" autoFocus value={search} onChange={setSearch} fullWidth />
            <ul className="deprecated-space-y-2">
                {assignee && (
                    <li>
                        <LemonButton
                            fullWidth
                            role="menuitem"
                            size="small"
                            icon={<IconX />}
                            onClick={() => onChange(null)}
                        >
                            Remove assignee
                        </LemonButton>
                    </li>
                )}

                {currentUserMember && (
                    <li>
                        <AssigneeItem
                            item={{
                                id: currentUserMember.user.id,
                                type: 'user',
                                user: currentUserMember.user,
                            }}
                            onSelect={onChange}
                            activeId={assignee?.id}
                            labelSuffix={<span className="text-secondary">(you)</span>}
                        />
                    </li>
                )}

                <Section
                    title="Roles"
                    loading={rolesLoading}
                    search={!!search}
                    items={filteredRoles.map((role) => ({
                        id: role.id,
                        type: 'role' as const,
                        role: role,
                    }))}
                    onSelect={onChange}
                    activeId={assignee?.id}
                    emptyState={
                        <LemonButton
                            fullWidth
                            size="small"
                            icon={<IconPlusSmall />}
                            to={urls.settings('organization-roles')}
                        >
                            <div className="text-secondary">Create role</div>
                        </LemonButton>
                    }
                />

                {(!!search || membersLoading || otherFilteredMembers.length > 0) && (
                    <Section
                        title="Users"
                        loading={membersLoading}
                        search={!!search}
                        items={otherFilteredMembers.map((member) => ({
                            id: member.user.id,
                            type: 'user' as const,
                            user: member.user,
                        }))}
                        onSelect={onChange}
                        activeId={assignee?.id}
                    />
                )}
            </ul>
        </div>
    )
}

const AssigneeItem = ({
    item,
    onSelect,
    activeId,
    labelSuffix,
}: {
    item: Assignee
    onSelect: (value: TicketAssignee) => void
    activeId?: string | number
    labelSuffix?: JSX.Element
}): JSX.Element => {
    return (
        <LemonButton
            fullWidth
            role="menuitem"
            size="small"
            icon={<AssigneeIconDisplay assignee={item} />}
            onClick={() => item?.id && onSelect(String(activeId) === String(item.id) ? null : toTicketAssignee(item))}
            active={String(activeId) === String(item?.id)}
        >
            <span className="flex items-center gap-1">
                <AssigneeLabelDisplay assignee={item} />
                {labelSuffix}
            </span>
        </LemonButton>
    )
}

const Section = ({
    loading,
    search,
    items,
    onSelect,
    activeId,
    emptyState,
    title,
}: {
    title: string
    loading: boolean
    search: boolean
    items: Assignee[]
    onSelect: (value: TicketAssignee) => void
    activeId?: string | number
    emptyState?: JSX.Element
}): JSX.Element => {
    return (
        <li>
            <section className="deprecated-space-y-px">
                <h5 className="mx-2 my-0.5">{title}</h5>
                {items.map((item) => (
                    <li key={item?.id || 'unassigned'}>
                        <AssigneeItem item={item} onSelect={onSelect} activeId={activeId} />
                    </li>
                ))}

                {loading ? (
                    <div className="p-2 text-secondary italic truncate border-t">Loading...</div>
                ) : items.length === 0 ? (
                    search ? (
                        <div className="p-2 text-secondary italic truncate border-t">
                            <span>No matches</span>
                        </div>
                    ) : (
                        <div className="border-t pt-1">{emptyState}</div>
                    )
                ) : null}
            </section>
        </li>
    )
}