import { useEffect, useState } from 'react'
import {
View, Text, FlatList, TouchableOpacity,
ActivityIndicator, RefreshControl, StyleSheet,
} from 'react-native'
import { SafeAreaView } from 'react-native-safe-area-context'
import { useRouter } from 'expo-router'
import { ChevronRight, BookOpen, Folder, Plus } from 'lucide-react-native'
import { apiFetch } from '@/lib/api'
import { ENDPOINTS } from '@/lib/config'
import { C } from '@/lib/theme'
interface Notebook {
id: string
name: string
icon: string | null
color: string | null
_count: { notes: number }
}
// icônes Lucide stockées en string → afficher composant, sinon emoji
const LUCIDE_ICONS = new Set(['folder','book','archive','bookmark','file','note','inbox'])
function NotebookIcon({ icon, color }: { icon: string | null, color: string | null }) {
const tint = color || C.brand
if (!icon || LUCIDE_ICONS.has(icon.toLowerCase())) {
return
}
// emoji ou autre caractère unicode
return {icon}
}
export default function NotebooksScreen() {
const [notebooks, setNotebooks] = useState([])
const [loading, setLoading] = useState(true)
const [refreshing, setRefreshing] = useState(false)
const router = useRouter()
const load = async () => {
try {
const res = await apiFetch(ENDPOINTS.notebooks)
if (res.ok) {
const data = await res.json()
setNotebooks(data.notebooks ?? [])
}
} finally {
setLoading(false)
setRefreshing(false)
}
}
useEffect(() => { load() }, [])
if (loading) {
return (
)
}
return (
Carnets
{notebooks.length}
router.push({ pathname: '/note/create' })}
style={s.newNoteBtn}
>
item.id}
contentContainerStyle={s.list}
refreshControl={ { setRefreshing(true); load() }} tintColor={C.brand} />}
renderItem={({ item }) => (
router.push({ pathname: '/notebook/[id]', params: { id: item.id } })} style={s.card} activeOpacity={0.7}>
{item.name}
{item._count?.notes ?? 0} note{(item._count?.notes ?? 0) !== 1 ? 's' : ''}
)}
ListEmptyComponent={
Aucun carnet
}
/>
)
}
const s = StyleSheet.create({
safe: { flex: 1, backgroundColor: C.paper },
header: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 20, paddingTop: 16, paddingBottom: 12, borderBottomWidth: 1, borderBottomColor: C.border },
title: { fontSize: 20, fontWeight: '700', color: C.ink, flex: 1 },
count: { fontSize: 12, color: C.concrete, backgroundColor: C.border, paddingHorizontal: 8, paddingVertical: 2, borderRadius: 10, overflow: 'hidden', marginRight: 8 },
newNoteBtn: { width: 34, height: 34, borderRadius: 17, backgroundColor: '#f3ece4', alignItems: 'center', justifyContent: 'center' },
list: { padding: 12 },
card: { flexDirection: 'row', alignItems: 'center', gap: 12, backgroundColor: C.white, borderWidth: 1, borderColor: C.border, borderRadius: 14, padding: 14, marginBottom: 8 },
iconWrap: { width: 38, height: 38, borderRadius: 10, alignItems: 'center', justifyContent: 'center' },
cardTitle: { fontSize: 14, fontWeight: '600', color: C.ink, marginBottom: 2 },
cardMeta: { fontSize: 12, color: C.concrete },
emptyWrap: { alignItems: 'center', marginTop: 60, gap: 12 },
empty: { color: C.concrete, fontSize: 14 },
})