All files / components/analytics AnalyticsDashboard.tsx

95.96% Statements 452/471
88.33% Branches 53/60
28.57% Functions 4/14
95.96% Lines 452/471

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 5891x                                                                                             1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   148x 148x 111x 111x 111x 148x   148x 148x 148x   148x   71x 71x 71x 71x 71x 71x 71x 71x   71x   1x 37x   37x   37x 37x   37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x   37x 37x   37x 37x 148x 148x 148x 148x 148x 148x 148x 148x 148x 148x 148x 148x 37x     37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x     37x 37x 37x 37x 37x 37x 37x 37x     37x 37x 37x 37x 37x 37x 37x 36x 36x 36x 36x 36x 36x           36x 36x 36x 36x 36x             36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x   1x   1x   37x 37x     37x   37x 37x 37x 37x 37x 37x 37x 37x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x   1x   1x   37x 37x     37x 37x 37x 37x 37x 37x 37x 37x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x   36x 108x 36x 36x 36x 36x 36x 36x   1x   1x   37x 37x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 37x 37x 37x 37x 37x     37x   37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x           37x 37x     37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x   37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x           37x 37x 37x     37x   37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x   37x 37x 37x 37x 37x 37x 37x 37x   37x 37x 37x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x   111x 111x 111x 111x 74x 74x 74x   111x 37x 37x           37x 37x     37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x   37x 37x           37x 37x 37x     37x 37x 12x   37x 37x 37x 37x 37x 37x 37x 37x 37x 36x 36x 108x 108x   108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x   36x 36x   1x   1x   37x 37x 37x   37x  
'use client';
 
import { useState } from 'react';
import {
  Eye,
  MousePointer,
  MessageCircle,
  TrendingUp,
  Globe,
  Smartphone,
  Monitor,
  Tablet,
  Lock,
  ArrowUp,
  ArrowDown,
  Users,
  Clock,
  Target,
  ChevronDown,
  FileText,
} from 'lucide-react';
import {
  LineChart,
  Line,
  BarChart,
  Bar,
  PieChart,
  Pie,
  Cell,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  ResponsiveContainer,
  Legend,
} from 'recharts';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { type SubscriptionTier } from '@/lib/subscription/tiers';
import { type AnalyticsData } from '@/app/(dashboard)/analytics/page';
 
interface AnalyticsDashboardProps {
  data: AnalyticsData;
  analyticsLevel: 'basic' | 'advanced' | 'full';
  tier: SubscriptionTier;
}
 
const COLORS = ['#7c3aed', '#06b6d4', '#f59e0b', '#10b981', '#ec4899', '#6366f1'];
 
const EVENT_LABELS: Record<string, string> = {
  profile_view: 'Profile Views',
  sample_click: 'Sample Clicks',
  testimonial_view: 'Testimonial Views',
  package_view: 'Package Views',
  whatsapp_click: 'WhatsApp Clicks',
  email_click: 'Email Clicks',
  linkedin_click: 'LinkedIn Clicks',
  website_click: 'Website Clicks',
  verification_view: 'Verifications',
};
 
const DEVICE_ICONS: Record<string, React.ReactNode> = {
  Mobile: <Smartphone className="h-4 w-4" />,
  Desktop: <Monitor className="h-4 w-4" />,
  Tablet: <Tablet className="h-4 w-4" />,
};
 
function GrowthIndicator({ value }: { value: number }) {
  if (value === 0) return <span className="text-xs text-muted-foreground">--</span>;
  const isPositive = value > 0;
  return (
    <span
      className={`inline-flex items-center gap-0.5 text-xs font-medium ${isPositive ? 'text-green-500' : 'text-red-500'}`}
    >
      {isPositive ? <ArrowUp className="h-3 w-3" /> : <ArrowDown className="h-3 w-3" />}
      {Math.abs(value).toFixed(1)}%
    </span>
  );
}
 
function LockedOverlay({ message }: { message: string }) {
  return (
    <div className="absolute inset-0 z-10 flex items-center justify-center bg-background/80 backdrop-blur-sm">
      <div className="text-center">
        <Lock className="mx-auto h-8 w-8 text-muted-foreground" />
        <p className="mt-2 text-sm text-muted-foreground">{message}</p>
      </div>
    </div>
  );
}
 
