279 lines
12 KiB
TypeScript
279 lines
12 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import {
|
|
Share2,
|
|
ShieldCheck,
|
|
Server,
|
|
Lock,
|
|
RefreshCw,
|
|
CheckCircle2,
|
|
AlertCircle,
|
|
TrendingUp,
|
|
DollarSign,
|
|
Key,
|
|
} from 'lucide-react';
|
|
import { toast } from 'sonner';
|
|
import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav';
|
|
import { integrationService } from '@/services/api/integrationService';
|
|
|
|
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="p-6 max-w-7xl mx-auto space-y-6">
|
|
<IntegrationsHeaderNav activeTab="meta-ads" />
|
|
|
|
{/* Top Banner */}
|
|
<div className="p-4 rounded-xl bg-indigo-50 dark:bg-indigo-950/30 border border-indigo-200 dark:border-indigo-900/50 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
|
|
<div>
|
|
<h2 className="text-base font-semibold text-indigo-900 dark:text-indigo-100 flex items-center gap-2">
|
|
<Share2 className="w-4 h-4 text-indigo-600" />
|
|
Meta Graph API & Social Ads Integration (v22.0)
|
|
</h2>
|
|
<p className="text-xs text-indigo-700 dark:text-indigo-300 mt-0.5">
|
|
Connects Facebook Ads Manager, Instagram Business Graph, lead generation webhooks, and catalog synchronization.
|
|
</p>
|
|
</div>
|
|
{creds.is_locked_by_config ? (
|
|
<span className="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200 text-xs font-semibold">
|
|
<ShieldCheck className="w-3.5 h-3.5 text-blue-600" />
|
|
Configured via Config.py
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-purple-100 dark:bg-purple-900 text-purple-800 dark:text-purple-200 text-xs font-semibold">
|
|
<Server className="w-3.5 h-3.5 text-purple-600" />
|
|
Database Configuration
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 lg:grid-cols-12 gap-6">
|
|
{/* Credentials Form (Cols 1-7) */}
|
|
<div className="lg:col-span-7 rounded-xl border border-gray-200 dark:border-gray-800 bg-white dark:bg-gray-900 p-6 shadow-sm">
|
|
<div className="flex items-center justify-between mb-4">
|
|
<h3 className="font-semibold text-gray-900 dark:text-white flex items-center gap-2">
|
|
<Key className="w-4 h-4 text-indigo-600" />
|
|
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="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
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="1208251298791396"
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 focus:ring-2 focus:ring-indigo-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
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="1307493795455160"
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 focus:ring-2 focus:ring-indigo-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
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="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 focus:ring-2 focus:ring-indigo-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
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="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 focus:ring-2 focus:ring-indigo-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
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="EAAR..."
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 focus:ring-2 focus:ring-indigo-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-3 pt-2">
|
|
<button
|
|
type="button"
|
|
onClick={handleTestToken}
|
|
disabled={testing || !creds.access_token}
|
|
className="flex-1 py-2 px-4 rounded-lg bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-200 text-xs font-semibold transition flex items-center justify-center gap-1.5"
|
|
>
|
|
<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="flex-1 py-2 px-4 rounded-lg bg-indigo-600 hover:bg-indigo-700 text-white text-xs font-semibold shadow-sm transition"
|
|
>
|
|
{saving ? 'Saving...' : 'Save Meta Credentials'}
|
|
</button>
|
|
)}
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
{/* Live Token Inspection Result (Cols 8-12) */}
|
|
<div className="lg:col-span-5 rounded-xl border border-gray-200 dark:border-gray-800 bg-white dark:bg-gray-900 p-6 shadow-sm flex flex-col justify-between">
|
|
<div>
|
|
<h3 className="font-semibold text-gray-900 dark:text-white flex items-center gap-2 mb-2">
|
|
<CheckCircle2 className="w-4 h-4 text-indigo-600" />
|
|
Meta Token Validation Status
|
|
</h3>
|
|
<p className="text-xs text-gray-500 dark:text-gray-400 mb-4">
|
|
Real-time inspection of token permissions, authenticated user profile, and linked Ad Account.
|
|
</p>
|
|
|
|
{testResult ? (
|
|
<div className="space-y-3">
|
|
<div className="p-3 rounded-lg bg-indigo-50 dark:bg-indigo-950/40 border border-indigo-100 dark:border-indigo-900/30">
|
|
<span className="text-[11px] font-semibold text-indigo-800 dark:text-indigo-300 uppercase">
|
|
Authenticated User
|
|
</span>
|
|
<p className="text-sm font-bold text-gray-900 dark:text-white mt-0.5">
|
|
{testResult.user?.name || 'Verified App User'}
|
|
</p>
|
|
<p className="text-xs font-mono text-gray-500">ID: {testResult.user?.id}</p>
|
|
</div>
|
|
|
|
{testResult.ad_account && (
|
|
<div className="p-3 rounded-lg bg-gray-50 dark:bg-gray-800 border border-gray-200 dark:border-gray-700 space-y-1 text-xs">
|
|
<span className="font-semibold text-gray-700 dark:text-gray-300 uppercase">
|
|
Ad Account Details
|
|
</span>
|
|
<p className="font-medium text-gray-900 dark:text-white">{testResult.ad_account.name}</p>
|
|
<div className="flex justify-between text-gray-600 dark:text-gray-400">
|
|
<span>Status:</span>
|
|
<span className="font-semibold text-emerald-600">Active</span>
|
|
</div>
|
|
<div className="flex justify-between text-gray-600 dark:text-gray-400">
|
|
<span>Currency:</span>
|
|
<span className="font-mono">{testResult.ad_account.currency || 'INR'}</span>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<div className="p-6 border-2 border-dashed border-gray-200 dark:border-gray-800 rounded-xl text-center space-y-2">
|
|
<Share2 className="w-8 h-8 text-indigo-400 mx-auto" />
|
|
<p className="text-xs text-gray-500">
|
|
Click "Test Graph API Token" to perform live handshake with Meta Graph API v22.0.
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="mt-6 pt-4 border-t border-gray-100 dark:border-gray-800 text-xs text-gray-500">
|
|
Graph Base: <code className="font-mono text-indigo-600">https://graph.facebook.com/v22.0</code>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|