← back to Hub

components/SDCCTab.tsx

138 lines

'use client';

import { useState, useEffect } from 'react';
import { SkeletonList } from './Skeleton';
import { Mail, FileText, Newspaper, Megaphone, Handshake, Users, BookOpen, ExternalLink, RefreshCw } from 'lucide-react';

interface SDCCData {
  drafts: { total: number; byStatus: Record<string, number> };
  news: number;
  petitions: number;
  collaborations: number;
  partners: number;
  leads: number;
  library: number;
  recentDrafts: { id: string; subject: string; lane: string; status: string; created_at: string }[];
}

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

  async function fetchData() {
    try {
      const res = await fetch('/api/sdcc/detail', { credentials: 'include' });
      if (res.ok) {
        setData(await res.json());
      }
    } catch (err) {
      console.error('SDCC 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(59,130,246,0.15)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <Mail size={20} style={{ color: '#3b82f6' }} />
          </div>
          <div>
            <h2 className="text-xl font-bold" style={{ color: 'var(--color-text)' }}>SDCC</h2>
            <p className="text-sm" style={{ color: 'var(--color-text-muted)' }}>Email Draft System</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:7400" target="_blank" rel="noopener noreferrer" className="btn btn-primary btn-sm" style={{ backgroundColor: '#3b82f6', borderColor: '#3b82f6' }}>
            Open SDCC <ExternalLink size={12} />
          </a>
        </div>
      </div>

      {/* Stats Grid */}
      <div className="grid grid-cols-4 gap-4">
        {[
          { label: 'Drafts', value: data?.drafts.total || 0, icon: FileText, color: '#3b82f6' },
          { label: 'News Items', value: data?.news || 0, icon: Newspaper, color: '#f59e0b' },
          { label: 'Petitions', value: data?.petitions || 0, icon: Megaphone, color: '#ef4444' },
          { label: 'Collaborations', value: data?.collaborations || 0, icon: Handshake, color: '#22c55e' },
        ].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: 'Partners', value: data?.partners || 0, icon: Users },
          { label: 'Old Leads', value: data?.leads || 0, icon: Users },
          { label: 'Library Items', value: data?.library || 0, icon: BookOpen },
        ].map((s) => (
          <div key={s.label} className="stat-card">
            <div className="flex items-center gap-2 mb-1">
              <s.icon size={14} style={{ color: 'var(--color-accent)' }} />
              <span className="text-xs font-medium" style={{ color: 'var(--color-text-muted)' }}>{s.label}</span>
            </div>
            <div className="text-xl font-bold" style={{ color: 'var(--color-text)' }}>{s.value.toLocaleString()}</div>
          </div>
        ))}
      </div>

      {/* Draft Status Breakdown */}
      {data?.drafts.byStatus && Object.keys(data.drafts.byStatus).length > 0 && (
        <div className="card">
          <h3 className="text-sm font-semibold mb-3" style={{ color: 'var(--color-text)' }}>Drafts by Status</h3>
          <div className="flex gap-3 flex-wrap">
            {Object.entries(data.drafts.byStatus).map(([status, count]) => (
              <div key={status} className="badge" style={{ backgroundColor: 'var(--color-surface-el)', color: 'var(--color-text-secondary)', border: '1px solid var(--color-border)' }}>
                {status}: {count}
              </div>
            ))}
          </div>
        </div>
      )}

      {/* Recent Drafts */}
      <div className="card">
        <h3 className="text-sm font-semibold mb-3" style={{ color: 'var(--color-text)' }}>Recent Drafts</h3>
        <div className="space-y-2">
          {(data?.recentDrafts || []).map((draft) => (
            <div key={draft.id} className="flex items-center justify-between p-2 rounded-lg" style={{ backgroundColor: 'var(--color-surface-el)' }}>
              <div className="flex items-center gap-2 min-w-0">
                <FileText size={14} style={{ color: '#3b82f6' }} />
                <span className="text-sm truncate" style={{ color: 'var(--color-text)' }}>{draft.subject}</span>
              </div>
              <div className="flex items-center gap-2 shrink-0">
                <span className="badge" style={{ backgroundColor: 'rgba(59,130,246,0.15)', color: '#3b82f6', border: '1px solid rgba(59,130,246,0.3)', fontSize: 10, padding: '1px 6px' }}>
                  {draft.lane}
                </span>
                <span className="text-xs" style={{ color: 'var(--color-text-muted)' }}>{draft.status}</span>
              </div>
            </div>
          ))}
          {(!data?.recentDrafts || data.recentDrafts.length === 0) && (
            <p className="text-sm text-center py-4" style={{ color: 'var(--color-text-muted)' }}>No drafts found</p>
          )}
        </div>
      </div>
    </div>
  );
}