export function AnalyticsDashboard({ data, analyticsLevel }: AnalyticsDashboardProps) {
  const [timeRange, setTimeRange] = useState<'7d' | '30d'>('30d');
 
  const filteredDailyData = timeRange === '7d' ? data.dailyViews.slice(-7) : data.dailyViews;
 
  const isLocked = analyticsLevel === 'basic';
  const isPremiumLocked = analyticsLevel !== 'full';
 
  const statCards = [
    {
      title: 'Total Views',
      value: data.totals.views.toLocaleString(),
      growth: data.growth.views,
      icon: Eye,
      color: 'text-blue-500',
    },
    {
      title: 'Total Clicks',
      value: data.totals.clicks.toLocaleString(),
      growth: data.growth.clicks,
      icon: MousePointer,
      color: 'text-green-500',
    },
    {
      title: 'WhatsApp Clicks',
      value: data.totals.whatsappClicks.toLocaleString(),
      growth: data.growth.whatsappClicks,
      icon: MessageCircle,
      color: 'text-emerald-500',
    },
    {
      title: 'CTR',
      value: `${data.totals.ctr.toFixed(1)}%`,
      growth: data.growth.ctr,
      icon: TrendingUp,
      color: 'text-purple-500',
    },
  ];
 
  return (
    <div className="space-y-6">
      {/* Stats Grid */}
      <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4 xl:grid-cols-5">
        {statCards.map((stat) => (
          <Card key={stat.title}>
            <CardContent className="pt-6">
              <div className="flex items-center justify-between">
                <div>
                  <p className="text-sm text-muted-foreground">{stat.title}</p>
                  <p className="text-3xl font-bold">{stat.value}</p>
                  <GrowthIndicator value={stat.growth} />
                </div>
                <stat.icon className={`h-8 w-8 ${stat.color}`} />
              </div>
            </CardContent>
          </Card>
        ))}
 
        {/* Unique Visitors - Pro */}
        <Card className={isLocked ? 'relative overflow-hidden' : ''}>
          {isLocked && <LockedOverlay message="Upgrade to Pro" />}
          <CardContent className="pt-6">
            <div className="flex items-center justify-between">
              <div>
                <p className="text-sm text-muted-foreground">Unique Visitors</p>
                <p className="text-3xl font-bold">{data.uniqueVisitors.toLocaleString()}</p>
                <span className="text-xs text-muted-foreground">
                  of {data.totals.views.toLocaleString()} total views
                </span>
              </div>
              <Users className="h-8 w-8 text-indigo-500" />
            </div>
          </CardContent>
        </Card>
      </div>
 
      {/* Time Range Selector */}
      <div className="flex justify-end">
        <Tabs value={timeRange} onValueChange={(v) => setTimeRange(v as '7d' | '30d')}>
          <TabsList>
            <TabsTrigger value="7d">7 Days</TabsTrigger>
            <TabsTrigger value="30d">30 Days</TabsTrigger>
          </TabsList>
        </Tabs>
      </div>
 
      {/* Views Over Time Chart */}
      <Card>
        <CardHeader>
          <CardTitle>Views & Clicks Over Time</CardTitle>
          <CardDescription>Daily traffic to your profile</CardDescription>
        </CardHeader>
        <CardContent>
          {filteredDailyData.length > 0 ? (
            <ResponsiveContainer width="100%" height={300}>
              <LineChart data={filteredDailyData}>
                <CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
                <XAxis
                  dataKey="date"
                  tickFormatter={(date) =>
                    new Date(date).toLocaleDateString('en-US', {
                      month: 'short',
                      day: 'numeric',
                    })
                  }
                  className="text-xs"
                />
                <YAxis className="text-xs" />
                <Tooltip
                  labelFormatter={(date) =>
                    new Date(date).toLocaleDateString('en-US', {
                      month: 'long',
                      day: 'numeric',
                      year: 'numeric',
                    })
                  }
                />
                <Legend />
                <Line
                  type="monotone"
                  dataKey="views"
                  stroke="#7c3aed"
                  strokeWidth={2}
                  dot={false}
                  name="Views"
                />
                <Line
                  type="monotone"
                  dataKey="clicks"
                  stroke="#06b6d4"
                  strokeWidth={2}
                  dot={false}
                  name="Clicks"
                />
              </LineChart>
            </ResponsiveContainer>
          ) : (
            <div className="flex h-[300px] items-center justify-center text-muted-foreground">
              No data yet. Share your profile to start tracking!
            </div>
          )}
        </CardContent>
      </Card>
 
      {/* Advanced Analytics - Locked for basic tier */}
      <div className="grid gap-6 md:grid-cols-2">
        {/* Event Breakdown */}
        <Card className={isLocked ? 'relative overflow-hidden' : ''}>
          {isLocked && <LockedOverlay message="Upgrade to Pro for detailed event breakdown" />}
          <CardHeader>
            <CardTitle>Event Breakdown</CardTitle>
            <CardDescription>What actions visitors take</CardDescription>
          </CardHeader>
          <CardContent>
            {data.eventBreakdown.length > 0 ? (
              <ResponsiveContainer width="100%" height={250}>
                <BarChart data={data.eventBreakdown} layout="vertical">
                  <CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
                  <XAxis type="number" className="text-xs" />
                  <YAxis
                    type="category"
                    dataKey="type"
                    width={100}
                    tickFormatter={(type) => EVENT_LABELS[type] || type}
                    className="text-xs"
                  />
                  <Tooltip
                    formatter={(value) => [value, 'Count']}
                    labelFormatter={(type) => EVENT_LABELS[type as string] || type}
                  />
                  <Bar dataKey="count" fill="#7c3aed" radius={[0, 4, 4, 0]} />
                </BarChart>
              </ResponsiveContainer>
            ) : (
              <div className="flex h-[250px] items-center justify-center text-muted-foreground">
                No events recorded yet
              </div>
            )}
          </CardContent>
        </Card>
 
        {/* Device Breakdown */}
        <Card className={isLocked ? 'relative overflow-hidden' : ''}>
          {isLocked && <LockedOverlay message="Upgrade to Pro for device analytics" />}
          <CardHeader>
            <CardTitle>Device Breakdown</CardTitle>
            <CardDescription>How visitors access your profile</CardDescription>
          </CardHeader>
          <CardContent>
            {data.deviceBreakdown.length > 0 ? (
              <div className="flex items-center justify-center">
                <ResponsiveContainer width="100%" height={200}>
                  <PieChart>
                    <Pie
                      data={data.deviceBreakdown}
                      dataKey="count"
                      nameKey="device"
                      cx="50%"
                      cy="50%"
                      outerRadius={80}
                      label={({ device, percent }) => `${device} ${(percent * 100).toFixed(0)}%`}
                    >
                      {data.deviceBreakdown.map((entry, index) => (
                        <Cell key={entry.device} fill={COLORS[index % COLORS.length]} />
                      ))}
                    </Pie>
                    <Tooltip />
                  </PieChart>
                </ResponsiveContainer>
              </div>
            ) : (
              <div className="flex h-[200px] items-center justify-center text-muted-foreground">
                No device data yet
              </div>
            )}
            <div className="mt-4 flex justify-center gap-6">
              {data.deviceBreakdown.map((item, index) => (
                <div key={item.device} className="flex items-center gap-2">
                  <div
                    className="h-3 w-3 rounded-full"
                    style={{ backgroundColor: COLORS[index % COLORS.length] }}
                  />
                  <span className="flex items-center gap-1 text-sm">
                    {DEVICE_ICONS[item.device]}
                    {item.device}
                  </span>
                </div>
              ))}
            </div>
          </CardContent>
        </Card>
      </div>
 
      {/* Pro Tier: Peak Hours & Visitor Retention */}
      <div className="grid gap-6 md:grid-cols-2">
        {/* Peak Activity Hours */}
        <Card className={isLocked ? 'relative overflow-hidden' : ''}>
          {isLocked && <LockedOverlay message="Upgrade to Pro for peak hours analysis" />}
          <CardHeader>
            <div className="flex items-center gap-2">
              <Clock className="h-5 w-5" />
              <CardTitle>Peak Activity Hours</CardTitle>
            </div>
            <CardDescription>Best times to share your profile</CardDescription>
          </CardHeader>
          <CardContent>
            {data.peakHours.some((h) => h.count > 0) ? (
              <ResponsiveContainer width="100%" height={200}>
                <BarChart data={data.peakHours}>
                  <CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
                  <XAxis
                    dataKey="hour"
                    className="text-xs"
                    tickFormatter={(h) => `${h}:00`}
                    interval={2}
                  />
                  <YAxis className="text-xs" />
                  <Tooltip
                    labelFormatter={(h) => `${h}:00 - ${h}:59`}
                    formatter={(value) => [value, 'Events']}
                  />
                  <Bar dataKey="count" fill="#7c3aed" radius={[4, 4, 0, 0]} />
                </BarChart>
              </ResponsiveContainer>
            ) : (
              <div className="flex h-[200px] items-center justify-center text-muted-foreground">
                No activity data yet
              </div>
            )}
          </CardContent>
        </Card>
 
        {/* New vs Returning Visitors */}
        <Card className={isLocked ? 'relative overflow-hidden' : ''}>
          {isLocked && <LockedOverlay message="Upgrade to Pro for visitor retention" />}
          <CardHeader>
            <div className="flex items-center gap-2">
              <Users className="h-5 w-5" />
              <CardTitle>New vs Returning Visitors</CardTitle>
            </div>
            <CardDescription>How sticky your profile is</CardDescription>
          </CardHeader>
          <CardContent>
            {data.visitorRetention.new + data.visitorRetention.returning > 0 ? (
              <>
                <div className="flex items-center justify-center">
                  <ResponsiveContainer width="100%" height={200}>
                    <PieChart>
                      <Pie
                        data={[
                          { name: 'New', value: data.visitorRetention.new },
                          { name: 'Returning', value: data.visitorRetention.returning },
                        ]}
                        dataKey="value"
                        nameKey="name"
                        cx="50%"
                        cy="50%"
                        innerRadius={50}
                        outerRadius={80}
                        label={({ name, percent }) => `${name} ${(percent * 100).toFixed(0)}%`}
                      >
                        <Cell fill="#06b6d4" />
                        <Cell fill="#7c3aed" />
                      </Pie>
                      <Tooltip />
                    </PieChart>
                  </ResponsiveContainer>
                </div>
                <div className="mt-2 flex justify-center gap-6">
                  <div className="flex items-center gap-2">
                    <div className="h-3 w-3 rounded-full" style={{ backgroundColor: '#06b6d4' }} />
                    <span className="text-sm">
                      New ({data.visitorRetention.new.toLocaleString()})
                    </span>
                  </div>
                  <div className="flex items-center gap-2">
                    <div className="h-3 w-3 rounded-full" style={{ backgroundColor: '#7c3aed' }} />
                    <span className="text-sm">
                      Returning ({data.visitorRetention.returning.toLocaleString()})
                    </span>
                  </div>
                </div>
              </>
            ) : (
              <div className="flex h-[200px] items-center justify-center text-muted-foreground">
                No visitor data yet
              </div>
            )}
          </CardContent>
        </Card>
      </div>
 
      {/* Premium Tier: Conversion Funnel & Top Content */}
      <div className="grid gap-6 md:grid-cols-2">
        {/* Conversion Funnel */}
        <Card className={isPremiumLocked ? 'relative overflow-hidden' : ''}>
          {isPremiumLocked && <LockedOverlay message="Upgrade to Premium for conversion funnel" />}
          <CardHeader>
            <div className="flex items-center gap-2">
              <Target className="h-5 w-5" />
              <CardTitle>Conversion Funnel</CardTitle>
            </div>
            <CardDescription>From profile view to contact</CardDescription>
          </CardHeader>
          <CardContent>
            {data.conversionFunnel.views > 0 ? (
              <div className="space-y-3">
                {[
                  {
                    label: 'Profile Views',
                    value: data.conversionFunnel.views,
                    pct: 100,
                    color: '#7c3aed',
                  },
                  {
                    label: 'Content Clicks',
                    value: data.conversionFunnel.contentClicks,
                    pct:
                      data.conversionFunnel.views > 0
                        ? (data.conversionFunnel.contentClicks / data.conversionFunnel.views) * 100
                        : 0,
                    color: '#06b6d4',
                  },
                  {
                    label: 'Contact Actions',
                    value: data.conversionFunnel.contacts,
                    pct:
                      data.conversionFunnel.views > 0
                        ? (data.conversionFunnel.contacts / data.conversionFunnel.views) * 100
                        : 0,
                    color: '#10b981',
                  },
                ].map((step, i) => (
                  <div key={step.label}>
                    <div className="mb-1 flex items-center justify-between text-sm">
                      <span className="font-medium">{step.label}</span>
                      <span className="text-muted-foreground">
                        {step.value.toLocaleString()} ({step.pct.toFixed(1)}%)
                      </span>
                    </div>
                    <div className="h-8 overflow-hidden rounded bg-muted">
                      <div
                        className="flex h-full items-center justify-center rounded text-xs font-medium text-white transition-all"
                        style={{ width: `${Math.max(step.pct, 2)}%`, backgroundColor: step.color }}
                      >
                        {step.pct >= 10 && `${step.pct.toFixed(0)}%`}
                      </div>
                    </div>
                    {i < 2 && (
                      <div className="flex justify-center py-1">
                        <ChevronDown className="h-4 w-4 text-muted-foreground" />
                      </div>
                    )}
                  </div>
                ))}
              </div>
            ) : (
              <div className="flex h-[200px] items-center justify-center text-muted-foreground">
                No funnel data yet
              </div>
            )}
          </CardContent>
        </Card>
 
        {/* Top Performing Content */}
        <Card className={isPremiumLocked ? 'relative overflow-hidden' : ''}>
          {isPremiumLocked && <LockedOverlay message="Upgrade to Premium for content analytics" />}
          <CardHeader>
            <div className="flex items-center gap-2">
              <FileText className="h-5 w-5" />
              <CardTitle>Top Performing Content</CardTitle>
            </div>
            <CardDescription>Your most clicked work samples</CardDescription>
          </CardHeader>
          <CardContent>
            {data.topContent.length > 0 ? (
              <div className="space-y-4">
                {data.topContent.map((item, index) => {
                  const maxClicks = data.topContent[0].clicks;
                  const percentage = (item.clicks / maxClicks) * 100;
                  return (
                    <div key={item.sampleId} className="space-y-1">
                      <div className="flex items-center justify-between text-sm">
                        <span className="flex items-center gap-2 font-medium">
                          <span className="flex h-5 w-5 items-center justify-center rounded-full bg-primary/10 text-xs font-bold text-primary">
                            {index + 1}
                          </span>
                          <span className="max-w-[180px] truncate">{item.title}</span>
                        </span>
                        <span className="text-muted-foreground">
                          {item.clicks} click{item.clicks !== 1 ? 's' : ''}
                        </span>
                      </div>
                      <div className="h-2 overflow-hidden rounded-full bg-muted">
                        <div
                          className="h-full rounded-full bg-primary transition-all"
                          style={{ width: `${percentage}%` }}
                        />
                      </div>
                    </div>
                  );
                })}
              </div>
            ) : (
              <div className="flex h-[200px] items-center justify-center text-muted-foreground">
                No content click data yet
              </div>
            )}
          </CardContent>
        </Card>
      </div>
 
      {/* Top Referrers - Premium only */}
      <Card className={isPremiumLocked ? 'relative overflow-hidden' : ''}>
        {isPremiumLocked && (
          <LockedOverlay message="Upgrade to Premium for traffic source analytics" />
        )}
        <CardHeader>
          <div className="flex items-center gap-2">
            <Globe className="h-5 w-5" />
            <CardTitle>Top Traffic Sources</CardTitle>
          </div>
          <CardDescription>Where your visitors come from</CardDescription>
        </CardHeader>
        <CardContent>
          {data.topReferrers.length > 0 ? (
            <div className="space-y-4">
              {data.topReferrers.map((referrer) => {
                const maxCount = data.topReferrers[0].count;
                const percentage = (referrer.count / maxCount) * 100;
 
                return (
                  <div key={referrer.source} className="space-y-1">
                    <div className="flex items-center justify-between text-sm">
                      <span className="font-medium">{referrer.source}</span>
                      <span className="text-muted-foreground">{referrer.count} visits</span>
                    </div>
                    <div className="h-2 overflow-hidden rounded-full bg-muted">
                      <div
                        className="h-full rounded-full bg-primary"
                        style={{ width: `${percentage}%` }}
                      />
                    </div>
                  </div>
                );
              })}
            </div>
          ) : (
            <div className="flex h-[200px] items-center justify-center text-muted-foreground">
              No referrer data yet
            </div>
          )}
        </CardContent>
      </Card>
    </div>
  );
}