feat: agency portfolio with per-client integrations hub
Admin home lists tenants, alerts, and connection health; header selector enables superadmin context; Meta/GA4/GSC/GTM/CRM/email slots live per tenant (manual status until OAuth). Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
190
src/components/ClientIntegrationsPanel.tsx
Normal file
190
src/components/ClientIntegrationsPanel.tsx
Normal file
@@ -0,0 +1,190 @@
|
||||
'use client';
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { ArrowLeft, Loader2 } from 'lucide-react';
|
||||
|
||||
type Integ = {
|
||||
id?: string;
|
||||
provider: string;
|
||||
status: string;
|
||||
label?: string | null;
|
||||
external_account_id?: string | null;
|
||||
error_message?: string | null;
|
||||
last_sync_at?: string | null;
|
||||
};
|
||||
|
||||
type Provider = { id: string; label: string; group: string };
|
||||
|
||||
const STATUS_OPTS = [
|
||||
{ value: 'disconnected', label: 'Desconectado' },
|
||||
{ value: 'pending', label: 'Pendiente' },
|
||||
{ value: 'connected', label: 'Conectado' },
|
||||
{ value: 'error', label: 'Error' },
|
||||
] as const;
|
||||
|
||||
export default function ClientIntegrationsPanel({
|
||||
tenantId,
|
||||
tenantName,
|
||||
}: {
|
||||
tenantId: string;
|
||||
tenantName: string;
|
||||
}) {
|
||||
const [integrations, setIntegrations] = useState<Integ[]>([]);
|
||||
const [providers, setProviders] = useState<Provider[]>([]);
|
||||
const [warning, setWarning] = useState<string | null>(null);
|
||||
const [saving, setSaving] = useState<string | null>(null);
|
||||
const [accountDraft, setAccountDraft] = useState<Record<string, string>>({});
|
||||
|
||||
async function load() {
|
||||
const res = await fetch(
|
||||
`/api/admin/integrations?tenantId=${encodeURIComponent(tenantId)}`
|
||||
);
|
||||
const data = await res.json();
|
||||
setIntegrations(data.integrations || []);
|
||||
setProviders(data.providers || []);
|
||||
setWarning(data.warning || null);
|
||||
const drafts: Record<string, string> = {};
|
||||
for (const i of data.integrations || []) {
|
||||
drafts[i.provider] = i.external_account_id || '';
|
||||
}
|
||||
setAccountDraft(drafts);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [tenantId]);
|
||||
|
||||
async function save(provider: string, status: string) {
|
||||
setSaving(provider);
|
||||
await fetch('/api/admin/integrations', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
tenantId,
|
||||
provider,
|
||||
status,
|
||||
externalAccountId: accountDraft[provider] || '',
|
||||
}),
|
||||
});
|
||||
await load();
|
||||
setSaving(null);
|
||||
}
|
||||
|
||||
const byProvider = new Map(integrations.map((i) => [i.provider, i]));
|
||||
|
||||
const groups = [
|
||||
{ id: 'paid', title: 'Paid media' },
|
||||
{ id: 'analytics', title: 'Analítica y medición' },
|
||||
{ id: 'crm', title: 'CRM' },
|
||||
{ id: 'email', title: 'Email / lifecycle' },
|
||||
];
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center gap-3">
|
||||
<Link
|
||||
href="/dashboard/agencia"
|
||||
className="text-gray-400 hover:text-white"
|
||||
>
|
||||
<ArrowLeft className="w-4 h-4" />
|
||||
</Link>
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-white tracking-tight">
|
||||
{tenantName}
|
||||
</h2>
|
||||
<p className="text-sm text-gray-400 mt-0.5">
|
||||
Integraciones del cliente · marca estado y account id (OAuth live en
|
||||
siguiente fase)
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{warning && (
|
||||
<p className="text-xs text-amber-400 border border-amber-500/20 rounded-lg px-3 py-2">
|
||||
{warning}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{groups.map((g) => {
|
||||
const list = providers.filter((p) => p.group === g.id);
|
||||
if (!list.length) return null;
|
||||
return (
|
||||
<section key={g.id} className="space-y-2">
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-gray-500">
|
||||
{g.title}
|
||||
</h3>
|
||||
<ul className="space-y-2">
|
||||
{list.map((p) => {
|
||||
const row = byProvider.get(p.id);
|
||||
const status = row?.status || 'disconnected';
|
||||
return (
|
||||
<li
|
||||
key={p.id}
|
||||
className="rounded-xl border border-gray-800 bg-[#111827] p-4 space-y-3"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div>
|
||||
<p className="text-sm font-medium text-white">
|
||||
{p.label}
|
||||
</p>
|
||||
{row?.last_sync_at && (
|
||||
<p className="text-[10px] text-gray-500">
|
||||
Último sync{' '}
|
||||
{new Date(row.last_sync_at).toLocaleString('es-ES')}
|
||||
</p>
|
||||
)}
|
||||
{row?.error_message && (
|
||||
<p className="text-[11px] text-red-400 mt-1">
|
||||
{row.error_message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<select
|
||||
className="text-xs rounded-lg bg-gray-900 border border-gray-700 text-gray-200 px-2 py-1.5"
|
||||
value={status}
|
||||
disabled={saving === p.id}
|
||||
onChange={(e) => save(p.id, e.target.value)}
|
||||
>
|
||||
{STATUS_OPTS.map((o) => (
|
||||
<option key={o.value} value={o.value}>
|
||||
{o.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="flex gap-2 items-center">
|
||||
<input
|
||||
className="flex-1 text-xs rounded-lg bg-gray-900 border border-gray-700 px-3 py-2 text-white placeholder:text-gray-600"
|
||||
placeholder="Account / Property ID (act_…, G-…, UA…)"
|
||||
value={accountDraft[p.id] || ''}
|
||||
onChange={(e) =>
|
||||
setAccountDraft((d) => ({
|
||||
...d,
|
||||
[p.id]: e.target.value,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
disabled={saving === p.id}
|
||||
onClick={() => save(p.id, status)}
|
||||
className="text-xs px-3 py-2 rounded-lg border border-gray-700 text-gray-300 hover:text-white hover:border-gray-500 inline-flex items-center gap-1"
|
||||
>
|
||||
{saving === p.id ? (
|
||||
<Loader2 className="w-3 h-3 animate-spin" />
|
||||
) : (
|
||||
'Guardar'
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</section>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user