import { useTheme } from '@/components/ThemeContext'; import { useInfiniteQuery } from '@tanstack/react-query'; import * as Clipboard from 'expo-clipboard'; import { useRouter } from 'expo-router'; import { ArrowLeft, CopyIcon, Gift, HandCoins, Plus, Users } from 'lucide-react-native'; import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { ActivityIndicator, FlatList, Platform, Pressable, RefreshControl, Share, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { Toast } from 'toastify-react-native'; import { user_api } from '../lib/api'; const PAGE_SIZE = 10; export function ReferralsTab() { const router = useRouter(); const { isDark } = useTheme(); const { t } = useTranslation(); const [refreshing, setRefreshing] = useState(false); const theme = { background: isDark ? '#0f172a' : '#f8fafc', cardBg: isDark ? '#1e293b' : '#ffffff', text: isDark ? '#ffffff' : '#0f172a', subText: isDark ? '#94a3b8' : '#64748b', primary: '#3b82f6', success: '#10b981', }; const { data, isLoading, isError, fetchNextPage, hasNextPage, refetch } = useInfiniteQuery({ queryKey: ['my_referrals'], queryFn: async ({ pageParam = 1 }) => { const res = await user_api.my_referrals({ page: pageParam, page_size: PAGE_SIZE, }); const d = res.data.data; return { results: d.results ?? [], current_page: d.current_page, total_pages: d.total_pages, }; }, getNextPageParam: (lastPage) => lastPage.current_page < lastPage.total_pages ? lastPage.current_page + 1 : undefined, initialPageParam: 1, }); const referrals = data?.pages.flatMap((p) => p.results) ?? []; const onRefresh = async () => { setRefreshing(true); await refetch(); setRefreshing(false); }; // Clipboard + Share funksiyasi const handleCopyAndShare = async (code: string) => { const referralLink = `${code}`; // Clipboard-ga nusxa olish await Clipboard.setStringAsync(referralLink); // Share qilish try { await Share.share({ message: referralLink, title: t('Referal linkni ulashish'), }); } catch (err) { console.log('Share error:', err); } if (Platform.OS === 'android') { Toast.success(t('Refferal kopiya qilindi')); } }; if (isLoading) { return ( ); } if (isError) { return ( {t('Xatolik yuz berdi')} ); } return ( {/* Header */} router.push('/profile')}> {t('Refferallarim')} router.push('/profile/added-referalls')}> item.id.toString()} contentContainerStyle={styles.list} refreshControl={ } onEndReached={() => hasNextPage && fetchNextPage()} renderItem={({ item }) => ( {item.code} handleCopyAndShare(item.code)}> {item.description} {item.referral_registered_count} {t('foydalanuvchi')} {item.referral_income_amount} {t("so'm")} )} ListEmptyComponent={() => ( {t("Refferallar mavjud emas")} )} /> ); } const styles = StyleSheet.create({ container: { flex: 1 }, center: { flex: 1, justifyContent: 'center', alignItems: 'center' }, topHeader: { flexDirection: 'row', justifyContent: 'space-between', padding: 16, alignItems: 'center', }, headerTitle: { fontSize: 18, fontWeight: '700' }, list: { padding: 16, gap: 12, paddingBottom: 30, flexGrow: 1 }, card: { borderRadius: 16, padding: 16, gap: 10, }, cardRow: { flexDirection: 'row', justifyContent: 'space-between', alignContent: 'center', alignItems: 'center', }, cardHeader: { flexDirection: 'row', gap: 8, alignItems: 'center', }, code: { fontSize: 16, fontWeight: '700', }, footer: { flexDirection: 'row', justifyContent: 'space-between', marginTop: 6, }, row: { flexDirection: 'row', gap: 6, alignItems: 'center', }, meta: {}, amount: { fontWeight: '700', }, emptyContainer: { flex: 1, alignItems: 'center', justifyContent: 'center', paddingHorizontal: 30, }, emptyIconWrapper: { width: 80, height: 80, borderRadius: 40, alignItems: 'center', justifyContent: 'center', marginBottom: 20, elevation: 4, }, emptyTitle: { fontSize: 18, fontWeight: '700', marginBottom: 8, textAlign: 'center', }, emptyDesc: { fontSize: 14, textAlign: 'center', marginBottom: 20, lineHeight: 20, }, emptyButton: { paddingHorizontal: 24, paddingVertical: 12, borderRadius: 12, }, emptyButtonText: { color: '#fff', fontWeight: '600', fontSize: 15, }, });