speech-button.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use client';
  2. import { useCallback, useEffect, useRef } from 'react';
  3. import { Mic, Loader2 } from 'lucide-react';
  4. import { useAudioRecorder } from '@/lib/hooks/use-audio-recorder';
  5. import { useI18n } from '@/lib/hooks/use-i18n';
  6. import { cn } from '@/lib/utils';
  7. import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
  8. import { toast } from 'sonner';
  9. interface SpeechButtonProps {
  10. onTranscription: (text: string) => void;
  11. className?: string;
  12. disabled?: boolean;
  13. size?: 'sm' | 'md';
  14. }
  15. export function SpeechButton({
  16. onTranscription,
  17. className,
  18. disabled,
  19. size = 'sm',
  20. }: SpeechButtonProps) {
  21. const { t } = useI18n();
  22. // Ref to always call the latest onTranscription, avoiding stale closures
  23. const onTranscriptionRef = useRef(onTranscription);
  24. useEffect(() => {
  25. onTranscriptionRef.current = onTranscription;
  26. }, [onTranscription]);
  27. const stableOnTranscription = useCallback((text: string) => {
  28. onTranscriptionRef.current(text);
  29. }, []);
  30. const handleError = useCallback((error: string) => {
  31. toast.error(error);
  32. }, []);
  33. const { isRecording, isProcessing, startRecording, stopRecording } = useAudioRecorder({
  34. onTranscription: stableOnTranscription,
  35. onError: handleError,
  36. });
  37. const active = isRecording || isProcessing;
  38. const handleClick = () => {
  39. if (isRecording) {
  40. stopRecording();
  41. } else if (!isProcessing) {
  42. startRecording();
  43. }
  44. };
  45. const isMd = size === 'md';
  46. const sizeClasses = isMd ? 'h-8 w-8' : 'h-6 w-6';
  47. const iconSize = isMd ? 'w-4 h-4' : 'w-3.5 h-3.5';
  48. const barH = isMd ? 14 : 10;
  49. return (
  50. <Tooltip>
  51. <TooltipTrigger asChild>
  52. <button
  53. type="button"
  54. disabled={disabled || isProcessing}
  55. onClick={handleClick}
  56. className={cn(
  57. 'relative flex items-center justify-center rounded-lg transition-all duration-200 shrink-0 cursor-pointer',
  58. sizeClasses,
  59. active
  60. ? 'bg-violet-500/90 dark:bg-violet-600/80 text-white shadow-[0_0_12px_rgba(139,92,246,0.45)] dark:shadow-[0_0_12px_rgba(139,92,246,0.3)]'
  61. : 'text-muted-foreground/60 hover:text-muted-foreground hover:bg-muted/80',
  62. disabled && 'opacity-40 pointer-events-none',
  63. className,
  64. )}
  65. >
  66. {/* Breathing ring when recording */}
  67. {isRecording && (
  68. <span
  69. className="absolute inset-[-4px] rounded-[10px] border border-violet-400/40 dark:border-violet-400/25"
  70. style={{
  71. animation: 'speech-ring 2s ease-in-out infinite',
  72. }}
  73. />
  74. )}
  75. {isProcessing ? (
  76. <Loader2 className={cn(iconSize, 'animate-spin')} />
  77. ) : isRecording ? (
  78. /* Mini equalizer bars */
  79. <span className="flex items-center gap-[2.5px] relative z-10">
  80. {[0, 1, 2].map((i) => (
  81. <span
  82. key={i}
  83. className="rounded-full bg-white"
  84. style={{
  85. width: isMd ? 2.5 : 2,
  86. animation: `speech-bar ${0.4 + i * 0.15}s ease-in-out ${i * 0.1}s infinite alternate`,
  87. height: 3,
  88. }}
  89. />
  90. ))}
  91. </span>
  92. ) : (
  93. <Mic className={cn(iconSize, 'relative z-10')} />
  94. )}
  95. {/* Injected keyframes */}
  96. <style jsx>{`
  97. @keyframes speech-bar {
  98. 0% {
  99. height: 3px;
  100. }
  101. 100% {
  102. height: ${barH}px;
  103. }
  104. }
  105. @keyframes speech-ring {
  106. 0%,
  107. 100% {
  108. opacity: 0.3;
  109. transform: scale(1);
  110. }
  111. 50% {
  112. opacity: 0.7;
  113. transform: scale(1.08);
  114. }
  115. }
  116. `}</style>
  117. </button>
  118. </TooltipTrigger>
  119. <TooltipContent side="top" className="text-xs">
  120. {isProcessing
  121. ? t('roundtable.processing')
  122. : isRecording
  123. ? t('voice.stopListening')
  124. : t('voice.startListening')}
  125. </TooltipContent>
  126. </Tooltip>
  127. );
  128. }