|
| 1 | +import { View, Text, ScrollView } from "react-native"; |
| 2 | +import { SafeAreaView } from "react-native-safe-area-context"; |
| 3 | +import { TrendingUp, TrendingDown, Users, ShoppingCart, DollarSign, Activity } from "lucide-react-native"; |
| 4 | +import { Card, CardHeader, CardTitle, CardContent } from "~/components/ui/Card"; |
| 5 | + |
| 6 | +interface DashboardCardMeta { |
| 7 | + type: "card"; |
| 8 | + title: string; |
| 9 | + value: string; |
| 10 | + trend: string; |
| 11 | + icon: string; |
| 12 | +} |
| 13 | + |
| 14 | +const dashboardMetadata: DashboardCardMeta[] = [ |
| 15 | + { type: "card", title: "Monthly Sales", value: "$120,000", trend: "+12%", icon: "dollar-sign" }, |
| 16 | + { type: "card", title: "Active Users", value: "8,420", trend: "+5.2%", icon: "users" }, |
| 17 | + { type: "card", title: "Orders", value: "1,340", trend: "-2.1%", icon: "shopping-cart" }, |
| 18 | + { type: "card", title: "Revenue Growth", value: "23.5%", trend: "+8.7%", icon: "activity" }, |
| 19 | +]; |
| 20 | + |
| 21 | +const iconMap: Record<string, React.ComponentType<{ size: number; color: string }>> = { |
| 22 | + "dollar-sign": DollarSign, |
| 23 | + users: Users, |
| 24 | + "shopping-cart": ShoppingCart, |
| 25 | + activity: Activity, |
| 26 | +}; |
| 27 | + |
| 28 | +function TrendBadge({ trend }: { trend: string }) { |
| 29 | + const isPositive = trend.startsWith("+"); |
| 30 | + const TrendIcon = isPositive ? TrendingUp : TrendingDown; |
| 31 | + return ( |
| 32 | + <View |
| 33 | + className={`flex-row items-center rounded-full px-2.5 py-1 ${ |
| 34 | + isPositive ? "bg-emerald-50" : "bg-red-50" |
| 35 | + }`} |
| 36 | + > |
| 37 | + <TrendIcon size={12} color={isPositive ? "#059669" : "#dc2626"} /> |
| 38 | + <Text |
| 39 | + className={`ml-1 text-xs font-semibold ${ |
| 40 | + isPositive ? "text-emerald-700" : "text-red-600" |
| 41 | + }`} |
| 42 | + > |
| 43 | + {trend} |
| 44 | + </Text> |
| 45 | + </View> |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +function MetadataCardRenderer({ meta }: { meta: DashboardCardMeta }) { |
| 50 | + const IconComponent = iconMap[meta.icon] ?? Activity; |
| 51 | + |
| 52 | + return ( |
| 53 | + <Card className="mb-3"> |
| 54 | + <CardHeader className="flex-row items-center justify-between pb-2"> |
| 55 | + <CardTitle className="text-sm font-medium text-muted-foreground"> |
| 56 | + {meta.title} |
| 57 | + </CardTitle> |
| 58 | + <View className="rounded-lg bg-primary/10 p-2"> |
| 59 | + <IconComponent size={18} color="#1e40af" /> |
| 60 | + </View> |
| 61 | + </CardHeader> |
| 62 | + <CardContent> |
| 63 | + <Text className="text-2xl font-bold text-card-foreground"> |
| 64 | + {meta.value} |
| 65 | + </Text> |
| 66 | + <View className="mt-2"> |
| 67 | + <TrendBadge trend={meta.trend} /> |
| 68 | + </View> |
| 69 | + </CardContent> |
| 70 | + </Card> |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +function renderFromMetadata(metadata: DashboardCardMeta[]) { |
| 75 | + return metadata.map((item, index) => { |
| 76 | + switch (item.type) { |
| 77 | + case "card": |
| 78 | + return <MetadataCardRenderer key={index} meta={item} />; |
| 79 | + default: |
| 80 | + return null; |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +export default function HomeScreen() { |
| 86 | + return ( |
| 87 | + <SafeAreaView className="flex-1 bg-background" edges={["left", "right"]}> |
| 88 | + <ScrollView |
| 89 | + className="flex-1" |
| 90 | + contentContainerClassName="px-5 pb-8 pt-4" |
| 91 | + showsVerticalScrollIndicator={false} |
| 92 | + > |
| 93 | + <View className="mb-5"> |
| 94 | + <Text className="text-2xl font-bold text-foreground"> |
| 95 | + Dashboard |
| 96 | + </Text> |
| 97 | + <Text className="mt-1 text-sm text-muted-foreground"> |
| 98 | + Welcome back. Here's your overview. |
| 99 | + </Text> |
| 100 | + </View> |
| 101 | + |
| 102 | + {renderFromMetadata(dashboardMetadata)} |
| 103 | + </ScrollView> |
| 104 | + </SafeAreaView> |
| 105 | + ); |
| 106 | +} |
0 commit comments