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.

4.9 KB
Raw Blame
// The one row for a setting, everywhere (Server admin, workspace, assistant and account settings): its name (13px,
// medium), at most one grey line under it, and its control at the right end: a switch, a select, a button, a badge.
// Rows in a list are divided by hairlines. FeatureLine (ui/FeatureLine.tsx) is this row in its own card, with Change
// opening its settings under it. Styles in setting-row.css.
import type { ReactNode } from 'react'
import { type HelpContent, HelpTip, helpContent } from './HelpTip.tsx'
import { cx } from './kit.tsx'
import { Skeleton } from './LoadStates.tsx'
import './setting-row.css'

export type SettingRowProps = {
  title: ReactNode
  /** One grey line under the title. */
  description?: ReactNode
  /** A quiet line under the description: where the value comes from (FollowsLine), what callers hear. */
  note?: ReactNode
  /** Quiet words next to the title ("Team default"). */
  aside?: ReactNode
  /** Detail behind a (?) next to the title. */
  help?: HelpContent
  /** The control at the right end: a Switch, a Select (size sm), a Button (size sm), a Badge, or several of them. */
  control?: ReactNode
  /** Before the words: a 24px avatar where the row is a person. */
  lead?: ReactNode
  /** Greyed out: it has no effect yet (browser notifications before the browser allows them). */
  dimmed?: boolean
  /** An option of the row above: indented under it, without the line between them. */
  nested?: boolean
  id?: string
  className?: string
  /** `li` for a row in a `<ul>` (sessions, passkeys). */
  as?: 'div' | 'li'
}

export function SettingRow({
  title,
  description,
  note,
  aside,
  help,
  control,
  lead,
  dimmed,
  nested,
  id,
  className,
  as: Tag = 'div',
}: SettingRowProps) {
  return (
    <Tag
      id={id}
      className={cx('k-setrow', dimmed && 'is-dimmed', nested && 'is-nested', lead != null && 'has-lead', className)}
    >
      {lead}
      <div className="k-setrow-text">
        <b className="k-setrow-title">
          {title}
          {help && <HelpTip {...helpContent(help)} />}
          {aside && <span className="k-setrow-aside">{aside}</span>}
        </b>
        {description && <span className="help">{description}</span>}
        {note}
      </div>
      {control != null && control !== false && <div className="k-setrow-end">{control}</div>}
    </Tag>
  )
}

/** Rows divided by hairlines, the first without one (a card's or a fold-out's list of settings). */
export function SettingRows({ className, children }: { className?: string; children: ReactNode }) {
  return <div className={cx('k-setrows', className)}>{children}</div>
}

/** The control a loading row will have, as its grey shape. */
export type SkeletonControl = 'switch' | 'select' | 'button' | 'badge-button' | 'none'

const controlShape = (control: SkeletonControl) =>
  control === 'switch' ? (
    <Skeleton width={34} height={20} className="sk-round" />
  ) : control === 'select' ? (
    <Skeleton width={150} height={28} />
  ) : control === 'button' ? (
    <Skeleton width={64} height={28} />
  ) : control === 'badge-button' ? (
    <>
      <Skeleton width={72} height={20} className="sk-round" />
      <Skeleton width={64} height={28} />
    </>
  ) : null

/**
 * A SettingRow whose value is not read yet: its title at once when it is known (words that don't need the data), the
 * description's line and the control as grey shapes, the same height as the row that comes.
 */
export function SkeletonSettingRow({
  title,
  description = true,
  control = 'switch',
  lead = false,
  index = 0,
}: {
  title?: ReactNode
  description?: boolean
  control?: SkeletonControl
  lead?: boolean
  /** Varies the bars' widths from row to row. */
  index?: number
}) {
  const shape = controlShape(control)
  return (
    <div className={cx('k-setrow', lead && 'has-lead')} aria-hidden={title ? undefined : 'true'}>
      {lead && <Skeleton width={24} height={24} className="sk-round" />}
      <div className="k-setrow-text">
        {title ? (
          <b className="k-setrow-title">{title}</b>
        ) : (
          <Skeleton shape="text" width={120 + ((index * 37) % 60)} />
        )}
        {description && <Skeleton shape="text" width={220 + ((index * 53) % 90)} />}
      </div>
      {shape && <div className="k-setrow-end">{shape}</div>}
    </div>
  )
}

/** `rows` SettingRows while they load, divided like the list that comes. */
export function SkeletonSettingRows({
  rows = 2,
  description = true,
  control = 'switch',
  lead = false,
}: {
  rows?: number
  description?: boolean
  control?: SkeletonControl
  lead?: boolean
}) {
  return (
    <div className="k-setrows" aria-hidden="true">
      {Array.from({ length: rows }, (_, i) => (
        <SkeletonSettingRow
          // biome-ignore lint/suspicious/noArrayIndexKey: placeholder rows never reorder
          key={i}
          index={i}
          description={description}
          control={control}
          lead={lead}
        />
      ))}
    </div>
  )
}