'use client'; import React, { useState, useEffect } from 'react'; import { PhoneCall, RefreshCw, PhoneForwarded, History, Play, 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 KnowlarityCallIntegrationPage() { const [creds, setCreds] = useState({ 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([]); const [logsLoading, setLogsLoading] = useState(false); const [logsError, setLogsError] = 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); setLogsError(false); 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); setLogsError(true); } 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 (

Knowlarity

{creds.is_locked_by_config ? 'Config' : 'Database'}
{/* Credentials Form (Cols 1-7) */}

SuperReceptionist Credentials

setCreds({ ...creds, virtual_number: e.target.value })} placeholder="Number" className={fieldClass} />
setCreds({ ...creds, api_key: e.target.value })} placeholder="Knowlarity x-api-key" className={fieldClass} />
setCreds({ ...creds, authorization: e.target.value })} placeholder="Knowlarity Authorization" className={fieldClass} />
setCreds({ ...creds, call_url: e.target.value })} className={fieldClass} />
setCreds({ ...creds, log_url: e.target.value })} className={fieldClass} />
{!creds.is_locked_by_config && ( )}
{/* Live Click-to-Call Dialer (Cols 8-12) */}

Agent Click-to-Call Bridge

setAgentNumber(e.target.value)} className={fieldClass} />
setCustomerNumber(e.target.value)} className={fieldClass} />
Virtual Number: {creds.virtual_number || '—'}
{/* Call Logs Table */}

Telephony Call Logs & Recordings

{logsLoading ? ( ) : logsError ? ( ) : logs.length === 0 ? ( ) : ( logs.map((log: any, idx: number) => ( )) )}
Date / Time Customer Agent Status Duration Recording
Call logs could not be loaded.{' '}
No call logs recorded yet.
{log.start_time || log.call_date || '—'} {log.customer_number || log.caller_id || '-'} {log.agent_number || log.destination || '-'} {log.status || log.call_type || '—'} {log.duration ? `${log.duration}s` : '—'} {log.recording_url ? ( Audio ) : ( - )}
); }