import React, { useState, useRef } from 'react'; import { X, Send, Mic, Square, Loader2 } from 'lucide-react'; interface AddInspirationDialogProps { isOpen: boolean; onClose: () => void; onSubmit: (content: string, isVoice: boolean) => Promise; } export const AddInspirationDialog: React.FC = ({ isOpen, onClose, onSubmit }) => { const [content, setContent] = useState(''); const [isRecording, setIsRecording] = useState(false); const [isProcessing, setIsProcessing] = useState(false); const mediaRecorderRef = useRef(null); const audioChunksRef = useRef([]); if (!isOpen) return null; const handleTextSubmit = async () => { if (!content.trim() || isProcessing) return; setIsProcessing(true); try { await onSubmit(content, false); setContent(''); onClose(); } catch (error) { console.error('Failed to submit inspiration:', error); } finally { setIsProcessing(false); } }; const startRecording = async () => { try { const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); const mediaRecorder = new MediaRecorder(stream); mediaRecorderRef.current = mediaRecorder; audioChunksRef.current = []; mediaRecorder.ondataavailable = (event) => { if (event.data.size > 0) { audioChunksRef.current.push(event.data); } }; mediaRecorder.onstop = async () => { const audioBlob = new Blob(audioChunksRef.current, { type: 'audio/webm' }); await processAudio(audioBlob); // Stop all tracks stream.getTracks().forEach(track => track.stop()); }; mediaRecorder.start(); setIsRecording(true); } catch (err) { console.error('Failed to start recording:', err); alert('无法访问麦克风,请检查权限设置'); } }; const stopRecording = () => { if (mediaRecorderRef.current && isRecording) { mediaRecorderRef.current.stop(); setIsRecording(false); } }; const processAudio = async (audioBlob: Blob) => { setIsProcessing(true); try { const file = new File([audioBlob], 'recording.webm', { type: 'audio/webm' }); // 这里调用 API 处理音频 // 暂时使用文本提交的方式 await onSubmit('语音录制的灵感', true); onClose(); } catch (error) { console.error('Failed to process audio:', error); alert('语音处理失败,请重试'); } finally { setIsProcessing(false); } }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleTextSubmit(); } }; return (
{/* 头部 */}

✨ 记录灵感

{/* 输入区域 */}
{/* 文本输入 */}