import { useState, useRef } from 'react' import { View, Text, TextInput, TouchableOpacity, ScrollView, KeyboardAvoidingView, Platform, ActivityIndicator, Alert, StyleSheet, } from 'react-native' import { SafeAreaView } from 'react-native-safe-area-context' import { useRouter, useLocalSearchParams } from 'expo-router' import { Sparkles, X } from 'lucide-react-native' import { apiFetch } from '@/lib/api' import { ENDPOINTS } from '@/lib/config' import { C } from '@/lib/theme' import { AISheet } from '@/components/AISheet' import { TitleSheet } from '@/components/TitleSheet' import { MicButton } from '@/components/MicButton' import { useAudioRecorder } from '@/lib/useAudioRecorder' export default function CreateNoteScreen() { const router = useRouter() const { notebookId } = useLocalSearchParams<{ notebookId?: string }>() const [title, setTitle] = useState('') const [content, setContent] = useState('') const [saving, setSaving] = useState(false) const [aiSheetOpen, setAiSheetOpen] = useState(false) const [titleSheetOpen, setTitleSheetOpen] = useState(false) const contentRef = useRef(null) // Audio const { state: audioState, startRecording, stopAndTranscribe, cancelRecording } = useAudioRecorder( (text) => setContent((prev) => prev ? prev + ' ' + text : text) ) const handleMic = () => { if (audioState === 'idle' || audioState === 'error') startRecording() else if (audioState === 'recording') stopAndTranscribe() else cancelRecording() } const handleSave = async () => { if (!title.trim()) { Alert.alert('Titre requis', 'Donnez un titre à votre note.') return } setSaving(true) try { const res = await apiFetch(ENDPOINTS.createNote, { method: 'POST', body: JSON.stringify({ title: title.trim(), content, notebookId }), }) if (!res.ok) { const d = await res.json().catch(() => ({})) throw new Error(d.error ?? 'Erreur serveur') } const { note } = await res.json() router.replace({ pathname: '/note/[id]', params: { id: note.id } }) } catch (e: any) { Alert.alert('Erreur', e.message) } finally { setSaving(false) } } return ( {/* Header */} router.back()} style={s.cancelBtn}> Nouvelle note {saving ? : Enregistrer} {/* Titre */} contentRef.current?.focus()} autoFocus /> {content.trim().length >= 10 && ( setTitleSheetOpen(true)} style={s.sparkleBtn} activeOpacity={0.8}> )} {/* Contenu */} {/* Barre outils bas */} {audioState === 'recording' && ( ● Enregistrement… Appuyez pour arrêter )} {audioState !== 'recording' && ( setAiSheetOpen(true)} disabled={!content.trim()} style={[s.aiBtn, !content.trim() && s.aiBtnDisabled]} activeOpacity={0.8} > Améliorer avec l'IA )} {/* Modaux propres */} setAiSheetOpen(false)} text={content} onApply={(improved) => setContent(improved)} /> setTitleSheetOpen(false)} content={content} onSelect={(t) => setTitle(t)} /> ) } const s = StyleSheet.create({ safe: { flex: 1, backgroundColor: C.paper }, header: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 16, paddingVertical: 12, borderBottomWidth: 1, borderBottomColor: C.border }, cancelBtn: { padding: 4, marginRight: 4 }, headerTitle: { flex: 1, fontSize: 15, fontWeight: '600', color: C.ink, textAlign: 'center' }, saveBtn: { backgroundColor: C.brand, paddingHorizontal: 14, paddingVertical: 7, borderRadius: 10 }, saveBtnDisabled: { opacity: 0.35 }, saveBtnText: { color: C.white, fontWeight: '700', fontSize: 13 }, titleRow: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 20, paddingTop: 20, paddingBottom: 8, gap: 8 }, titleInput: { flex: 1, fontSize: 26, fontWeight: '800', color: C.ink, letterSpacing: -0.5, lineHeight: 32 }, sparkleBtn: { padding: 8, borderRadius: 10, backgroundColor: '#f3ece4' }, divider: { height: 1, backgroundColor: C.border, marginHorizontal: 20, marginBottom: 16 }, contentInput: { flex: 1, fontSize: 16, color: C.ink, lineHeight: 26, paddingHorizontal: 20, paddingBottom: 120, minHeight: 300 }, toolbar: { flexDirection: 'row', alignItems: 'center', gap: 10, paddingHorizontal: 16, paddingVertical: 12, borderTopWidth: 1, borderTopColor: C.border, backgroundColor: C.paper }, recordingHint: { flex: 1, fontSize: 13, color: '#e11d48', fontWeight: '500' }, aiBtn: { flex: 1, flexDirection: 'row', alignItems: 'center', gap: 6, backgroundColor: C.ink, paddingVertical: 11, paddingHorizontal: 14, borderRadius: 12, justifyContent: 'center' }, aiBtnDisabled: { opacity: 0.35 }, aiBtnText: { color: C.white, fontWeight: '600', fontSize: 14 }, })