329 lines
10 KiB
TypeScript
329 lines
10 KiB
TypeScript
// 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<string | null>(null);
|
|
|
|
// Spracherkennung (Web Speech API)
|
|
const [isListening, setIsListening] = useState(false);
|
|
const [speechSupported, setSpeechSupported] = useState(false);
|
|
const recognitionRef = useRef<any>(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 (
|
|
<div
|
|
style={{
|
|
position: 'fixed',
|
|
top: 0,
|
|
left: 0,
|
|
right: 0,
|
|
bottom: 0,
|
|
background: 'rgba(0,0,0,0.5)',
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
zIndex: 1000,
|
|
}}
|
|
>
|
|
<div
|
|
style={{
|
|
background: '#fff',
|
|
borderRadius: 8,
|
|
padding: 24,
|
|
width: '90%',
|
|
maxWidth: 600,
|
|
maxHeight: '90vh',
|
|
overflow: 'auto',
|
|
boxShadow: '0 4px 20px rgba(0,0,0,0.3)',
|
|
}}
|
|
>
|
|
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 16 }}>
|
|
<h2 style={{ margin: 0, fontSize: 18 }}>Neue Notiz erfassen</h2>
|
|
{speechSupported && (
|
|
<button
|
|
onClick={toggleListening}
|
|
type="button"
|
|
style={{
|
|
padding: '6px 12px',
|
|
borderRadius: 20,
|
|
border: 'none',
|
|
background: isListening ? '#ff4d4f' : '#1890ff',
|
|
color: '#fff',
|
|
cursor: 'pointer',
|
|
fontSize: 13,
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
gap: 6,
|
|
fontWeight: 500,
|
|
boxShadow: isListening ? '0 0 10px rgba(255, 77, 79, 0.7)' : 'none',
|
|
}}
|
|
>
|
|
<span>{isListening ? '🔴 Höre zu...' : '🎤 Diktieren'}</span>
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
<div style={{ marginBottom: 12 }}>
|
|
<label style={{ display: 'block', fontSize: 12, color: '#666', marginBottom: 4 }}>
|
|
Titel (optional)
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={title}
|
|
onChange={(e) => 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',
|
|
}}
|
|
/>
|
|
</div>
|
|
|
|
<div style={{ marginBottom: 12 }}>
|
|
<label style={{ display: 'block', fontSize: 12, color: '#666', marginBottom: 4 }}>
|
|
Inhalt (Markdown oder Freitext)
|
|
</label>
|
|
<textarea
|
|
value={body}
|
|
onChange={(e) => setBody(e.target.value)}
|
|
placeholder={isListening ? 'Spreche jetzt ins Mikrofon...' : 'Tippe hier oder klicke auf "Diktieren"...'}
|
|
rows={8}
|
|
style={{
|
|
width: '100%',
|
|
padding: 8,
|
|
fontSize: 14,
|
|
fontFamily: 'inherit',
|
|
border: isListening ? '2px solid #ff4d4f' : '1px solid #ccc',
|
|
borderRadius: 4,
|
|
boxSizing: 'border-box',
|
|
resize: 'vertical',
|
|
}}
|
|
/>
|
|
</div>
|
|
|
|
<div style={{ marginBottom: 14, display: 'flex', gap: 12 }}>
|
|
<div style={{ flex: 1 }}>
|
|
<label style={{ display: 'block', fontSize: 12, color: '#666', marginBottom: 4 }}>
|
|
Tags (kommasepariert)
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={tags}
|
|
onChange={(e) => setTags(e.target.value)}
|
|
placeholder="notiz, idee"
|
|
style={{
|
|
width: '100%',
|
|
padding: 8,
|
|
fontSize: 14,
|
|
border: '1px solid #ccc',
|
|
borderRadius: 4,
|
|
boxSizing: 'border-box',
|
|
}}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div style={{ marginBottom: 16, display: 'flex', flexDirection: 'column', gap: 8 }}>
|
|
<label style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: 13, cursor: 'pointer' }}>
|
|
<input type="checkbox" checked={inbox} onChange={(e) => setInbox(e.target.checked)} />
|
|
In 00_Inbox ablegen (KI-Ingest arbeitet Notiz ins Wiki ein)
|
|
</label>
|
|
<label style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: 13, cursor: 'pointer', fontWeight: 500 }}>
|
|
<input type="checkbox" checked={autoSync} onChange={(e) => setAutoSync(e.target.checked)} />
|
|
🚀 Direkt Ingest & Git-Push ausführen (vollautomatisch)
|
|
</label>
|
|
</div>
|
|
|
|
{error && <div style={{ color: 'red', marginBottom: 12, fontSize: 13 }}>{error}</div>}
|
|
|
|
<div style={{ display: 'flex', gap: 8, justifyContent: 'flex-end' }}>
|
|
<button
|
|
onClick={onClose}
|
|
disabled={saving}
|
|
style={{
|
|
padding: '8px 16px',
|
|
background: 'transparent',
|
|
border: '1px solid #ccc',
|
|
borderRadius: 4,
|
|
cursor: saving ? 'default' : 'pointer',
|
|
fontSize: 14,
|
|
}}
|
|
>
|
|
Abbrechen
|
|
</button>
|
|
<button
|
|
onClick={handleSave}
|
|
disabled={saving}
|
|
style={{
|
|
padding: '8px 20px',
|
|
background: saving ? '#ccc' : '#5AD8A6',
|
|
color: '#fff',
|
|
border: 'none',
|
|
borderRadius: 4,
|
|
cursor: saving ? 'default' : 'pointer',
|
|
fontSize: 14,
|
|
fontWeight: 600,
|
|
}}
|
|
>
|
|
{saving ? 'Verarbeite & Sende...' : autoSync ? 'Speichern & Sync 🚀' : 'Speichern'}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|