← back to Hub

components/PattyTab.tsx

135 lines

'use client';

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

interface PattyData {
  petitions: { total: number; byStatus: Record<string, number> };
  signatures: number;
  campaigns: { total: number; byStatus: Record<string, number> };
  subscribers: number;
  templates: number;
  activePetitions: { id: string; title: string; status: string; signature_count: number; created_at: string }[];
}

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

  async function fetchData() {
    try {
      const res = await fetch('/api/patty/detail', { credentials: 'include' });
      if (res.ok) setData(await res.json());
    } catch (err) {
      console.error('Patty 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(124,58,237,0.15)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <Megaphone size={20} style={{ color: '#7c3aed' }} />
          </div>
          <div>
            <h2 className="text-xl font-bold" style={{ color: 'var(--color-text)' }}>Patty</h2>
            <p className="text-sm" style={{ color: 'var(--color-text-muted)' }}>Petition Platform</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:7460" target="_blank" rel="noopener noreferrer" className="btn btn-primary btn-sm" style={{ backgroundColor: '#7c3aed', borderColor: '#7c3aed' }}>
            Open Patty <ExternalLink size={12} />
          </a>
        </div>
      </div>

      {/* Stats */}
      <div className="grid grid-cols-5 gap-4">
        {[
          { label: 'Petitions', value: data?.petitions.total || 0, icon: Megaphone, color: '#7c3aed' },
          { label: 'Signatures', value: data?.signatures || 0, icon: PenTool, color: '#22c55e' },
          { label: 'Campaigns', value: data?.campaigns.total || 0, icon: Mail, color: '#3b82f6' },
          { label: 'Subscribers', value: data?.subscribers || 0, icon: Users, color: '#f59e0b' },
          { label: 'Templates', value: data?.templates || 0, icon: Layout, color: '#ef4444' },
        ].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>

      {/* Petition Status */}
      {data?.petitions.byStatus && Object.keys(data.petitions.byStatus).length > 0 && (
        <div className="card">
          <h3 className="text-sm font-semibold mb-3" style={{ color: 'var(--color-text)' }}>Petitions by Status</h3>
          <div className="flex gap-3 flex-wrap">
            {Object.entries(data.petitions.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>
      )}

      {/* Campaign Status */}
      {data?.campaigns.byStatus && Object.keys(data.campaigns.byStatus).length > 0 && (
        <div className="card">
          <h3 className="text-sm font-semibold mb-3" style={{ color: 'var(--color-text)' }}>Campaigns by Status</h3>
          <div className="flex gap-3 flex-wrap">
            {Object.entries(data.campaigns.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>
      )}

      {/* Active Petitions */}
      <div className="card">
        <h3 className="text-sm font-semibold mb-3" style={{ color: 'var(--color-text)' }}>Recent Petitions</h3>
        <div className="space-y-2">
          {(data?.activePetitions || []).map((p) => (
            <div key={p.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">
                <Megaphone size={14} style={{ color: '#7c3aed' }} />
                <span className="text-sm truncate" style={{ color: 'var(--color-text)' }}>{p.title}</span>
              </div>
              <div className="flex items-center gap-3 shrink-0">
                <span className="text-xs" style={{ color: 'var(--color-text-secondary)' }}>
                  {p.signature_count} sigs
                </span>
                <span className="badge" style={{ backgroundColor: 'rgba(124,58,237,0.15)', color: '#7c3aed', border: '1px solid rgba(124,58,237,0.3)', fontSize: 10, padding: '1px 6px' }}>
                  {p.status}
                </span>
              </div>
            </div>
          ))}
          {(!data?.activePetitions || data.activePetitions.length === 0) && (
            <p className="text-sm text-center py-4" style={{ color: 'var(--color-text-muted)' }}>No petitions found</p>
          )}
        </div>
      </div>
    </div>
  );
}