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.

3.4 KB
Raw Blame
// Quiet toasts, plus the "Next time, press E" tip after an action was triggered with the mouse.
import { useCallback, useEffect, useRef, useState } from 'react'
import { i18n } from '../i18n/index.tsx'
import { HINT_EVENT, type ShortcutHint } from '../ui/tooltips.ts'

export type ToastState = { text: string; undo: boolean }
/** One line of the result log: what a toast said and when (ISO). */
export type ResultLine = { id: number; text: string; at: string }
/** How many results the log keeps (ResultLog in Toast.tsx). */
const LOG_LINES = 5
/** How the current action was triggered: a shortcut, ⌘K + Enter, or the mouse. */
export type Source = 'key' | 'menu-key' | 'mouse'

/** "Archived · Next time, press E", or the tip alone when there is no label. */
const shortcutTip = (label: string | undefined, key: string) =>
  label ? i18n().t('shell.shortcutTip.withLabel', { label, key }) : i18n().t('shell.shortcutTip.alone', { key })

/** A touch screen without a mouse (a phone): no "Next time, press M", there is usually no keyboard to press it on. */
const touchOnly = () =>
  typeof window !== 'undefined' && !!window.matchMedia?.('(hover: none) and (pointer: coarse)').matches

const PLAIN_MS = 2600
const UNDO_MS = 5000

export function useToast() {
  const [toast, setToast] = useState<ToastState | null>(null)
  const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
  const lastShownAt = useRef(0)
  const source = useRef<Source>('mouse')
  // What the toasts said, newest first: a toast is gone after seconds, the log stays in the page (ResultLog).
  const [log, setLog] = useState<ResultLine[]>([])
  const lines = useRef(0)

  /** `ms`: how long it stays; by default 2.6 s, 5 s with Undo. `result` false: a tip, not the answer to an action. */
  const show = useCallback((text: string, undo = false, ms?: number, result = true) => {
    lastShownAt.current = Date.now()
    setToast({ text, undo })
    if (result) {
      const line = { id: ++lines.current, text, at: new Date().toISOString() }
      setLog(log => [line, ...log].slice(0, LOG_LINES))
    }
    clearTimeout(timer.current)
    timer.current = setTimeout(() => setToast(null), ms ?? (undo ? UNDO_MS : PLAIN_MS))
  }, [])

  /** Run `fn` as triggered by `src`; tips inside it follow that source. */
  const via = useCallback(<T>(src: Source, fn: () => T): T => {
    const previous = source.current
    source.current = src
    try {
      return fn()
    } finally {
      source.current = previous
    }
  }, [])

  // `key` '': a change without a shortcut (Block on the contact page) gets no tip.
  const withTip = useCallback(
    (label: string, key: string) =>
      key && source.current === 'mouse' && !touchOnly() ? shortcutTip(label, key) : label,
    [],
  )

  // A click on a control with a shortcut that produced no toast of its own still gets the tip.
  useEffect(() => {
    function onHint(e: Event) {
      if (touchOnly()) return
      const { label, keys } = (e as CustomEvent<ShortcutHint>).detail
      setTimeout(() => {
        if (Date.now() - lastShownAt.current > 80) show(shortcutTip(label, keys.join(' ')), false, undefined, false)
      }, 30)
    }
    window.addEventListener(HINT_EVENT, onHint)
    return () => window.removeEventListener(HINT_EVENT, onHint)
  }, [show])

  return { toast, log, show, via, withTip, currentSource: () => source.current }
}
export type Toaster = ReturnType<typeof useToast>