ifixkart-admin/app/(admin)/integrations/page.tsx

185 lines
6.8 KiB
TypeScript

'use client';
import React, { useState, useEffect } from 'react';
import Link from 'next/link';
import {
MessageSquare,
Mail,
BarChart3,
Share2,
PhoneCall,
Truck,
RefreshCw,
ArrowRight,
} from 'lucide-react';
import { toast } from 'sonner';
import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav';
import { integrationService, IntegrationOverview } from '@/services/api/integrationService';
export default function IntegrationsOverviewPage() {
const [data, setData] = useState<IntegrationOverview | null>(null);
const [loading, setLoading] = useState<boolean>(true);
const [refreshing, setRefreshing] = useState<boolean>(false);
const fetchOverview = async (showToast = false) => {
try {
if (showToast) setRefreshing(true);
const res = await integrationService.getOverview();
setData(res);
if (showToast) toast.success('Integration states refreshed successfully');
} catch (err: any) {
toast.error(err.message || 'Failed to fetch integrations overview');
} finally {
setLoading(false);
setRefreshing(false);
}
};
useEffect(() => {
fetchOverview();
}, []);
const sourceLabel = (source?: string) => {
if (source === 'config.py') return 'Config';
if (source === 'database') return 'Database';
return 'Unconfigured';
};
const statusClass = (status: string) => {
const value = status.toUpperCase();
if (value === 'CONNECTED' || value === 'QR_READY' || value === 'AUTHENTICATED') return 'bg-success/15 text-success';
return 'bg-warning/15 text-warning';
};
const shell =
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0';
const cards = data
? [
{
title: 'Internal WhatsApp',
icon: MessageSquare,
status: data.whatsapp_internal.status,
source: data.whatsapp_internal.source,
detail: data.whatsapp_internal.phone
? `+${data.whatsapp_internal.phone}${data.whatsapp_internal.push_name ? ` · ${data.whatsapp_internal.push_name}` : ''}`
: '',
href: '/integrations/whatsapp',
action: 'Open',
},
{
title: 'Customer WhatsApp',
icon: MessageSquare,
status: data.whatsapp_customer.status,
source: data.whatsapp_customer.source,
detail: data.whatsapp_customer.endpoint || '',
href: '/integrations/whatsapp?tab=wati',
action: 'Open',
},
{
title: 'Email',
icon: Mail,
status: data.email.status,
source: data.email.source,
detail: [data.email.provider, data.email.sender].filter(Boolean).join(' · '),
href: '/integrations/email',
action: 'Open',
},
{
title: 'Google Analytics',
icon: BarChart3,
status: data.google_analytics.status,
source: data.google_analytics.source,
detail: data.google_analytics.property_id ? `Property ${data.google_analytics.property_id}` : '',
href: '/integrations/google-analytics',
action: 'Open',
},
{
title: 'Meta ads',
icon: Share2,
status: data.meta_ads.status,
source: data.meta_ads.source,
detail: data.meta_ads.ad_account_id || '',
href: '/integrations/meta-ads',
action: 'Open',
},
{
title: 'Knowlarity',
icon: PhoneCall,
status: data.knowlarity.status,
source: data.knowlarity.source,
detail: data.knowlarity.virtual_number || '',
href: '/integrations/call',
action: 'Open',
},
{
title: 'Shiprocket',
icon: Truck,
status: data.shiprocket.status,
source: data.shiprocket.source,
detail: data.shiprocket.pickup_location || '',
href: '/integrations/shipping',
action: 'Open',
},
]
: [];
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">Integrations</h1>
<button
type="button"
onClick={() => fetchOverview(true)}
disabled={refreshing}
className="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"
>
<RefreshCw className={`w-3.5 h-3.5 ${refreshing ? 'animate-spin' : ''}`} />
Refresh
</button>
</div>
<IntegrationsHeaderNav activeTab="overview" />
{loading ? (
<div className={`${shell} py-16 flex justify-center`}>
<RefreshCw className="w-5 h-5 text-primary animate-spin" />
</div>
) : cards.length === 0 ? (
<div className={`${shell} py-16 text-center text-[13px] text-muted-foreground`}>No integrations yet</div>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
{cards.map((card) => {
const Icon = card.icon;
return (
<div key={card.title} className={`${shell} p-4 flex flex-col gap-4`}>
<div className="flex items-start justify-between gap-3">
<div className="flex items-center gap-2 min-w-0">
<span className="w-9 h-9 rounded-full bg-primary/10 text-primary flex items-center justify-center shrink-0">
<Icon className="w-4 h-4" />
</span>
<div className="min-w-0">
<h2 className="text-[13px] font-semibold text-foreground">{card.title}</h2>
{card.detail && <p className="text-[12px] text-muted-foreground mt-0.5 truncate">{card.detail}</p>}
</div>
</div>
<span className={`inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold shrink-0 ${statusClass(card.status)}`}>
{card.status}
</span>
</div>
<div className="flex items-center justify-between gap-2 border-t border-border pt-3">
<span className="inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold bg-muted text-foreground">
{sourceLabel(card.source)}
</span>
<Link href={card.href} className="inline-flex items-center gap-1 text-[13px] font-semibold text-primary">
{card.action}
<ArrowRight className="w-3.5 h-3.5" />
</Link>
</div>
</div>
);
})}
</div>
)}
</div>
);
}