feat: agency control panel for per-client connections

Matrix view of Meta/Google/Drive/Clientify, follow/archive clients,
and per-channel ON/OFF with account IDs in the client hub.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
4Ventures
2026-09-22 18:47:29 +02:00
parent 1eb02dac4d
commit c4c527072f
10 changed files with 637 additions and 140 deletions

View File

@@ -2,7 +2,7 @@
import React, { useEffect, useState } from 'react';
import Link from 'next/link';
import { ArrowLeft, Loader2 } from 'lucide-react';
import { ArrowLeft, Loader2, Power } from 'lucide-react';
type Integ = {
id?: string;
@@ -12,16 +12,23 @@ type Integ = {
external_account_id?: string | null;
error_message?: string | null;
last_sync_at?: string | null;
enabled?: boolean;
meta?: { folder_name?: string; match_score?: number } | 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;
const PLACEHOLDERS: Record<string, string> = {
meta_ads: 'act_… (Ad Account ID)',
google_ads: 'Customer ID MCC / cuenta',
ga4: 'G-… o Property ID',
search_console: 'sc-domain:ejemplo.com',
tag_manager: 'GTM-…',
clientify: 'ID / workspace Clientify',
klaviyo: 'Account / list ID',
mailchimp: 'Audience ID',
google_drive: 'Folder ID (auto-match o pegar)',
};
export default function ClientIntegrationsPanel({
tenantId,
@@ -35,6 +42,8 @@ export default function ClientIntegrationsPanel({
const [warning, setWarning] = useState<string | null>(null);
const [saving, setSaving] = useState<string | null>(null);
const [accountDraft, setAccountDraft] = useState<Record<string, string>>({});
const [followed, setFollowed] = useState(true);
const [followBusy, setFollowBusy] = useState(false);
async function load() {
const res = await fetch(
@@ -53,24 +62,43 @@ export default function ClientIntegrationsPanel({
useEffect(() => {
void load();
fetch('/api/admin/portfolio')
.then((r) => r.json())
.then((d) => {
const row = (d.portfolio || []).find(
(t: { id: string }) => t.id === tenantId
);
if (row) setFollowed(row.agency_followed !== false);
})
.catch(() => {});
}, [tenantId]);
async function save(provider: string, status: string) {
async function patch(
provider: string,
body: Record<string, unknown>
) {
setSaving(provider);
await fetch('/api/admin/integrations', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tenantId,
provider,
status,
externalAccountId: accountDraft[provider] || '',
}),
body: JSON.stringify({ tenantId, provider, ...body }),
});
await load();
setSaving(null);
}
async function toggleFollow() {
setFollowBusy(true);
const next = !followed;
await fetch('/api/admin/tenants', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ tenantId, followed: next }),
});
setFollowed(next);
setFollowBusy(false);
}
const byProvider = new Map(integrations.map((i) => [i.provider, i]));
const groups = [
@@ -83,22 +111,41 @@ export default function ClientIntegrationsPanel({
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 className="flex flex-col sm:flex-row sm:items-start justify-between gap-3">
<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">
Hub de conexiones · asocia cuentas y decide qué entra a la
plataforma
</p>
</div>
</div>
<button
type="button"
disabled={followBusy}
onClick={() => void toggleFollow()}
className={`inline-flex items-center gap-1.5 text-xs px-3 py-1.5 rounded-lg border ${
followed
? 'border-emerald-500/40 text-emerald-400'
: 'border-gray-700 text-gray-400'
}`}
>
{followBusy ? (
<Loader2 className="w-3.5 h-3.5 animate-spin" />
) : (
<Power className="w-3.5 h-3.5" />
)}
{followed ? 'Cliente activo en portafolio' : 'Archivado'}
</button>
</div>
{warning && (
@@ -119,16 +166,30 @@ export default function ClientIntegrationsPanel({
{list.map((p) => {
const row = byProvider.get(p.id);
const status = row?.status || 'disconnected';
const enabled = row?.enabled !== false;
const folderHint =
p.id === 'google_drive'
? row?.meta?.folder_name || row?.label
: null;
return (
<li
key={p.id}
className="rounded-xl border border-gray-800 bg-[#111827] p-4 space-y-3"
className={`rounded-xl border p-4 space-y-3 ${
enabled
? 'border-gray-800 bg-[#111827]'
: 'border-gray-800/50 bg-[#0d121c] opacity-70'
}`}
>
<div className="flex items-center justify-between gap-3">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3">
<div>
<p className="text-sm font-medium text-white">
{p.label}
</p>
{folderHint && (
<p className="text-[11px] text-brand-400 mt-0.5">
Carpeta: {folderHint}
</p>
)}
{row?.last_sync_at && (
<p className="text-[10px] text-gray-500">
Último sync{' '}
@@ -141,24 +202,50 @@ export default function ClientIntegrationsPanel({
</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 className="flex flex-wrap items-center gap-2">
<button
type="button"
disabled={saving === p.id}
onClick={() =>
void patch(p.id, { enabled: !enabled })
}
className={`text-[11px] px-2.5 py-1 rounded-md border ${
enabled
? 'border-emerald-500/40 text-emerald-400'
: 'border-gray-700 text-gray-500'
}`}
title="Si está OFF, no cuenta en salud ni se usa"
>
{enabled ? 'Canal ON' : 'Canal OFF'}
</button>
<button
type="button"
disabled={saving === p.id || !enabled}
onClick={() =>
void patch(p.id, {
status:
status === 'connected'
? 'disconnected'
: 'connected',
externalAccountId: accountDraft[p.id] || '',
})
}
className={`text-[11px] px-2.5 py-1 rounded-md border ${
status === 'connected'
? 'border-brand-500/40 text-brand-400'
: 'border-gray-700 text-gray-400'
}`}
>
{status === 'connected' ? 'Desconectar' : 'Conectar'}
</button>
</div>
</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…)"
placeholder={PLACEHOLDERS[p.id] || 'Account ID'}
value={accountDraft[p.id] || ''}
disabled={!enabled}
onChange={(e) =>
setAccountDraft((d) => ({
...d,
@@ -168,17 +255,29 @@ export default function ClientIntegrationsPanel({
/>
<button
type="button"
disabled={saving === p.id}
onClick={() => save(p.id, status)}
disabled={saving === p.id || !enabled}
onClick={() =>
void patch(p.id, {
status: status === 'disconnected' ? 'pending' : status,
externalAccountId: accountDraft[p.id] || '',
})
}
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'
'Guardar ID'
)}
</button>
</div>
{p.id === 'google_drive' && (
<p className="text-[10px] text-gray-500">
Preferible: Agencia → Conectar Drive → Re-emparejar
(asigna carpeta por nombre). Aquí puedes pegar un Folder
ID manual.
</p>
)}
</li>
);
})}