'use client' import { useRef, useState } from 'react' import { motion } from 'motion/react' import { FileText, Upload, Sparkles, CheckCircle, AlertCircle } from 'lucide-react' import { Button } from '@/components/ui/button' import { useLanguage } from '@/lib/i18n' import { markdownToHTML, extractMarkdownTitle } from '@/lib/editor/markdown-export' interface Props { noteCount: number onNext: (justCreated: boolean) => void onSkip: () => void locale: string } async function createNote(title: string | null, content: string) { const res = await fetch('/api/notes', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: title ?? 'Untitled', content }), }) if (!res.ok) throw new Error('Failed to create note') } export function OnboardingStepNotes({ noteCount, onNext, onSkip, locale }: Props) { const { t } = useLanguage() const [loading, setLoading] = useState(false) const [created, setCreated] = useState(false) const [importError, setImportError] = useState(null) const [importedCount, setImportedCount] = useState(0) const fileInputRef = useRef(null) async function handleCreateDemo() { setLoading(true) try { await fetch('/api/onboarding/seed-demo-notes', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ locale }), }) setCreated(true) setTimeout(() => onNext(true), 1200) } finally { setLoading(false) } } async function handleImportFiles(e: React.ChangeEvent) { const files = Array.from(e.target.files ?? []) if (!files.length) return setLoading(true) setImportError(null) let count = 0 try { for (const file of files) { const text = await file.text() const isMd = file.name.endsWith('.md') const title = extractMarkdownTitle(text) ?? file.name.replace(/\.(md|txt)$/, '') const content = isMd ? markdownToHTML(text) : `

${text.replace(/\n\n+/g, '

').replace(/\n/g, '
')}

` await createNote(title, content) count++ } setImportedCount(count) setCreated(true) setTimeout(() => onNext(false), 1200) } catch { setImportError(t('onboarding.import_error')) } finally { setLoading(false) if (fileInputRef.current) fileInputRef.current.value = '' } } const hasNotes = noteCount > 0 return (

{t('onboarding.step_notes_title')}

{hasNotes ? t('onboarding.step_notes_has_notes', { count: noteCount }) : t('onboarding.step_notes_empty')}

{!hasNotes && (

{t('onboarding.step_notes_hint')}

)}
{hasNotes ? (
) : (
{created ? ( {importedCount > 0 ? t('onboarding.import_notes_ready', { count: importedCount }) : t('onboarding.demo_notes_ready')} ) : ( <> {/* Hidden file input — accepts .md and .txt */}

{t('onboarding.import_formats')}

)} {importError && ( {importError} )}
)}
) }