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.

4.0 KB
Raw Blame
import { useValues } from 'kea'
import React, { useMemo } from 'react'
import { match } from 'ts-pattern'

import { IconPerson } from '@posthog/icons'
import { ProfilePicture } from '@posthog/lemon-ui'

import { cn } from 'lib/utils/css-classes'
import { UnexpectedNeverError } from 'lib/utils/guards'
import { fullName } from 'lib/utils/strings'

import { assigneeSelectLogic } from './assigneeSelectLogic'
import { Assignee, RoleAssignee, TicketAssignee, UserAssignee } from './types'

export interface AssigneeResolverProps {
    children: (props: { assignee: Assignee }) => React.ReactElement
    assignee: TicketAssignee
}

export const AssigneeResolver = ({ children, assignee }: AssigneeResolverProps): React.ReactElement => {
    const { resolveAssignee } = useValues(assigneeSelectLogic)
    const resolvedAssignee = useMemo(() => resolveAssignee(assignee), [assignee, resolveAssignee])
    return children({ assignee: resolvedAssignee })
}

export interface AssigneeBaseDisplayProps {
    assignee: Assignee
    size?: 'xsmall' | 'small' | 'medium' | 'large'
}

export interface AssigneeIconDisplayProps extends AssigneeBaseDisplayProps {}

function getIconClassname(size: 'xsmall' | 'small' | 'medium' | 'large' = 'medium'): string {
    switch (size) {
        case 'xsmall':
            return 'text-[0.6rem] h-3 w-3'
        case 'small':
            return 'text-[0.75rem] h-4 w-4'
        case 'medium':
            return 'text-[0.85rem] h-5 w-5'
        case 'large':
            return 'text-[1rem] h-6 w-6'
        default:
            throw new UnexpectedNeverError(size)
    }
}

export const AssigneeIconDisplay = ({ assignee, size }: AssigneeIconDisplayProps): JSX.Element => {
    return match(assignee)
        .with({ type: 'role' }, ({ role }: RoleAssignee) => (
            <ProfilePicture
                user={{ first_name: role.name, last_name: undefined, email: undefined }}
                className={getIconClassname(size)}
            />
        ))
        .with({ type: 'user' }, ({ user }: UserAssignee) => (
            <ProfilePicture user={user} className={getIconClassname(size)} />
        ))
        .otherwise(() => (
            <IconPerson
                className={cn(
                    'rounded-full border border-dashed border-secondary text-secondary flex items-center justify-center p-0.5',
                    getIconClassname(size)
                )}
            />
        ))
}

export interface AssigneeLabelDisplayProps extends AssigneeBaseDisplayProps {
    placeholder?: string
    className?: string
}

export const AssigneeLabelDisplay = ({
    assignee,
    className,
    size,
    placeholder,
}: AssigneeLabelDisplayProps): JSX.Element => {
    return (
        <span
            className={cn(className, {
                'text-xs': size === 'xsmall',
                'text-sm': size === 'small',
                'text-base': size === 'medium',
                'text-lg': size === 'large',
            })}
        >
            {match(assignee)
                .with({ type: 'role' }, ({ role }: RoleAssignee) => role.name)
                .with({ type: 'user' }, ({ user }: UserAssignee) => (
                    <span className="ph-no-capture">{fullName(user)}</span>
                ))
                .otherwise(() => placeholder || 'Unassigned')}
        </span>
    )
}

interface AssigneeDisplayProps
    extends AssigneeBaseDisplayProps, Omit<AssigneeLabelDisplayProps, 'className'>, AssigneeIconDisplayProps {
    className?: string
    labelClassname?: string
}

export const AssigneeDisplay = ({
    assignee,
    placeholder,
    className,
    labelClassname,
    size,
}: AssigneeDisplayProps): JSX.Element => {
    return (
        <div className={cn('flex justify-start items-center gap-1', className)}>
            <AssigneeIconDisplay assignee={assignee} size={size} />
            <AssigneeLabelDisplay
                className={labelClassname}
                size={size}
                assignee={assignee}
                placeholder={placeholder}
            />
        </div>
    )
}