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.2 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { useState } from 'react'

import { IconDocument } from '@posthog/icons'
import { LemonButton, LemonInput, LemonMenu, Link } from '@posthog/lemon-ui'

import { urls } from 'scenes/urls'

import type { TicketMacroApi } from 'products/conversations/frontend/generated/api.schemas'

import { macroPreview, matchMacros } from './renderMacro'
import { supportMacrosLogic } from './supportMacrosLogic'

export function MacroPicker({
    disabledReason,
    onInsert,
}: {
    disabledReason?: string | JSX.Element
    onInsert: (macro: TicketMacroApi) => void
}): JSX.Element {
    const { macrosSorted, macrosLoading, macrosLoadFailed } = useValues(supportMacrosLogic)
    const { ensureMacrosLoaded } = useActions(supportMacrosLogic)
    const [query, setQuery] = useState('')
    const matches = matchMacros(macrosSorted, query)

    return (
        <LemonMenu
            placement="top-start"
            closeOnClickInside
            focusBasedKeyboardNavigation={false}
            onVisibilityChange={(visible) => {
                if (visible) {
                    ensureMacrosLoaded()
                } else {
                    setQuery('')
                }
            }}
            items={[
                {
                    label: () => (
                        <LemonInput
                            autoFocus
                            fullWidth
                            size="small"
                            placeholder="Search macros"
                            value={query}
                            onChange={setQuery}
                            onClick={(event) => event.stopPropagation()}
                        />
                    ),
                },
                ...matches.map((macro) => ({
                    key: macro.id,
                    label: (
                        <span className="flex min-w-0 flex-col items-start">
                            <span className="flex max-w-full items-center gap-2">
                                <span className="truncate">{macro.name}</span>
                                {macro.shortcut ? (
                                    <span className="shrink-0 text-xs text-muted">/{macro.shortcut}</span>
                                ) : null}
                                {macro.visibility === 'personal' ? (
                                    <span className="shrink-0 text-xs text-muted">Only you</span>
                                ) : null}
                            </span>
                            <span className="max-w-full truncate text-xs text-muted">{macroPreview(macro.body)}</span>
                        </span>
                    ),
                    onClick: () => onInsert(macro),
                    'data-attr': 'support-macro-insert',
                })),
                matches.length === 0
                    ? {
                          label: () => (
                              <span className="block px-2 py-1 text-sm text-muted">
                                  {macrosLoading ? (
                                      'Loading macros'
                                  ) : macrosLoadFailed ? (
                                      <>
                                          Couldn't load macros.{' '}
                                          <Link
                                              onClick={(event) => {
                                                  event.stopPropagation()
                                                  ensureMacrosLoaded()
                                              }}
                                          >
                                              Try again
                                          </Link>
                                      </>
                                  ) : macrosSorted.length === 0 ? (
                                      <>
                                          No macros yet. Create one in{' '}
                                          <Link to={urls.settings('environment-conversations', 'conversations-macros')}>
                                              Support settings
                                          </Link>
                                          .
                                      </>
                                  ) : (
                                      'No macros match.'
                                  )}
                              </span>
                          ),
                      }
                    : null,
                {
                    label: () => (
                        <Link
                            to={urls.settings('environment-conversations', 'conversations-macros')}
                            className="block px-2 py-1 text-sm"
                        >
                            Manage macros
                        </Link>
                    ),
                },
            ]}
        >
            <LemonButton
                type="secondary"
                size="small"
                icon={<IconDocument />}
                disabledReason={disabledReason}
                data-attr="support-macro-picker"
            >
                Macros
            </LemonButton>
        </LemonMenu>
    )
}