File size: 5,785 Bytes
a995c58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Call Dashboard</title>
   <script src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
   <script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
   <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
   <script src="https://unpkg.com/recharts/umd/Recharts.js"></script>
</head>

<body>
   <div id="root"></div>

   <script type="text/babel">
       const { useState, useEffect } = React;
       const { BarChart, Bar, XAxis, YAxis, Tooltip, Legend, ResponsiveContainer } = Recharts;

       const CallDashboard = () => {
           const [calls, setCalls] = useState([]);
           const [stats, setStats] = useState({ incoming: 0, outgoing: 0, answered: 0, voicemail: 0, missed: 0 });
           const [loading, setLoading] = useState(true);
           const [error, setError] = useState(null);

           useEffect(() => {
               const fetchData = async () => {
                   try {
                       setLoading(true);
                       const response = await fetch('YOUR_VAPI_ENDPOINT');
                       if (!response.ok) {
                           throw new Error('Failed to fetch data from VAPI');
                       }
                       const data = await response.json();
                       setCalls(data);
                       updateStats(data);
                   } catch (err) {
                       setError(err.message);
                   } finally {
                       setLoading(false);
                   }
               };

               fetchData();

               // Set up polling for real-time updates (every 30 seconds)
               const interval = setInterval(fetchData, 30000);
               return () => clearInterval(interval);
           }, []);

           const updateStats = (callData) => {
               const newStats = callData.reduce((acc, call) => {
                   acc[call.direction]++;
                   acc[call.outcome]++;
                   return acc;
               }, { incoming: 0, outgoing: 0, answered: 0, voicemail: 0, missed: 0 });
               setStats(newStats);
           };

           const chartData = [
               { name: 'Incoming', value: stats.incoming },
               { name: 'Outgoing', value: stats.outgoing },
               { name: 'Answered', value: stats.answered },
               { name: 'Voicemail', value: stats.voicemail },
               { name: 'Missed', value: stats.missed },
           ];

           if (loading) {
               return <div>Loading...</div>;
           }

           if (error) {
               return <div>Error: {error}</div>;
           }

           return (
               <div style={{ fontFamily: 'Arial, sans-serif', maxWidth: '800px', margin: '0 auto', padding: '20px' }}>
                   <h1 style={{ textAlign: 'center', color: '#333' }}>Call Dashboard</h1>

                   <div style={{ marginBottom: '20px' }}>
                       <h2 style={{ color: '#666' }}>Call Statistics</h2>
                       <ResponsiveContainer width="100%" height={300}>
                           <BarChart data={chartData}>
                               <XAxis dataKey="name" />
                               <YAxis />
                               <Tooltip />
                               <Legend />
                               <Bar dataKey="value" fill="#8884d8" />
                           </BarChart>
                       </ResponsiveContainer>
                   </div>

                   <div>
                       <h2 style={{ color: '#666' }}>Recent Calls</h2>
                       <table style={{ width: '100%', borderCollapse: 'collapse' }}>
                           <thead>
                               <tr style={{ backgroundColor: '#f2f2f2' }}>
                                   <th style={{ padding: '10px', textAlign: 'left', border: '1px solid #ddd' }}>Phone Number</th>
                                   <th style={{ padding: '10px', textAlign: 'left', border: '1px solid #ddd' }}>Direction</th>
                                   <th style={{ padding: '10px', textAlign: 'left', border: '1px solid #ddd' }}>Outcome</th>
                                   <th style={{ padding: '10px', textAlign: 'left', border: '1px solid #ddd' }}>Audio</th>
                               </tr>
                           </thead>
                           <tbody>
                               {calls.map((call) => (
                                   <tr key={call.id}>
                                       <td style={{ padding: '10px', border: '1px solid #ddd' }}>{call.phoneNumber}</td>
                                       <td style={{ padding: '10px', border: '1px solid #ddd' }}>{call.direction}</td>
                                       <td style={{ padding: '10px', border: '1px solid #ddd' }}>{call.outcome}</td>
                                       <td style={{ padding: '10px', border: '1px solid #ddd' }}>
                                           <a href={call.audioUrl} target="_blank" rel="noopener noreferrer" style={{ color: '#0066cc', textDecoration: 'none' }}>
                                               Listen
                                           </a>
                                       </td>
                                   </tr>
                               ))}
                           </tbody>
                       </table>
                   </div>
               </div>
           );
       };

       ReactDOM.render(<CallDashboard />, document.getElementById('root'));
   </script>
</body>
</html>