Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

2.9 KB
Raw Blame
import type { Call } from '@linedesk/db/types'
import { useT } from '@linedesk/i18n/react'
import { isTestCall, testCallLabel } from '../inbox-model.ts'
import type { Change } from '../inbox/changes.ts'
import { ContactPanel, type MenuItem, type PanelSuggestions } from '../contacts/ContactPanel.tsx'
import { useContact, useRows } from '../store.tsx'
import { parseTs } from '../time.ts'
import { Badge } from '../ui/kit.tsx'
import { EmailedAfterCall } from './EmailedAfterCall.tsx'
import { factSuggestions } from './factSuggestions.ts'

type Props = {
  call: Call
  onOpen: (call: Call) => void
  toast: (text: string) => void
  onToggleBlock?: (call: Call) => void
  /** Marks a call in the list read/unread or done, with undo. */
  onChange: (change: Change) => void
}

/**
 * The caller next to the call: the contact panel of Contacts (contacts/ContactPanel.tsx) with this call's key facts as
 * suggestions ("From this call · Keep") and every call of the caller, this one included and marked.
 */
export function ContactSide({ call, onOpen, toast, onToggleBlock, onChange }: Props) {
  const t = useT()
  const linked = useContact(call.contact_id)
  const contact = linked?.deleted_at ? undefined : linked // a deleted contact reads as an unknown caller
  const calls = useRows('calls')
    .filter(
      c =>
        !c.deleted_at &&
        (c.id === call.id || (call.contact_id ? c.contact_id === call.contact_id : c.from_number === call.from_number)),
    )
    .sort((a, b) => parseTs(b.started_at).getTime() - parseTs(a.started_at).getTime())
  const suggested: PanelSuggestions = {}
  if (contact)
    for (const [field, value] of Object.entries(factSuggestions(call.fields, contact)))
      suggested[field as keyof PanelSuggestions] = { value }
  const menu: MenuItem[] =
    contact && onToggleBlock
      ? [
          {
            label: contact.blocked_at ? t('call.contact.unblockNumber') : t('call.contact.blockNumber'),
            icon: 'ban',
            kbd: '⇧B',
            onSelect: () => onToggleBlock(call),
          },
        ]
      : []
  const badges = (
    <>
      {contact?.blocked_at && <Badge>{t('call.contact.blocked')}</Badge>}
      {call.spam_likely && <Badge title={t('call.contact.spamLikelyTitle')}>{t('call.contact.spamLikely')}</Badge>}
      {call.caller_attestation && call.caller_attestation !== 'A' && (
        <Badge title={t('call.contact.notVerifiedTitle')}>{t('call.contact.notVerified')}</Badge>
      )}
    </>
  )
  return (
    <aside className="cv-side" aria-label={t('call.contact.label')}>
      <ContactPanel
        contact={contact}
        name={isTestCall(call) ? testCallLabel() : undefined}
        number={call.from_number}
        badges={badges}
        menu={menu}
        suggested={suggested}
        calls={calls}
        currentCallId={call.id}
        onOpenCall={onOpen}
        onChangeCall={onChange}
        toast={toast}
      >
        <EmailedAfterCall call={call} />
      </ContactPanel>
    </aside>
  )
}