258 lines
10 KiB
TypeScript
258 lines
10 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import { RefreshCw, CheckCircle2, Key } from 'lucide-react';
|
|
import { toast } from 'sonner';
|
|
import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav';
|
|
import { integrationService } from '@/services/api/integrationService';
|
|
|
|
const fieldClass =
|
|
'w-full h-9 px-3 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary disabled:opacity-60';
|
|
const areaClass =
|
|
'w-full px-3 py-2 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary disabled:opacity-60';
|
|
const primaryButton =
|
|
'inline-flex items-center justify-center gap-1.5 h-9 px-3 crm-radius-control bg-primary hover:bg-primary/90 text-white text-[13px] font-semibold cursor-pointer disabled:opacity-50';
|
|
const secondaryButton =
|
|
'inline-flex items-center justify-center gap-1.5 h-9 px-3 crm-radius-control border border-border bg-card text-foreground text-[13px] font-medium hover:bg-muted cursor-pointer disabled:opacity-50';
|
|
const shell =
|
|
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0';
|
|
const labelClass = 'block text-[11px] font-semibold text-muted-foreground uppercase mb-1.5';
|
|
|
|
export default function MetaAdsIntegrationPage() {
|
|
const [creds, setCreds] = useState<any>({
|
|
app_id: '',
|
|
ad_account_id: '',
|
|
page_id: '',
|
|
instagram_id: '',
|
|
access_token: '',
|
|
source: 'unconfigured',
|
|
is_locked_by_config: false,
|
|
});
|
|
const [loading, setLoading] = useState(true);
|
|
const [saving, setSaving] = useState(false);
|
|
const [testing, setTesting] = useState(false);
|
|
const [testResult, setTestResult] = useState<any>(null);
|
|
|
|
const fetchMetaConfig = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await integrationService.getMetaCredentials();
|
|
setCreds(data);
|
|
} catch (err: any) {
|
|
toast.error('Failed to load Meta credentials');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchMetaConfig();
|
|
}, []);
|
|
|
|
const handleSave = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setSaving(true);
|
|
try {
|
|
await integrationService.saveMetaCredentials(creds);
|
|
toast.success('Meta credentials saved successfully');
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to save Meta credentials');
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
const handleTestToken = async () => {
|
|
if (!creds.access_token) {
|
|
toast.error('No Meta access token configured to test');
|
|
return;
|
|
}
|
|
setTesting(true);
|
|
try {
|
|
const res = await integrationService.testMetaConnection({
|
|
access_token: creds.access_token,
|
|
ad_account_id: creds.ad_account_id,
|
|
});
|
|
if (res.status === 'SUCCESS') {
|
|
toast.success(`Meta API Validated! Connected as: ${res.user?.name || 'Verified User'}`);
|
|
setTestResult(res);
|
|
} else {
|
|
toast.error(`Token verification failed: ${res.detail || 'Invalid token'}`);
|
|
}
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Meta API verification failed');
|
|
} finally {
|
|
setTesting(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-5 min-w-0">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<h1 className="text-lg font-semibold text-foreground">Meta</h1>
|
|
<span className={`inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold ${creds.is_locked_by_config ? 'bg-primary/10 text-primary' : 'bg-muted text-foreground'}`}>
|
|
{creds.is_locked_by_config ? 'Config' : 'Database'}
|
|
</span>
|
|
</div>
|
|
<IntegrationsHeaderNav activeTab="meta-ads" />
|
|
|
|
<div className="grid grid-cols-1 lg:grid-cols-12 gap-6">
|
|
{/* Credentials Form (Cols 1-7) */}
|
|
<div className={`${shell} p-4 lg:col-span-7`}>
|
|
<div className="flex items-center justify-between mb-4">
|
|
<h3 className="font-semibold text-foreground flex items-center gap-2">
|
|
<Key className="w-4 h-4 text-primary" />
|
|
Meta App & Ad Account Coordinates
|
|
</h3>
|
|
</div>
|
|
|
|
<form onSubmit={handleSave} className="space-y-4">
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className={labelClass}>
|
|
Meta App ID
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.app_id || ''}
|
|
onChange={(e) => setCreds({ ...creds, app_id: e.target.value })}
|
|
placeholder="App ID"
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>
|
|
Meta Ad Account ID
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.ad_account_id || ''}
|
|
onChange={(e) => setCreds({ ...creds, ad_account_id: e.target.value })}
|
|
placeholder="Ad account ID"
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className={labelClass}>
|
|
Facebook Page ID
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.page_id || ''}
|
|
onChange={(e) => setCreds({ ...creds, page_id: e.target.value })}
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>
|
|
Instagram Account ID
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.instagram_id || ''}
|
|
onChange={(e) => setCreds({ ...creds, instagram_id: e.target.value })}
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>
|
|
Graph API System Access Token (v22.0)
|
|
</label>
|
|
<input
|
|
type="password"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.access_token || ''}
|
|
onChange={(e) => setCreds({ ...creds, access_token: e.target.value })}
|
|
placeholder="Token"
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-3 pt-2">
|
|
<button
|
|
type="button"
|
|
onClick={handleTestToken}
|
|
disabled={testing || !creds.access_token}
|
|
className={`${secondaryButton} flex-1`}
|
|
>
|
|
<RefreshCw className={`w-3.5 h-3.5 ${testing ? 'animate-spin' : ''}`} />
|
|
{testing ? 'Validating Token...' : 'Test Graph API Token'}
|
|
</button>
|
|
{!creds.is_locked_by_config && (
|
|
<button
|
|
type="submit"
|
|
disabled={saving}
|
|
className={`${primaryButton} flex-1`}
|
|
>
|
|
{saving ? 'Saving...' : 'Save Meta Credentials'}
|
|
</button>
|
|
)}
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
{/* Live Token Inspection Result (Cols 8-12) */}
|
|
<div className={`${shell} p-4 lg:col-span-5 flex flex-col justify-between`}>
|
|
<div>
|
|
<h3 className="font-semibold text-foreground flex items-center gap-2 mb-4">
|
|
<CheckCircle2 className="w-4 h-4 text-primary" />
|
|
Token status
|
|
</h3>
|
|
|
|
{testResult ? (
|
|
<div className="space-y-3">
|
|
<div className="p-3 crm-radius-control bg-muted/40 border border-border">
|
|
<span className="text-[11px] font-semibold text-muted-foreground uppercase">
|
|
User
|
|
</span>
|
|
<p className="text-[13px] font-semibold text-foreground mt-0.5">
|
|
{testResult.user?.name || '—'}
|
|
</p>
|
|
{testResult.user?.id && (
|
|
<p className="text-[12px] font-mono text-muted-foreground">{testResult.user.id}</p>
|
|
)}
|
|
</div>
|
|
|
|
{testResult.ad_account && (
|
|
<div className="p-3 crm-radius-control bg-muted/40 border border-border space-y-1 text-[12px]">
|
|
<span className="font-semibold text-foreground uppercase">
|
|
Ad account
|
|
</span>
|
|
<p className="font-medium text-foreground">{testResult.ad_account.name || '—'}</p>
|
|
<div className="flex justify-between text-muted-foreground">
|
|
<span>Status</span>
|
|
<span className="font-semibold text-foreground">{testResult.ad_account.account_status || testResult.ad_account.status || '—'}</span>
|
|
</div>
|
|
<div className="flex justify-between text-muted-foreground">
|
|
<span>Currency</span>
|
|
<span className="font-mono">{testResult.ad_account.currency || '—'}</span>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<div className="py-10 text-center text-[13px] text-muted-foreground">
|
|
Run a token test to see the Graph API response.
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="mt-6 pt-4 border-t border-border text-[12px] text-muted-foreground">
|
|
https://graph.facebook.com/v22.0
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|