374 lines
16 KiB
TypeScript
374 lines
16 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import {
|
|
PhoneCall,
|
|
ShieldCheck,
|
|
Server,
|
|
Lock,
|
|
RefreshCw,
|
|
CheckCircle2,
|
|
AlertCircle,
|
|
PhoneForwarded,
|
|
Clock,
|
|
History,
|
|
Play,
|
|
Key,
|
|
} from 'lucide-react';
|
|
import { toast } from 'sonner';
|
|
import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav';
|
|
import { integrationService } from '@/services/api/integrationService';
|
|
|
|
export default function KnowlarityCallIntegrationPage() {
|
|
const [creds, setCreds] = useState<any>({
|
|
api_key: '',
|
|
authorization: '',
|
|
virtual_number: '',
|
|
call_url: 'https://kpi.knowlarity.com/Basic/v1/account/call/makecall',
|
|
log_url: 'https://kpi.knowlarity.com/Basic/v1/account/calllog',
|
|
source: 'unconfigured',
|
|
is_locked_by_config: false,
|
|
});
|
|
const [loading, setLoading] = useState(true);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
// Click to call form state
|
|
const [agentNumber, setAgentNumber] = useState('');
|
|
const [customerNumber, setCustomerNumber] = useState('');
|
|
const [calling, setCalling] = useState(false);
|
|
|
|
// Call logs table
|
|
const [logs, setLogs] = useState<any[]>([]);
|
|
const [logsLoading, setLogsLoading] = useState(false);
|
|
const [page, setPage] = useState(1);
|
|
|
|
const fetchKnowlarityConfig = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await integrationService.getKnowlarityCredentials();
|
|
setCreds(data);
|
|
if (data.api_key || data.authorization) {
|
|
fetchLogs();
|
|
}
|
|
} catch (err: any) {
|
|
toast.error('Failed to load Knowlarity settings');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const fetchLogs = async () => {
|
|
setLogsLoading(true);
|
|
try {
|
|
const res = await integrationService.getKnowlarityCallLogs(page, 20);
|
|
const callData = res.data?.call_logs || res.data?.objects || res.call_logs || [];
|
|
setLogs(callData);
|
|
} catch (err) {
|
|
console.warn('Call logs error:', err);
|
|
} finally {
|
|
setLogsLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchKnowlarityConfig();
|
|
}, []);
|
|
|
|
const handleSave = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setSaving(true);
|
|
try {
|
|
await integrationService.saveKnowlarityCredentials(creds);
|
|
toast.success('Knowlarity credentials saved successfully');
|
|
fetchLogs();
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to save Knowlarity credentials');
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
const handleMakeCall = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!agentNumber.trim() || !customerNumber.trim()) {
|
|
toast.error('Both Agent number and Customer number are required for bridging');
|
|
return;
|
|
}
|
|
setCalling(true);
|
|
try {
|
|
const res = await integrationService.makeKnowlarityCall({
|
|
agent_number: agentNumber.trim(),
|
|
customer_number: customerNumber.trim(),
|
|
});
|
|
if (res.status === 'SUCCESS') {
|
|
toast.success('Call initiated! Knowlarity is ringing the agent first, then connecting customer.');
|
|
fetchLogs();
|
|
} else {
|
|
toast.error(`Call failed: ${res.detail || JSON.stringify(res)}`);
|
|
}
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Call initiation failed');
|
|
} finally {
|
|
setCalling(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="p-6 max-w-7xl mx-auto space-y-6">
|
|
<IntegrationsHeaderNav activeTab="call" />
|
|
|
|
{/* Top Banner */}
|
|
<div className="p-4 rounded-xl bg-rose-50 dark:bg-rose-950/30 border border-rose-200 dark:border-rose-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-rose-900 dark:text-rose-100 flex items-center gap-2">
|
|
<PhoneCall className="w-4 h-4 text-rose-600" />
|
|
Knowlarity Cloud Telephony & SuperReceptionist IVR
|
|
</h2>
|
|
<p className="text-xs text-rose-700 dark:text-rose-300 mt-0.5">
|
|
Cloud IVR call routing, technician click-to-call bridging, customer service recordings, and live call logs.
|
|
</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-rose-600" />
|
|
SuperReceptionist Credentials
|
|
</h3>
|
|
</div>
|
|
|
|
<form onSubmit={handleSave} className="space-y-4">
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Virtual SR Number (Caller ID)
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.virtual_number || ''}
|
|
onChange={(e) => setCreds({ ...creds, virtual_number: e.target.value })}
|
|
placeholder="e.g. +91XXXXXXXXXX"
|
|
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-rose-500 focus:outline-none"
|
|
/>
|
|
</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">
|
|
x-api-key
|
|
</label>
|
|
<input
|
|
type="password"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.api_key || ''}
|
|
onChange={(e) => setCreds({ ...creds, api_key: e.target.value })}
|
|
placeholder="Knowlarity x-api-key"
|
|
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-rose-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Authorization Token
|
|
</label>
|
|
<input
|
|
type="password"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.authorization || ''}
|
|
onChange={(e) => setCreds({ ...creds, authorization: e.target.value })}
|
|
placeholder="Knowlarity Authorization"
|
|
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-rose-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">
|
|
MakeCall API Endpoint
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.call_url || 'https://kpi.knowlarity.com/Basic/v1/account/call/makecall'}
|
|
onChange={(e) => setCreds({ ...creds, call_url: 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"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
CallLog API Endpoint
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.log_url || 'https://kpi.knowlarity.com/Basic/v1/account/calllog'}
|
|
onChange={(e) => setCreds({ ...creds, log_url: 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"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{!creds.is_locked_by_config && (
|
|
<button
|
|
type="submit"
|
|
disabled={saving}
|
|
className="w-full py-2.5 px-4 rounded-lg bg-rose-600 hover:bg-rose-700 text-white text-xs font-semibold shadow-sm transition"
|
|
>
|
|
{saving ? 'Saving...' : 'Save Knowlarity Settings'}
|
|
</button>
|
|
)}
|
|
</form>
|
|
</div>
|
|
|
|
{/* Live Click-to-Call Dialer (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">
|
|
<PhoneForwarded className="w-4 h-4 text-rose-600" />
|
|
Agent Click-to-Call Bridge
|
|
</h3>
|
|
<p className="text-xs text-gray-500 dark:text-gray-400 mb-4">
|
|
Connect a technician or sales rep with a customer through Knowlarity without revealing personal numbers.
|
|
</p>
|
|
|
|
<form onSubmit={handleMakeCall} className="space-y-4">
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Agent Mobile Number (Rings First)
|
|
</label>
|
|
<input
|
|
type="text"
|
|
placeholder="e.g. 919876543210"
|
|
value={agentNumber}
|
|
onChange={(e) => setAgentNumber(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 focus:ring-2 focus:ring-rose-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Customer Mobile Number (Rings Second)
|
|
</label>
|
|
<input
|
|
type="text"
|
|
placeholder="e.g. 919123456789"
|
|
value={customerNumber}
|
|
onChange={(e) => setCustomerNumber(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 focus:ring-2 focus:ring-rose-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={calling}
|
|
className="w-full py-2.5 px-4 rounded-lg bg-rose-600 hover:bg-rose-700 text-white text-xs font-semibold shadow-sm transition flex items-center justify-center gap-2"
|
|
>
|
|
<PhoneCall className={`w-3.5 h-3.5 ${calling ? 'animate-bounce' : ''}`} />
|
|
{calling ? 'Bridging Call...' : 'Initiate Call Bridge'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
<div className="mt-6 pt-4 border-t border-gray-100 dark:border-gray-800 text-xs text-gray-500 flex items-center justify-between">
|
|
<span>Virtual Number:</span>
|
|
<span className="font-mono text-rose-600 font-semibold">{creds.virtual_number || 'SR Number'}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Call Logs Table */}
|
|
<div className="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">
|
|
<History className="w-4 h-4 text-rose-600" />
|
|
Telephony Call Logs & Recordings
|
|
</h3>
|
|
<button
|
|
onClick={fetchLogs}
|
|
disabled={logsLoading}
|
|
className="px-3 py-1.5 text-xs font-medium rounded-lg bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-200 transition flex items-center gap-1.5"
|
|
>
|
|
<RefreshCw className={`w-3.5 h-3.5 ${logsLoading ? 'animate-spin' : ''}`} />
|
|
Refresh Logs
|
|
</button>
|
|
</div>
|
|
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-left text-xs">
|
|
<thead className="bg-gray-50 dark:bg-gray-800/50 text-gray-500 uppercase font-medium border-y border-gray-100 dark:border-gray-800">
|
|
<tr>
|
|
<th className="py-2.5 px-3">Date / Time</th>
|
|
<th className="py-2.5 px-3">Customer</th>
|
|
<th className="py-2.5 px-3">Agent</th>
|
|
<th className="py-2.5 px-3">Status</th>
|
|
<th className="py-2.5 px-3">Duration</th>
|
|
<th className="py-2.5 px-3">Recording</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-100 dark:divide-gray-800">
|
|
{logs.length === 0 ? (
|
|
<tr>
|
|
<td colSpan={6} className="py-8 text-center text-gray-400">
|
|
{logsLoading ? 'Fetching call logs from Knowlarity...' : 'No call logs recorded yet.'}
|
|
</td>
|
|
</tr>
|
|
) : (
|
|
logs.map((log: any, idx: number) => (
|
|
<tr key={idx} className="hover:bg-gray-50 dark:hover:bg-gray-800/50">
|
|
<td className="py-2.5 px-3 font-mono text-gray-600 dark:text-gray-400">
|
|
{log.start_time || log.call_date || 'Recent'}
|
|
</td>
|
|
<td className="py-2.5 px-3 font-medium text-gray-900 dark:text-white">
|
|
{log.customer_number || log.caller_id || '-'}
|
|
</td>
|
|
<td className="py-2.5 px-3 text-gray-600 dark:text-gray-300">
|
|
{log.agent_number || log.destination || '-'}
|
|
</td>
|
|
<td className="py-2.5 px-3">
|
|
<span className="inline-flex px-2 py-0.5 rounded-full text-[10px] font-semibold bg-emerald-100 dark:bg-emerald-950 text-emerald-800 dark:text-emerald-300">
|
|
{log.status || log.call_type || 'COMPLETED'}
|
|
</span>
|
|
</td>
|
|
<td className="py-2.5 px-3 font-mono text-gray-600 dark:text-gray-400">
|
|
{log.duration ? `${log.duration}s` : '0s'}
|
|
</td>
|
|
<td className="py-2.5 px-3">
|
|
{log.recording_url ? (
|
|
<a
|
|
href={log.recording_url}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="inline-flex items-center gap-1 text-rose-600 hover:text-rose-700 font-semibold"
|
|
>
|
|
<Play className="w-3 h-3" /> Audio
|
|
</a>
|
|
) : (
|
|
<span className="text-gray-400">-</span>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|