The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(voiceNotes): add voice note player component
Steven Enamakel6c6fca8c8e44
diff --git a/app/src/features/voiceNotes/VoiceNotePlayer.tsx b/app/src/features/voiceNotes/VoiceNotePlayer.tsxnew file mode 100644index 0000000000..27734cde38--- /dev/null+++ b/app/src/features/voiceNotes/VoiceNotePlayer.tsx@@ -0,0 +1,203 @@+/**+ * VoiceNotePlayer - play/pause, scrub, duration and transcript disclosure for+ * one voice note, user recording or assistant spoken reply alike.+ *+ * Audio comes from either `src` (a draft's object URL) or `noteId`, fetched+ * from the core on first play so a thread with many notes loads none of them+ * until asked.+ */+import { PauseIcon, PlayIcon } from 'lucide-react';+import { useCallback, useEffect, useRef, useState } from 'react';++import { useT } from '../../lib/i18n/I18nContext';+import { cn } from '../../lib/cn';+import { trackAnalyticsEvent } from '../../components/analytics';+import Button from '../../components/ui/Button';+import { CollapsibleContent, CollapsibleRoot, CollapsibleTrigger } from '../../components/ui/Collapsible';+import Slider from '../../components/ui/Slider';+import { formatDuration, readVoiceNote } from './voiceNoteApi';++export interface VoiceNotePlayerProps {+ noteId?: string;+ /** Ready-to-play URL (a draft preview); skips the core fetch. */+ src?: string;+ durationMs?: number;+ waveform?: number[];+ /** When set, a transcript disclosure is shown under the player. */+ transcript?: string;+ className?: string;+}++export function VoiceNotePlayer({+ noteId,+ src,+ durationMs,+ waveform,+ transcript,+ className,+}: VoiceNotePlayerProps) {+ const { t } = useT();+ const audioRef = useRef<HTMLAudioElement | null>(null);+ const objectUrlRef = useRef<string | null>(null);+ const [playing, setPlaying] = useState(false);+ const [positionMs, setPositionMs] = useState(0);+ const [totalMs, setTotalMs] = useState(durationMs ?? 0);+ const [loading, setLoading] = useState(false);+ const [error, setError] = useState(false);++ const release = useCallback(() => {+ audioRef.current?.pause();+ audioRef.current = null;+ if (objectUrlRef.current) {+ URL.revokeObjectURL(objectUrlRef.current);+ objectUrlRef.current = null;+ }+ }, []);++ // A different note (or draft) invalidates whatever element was prepared.+ useEffect(() => {+ release();+ setPlaying(false);+ setPositionMs(0);+ setError(false);+ setTotalMs(durationMs ?? 0);+ return release;+ }, [noteId, src, durationMs, release]);++ const ensureAudio = useCallback(async (): Promise<HTMLAudioElement | null> => {+ if (audioRef.current) return audioRef.current;+ let url = src;+ if (!url) {+ if (!noteId) return null;+ setLoading(true);+ try {+ const blob = await readVoiceNote(noteId);+ if (!blob) throw new Error('voice note audio missing');+ url = URL.createObjectURL(blob);+ objectUrlRef.current = url;+ } catch (err) {+ console.warn('[voice-note] load failed', err);+ setError(true);+ return null;+ } finally {+ setLoading(false);+ }+ }+ const audio = new Audio(url);+ audio.addEventListener('timeupdate', () => setPositionMs(audio.currentTime * 1000));+ audio.addEventListener('loadedmetadata', () => {+ if (Number.isFinite(audio.duration) && audio.duration > 0) {+ setTotalMs(audio.duration * 1000);+ }+ });+ audio.addEventListener('ended', () => {+ setPlaying(false);+ setPositionMs(0);+ });+ audio.addEventListener('pause', () => setPlaying(false));+ audio.addEventListener('play', () => setPlaying(true));+ audioRef.current = audio;+ return audio;+ }, [noteId, src]);++ const toggle = useCallback(async () => {+ const audio = await ensureAudio();+ if (!audio) return;+ if (audio.paused) {+ trackAnalyticsEvent('voice_note_played', {});+ await audio.play().catch(err => {+ console.warn('[voice-note] play failed', err);+ setError(true);+ });+ } else {+ audio.pause();+ }+ }, [ensureAudio]);++ const seek = useCallback(+ async ([value]: number[]) => {+ const audio = await ensureAudio();+ if (!audio) return;+ audio.currentTime = value / 1000;+ setPositionMs(value);+ },+ [ensureAudio]+ );++ const progress = totalMs > 0 ? Math.min(1, positionMs / totalMs) : 0;+ const label = t('voiceNote.label').replace('{duration}', formatDuration(totalMs));++ return (+ <div data-testid="voice-note-player" className={cn('flex min-w-56 flex-col gap-1.5', className)}>+ <div className="flex items-center gap-2">+ <Button+ type="button"+ iconOnly+ variant="secondary"+ size="sm"+ analyticsId="voice-note-toggle"+ data-testid="voice-note-toggle"+ aria-label={playing ? t('voiceNote.pause') : t('voiceNote.play')}+ disabled={loading}+ onClick={() => void toggle()}>+ {playing ? <PauseIcon className="size-4" /> : <PlayIcon className="size-4" />}+ </Button>+ <div className="flex min-w-0 flex-1 flex-col gap-1">+ {waveform && waveform.length > 0 ? (+ <div+ data-testid="voice-note-waveform"+ aria-hidden="true"+ className="flex h-6 items-center gap-px">+ {waveform.map((bar, index) => (+ <span+ // eslint-disable-next-line react/no-array-index-key+ key={index}+ className={cn(+ 'w-0.5 flex-1 rounded-full',+ index / waveform.length < progress ? 'bg-primary-500' : 'bg-surface-strong'+ )}+ style={{ height: `${Math.max(12, Math.round(bar * 100))}%` }}+ />+ ))}+ </div>+ ) : null}+ <Slider+ size="sm"+ min={0}+ max={Math.max(totalMs, 1)}+ step={50}+ value={[Math.min(positionMs, Math.max(totalMs, 1))]}+ disabled={totalMs <= 0}+ thumbLabels={[t('voiceNote.seek')]}+ onValueChange={values => void seek(values)}+ />+ </div>+ <span+ data-testid="voice-note-time"+ aria-label={label}+ className="text-content-muted w-10 shrink-0 text-right text-xs tabular-nums">+ {formatDuration(playing || positionMs > 0 ? positionMs : totalMs)}+ </span>+ </div>+ {error ? (+ <p role="alert" className="text-danger text-xs">+ {t('voiceNote.loadFailed')}+ </p>+ ) : null}+ {transcript !== undefined ? (+ <CollapsibleRoot>+ <CollapsibleTrigger data-testid="voice-note-transcript-toggle">+ {t('voiceNote.transcript')}+ </CollapsibleTrigger>+ <CollapsibleContent>+ <p data-testid="voice-note-transcript" className="text-sm whitespace-pre-wrap">+ {transcript.trim() || t('voiceNote.noTranscript')}+ </p>+ </CollapsibleContent>+ </CollapsibleRoot>+ ) : null}+ </div>+ );+}++export default VoiceNotePlayer;