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.
TypeScript ai-receptionistanswering-serviceappointment-bookinglivekitlocal-firstmcpopen-sourceself-hostedsiptelephonytypescriptvoice-agentvoice-ai
// Settings, Phone numbers: a short list of the workspace's numbers; Manage opens one (which assistant answers it,
// how it is used, forwarding codes, disconnecting), Add number connects another in two steps: get a new number (its
// monthly price; a number already in the carrier account behind a quiet link), then whether the current number forwards
// to it. Connecting runs phoneNumber.setup through the outbox; the new row arrives by sync. Where a number can be bought
// comes from the install's buy offers (the phone setup status), the same list as onboarding's.
import { useDialogPlace } from '../shell/dialogAddress.ts'
import type { Assistant, PhoneNumber } from '@linedesk/db/types'
import { useT } from '@linedesk/i18n/react'
import { type ReactNode, useEffect, useState } from 'react'
import type { PhoneSetupStatus } from '../auth.ts'
import { ForwardingCheck } from '../onboarding/ForwardingCheck.tsx'
import { AbroadWarning, ForwardingSteps, GuidePickers, useForwardingPick } from '../onboarding/ForwardingGuide.tsx'
import { forwardingUnverified, type GuidePickedBy } from '../onboarding/forwarding-check.ts'
import {
areaCodeIn,
areaCodeOf,
beingSetUpText,
billedExtraNumberCents,
billedNumberNote,
billedNumberPrice,
buyOffersOf,
buyQuestion,
buyingCountry,
buyUnavailableText,
carrierName,
existingLinkText,
firstPickup,
formatNumber,
offersBuying,
type PhoneOption,
phoneOptionSetup,
phoneOptionText,
isNoneFreeText,
numberCharge,
numbersBeingSetUp,
phoneSetupErrorText,
pickupNumberMode,
priceCountry,
priceNote,
} from '../onboarding/flow.ts'
import {
areaCodeShown,
BASE_BUY_OFFERS,
buyableAreaCode,
buyOfferIn,
confirmsNumber,
countryOfNumber,
offerAreaCodes,
offerHasAreaCode,
phoneFromValue,
} from '@linedesk/actions'
import { isNativeApp } from '../native/mobile/platform.ts'
import { useDefaultPhoneCountry } from '../phoneCountry.ts'
import { providerAudience } from '../providerAudience.ts'
import { notAnsweringWhy } from '../inbox/goLive.ts'
import { usePresence } from '../shell/presence.ts'
import { useAssistantName, useAssistants, useClaimedRejection, useRows, useRun } from '../store.tsx'
import { RadioCard, Select } from '../ui/controls.tsx'
import { Card, Dialog, FormField } from '../ui/form.tsx'
import { AppLink } from '../ui/AppLink.tsx'
import { Button, Note } from '../ui/kit.tsx'
import { StatusMark } from '../ui/StatusMark.tsx'
import { ListEmpty } from '../ui/list.tsx'
import { PhoneInput } from '../ui/PhoneInput.tsx'
import { Loading, Skeleton, useLoadState } from '../ui/LoadStates.tsx'
import { SettingRows, SkeletonSettingRows } from '../ui/SettingRow.tsx'
import { LineGroup } from '../ui/FeatureLine.tsx'
import { NumberCard } from './NumberCard.tsx'
import { sectionHash, sectionParts } from '../ui/SettingsLayout.tsx'
import { CarrierLine, useCarrierStatus } from './Carrier.tsx'
import { OwnPhoneSystemLine } from './OwnPhoneSystem.tsx'
import { AreaCodeInput } from './AreaCodeInput.tsx'
import { NearbyAreaCodes } from './NearbyAreaCodes.tsx'
import { CarrierNumberInput } from './CarrierNumberInput.tsx'
import { NumberModeField } from './NumberMode.tsx'
import { useWorkspaceSettings } from './settings-api.ts'
import { useWorkspaceCountry } from '../workspaceLocale.ts'
import { brandName } from '../brand.ts'
import { i18n } from '../i18n/index.tsx'
type Props = {
canEdit: boolean
status: PhoneSetupStatus | null
isInstanceAdmin: boolean
/** On a bill: what each number beyond the included one costs a month (from the balance); null otherwise. */
extraNumberCents?: number | null
}
/** The assistant that answers a number: its own, else the workspace's first. */
const answeredBy = (number: PhoneNumber, assistants: Assistant[]) =>
assistants.find(a => a.id === number.assistant_id) ?? assistants[0]
/**
* Where this workspace can get numbers: a workspace with its own carrier account can add numbers there, whatever the
* install has (the session's status may be from before the carrier was changed here); else the server's status.
*/
export function usePhoneStatus(canEdit: boolean, serverStatus: PhoneSetupStatus | null) {
const carrier = useCarrierStatus(canEdit)
const ownCarrier = !!carrier && carrier.carrier !== 'default'
const status: PhoneSetupStatus | null = ownCarrier
? {
provider: carrier.carrier,
available: carrier.sipReady,
detail: null,
canAttach: true,
account: 'workspace',
// A workspace's own carrier account buys where every install does (Germany only on the install's).
buy: [...BASE_BUY_OFFERS],
}
: serverStatus
return { status, ownCarrier }
}
export function PhoneNumbers({ canEdit, status: serverStatus, isInstanceAdmin, extraNumberCents = null }: Props) {
const t = useT()
const { status, ownCarrier } = usePhoneStatus(canEdit, serverStatus)
const numbers = useRows('phone_numbers').sort((a, b) => a.created_at.localeCompare(b.created_at))
const numbersLoad = useLoadState('phone_numbers')
const calls = useRows('calls')
const assistants = useAssistants()
const presence = usePresence()
const answering = presence.kind === 'answering'
const canAdd = canEdit && !!status?.available
// Add a number has its own address (`#workspace/phone/add`): a link opens it, Back closes it. While the server's
// status loads the address waits; without the right (or a server that can't add numbers) the page shows without it.
const [adding, showAdding, hideAdding] = useDialogPlace('workspace/phone/add', canAdd || (canEdit && !status))
const setAdding = (on: boolean) => (on ? showAdding() : hideAdding())
return (
<>
{/* Why calls are not answered: above the cards, not a box inside one (settings audit, 2026-11-26). */}
{numbersLoad === 'ready' && (
<NotAnsweringNote
number={numbers.find(n => !answering && !forwardingUnverified(n, calls))}
assistants={assistants}
canFix={isInstanceAdmin}
/>
)}
{/* The page's label with Add number at its right: a LineGroup has no place for a button, so the two share a row. */}
<div className="pn-head" id="st-phone">
<LineGroup as="h2">{t('settings.phone.title')}</LineGroup>
{canAdd && (
<Button size="sm" icon="plus" onClick={() => setAdding(true)}>
{t('settings.phone.add')}
</Button>
)}
</div>
{canEdit && status && !status.available && (
<Unavailable detail={status.detail} isInstanceAdmin={isInstanceAdmin} ownCarrier={ownCarrier} />
)}
<NumberList
numbers={numbers}
assistants={assistants}
canEdit={canEdit}
page="workspace"
empty={
<Card>
<ListEmpty>{canEdit ? t('settings.phone.empty') : t('settings.phone.emptyMember')}</ListEmpty>
</Card>
}
/>
{adding && canAdd && (
<AddNumberDialog status={status} extraNumberCents={extraNumberCents} onClose={() => setAdding(false)} />
)}
{/* Where numbers come from: the carrier and the own phone system, as setting rows in their own quiet card. */}
{canEdit && (
<Card id="st-phone-carrier">
<SettingRows>
<CarrierLine serverProvider={serverStatus?.provider ?? null} />
<OwnPhoneSystemLine />
</SettingRows>
</Card>
)}
</>
)
}
/** Add number in its dialog; from an assistant's Phone section the new number is that assistant's. */
export function AddNumberDialog({
status,
extraNumberCents,
assistant,
onClose,
}: {
status: PhoneSetupStatus | null
extraNumberCents: number | null
assistant?: Assistant
onClose: () => void
}) {
const t = useT()
return (
<Dialog title={t('settings.phone.addTitle')} size="md" onClose={onClose}>
<ConnectNumber
status={status}
// The workspace's own carrier account is not billed by us: its numbers cost what that carrier charges.
extraNumberCents={billedExtraNumberCents(extraNumberCents, status)}
assistant={assistant}
onDone={onClose}
onCancel={onClose}
/>
</Dialog>
)
}
/**
* The number cards of Workspace settings › Phone numbers (every number) and of an assistant's Phone section (its own),
* with their ⋯ menu and Manage dialog. Only the workspace's list says and changes who answers a number
* (`answeredBy`): on an assistant's page that is the page itself. A link to one number (`#<page>/phone/<id>`, from the
* Settings overview's "Forwarding not checked") scrolls to its card once the numbers are loaded; the address then names
* the section only, so a reload starts at the top.
*/
export function NumberList({
numbers,
assistants,
canEdit,
page,
answeredBy: showAnsweredBy = true,
empty,
}: {
numbers: PhoneNumber[]
assistants: Assistant[]
canEdit: boolean
/** The page the cards are on, for links to one number: `workspace` or `assistant/<id>`. */
page: string
answeredBy?: boolean
/** The words of the empty list; null: the page shows its own. */
empty: ReactNode
}) {
const t = useT()
const numbersLoad = useLoadState('phone_numbers')
const calls = useRows('calls')
/** The number whose dialog is open, and whether it opens on releasing it. */
const [managing, setManaging] = useState<{ id: string; release: boolean } | null>(null)
const managed = numbers.find(n => n.id === managing?.id)
const linkedNumber = numbers.find(n => n.id === sectionParts(page, window.location.hash).item)?.id
useEffect(() => {
if (!linkedNumber) return
requestAnimationFrame(() => document.getElementById(`pn-${linkedNumber}`)?.scrollIntoView({ block: 'center' }))
window.history.replaceState(null, '', sectionHash(page, 'phone'))
}, [linkedNumber, page])
const menu = (n: PhoneNumber) => [
{
id: 'manage',
label: canEdit ? t('settings.phone.row.manage') : t('settings.phone.row.details'),
onSelect: () => setManaging({ id: n.id, release: false }),
},
...(canEdit
? [
{
id: 'release',
label: n.purchased ? t('settings.phone.release.menu') : t('settings.phone.disconnect.menu'),
danger: true,
onSelect: () => setManaging({ id: n.id, release: true }),
},
]
: []),
]
return (
<>
<Loading
what={t('settings.phone.what')}
loading={numbersLoad !== 'ready'}
error={numbersLoad === 'slow'}
className="pn-cards"
skeleton={<NumberCardSkeleton rows={showAnsweredBy ? 3 : 2} />}
>
{numbers.length > 0
? numbers.map(n => (
<NumberCard
key={n.id}
number={n}
assistant={answeredBy(n, assistants)}
assistants={assistants}
calls={calls}
canEdit={canEdit}
answeredBy={showAnsweredBy}
menu={menu(n)}
onManage={() => setManaging({ id: n.id, release: false })}
/>
))
: empty}
</Loading>
{managed && (
<ManageNumber
number={managed}
assistants={assistants}
canEdit={canEdit}
pickAssistant={showAnsweredBy}
release={managing?.release}
onClose={() => setManaging(null)}
/>
)}
</>
)
}
/** A number's card while the numbers load: its sentence, the grey line and its rows, as grey shapes. */
function NumberCardSkeleton({ rows }: { rows: number }) {
return (
<Card className="pn-card">
<div className="pn-card-sk">
<Skeleton shape="text" width={300} />
<Skeleton shape="text" width={240} />
</div>
<SkeletonSettingRows rows={rows} description={false} control="button" />
</Card>
)
}
/**
* Why a number shows "Not answering", and the fix: the inbox header's reasons (notAnsweringWhy), as a warning under
* the list. Every number shares the reason (the voice service is the server's), so one note, named after the first
* number that is not answering. The fix links to Server admin only for instance admins; everyone else reads what
* happens without them.
*/
function NotAnsweringNote({
number,
assistants,
canFix,
}: {
number: PhoneNumber | undefined
assistants: Assistant[]
canFix: boolean
}) {
const t = useT()
const presence = usePresence()
if (!number) return null
const assistant = answeredBy(number, assistants)?.name ?? t('settings.phone.row.theAssistant')
const reason = notAnsweringWhy(presence, assistant, canFix)
if (!reason) return null
return (
<Note
tone="warning"
title={reason.title}
action={
reason.fix && (
<a className="k-btn k-btn-secondary k-btn-sm" href={reason.fix.href}>
{reason.fix.label}
</a>
)
}
>
{reason.why}
</Note>
)
}
type UnavailableProps = {
detail: string | null
isInstanceAdmin: boolean
ownCarrier: boolean
}
function Unavailable({ detail, isInstanceAdmin, ownCarrier }: UnavailableProps) {
const t = useT()
return (
<p className="help" role="status">
{/* With its own carrier account only the server's side can be missing (where calls are sent). */}
{ownCarrier ? t('settings.phone.unavailable.ownCarrier') : t('settings.phone.unavailable.noCarrier')}{' '}
{isInstanceAdmin ? (
<AppLink to="#admin/providers/telephony" title={detail ?? undefined}>
{t('settings.phone.unavailable.connect')}
</AppLink>
) : (
t('settings.phone.unavailable.ask')
)}
</p>
)
}
type ManageProps = {
number: PhoneNumber
assistants: Assistant[]
canEdit: boolean
/** Who answers it can be picked here (not on an assistant's own page, which is that assistant's). */
pickAssistant?: boolean
/** Opens on releasing (or disconnecting) the number, from the row's menu. */
release?: boolean
onClose: () => void
}
/**
* Everything about one number: how it is used, who answers it, the forwarding codes, and disconnecting it. Also opened
* from the agent setup page's Call forwarding row (onboarding/SetupRowActions.tsx).
*/
export function ManageNumber({
number,
assistants,
canEdit,
pickAssistant = true,
release = false,
onClose,
}: ManageProps) {
const t = useT()
const run = useRun()
const [releasing, setReleasing] = useState(canEdit && release)
const forwards = number.mode === 'forward_conditional' || number.mode === 'forward_all'
const carrier = carrierName(number.provider)
/** A number of the business's own phone system (OwnPhoneSystem.tsx): no forwarding and no way of use to pick. */
const ownSystem = number.carrier_account === 'sip'
const who = answeredBy(number, assistants)
const pick = useForwardingPick(number)
return (
<Dialog
title={formatNumber(number.e164)}
description={
ownSystem
? t('settings.sip.manageDescription')
: carrier
? t('settings.phone.manage.description', {
carrier,
own: number.carrier_account === 'workspace',
bought: number.purchased,
brand: brandName(),
})
: undefined
}
onClose={onClose}
footer={
!releasing && (
<>
{canEdit && (
<Button variant="ghost" className="k-dialog-foot-start pn-release" onClick={() => setReleasing(true)}>
{number.purchased ? t('settings.phone.release.menu') : t('settings.phone.disconnect.menu')}
</Button>
)}
<Button variant="primary" onClick={onClose}>
{t('settings.phone.manage.done')}
</Button>
</>
)
}
>
{releasing ? (
<ReleaseNumber number={number} onCancel={() => setReleasing(false)} />
) : (
<>
{pickAssistant && assistants.length > 1 && (
<FormField
id="pn-who"
label={t('settings.phone.manage.assistant')}
help={t('settings.phone.manage.assistantTip')}
>
<div>
<Select
id="pn-who"
value={who?.id ?? ''}
label={t('settings.phone.manage.assistant')}
disabled={!canEdit}
options={assistants.map(a => ({
value: a.id,
label: a.name,
}))}
onChange={assistantId =>
void run('phoneNumber.update', {
phoneNumberId: number.id,
assistantId,
})
}
/>
</div>
</FormField>
)}
{ownSystem && assistants.length <= 1 && <Note tone="info">{t('settings.sip.manageNote')}</Note>}
{!ownSystem && (
<NumberModeField
mode={number.mode}
name={who?.name ?? t('common.assistant.unnamed')}
disabled={!canEdit}
onChange={mode =>
void run('phoneNumber.update', {
phoneNumberId: number.id,
mode,
})
}
/>
)}
{forwards && (
<CheckWhenEditing
number={number}
canEdit={canEdit}
onCarrier={pick.setGuide}
pickedByOwner={pick.pickedByOwner}
>
<FormField
id="pn-codes"
label={t('settings.phone.manage.forwarding')}
help={
number.mode === 'forward_all'
? t('settings.phone.manage.forwardingAllTip')
: t('settings.phone.manage.forwardingConditionalTip')
}
>
<Forwarding
pick={pick}
e164={number.e164}
mode={number.mode === 'forward_all' ? 'forward_all' : 'forward_conditional'}
testable={canEdit}
/>
</FormField>
</CheckWhenEditing>
)}
</>
)}
</Dialog>
)
}
/**
* The forwarding codes in the order of the check: owners and admins enter the business number first (its lookup picks
* the carrier), then the codes, then "Check forwarding". Others see only the codes.
*/
function CheckWhenEditing({
number,
canEdit,
onCarrier,
pickedByOwner,
children,
}: {
number: PhoneNumber
canEdit: boolean
onCarrier: (guideId: string, by?: GuidePickedBy) => void
pickedByOwner: string | null
children: ReactNode
}) {
if (!canEdit) return children
return (
<ForwardingCheck number={number} onCarrier={onCarrier} pickedByOwner={pickedByOwner}>
{children}
</ForwardingCheck>
)
}
/**
* Disconnecting a number is the one place Settings asks first: a released number cannot be bought
* back, so the number has to be typed. The row disappears through sync once the server is done.
*/
function ReleaseNumber({ number, onCancel }: { number: PhoneNumber; onCancel: () => void }) {
const t = useT()
const defaultCountry = useDefaultPhoneCountry()
const run = useRun()
const name = useAssistantName()
// E.164 as far as typed (PhoneInput), compared by digits with the number (confirmsNumber), as the server does.
const [typed, setTyped] = useState('')
// The field starts in the number's own country, so its digits are typed as written there.
const phoneCountry = countryOfNumber(number.e164) ?? defaultCountry
const [sent, setSent] = useState(false)
const [problem, setProblem] = useState<string | null>(null)
const carrier = carrierName(number.provider)
const formatted = formatNumber(number.e164)
// Shown here, not also as a toast.
const { rejection, clearRejection } = useClaimedRejection('phoneNumber.release', sent)
useEffect(() => {
if (!sent || !rejection) return
setSent(false)
setProblem(releaseErrorText(rejection.error))
clearRejection()
}, [rejection, sent, clearRejection])
function release() {
if (!confirmsNumber(typed, number.e164) || sent) return
setProblem(null)
setSent(true)
void run('phoneNumber.release', {
phoneNumberId: number.id,
confirm: typed,
})
}
const bought = number.purchased && number.provider !== 'dev'
return (
<form
className="st-release"
role="alertdialog"
aria-labelledby={`rel-${number.id}`}
onSubmit={e => {
e.preventDefault()
release()
}}
onKeyDown={e => {
if (e.key === 'Escape') {
e.stopPropagation()
onCancel()
}
}}
>
<b id={`rel-${number.id}`}>
{bought
? t('settings.phone.release.title', { number: formatted })
: t('settings.phone.disconnect.title', { number: formatted })}
</b>
<span className="help">
{bought
? carrier
? t('settings.phone.release.help', { name, carrier })
: t('settings.phone.release.helpNoCarrier', { name })
: [
carrier
? t('settings.phone.disconnect.help', { name, carrier })
: t('settings.phone.disconnect.helpNoCarrier', { name }),
...(number.carrier_account === 'workspace'
? [t('settings.phone.disconnect.ownAccount', { brand: brandName() })]
: []),
...(number.mode === 'forward_conditional' || number.mode === 'forward_all'
? [t('settings.phone.disconnect.forwarding')]
: []),
].join(' ')}
</span>
<FormField id={`rel-in-${number.id}`} label={t('settings.phone.release.confirmLabel')}>
<PhoneInput
id={`rel-in-${number.id}`}
value={typed}
defaultCountry={phoneCountry}
autoComplete="off"
onChange={v => {
setTyped(v)
setProblem(null)
}}
placeholder={phoneFromValue(number.e164, phoneCountry).display}
/>
</FormField>
{problem && <Note tone="danger">{problem}</Note>}
<div className="st-connect-foot">
<Button size="sm" variant="ghost" onClick={onCancel} disabled={sent}>
{t('common.action.cancel')}
</Button>
<Button
busy={sent}
type="submit"
size="sm"
variant="danger"
disabled={sent || !confirmsNumber(typed, number.e164)}
>
{sent
? bought
? t('settings.phone.release.busy')
: t('settings.phone.disconnect.busy')
: bought
? t('settings.phone.release.button')
: t('settings.phone.disconnect.button')}
</Button>
</div>
</form>
)
}
/** A `phoneNumber.release` refusal in plain words. */
function releaseErrorText(error: string): string {
const { t } = i18n()
if (/is the carrier in use/i.test(error))
return providerAudience() === 'admin'
? t('settings.phone.release.error.otherCarrierAdmin')
: t('settings.phone.release.error.otherCarrier')
if (/to confirm/i.test(error)) return t('settings.phone.release.error.mismatch')
return t('settings.phone.release.error.failed', { error })
}
/** The forwarding instructions for the number's mode (conditional or all calls), per carrier or phone system. */
function Forwarding({
pick,
e164,
mode,
testable = true,
}: {
pick: ReturnType<typeof useForwardingPick>
e164: string
mode: 'forward_conditional' | 'forward_all'
/** "Check forwarding" follows (owners and admins). */
testable?: boolean
}) {
const t = useT()
const { guide, country } = pick
return (
<div className="st-codes">
<GuidePickers
id={`pn-carrier-${e164}`}
label={t('settings.phone.forwarding.carrier')}
country={country}
onCountry={pick.setCountry}
guideId={pick.guideId}
onGuide={pick.setGuide}
/>
<AbroadWarning e164={e164} country={country} guide={guide} />
{guide ? (
<ForwardingSteps guide={guide} mode={mode} e164={e164} country={country} compact testable={testable} />
) : (
<p className="help">{t('settings.phone.forwarding.pick')}</p>
)}
</div>
)
}
/** Add number's two steps: get the number (or connect one already in the carrier account), then how calls reach it. */
type ConnectStep = 'buy' | 'existing'
/**
* Add number, in two steps. Step 1 gets a new number for the assistant: the area code and what it costs a month (from
* the bill: `extraNumberCents`; else the carrier's list price), with the quiet link to a number already in the carrier
* account where the member may connect one (canAttach). Buying asks once more and then runs phoneNumber.setup, so the
* number is bought and saved before step 2: closing the dialog later loses nothing. It is bought as the business's
* forwarding target (the pick-up choice's mode, the recommended way, as before). Step 2 (ForwardStep) asks whether the
* business's current number forwards here and saves the answer as the number's mode.
*/
export function ConnectNumber({
status,
extraNumberCents = null,
onDone,
onCancel,
assistant,
}: {
status: PhoneSetupStatus | null
/** The assistant that answers the new number (Assistant › Phone); default: the workspace's first. */
assistant?: Assistant
/** On a bill and the install's carrier account: the extra number's monthly price, said instead of the carrier's. */
extraNumberCents?: number | null
/** Step 2 finished (Done). */
onDone: () => void
onCancel?: () => void
}) {
const t = useT()
const run = useRun()
const defaultName = useAssistantName()
const name = assistant?.name ?? defaultName
const numbers = useRows('phone_numbers')
const count = numbers.length
// Numbers already in the carrier account: the server says who may connect one (canAttach: the workspace's own
// account; the install's, whatever carrier and bill, only for an instance admin). Unknown: not offered.
const canAttach = status?.canAttach === true
// Outside the countries numbers can be bought in (the install's buy offers), only a number the business already has
// (offersBuying). Unknown yet: every buyable area code, and the one picked names the country (buyingCountry).
const offers = buyOffersOf(status)
// The country the owner was shown (picked, the listing's, the workspace's, else its hint) when it has a buy offer.
const country = buyingCountry(useWorkspaceCountry(), offers)
const offer = buyOfferIn(offers, country)
// The business's number forwards as chosen for pick-up (Settings › Hours); until loaded, the calls it misses.
const settings = useWorkspaceSettings().data ?? null
const forwarding = pickupNumberMode(firstPickup(settings))
const partnerManaged = settings?.partnerManaged === true
const bought = numbers.filter(n => n.purchased && n.carrier_account === 'install').length
// A white-label client's people see no prices (docs/white-label.md): its partner pays for the number.
const monthly = partnerManaged ? null : billedNumberPrice(extraNumberCents, status?.provider ?? null, bought)
// The iOS and Android app buys nothing (LIN-874): a number the plan does not include (`monthly`, paid from the
// balance) is not offered there; the plan's own first number still is.
const paidInApp = isNativeApp() && monthly !== null
const buyable = offersBuying(country, offers) && !paidInApp
const settingUp = numbersBeingSetUp(country, offers)
const noAreaCode = !offerHasAreaCode(offer)
const [chosen, setStep] = useState<ConnectStep>('buy')
const step: ConnectStep = buyable ? chosen : 'existing'
// Nothing to connect here: no buying, and the carrier account's numbers are not this member's to connect.
const nothingToConnect = !buyable && !canAttach
// E.164 as far as typed (PhoneInput).
const [typed, setTyped] = useState('')
const phoneCountry = useDefaultPhoneCountry()
const businessPhone = (useAssistants()[0]?.business_profile as { phone?: string } | undefined)?.phone
// Like onboarding: the business phone's area code, else that of a number the workspace already has, else the
// country's first offered code (Germany).
const [picked, setAreaCode] = useState(() => areaCodeOf(country, businessPhone, ...numbers.map(n => n.e164)))
// Only a code of the workspace's country (its list): one prefilled while the country loaded is dropped.
const areaCode = areaCodeIn(picked, country, offers) ? picked : (offer?.areaCodes?.[0] ?? '')
const [confirming, setConfirming] = useState(false)
const [sentAt, setSentAt] = useState<number | null>(null)
const [problem, setProblem] = useState<string | null>(null)
/** The number just connected: step 2. */
const [addedId, setAddedId] = useState<string | null>(null)
const added = numbers.find(n => n.id === addedId)
const carrier = carrierName(status?.provider ?? null)
// A refusal while this waits is shown here, not also as a toast.
const { rejection, clearRejection } = useClaimedRejection('phoneNumber.setup', sentAt !== null)
const buying = step === 'buy'
const dev = status?.provider === 'dev'
const price = partnerManaged
? undefined
: (billedNumberNote(extraNumberCents, status?.provider ?? null, bought) ??
priceNote(status?.provider ?? null, priceCountry(areaCode, country)))
// Success is the new row arriving by sync (then step 2); a refusal ends the wait with its reason.
useEffect(() => {
if (sentAt === null || count <= sentAt) return
setSentAt(null)
const newest = [...numbers].sort((a, b) => b.created_at.localeCompare(a.created_at))[0]
if (newest) setAddedId(newest.id)
}, [count, sentAt, numbers])
useEffect(() => {
if (!rejection || sentAt === null) return
setSentAt(null)
setConfirming(false)
setProblem(phoneSetupErrorText(rejection.error))
clearRejection()
}, [rejection, sentAt, clearRejection])
if (added) return <ForwardStep number={added} name={name} forwarding={forwarding} onDone={onDone} />
// Step 1 buys as the forwarding target (the `forward` option); the number's mode is settled in step 2.
const option: PhoneOption = buying ? 'forward' : 'existing'
function send(confirmed: boolean) {
const result = phoneOptionSetup(
option,
{ number: typed, areaCode, confirmed, noAreaCode, offered: offer?.areaCodes },
forwarding,
)
if ('problem' in result) return setProblem(result.problem)
setProblem(null)
setSentAt(count)
void run('phoneNumber.setup', assistant ? { ...result.input, assistantId: assistant.id } : result.input)
}
function submit() {
if (sentAt !== null || nothingToConnect) return
if (!buying) return send(false)
// Buying needs a second, deliberate click: Enter only asks for the confirmation.
const check = phoneOptionSetup(
option,
{ number: typed, areaCode, confirmed: true, noAreaCode, offered: offer?.areaCodes },
forwarding,
)
if ('problem' in check) return setProblem(check.problem)
setProblem(null)
setConfirming(true)
}
const go = (next: ConnectStep) => {
setStep(next)
setConfirming(false)
setProblem(null)
}
const busy = sentAt !== null
const existing = phoneOptionText('existing', name, carrier)
// On a bill the monthly price stands out under the area code; otherwise the carrier's price is the field's help line
// (or the only line, for a number without an area code).
const billedLine = monthly && (
<p className="pn-price">
<span className="pn-price-amount">{monthly}</span>
<span className="pn-price-per">{t('settings.phone.connect.perMonth')}</span>
<span className="help">{t('settings.phone.connect.fromBalance')}</span>
</p>
)
return (
<form
className="st-connect pn-connect"
onSubmit={e => {
e.preventDefault()
submit()
}}
>
{nothingToConnect ? (
// Nothing this member can connect here: only why, and Cancel; no carrier name, no button that does nothing.
<p className="help" role="status">
{paidInApp
? t('common.plan.managedOnWeb')
: settingUp
? beingSetUpText()
: buyUnavailableText(carrier, canAttach, offers)}
</p>
) : buying ? (
<div className="st-pn-t">
<b>{t('settings.phone.connect.buyTitle', { name })}</b>
<span className="help">{t('settings.phone.connect.buyHelp', { name })}</span>
</div>
) : (
<div className="st-pn-t">
<b>{existing.title}</b>
<span className="help">
{buyable
? existing.description
: paidInApp
? t('common.plan.managedOnWeb')
: settingUp
? beingSetUpText()
: buyUnavailableText(carrier, canAttach, offers)}
</span>
</div>
)}
{nothingToConnect ? null : !buying ? (
<>
<FormField
id="st-pn-number"
label={carrier ? t('settings.phone.connect.numberIn', { carrier }) : t('settings.phone.connect.number')}
>
<CarrierNumberInput
id="st-pn-number"
value={typed}
onChange={v => {
setTyped(v)
setProblem(null)
}}
defaultCountry={phoneCountry}
/>
</FormField>
{dev && <p className="help">{t('settings.phone.connect.dev')}</p>}
{buyable && (
<div>
<Button size="sm" variant="ghost" onClick={() => go('buy')}>
{t('settings.phone.connect.back')}
</Button>
</div>
)}
</>
) : (
<>
{!noAreaCode && (
<FormField
id="st-pn-area"
label={t('settings.phone.connect.areaCode')}
description={monthly ? undefined : price}
>
<AreaCodeInput
id="st-pn-area"
openOnFocus={false}
country={country}
codes={offer?.areaCodes ? offerAreaCodes(offer) : undefined}
value={areaCode}
onChange={v => {
setAreaCode(v)
setConfirming(false)
setProblem(null)
}}
/>
</FormField>
)}
{/* While the question shows, it says the cost itself: the line would say it twice. */}
{!confirming && (billedLine || (noAreaCode && price && <p className="help">{price}</p>))}
{canAttach && (
<div>
<Button size="sm" variant="ghost" onClick={() => go('existing')}>
{existingLinkText(carrier)}
</Button>
</div>
)}
{confirming && (
<div className="ob-prompt" role="alert">
<span>
{buyQuestion(
status?.provider ?? null,
noAreaCode ? '' : areaCodeShown(areaCode, country),
partnerManaged ? null : numberCharge(extraNumberCents, status?.provider ?? null, bought),
)}
</span>
<span className="ob2-row">
<Button size="sm" variant="ghost" onClick={() => setConfirming(false)} disabled={busy}>
{t('common.action.cancel')}
</Button>
<Button busy={busy} size="sm" variant="primary" disabled={busy} onClick={() => send(true)}>
{busy
? t('settings.phone.connect.buying')
: dev
? t('settings.phone.connect.addTest')
: t('settings.phone.connect.buy')}
</Button>
</span>
</div>
)}
</>
)}
{problem && buying && buyableAreaCode(areaCode) && isNoneFreeText(problem) ? (
<NearbyAreaCodes
areaCode={areaCode}
message={problem}
onPick={code => {
setAreaCode(code)
setProblem(null)
// The next step is the purchase question for that code, as after the primary button.
setConfirming(true)
}}
/>
) : (
problem && <Note tone="danger">{problem}</Note>
)}
{!confirming && (
<div className="st-connect-foot">
{onCancel && (
<Button size="sm" variant="ghost" onClick={onCancel} disabled={busy}>
{t('common.action.cancel')}
</Button>
)}
{!nothingToConnect && (
<Button busy={busy} type="submit" size="sm" variant="primary" disabled={busy}>
{busy
? t('settings.phone.connect.connecting')
: buying
? dev
? t('settings.phone.connect.addATest')
: t('settings.phone.connect.get')
: t('settings.phone.connect.connect')}
</Button>
)}
</div>
)}
</form>
)
}
/** Step 2's answers: the business's current number forwards to the new one, or callers use the new one only. */
export type ForwardAnswer = 'forward' | 'none'
/** The number's mode for a step 2 answer: forwarding as the pick-up choice says, or a number of its own (`new`). */
export const modeForAnswer = (answer: ForwardAnswer, forwarding: 'forward_conditional' | 'forward_all') =>
answer === 'forward' ? forwarding : ('new' as const)
/** The step 2 answer a number's mode stands for (a forwarding mode: forward; anything else: none). */
export const answerForMode = (mode: PhoneNumber['mode']): ForwardAnswer =>
mode === 'forward_conditional' || mode === 'forward_all' ? 'forward' : 'none'
/**
* Add number, step 2: "Forward your current number here?" Each answer is saved on the number at once (phoneNumber.update,
* its mode); forwarding shows the same check and short codes as the number's Manage dialog.
*/
function ForwardStep({
number,
name,
forwarding,
onDone,
}: {
number: PhoneNumber
name: string
/** The forwarding mode of the workspace's pick-up choice. */
forwarding: 'forward_conditional' | 'forward_all'
onDone: () => void
}) {
const t = useT()
const run = useRun()
const pick = useForwardingPick(number)
const answer = answerForMode(number.mode)
const formatted = formatNumber(number.e164)
const choose = (next: ForwardAnswer) => {
if (next === answer) return
void run('phoneNumber.update', { phoneNumberId: number.id, mode: modeForAnswer(next, forwarding) })
}
const mode = number.mode === 'forward_all' ? 'forward_all' : 'forward_conditional'
return (
<div className="st-connect pn-connect">
<p className="pn-added" role="status">
<StatusMark state="works" label={t('settings.phone.forwardStep.added', { number: formatted, name })} />
{t('settings.phone.forwardStep.added', { number: formatted, name })}
</p>
<div className="st-pn-t">
<b>{t('settings.phone.forwardStep.title')}</b>
</div>
<div className="k-radios st-pn-kinds" role="radiogroup" aria-label={t('settings.phone.forwardStep.title')}>
<RadioCard
name="st-pn-forward"
checked={answer === 'forward'}
onPick={() => choose('forward')}
title={t('settings.phone.forwardStep.yes.title')}
description={t('settings.phone.forwardStep.yes.description', { name })}
/>
<RadioCard
name="st-pn-forward"
checked={answer === 'none'}
onPick={() => choose('none')}
title={t('settings.phone.forwardStep.no.title')}
description={t('settings.phone.forwardStep.no.description', { name, number: formatted })}
/>
</div>
{answer === 'forward' && (
<CheckWhenEditing number={number} canEdit onCarrier={pick.setGuide} pickedByOwner={pick.pickedByOwner}>
<FormField
id="pn-codes"
label={t('settings.phone.manage.forwarding')}
help={
mode === 'forward_all'
? t('settings.phone.manage.forwardingAllTip')
: t('settings.phone.manage.forwardingConditionalTip')
}
>
<Forwarding pick={pick} e164={number.e164} mode={mode} />
</FormField>
</CheckWhenEditing>
)}
<div className="st-connect-foot">
<Button size="sm" variant="primary" onClick={onDone}>
{t('settings.phone.manage.done')}
</Button>
</div>
</div>
)
}