import { View, Text, TouchableOpacity, Modal, FlatList, TextInput } from 'react-native'; import { useState, useEffect } from 'react'; import { ChevronDown, Plus, X, Search } from 'lucide-react-native'; import { getDatabase } from '@/lib/database'; import * as Crypto from 'expo-crypto'; interface Props { value: string; onChange: (topic: string) => void; } interface Topic { id: string; name: string; is_default: number; } export function TopicSelector({ value, onChange }: Props) { const [show, setShow] = useState(false); const [topics, setTopics] = useState([]); const [search, setSearch] = useState(''); const [newTopic, setNewTopic] = useState(''); async function loadTopics() { const db = await getDatabase(); const rows = await db.getAllAsync('SELECT * FROM topics ORDER BY is_default DESC, name ASC'); setTopics(rows); } useEffect(() => { if (show) loadTopics(); }, [show]); async function addCustomTopic() { if (!newTopic.trim()) return; const db = await getDatabase(); const id = Crypto.randomUUID(); const now = Math.floor(Date.now() / 1000); await db.runAsync('INSERT OR IGNORE INTO topics (id, name, is_default, created_at) VALUES (?, ?, 0, ?)', [id, newTopic.trim(), now]); setNewTopic(''); await loadTopics(); } const filtered = topics.filter((t) => t.name.toLowerCase().includes(search.toLowerCase())); return ( Topic setShow(true)} className="flex-row items-center border border-gray-200 rounded-xl px-4 py-3 bg-white" accessibilityRole="button" accessibilityLabel={value ? `Selected topic: ${value}` : 'Select topic'} accessibilityHint="Opens topic selector" style={{ minHeight: 48 }} > {value || 'Select topic...'} setShow(false)} accessibilityRole="button" accessibilityLabel="Close topic selector" style={{ minWidth: 44, minHeight: 44, alignItems: 'center', justifyContent: 'center' }} > Select Topic item.id} contentContainerStyle={{ paddingHorizontal: 16, paddingBottom: 40, gap: 8 }} renderItem={({ item }) => ( { onChange(item.name); setShow(false); }} className={`bg-white rounded-xl px-4 py-3 flex-row items-center justify-between ${value === item.name ? 'border-2 border-primary' : 'border border-gray-100'}`} accessibilityRole="button" accessibilityLabel={item.name} accessibilityState={{ selected: value === item.name }} style={{ minHeight: 48 }} > {item.name} {item.is_default === 1 && Default} )} /> ); }