import React, { useState } from 'react';
import { View, Text, TouchableOpacity, ActivityIndicator, ScrollView } from 'react-native';
import { PieChart } from 'react-native-gifted-charts';
import Icon from 'react-native-vector-icons/Ionicons';
import { useTheme } from '@theme';
import { getStyles } from './leadDistribution.styles';
import { LeadDistributionProps } from './leadDistribution.props';
export const LeadDistribution = ({
data = [],
loading = false,
activeAction,
onActionChange,
}: LeadDistributionProps) => {
const { theme: colors, isDark } = useTheme();
const styles = getStyles(colors, isDark);
const [menuVisible, setMenuVisible] = useState(false);
const actions = [
{ label: 'Day', value: 'day' },
{ label: 'Week', value: 'week' },
{ label: 'Month', value: 'month' },
{ label: 'Quarter', value: 'quarter' },
{ label: 'Year', value: 'year' },
];
const currentActionLabel =
actions.find((a) => a.value === activeAction)?.label || 'Month';
const totalLeadItem = data.find((item) => item.name === 'Total Lead');
const totalCount = totalLeadItem
? totalLeadItem.count
: data.reduce((sum, item) => sum + item.count, 0);
const displayData = data
.filter((item) => item.name !== 'Total Lead' && item.count > 0)
.map((item) => ({
...item,
percentage: totalCount > 0 ? Math.round((item.count / totalCount) * 100) : 0,
}));
const pieData = displayData.map((item) => ({
value: item.count,
color: item.color,
}));
const renderCenterLabel = () => {
return (
{totalCount}
Total
);
};
return (
{/* Header */}
Lead Distribution
by status · {currentActionLabel}
setMenuVisible(!menuVisible)}
activeOpacity={0.7}
>
{currentActionLabel}
{/* Dropdown Menu */}
{menuVisible && (
{actions.map((act) => {
const isSelected = act.value === activeAction;
return (
{
onActionChange(act.value);
setMenuVisible(false);
}}
>
{act.label}
{isSelected && (
)}
);
})}
)}
{/* Divider */}
{/* Body — fixed min-height so card never jumps */}
{loading ? (
) : displayData.length === 0 ? (
No lead data available
) : (
<>
{displayData.map((item, idx) => (
{item.name}
{item.count}
{item.percentage}%
))}
>
)}
);
};