// Neue Notiz erstellen // Mit Web Speech API (Spracherkennung auf Deutsch) und 1-Klick Auto-Sync zu Git import { useState, useEffect, useRef } from 'react'; import { saveNote, captureNote } from './notes-api'; interface Props { onClose: () => void; onCreated: (noteId: string) => void; } interface IWindow extends Window { SpeechRecognition?: any; webkitSpeechRecognition?: any; } function todayString(): string { return new Date().toISOString().slice(0, 10); } export default function NewNoteDialog({ onClose, onCreated }: Props) { const [title, setTitle] = useState(''); const [body, setBody] = useState(''); const [tags, setTags] = useState('notiz'); const [inbox, setInbox] = useState(true); const [autoSync, setAutoSync] = useState(true); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); // Spracherkennung (Web Speech API) const [isListening, setIsListening] = useState(false); const [speechSupported, setSpeechSupported] = useState(false); const recognitionRef = useRef(null); useEffect(() => { const win = window as unknown as IWindow; const hasSpeech = !!(win.SpeechRecognition || win.webkitSpeechRecognition); setSpeechSupported(hasSpeech); return () => { if (recognitionRef.current) { try { recognitionRef.current.stop(); } catch { // ignore } } }; }, []); const toggleListening = () => { if (isListening) { try { recognitionRef.current?.stop(); } catch { // ignore } setIsListening(false); return; } const win = window as unknown as IWindow; const SpeechRec = win.SpeechRecognition || win.webkitSpeechRecognition; if (!SpeechRec) { setError('Spracherkennung wird in diesem Browser nicht unterstützt.'); return; } try { const recognition = new SpeechRec(); recognition.lang = 'de-DE'; recognition.continuous = true; recognition.interimResults = true; recognition.onresult = (event: any) => { let finalTranscript = ''; for (let i = event.resultIndex; i < event.results.length; ++i) { if (event.results[i].isFinal) { finalTranscript += event.results[i][0].transcript + ' '; } } if (finalTranscript) { setBody((prev) => (prev ? `${prev.trim()} ${finalTranscript.trim()}` : finalTranscript.trim())); if (!title) { const words = finalTranscript.trim().split(' ').slice(0, 5).join(' '); setTitle(words); } } }; recognition.onerror = (evt: any) => { console.error('Speech recognition error:', evt); setIsListening(false); }; recognition.onend = () => { setIsListening(false); }; recognitionRef.current = recognition; recognition.start(); setIsListening(true); setError(null); } catch (err) { console.error('Failed to start speech recognition:', err); setIsListening(false); setError('Konnte Mikrofon nicht aktivieren'); } }; const handleSave = async () => { if (!title.trim() && !body.trim()) { setError('Bitte Titel oder Inhalt eingeben'); return; } setSaving(true); setError(null); const tagsArray = tags .split(',') .map((t) => t.trim()) .filter(Boolean); try { if (autoSync) { const res = await captureNote({ text: body || title, title: title || undefined, tags: tagsArray, autoIngest: inbox, autoSync: true, targetFolder: inbox ? '00_Inbox' : '01_Daily', }); onCreated(res.noteId); onClose(); } else { const date = todayString(); const tagLine = tagsArray.length > 0 ? `tags: [${tagsArray.join(', ')}]\n` : ''; const effectiveTitle = title || body.slice(0, 30); const fullBody = body.startsWith('#') ? body : effectiveTitle + '\n\n' + body; const content = `--- title: "${effectiveTitle}" created: ${date} aktualisiert: ${date} ${tagLine}--- ${fullBody} `; const noteId = inbox ? `00_Inbox/${date}_${effectiveTitle.replace(/[^a-zA-Z0-9]/g, '_')}.md` : `01_Daily/${date}_${effectiveTitle.replace(/[^a-zA-Z0-9]/g, '_')}.md`; await saveNote(noteId, content); onCreated(noteId); onClose(); } } catch (err) { setError((err as Error).message); setSaving(false); } }; return (

Neue Notiz erfassen

{speechSupported && ( )}
setTitle(e.target.value)} placeholder="z. B. Besprechung mit Max" autoFocus style={{ width: '100%', padding: 8, fontSize: 15, border: '1px solid #ccc', borderRadius: 4, boxSizing: 'border-box', }} />