Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

4.4 KB
Raw Blame
import { useId, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from 'react'
import { createPortal } from 'react-dom'
import { CharacterAvatar } from '../../components/Avatar'
import { RowLabel, Switch } from '../../components/Controls'
import { useEscape, usePresence } from '../../components/Overlay'
import { anchorPlacement } from '../../components/Menu'
import { VStack } from '../../components/Stack'
import { useTr } from '../../lib/use-language'
import { AVATAR_COLORS, AVATAR_SHAPES } from '../../lib/theme'
import './bots.css'

/** A labeled input inside a section row: label (and an optional note) over the field, which they name. */
export function Field({ label, detail, children }: { label: string; detail?: string | null; children: ReactNode }) {
  const id = useId()
  return (
    <VStack gap={8} role="group" aria-labelledby={`${id}-label`} aria-describedby={detail ? `${id}-detail` : undefined}>
      <RowLabel label={label} detail={detail} labelId={`${id}-label`} detailId={`${id}-detail`} />
      {children}
    </VStack>
  )
}

/** An on/off row: title and a short note, the switch on the right. */
export function SwitchRow({ title, detail, on, onChange }: { title: string; detail?: string | null; on: boolean; onChange: (on: boolean) => void }) {
  return (
    <Switch on={on} onChange={onChange}>
      <RowLabel label={title} detail={detail} />
    </Switch>
  )
}

/** A multi-line field that grows with its text between `minRows` and `maxRows` (`axis: .vertical`). */
export function AutoTextArea({ value, onChange, placeholder, minRows, maxRows, className, onKeyDown }: {
  value: string
  onChange: (v: string) => void
  placeholder?: string
  minRows: number
  maxRows: number
  className?: string
  onKeyDown?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void
}) {
  const tr = useTr()
  const ref = useRef<HTMLTextAreaElement>(null)
  useLayoutEffect(() => {
    const el = ref.current
    if (!el) return
    const style = getComputedStyle(el)
    const line = parseFloat(style.lineHeight) || 15
    const pad = parseFloat(style.paddingTop) + parseFloat(style.paddingBottom)
    el.style.height = '0px'
    el.style.height = `${Math.min(Math.max(el.scrollHeight, line * minRows + pad), line * maxRows + pad)}px`
  }, [value, minRows, maxRows])
  return <textarea ref={ref} rows={minRows} className={className} value={value} placeholder={placeholder === undefined ? undefined : tr(placeholder)} onChange={(e) => onChange(e.target.value)} onKeyDown={onKeyDown} />
}

/** The avatar's shape and color: each a wrapping, centered row of choices, ringed when picked. */
export function AvatarPicker({ shape, color, onShape, onColor }: { shape: string; color: string; onShape: (s: string) => void; onColor: (c: string) => void }) {
  const tr = useTr()
  return (
    <VStack gap={12} className="avatar-picker">
      <div className="swatch-row">
        {AVATAR_SHAPES.map((s) => (
          <button key={s} className={`swatch ${s === shape ? 'on' : ''}`} aria-label={tr('{shape} shape').replaceAll('{shape}', () => s)} aria-pressed={s === shape} onClick={() => onShape(s)}>
            <CharacterAvatar shape={s} color={color} size={36} />
          </button>
        ))}
      </div>
      <div className="swatch-row">
        {AVATAR_COLORS.map((c) => (
          <button key={c.id} className={`swatch ${c.id === color ? 'on' : ''}`} aria-label={tr(c.label)} aria-pressed={c.id === color} onClick={() => onColor(c.id)}>
            <span className="swatch-color" style={{ background: c.hex }} />
          </button>
        ))}
      </div>
    </VStack>
  )
}

/** A floating panel next to `anchor` that a click anywhere else closes (kit `AnchoredPanel`). */
export function AnchoredPanel({ open, onClose, anchor, children }: { open: boolean; onClose: () => void; anchor: RefObject<HTMLElement | null>; children: ReactNode }) {
  const { mounted, shown } = usePresence(open, 120)
  const [rect, setRect] = useState<DOMRect | null>(null)
  const layer = useRef<HTMLDivElement>(null)
  useLayoutEffect(() => {
    if (open && anchor.current) setRect(anchor.current.getBoundingClientRect())
  }, [open, anchor])
  useEscape(open, onClose, layer)
  if (!mounted || !rect) return null
  return createPortal(
    <div ref={layer} className={`anchored ${shown ? 'shown' : ''}`} onMouseDown={(e) => e.target === e.currentTarget && onClose()}>
      <div className="anchored-panel" style={anchorPlacement(rect).style}>
        {children}
      </div>
    </div>,
    document.body,
  )
}