Open History to see the last commit
1import { memo, useEffect, useRef, type ReactNode } from "react";
2import { useMediaQuery } from "~/hooks/useMediaQuery";
3
4const WAVEFORM_BAR_COUNT = 28;
5const WAVEFORM_BAR_IDS = Array.from({ length: WAVEFORM_BAR_COUNT }, (_, index) => String(index));
6
7export const VoiceWaveform = memo(function VoiceWaveform(props: { level: number }) {
8 const prefersReducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
9 const barsRef = useRef<Array<HTMLSpanElement | null>>([]);
10 const levelsRef = useRef(Array<number>(WAVEFORM_BAR_COUNT).fill(0));
11
12 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- updates native waveform bar styles when microphone levels arrive
13 useEffect(() => {
14 const levels = levelsRef.current;
15 levels.copyWithin(0, 1);
16 levels[levels.length - 1] = props.level;
17 barsRef.current.forEach((bar, index) => {
18 if (!bar) return;
19 const level = levels[index] ?? 0;
20 bar.style.opacity = String(0.22 + level * 0.78);
21 bar.style.transform = `scaleY(${prefersReducedMotion ? 0.35 : Math.max(0.08, level)})`;
22 });
23 }, [prefersReducedMotion, props.level]);
24
25 return (
26 <div
27 aria-hidden
28 className="flex h-5 min-w-0 flex-1 items-center justify-between gap-px overflow-hidden"
29 >
30 {WAVEFORM_BAR_IDS.map((id, index) => (
31 <span
32 key={id}
33 ref={(bar) => {
34 barsRef.current[index] = bar;
35 }}
36 className="h-full w-0.5 shrink-0 origin-center rounded-full bg-primary opacity-25 transition-[transform,opacity] duration-100 ease-out motion-reduce:transition-none"
37 style={{ transform: `scaleY(${prefersReducedMotion ? 0.35 : 0.08})` }}
38 />
39 ))}
40 </div>
41 );
42});
43
44export function VoiceInputPill({ children }: { children: ReactNode }) {
45 return (
46 // Same height as the toolbar's icon buttons, so starting a recording never resizes the composer.
47 <div
48 data-slot="voice-input-pill"
49 className="flex h-8 w-48 min-w-0 shrink-0 items-center gap-2 sm:h-7 sm:w-64"
50 >
51 {children}
52 </div>
53 );
54}