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
// A select with a search field on top of its list, for long lists (the workspace's country, its language). Arrow
// keys move, Enter picks, Esc closes; the search owns its keys, so none reaches the app's shortcuts.
import { useT } from '@linedesk/i18n/react'
import { Fragment, type KeyboardEvent, useEffect, useId, useMemo, useRef, useState } from 'react'
import { OptionCheck } from './controls.tsx'
import { useActiveOptionInView, useAnchoredList } from './floating.ts'
import { cx, Icon } from './kit.tsx'
import './search-select.css'
export type SearchOption = {
value: string
label: string
/** Shown before the label (a flag). */
icon?: string
/** Shown at the right of the option (a code). */
meta?: string
/** A heading over this option and the ones after it with the same group; only while nothing is searched. */
group?: string
}
type Props = {
value: string
options: readonly SearchOption[]
onChange: (value: string) => void
/** Accessible name; also the tooltip. */
label: string
/** The options matching what was typed, best first; default: the label or meta contains it. */
search?: (options: readonly SearchOption[], query: string) => SearchOption[]
placeholder?: string
id?: string
disabled?: boolean
/** Shown while the value is none of the options (nothing picked yet). */
empty?: string
/** The open list's least width in pixels (default 260), for options with a long meta (an email address). */
listWidth?: number
/** A quiet line under the options that cannot be picked ("More countries coming soon."). */
note?: string
/** Off for a short list: no search field, the list itself takes the keys. */
searchable?: boolean
}
const fold = (text: string) => text.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase()
/** Options whose label starts with what was typed first, then those that contain it, or whose meta is it. */
export function searchOptions(options: readonly SearchOption[], query: string): SearchOption[] {
const q = fold(query.trim())
if (!q) return [...options]
const rank = (o: SearchOption) => {
const label = fold(o.label)
if (label.startsWith(q) || fold(o.meta ?? '') === q) return 0
if (label.split(/[\s(,-]+/).some(word => word.startsWith(q))) return 1
return label.includes(q) ? 2 : -1
}
return options
.map(({ group: _group, ...o }) => o)
.filter(o => rank(o) >= 0)
.sort((a, b) => rank(a) - rank(b))
}
/**
* What one option of an open SearchSelect shows: its flag, the label in full (a long one wraps onto a second line
* within the list, never an ellipsis), its code and the check mark column.
*/
export function SearchOptionBody({ option, selected }: { option: SearchOption; selected: boolean }) {
return (
<>
<span className="k-select-opt-t">
{option.icon && <span className="k-ssel-icon">{option.icon}</span>}
<span className="k-select-opt-l">{option.label}</span>
</span>
{option.meta && <span className="k-ssel-meta">{option.meta}</span>}
<OptionCheck selected={selected} />
</>
)
}
export function SearchSelect({
value,
options,
onChange,
label,
search = searchOptions,
placeholder,
id,
disabled,
empty,
listWidth = 260,
note,
searchable = true,
}: Props) {
const t = useT()
const [open, setOpen] = useState(false)
const [query, setQuery] = useState('')
const [active, setActive] = useState(0)
const root = useRef<HTMLDivElement>(null)
const button = useRef<HTMLButtonElement>(null)
const list = useRef<HTMLDivElement>(null)
const listId = useId()
const matches = useMemo(() => (open ? search(options, query) : []), [open, options, query, search])
const current = options.find(o => o.value === value)
useAnchoredList(open, button, list, { minWidth: listWidth })
useEffect(() => {
if (!open) return
const outside = (e: MouseEvent) => {
if (!root.current?.contains(e.target as Node)) setOpen(false)
}
document.addEventListener('mousedown', outside)
return () => document.removeEventListener('mousedown', outside)
}, [open])
const pointAt = useActiveOptionInView(open, listId, active, setActive)
// Without a search field the list takes the focus, and with it the keys.
const optionList = useRef<HTMLDivElement>(null)
useEffect(() => {
if (open && !searchable) optionList.current?.focus()
}, [open, searchable])
function show() {
if (disabled) return
setQuery('')
setActive(
Math.max(
0,
options.findIndex(o => o.value === value),
),
)
setOpen(true)
}
function close() {
setOpen(false)
button.current?.focus()
}
function pick(next: string) {
close()
if (next !== value) onChange(next)
}
function onSearchKey(e: KeyboardEvent<HTMLElement>) {
e.stopPropagation()
if (e.key === 'ArrowDown') {
e.preventDefault()
setActive(i => Math.min(matches.length - 1, i + 1))
} else if (e.key === 'ArrowUp') {
e.preventDefault()
setActive(i => Math.max(0, i - 1))
} else if (e.key === 'Enter') {
e.preventDefault()
const o = matches[active]
if (o) pick(o.value)
} else if (e.key === 'Escape') {
e.preventDefault()
close()
} else if (e.key === 'Tab') setOpen(false)
}
return (
<div className={cx('k-select', 'k-select-md', 'k-ssel', open && 'open')} ref={root}>
<button
ref={button}
id={id}
type="button"
className="k-select-btn"
disabled={disabled}
title={label}
aria-label={`${label}: ${current?.label ?? (empty || value)}`}
aria-haspopup="listbox"
aria-expanded={open}
onClick={() => (open ? close() : show())}
onKeyDown={e => {
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault()
show()
}
}}
>
{current?.icon && <span className="k-ssel-icon">{current.icon}</span>}
<span className={cx('k-ssel-label', !current && empty && 'k-ssel-empty')}>
{current?.label ?? (empty || value)}
</span>
<Icon name="down" size={14} />
</button>
{open && (
<div ref={list} popover="manual" className="k-select-list k-ssel-list k-float">
{searchable && (
<input
autoFocus
className="k-ssel-search"
placeholder={placeholder ?? t('common.searchSelect.placeholder')}
aria-label={t('common.searchSelect.searchLabel', { label })}
aria-controls={listId}
aria-activedescendant={matches[active] ? `${listId}-${active}` : undefined}
value={query}
onChange={e => {
setQuery(e.target.value)
setActive(0)
}}
onKeyDown={onSearchKey}
/>
)}
<div
ref={optionList}
id={listId}
role="listbox"
aria-label={label}
className="k-ssel-options"
{...(searchable
? {}
: {
tabIndex: -1,
'aria-activedescendant': matches[active] ? `${listId}-${active}` : undefined,
onKeyDown: onSearchKey,
})}
>
{matches.map((o, i) => (
<Fragment key={o.value}>
{o.group && o.group !== matches[i - 1]?.group && (
<div className="k-ssel-group" aria-hidden>
{o.group}
</div>
)}
<div
id={`${listId}-${i}`}
role="option"
aria-selected={o.value === value}
className={cx('k-select-opt', i === active && 'active')}
onMouseEnter={() => pointAt(i)}
onClick={() => pick(o.value)}
>
<SearchOptionBody option={o} selected={o.value === value} />
</div>
</Fragment>
))}
{!matches.length && <div className="k-ssel-empty">{t('common.searchSelect.noMatch', { query })}</div>}
</div>
{note && <p className="k-ssel-note">{note}</p>}
</div>
)}
</div>
)
}