import { useState } from 'react'; import { View, Text, FlatList, TextInput, TouchableOpacity, ActivityIndicator, RefreshControl } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { useQuery } from '@tanstack/react-query'; import { router } from 'expo-router'; import { api } from '../../../services/api'; const STATUS_CONFIG: Record = { ACTIVE: { label: 'Active', color: '#166534', bg: '#DCFCE7', accent: '#16A34A' }, SUSPENDED: { label: 'Suspended', color: '#92400E', bg: '#FEF3C7', accent: '#D97706' }, CANCELLED: { label: 'Cancelled', color: '#991B1B', bg: '#FEE2E2', accent: '#DC2626' }, PENDING: { label: 'Pending', color: '#475569', bg: '#F1F5F9', accent: '#94A3B8' }, NO_SUB: { label: 'No Sub', color: '#6B7280', bg: '#F1F5F9', accent: '#CBD5E1' }, }; // Client.status is null in API — derive from subscription status instead function getClientStatus(client: any) { const sub = client?.subscriptions?.[0]; if (!sub) return 'NO_SUB'; return sub.status ?? 'PENDING'; } export default function ClientsScreen() { const [search, setSearch] = useState(''); const { data, isLoading, refetch, isRefetching } = useQuery({ queryKey: ['clients'], queryFn: () => api.get('/api/v1/clients?limit=100').then(r => r.data?.data ?? r.data), }); const clients = (data ?? []).filter((c: any) => `${c.firstName} ${c.lastName} ${c.accountNumber}`.toLowerCase().includes(search.toLowerCase()) ); return ( {/* Header */} Clients {data?.length ?? 0} subscribers {/* Search */} {search.length > 0 && ( setSearch('')} hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}> × )} {isLoading ? ( ) : ( item.id} refreshControl={} contentContainerStyle={{ padding: 16, paddingBottom: 32 }} renderItem={({ item }) => { const statusKey = getClientStatus(item); const st = STATUS_CONFIG[statusKey] ?? { label: statusKey, color: '#475569', bg: '#F1F5F9', accent: '#94A3B8' }; const plan = item.subscriptions?.[0]?.plan?.name; return ( router.push(`/(app)/clients/${item.id}`)} activeOpacity={0.7} > {item.firstName} {item.lastName} {item.accountNumber}{plan ? ` · ${plan}` : ''} {item.phone && {item.phone}} {st.label} ); }} ListEmptyComponent={ No clients found } /> )} ); }