← back to Hub

components/FreddyTab.tsx

139 lines

'use client';

import { useState, useEffect } from 'react';
import { SkeletonList } from './Skeleton';
import { DollarSign, Heart, Building2, Landmark, Zap, Users, ExternalLink, RefreshCw, TrendingUp } from 'lucide-react';

interface FreddyData {
  causes: number;
  organizations: number;
  granters: number;
  matches: number;
  contacts: number;
  revenue_total: number;
  topCauses: { id: string; title: string; total_votes: number; urgency_score: number; status: string }[];
}

export default function FreddyTab() {
  const [data, setData] = useState<FreddyData | null>(null);
  const [loading, setLoading] = useState(true);

  async function fetchData() {
    try {
      const res = await fetch('/api/freddy/detail', { credentials: 'include' });
      if (res.ok) setData(await res.json());
    } catch (err) {
      console.error('Freddy fetch error:', err);
    } finally {
      setLoading(false);
    }
  }

  useEffect(() => { fetchData(); }, []);

  if (loading) {
    return <div className="p-5"><SkeletonList count={4} /></div>;
  }

  return (
    <div className="p-5 space-y-6">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-3">
          <div style={{ width: 40, height: 40, borderRadius: 10, backgroundColor: 'rgba(217,119,6,0.15)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <DollarSign size={20} style={{ color: '#d97706' }} />
          </div>
          <div>
            <h2 className="text-xl font-bold" style={{ color: 'var(--color-text)' }}>Freddy</h2>
            <p className="text-sm" style={{ color: 'var(--color-text-muted)' }}>Fundraising Engine</p>
          </div>
        </div>
        <div className="flex items-center gap-2">
          <button onClick={() => { setLoading(true); fetchData(); }} className="btn btn-secondary btn-sm">
            <RefreshCw size={14} /> Refresh
          </button>
          <a href="http://45.61.58.125:7470" target="_blank" rel="noopener noreferrer" className="btn btn-primary btn-sm" style={{ backgroundColor: '#d97706', borderColor: '#d97706' }}>
            Open Freddy <ExternalLink size={12} />
          </a>
        </div>
      </div>

      {/* Stats */}
      <div className="grid grid-cols-3 gap-4">
        {[
          { label: 'Causes', value: data?.causes || 0, icon: Heart, color: '#ef4444' },
          { label: 'Organizations', value: data?.organizations || 0, icon: Building2, color: '#3b82f6' },
          { label: 'Granters', value: data?.granters || 0, icon: Landmark, color: '#059669' },
        ].map((s) => (
          <div key={s.label} className="stat-card" style={{ borderLeft: `3px solid ${s.color}` }}>
            <div className="flex items-center gap-2 mb-1">
              <s.icon size={14} style={{ color: s.color }} />
              <span className="text-xs font-medium" style={{ color: 'var(--color-text-muted)' }}>{s.label}</span>
            </div>
            <div className="text-2xl font-bold" style={{ color: 'var(--color-text)' }}>{s.value.toLocaleString()}</div>
          </div>
        ))}
      </div>

      <div className="grid grid-cols-3 gap-4">
        {[
          { label: 'Matches', value: data?.matches || 0, icon: Zap, color: '#d97706' },
          { label: 'Contacts', value: data?.contacts || 0, icon: Users, color: '#7c3aed' },
          { label: 'Total Revenue', value: `$${(data?.revenue_total || 0).toLocaleString(undefined, { minimumFractionDigits: 2 })}`, icon: DollarSign, color: '#22c55e', isString: true },
        ].map((s) => (
          <div key={s.label} className="stat-card" style={{ borderLeft: `3px solid ${s.color}` }}>
            <div className="flex items-center gap-2 mb-1">
              <s.icon size={14} style={{ color: s.color }} />
              <span className="text-xs font-medium" style={{ color: 'var(--color-text-muted)' }}>{s.label}</span>
            </div>
            <div className="text-2xl font-bold" style={{ color: 'var(--color-text)' }}>
              {typeof s.value === 'string' ? s.value : s.value.toLocaleString()}
            </div>
          </div>
        ))}
      </div>

      {/* Top Causes */}
      <div className="card">
        <div className="flex items-center gap-2 mb-3">
          <TrendingUp size={16} style={{ color: '#d97706' }} />
          <h3 className="text-sm font-semibold" style={{ color: 'var(--color-text)' }}>Top Causes by Votes</h3>
        </div>
        <div className="space-y-2">
          {(data?.topCauses || []).map((cause, i) => (
            <div key={cause.id} className="flex items-center justify-between p-2 rounded-lg" style={{ backgroundColor: 'var(--color-surface-el)' }}>
              <div className="flex items-center gap-3 min-w-0">
                <div
                  style={{
                    width: 24, height: 24, borderRadius: 6,
                    backgroundColor: 'rgba(217,119,6,0.15)',
                    display: 'flex', alignItems: 'center', justifyContent: 'center',
                    fontSize: 11, fontWeight: 700, color: '#d97706',
                  }}
                >
                  {i + 1}
                </div>
                <span className="text-sm truncate" style={{ color: 'var(--color-text)' }}>{cause.title}</span>
              </div>
              <div className="flex items-center gap-3 shrink-0">
                <span className="text-xs" style={{ color: 'var(--color-text-secondary)' }}>
                  {cause.total_votes} votes
                </span>
                <span className="text-xs" style={{ color: 'var(--color-text-muted)' }}>
                  urgency: {(cause.urgency_score * 100).toFixed(0)}%
                </span>
                <span className="badge" style={{ backgroundColor: 'rgba(217,119,6,0.15)', color: '#d97706', border: '1px solid rgba(217,119,6,0.3)', fontSize: 10, padding: '1px 6px' }}>
                  {cause.status}
                </span>
              </div>
            </div>
          ))}
          {(!data?.topCauses || data.topCauses.length === 0) && (
            <p className="text-sm text-center py-4" style={{ color: 'var(--color-text-muted)' }}>No causes found</p>
          )}
        </div>
      </div>
    </div>
  );
}