← back to Hub

components/GrantTab.tsx

146 lines

'use client';

import { useState, useEffect } from 'react';
import { SkeletonList } from './Skeleton';
import { Award, FileText, Newspaper, Handshake, DollarSign, ExternalLink, RefreshCw, Calendar } from 'lucide-react';

interface GrantData {
  grants: { total: number; byStatus: Record<string, number> };
  proposals: number;
  news: number;
  collaborations: { total: number; byType: Record<string, number> };
  total_funding: number;
  upcomingDeadlines: { id: string; title: string; funder: string; deadline: string; status: string }[];
}

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

  async function fetchData() {
    try {
      const res = await fetch('/api/grant/detail', { credentials: 'include' });
      if (res.ok) setData(await res.json());
    } catch (err) {
      console.error('Grant 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(5,150,105,0.15)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <Award size={20} style={{ color: '#059669' }} />
          </div>
          <div>
            <h2 className="text-xl font-bold" style={{ color: 'var(--color-text)' }}>Grant</h2>
            <p className="text-sm" style={{ color: 'var(--color-text-muted)' }}>Grant Discovery & Proposals</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:7450" target="_blank" rel="noopener noreferrer" className="btn btn-primary btn-sm" style={{ backgroundColor: '#059669', borderColor: '#059669' }}>
            Open Grant <ExternalLink size={12} />
          </a>
        </div>
      </div>

      {/* Stats */}
      <div className="grid grid-cols-4 gap-4">
        {[
          { label: 'Grants', value: data?.grants.total || 0, icon: Award, color: '#059669' },
          { label: 'Proposals', value: data?.proposals || 0, icon: FileText, color: '#3b82f6' },
          { label: 'News', value: data?.news || 0, icon: Newspaper, color: '#f59e0b' },
          { label: 'Collaborations', value: data?.collaborations.total || 0, icon: Handshake, color: '#7c3aed' },
        ].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>

      {/* Total Funding */}
      <div className="stat-card" style={{ borderLeft: '3px solid #059669' }}>
        <div className="flex items-center gap-2 mb-1">
          <DollarSign size={14} style={{ color: '#059669' }} />
          <span className="text-xs font-medium" style={{ color: 'var(--color-text-muted)' }}>Total Funding Awarded</span>
        </div>
        <div className="text-2xl font-bold" style={{ color: 'var(--color-text)' }}>
          ${(data?.total_funding || 0).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
        </div>
      </div>

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

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

      {/* Upcoming Deadlines */}
      <div className="card">
        <h3 className="text-sm font-semibold mb-3" style={{ color: 'var(--color-text)' }}>Upcoming Deadlines</h3>
        <div className="space-y-2">
          {(data?.upcomingDeadlines || []).map((g) => (
            <div key={g.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">
                <Award size={14} style={{ color: '#059669' }} />
                <div className="min-w-0">
                  <span className="text-sm truncate block" style={{ color: 'var(--color-text)' }}>{g.title}</span>
                  <span className="text-xs" style={{ color: 'var(--color-text-muted)' }}>{g.funder}</span>
                </div>
              </div>
              <div className="flex items-center gap-2 shrink-0">
                <Calendar size={12} style={{ color: 'var(--color-text-muted)' }} />
                <span className="text-xs" style={{ color: 'var(--color-warning)' }}>
                  {g.deadline ? new Date(g.deadline).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }) : 'No deadline'}
                </span>
              </div>
            </div>
          ))}
          {(!data?.upcomingDeadlines || data.upcomingDeadlines.length === 0) && (
            <p className="text-sm text-center py-4" style={{ color: 'var(--color-text-muted)' }}>No upcoming deadlines</p>
          )}
        </div>
      </div>
    </div>
  );
}