generated from cameronking4/VapiBlocks
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbigblock.tsx
213 lines (176 loc) · 5.99 KB
/
bigblock.tsx
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// VapiDemoComponent.tsx
"use client";
import React, { useEffect, useRef, useState } from "react";
import useVapi from "@/hooks/code-assistant";
import SandpackComponent from "@/components/code-preview";
import { MicOff, Mic } from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion';
interface LayoutProps {
children: React.ReactNode;
}
const Layout: React.FC<LayoutProps> = ({ children }) => {
return (
<div className="flex flex-col items-center p-4 min-h-full w-full">
<div className="w-full shadow-lg rounded-lg overflow-hidden">
{children}
</div>
</div>
);
};
interface TranscriberProps {
conversation: Array<{ role: string; text: string; timestamp: string }>;
}
const Transcriber: React.FC<TranscriberProps> = ({ conversation }) => {
return (
<div className="p-4 border-t-2 mt-4">
<h2 className="text-lg font-semibold">Conversation</h2>
<div className="max-h-64 overflow-y-auto">
{conversation.map((msg, index) => (
<div key={index} className="mt-2 text-lg">
<strong>{msg.role}:</strong> {msg.text} <span className="text-gray-500 text-xs">{msg.timestamp}</span>
</div>
))}
</div>
</div>
);
};
const AudioVisualizer: React.FC<{ audioData: Uint8Array; isSessionActive: boolean }> = ({ audioData, isSessionActive }) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
if (isSessionActive) {
const canvas = canvasRef.current;
const context = canvas?.getContext('2d');
if (canvas && context) {
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
}
draw();
}
}, [audioData, isSessionActive]);
const draw = () => {
const canvas = canvasRef.current;
if (!canvas) return;
const context = canvas.getContext('2d');
if (!context) return;
const { width, height } = canvas;
context.clearRect(0, 0, width, height);
const sliceWidth = (width / (audioData.length - 1)) * 2;
const centerY = height / 2;
context.lineWidth = 2;
context.strokeStyle = '#9E9E9E';
context.beginPath();
let prevX = 0;
let prevY = centerY;
context.moveTo(prevX, prevY);
for (let i = 0; i < audioData.length; i++) {
const avgValue = (audioData[i] + audioData[Math.max(0, i - 1)]) / 2; // Averaging current and previous data points
const v = avgValue / 255.0;
const y = centerY + (v - 0.5) * height;
const x = i * sliceWidth;
context.bezierCurveTo((prevX + x) / 2, prevY, (prevX + x) / 2, y, x, y);
prevX = x;
prevY = y;
}
context.stroke();
};
return (
<motion.canvas
ref={canvasRef}
className="w-full h-full"
initial={{ opacity: 0 }}
animate={{ opacity: isSessionActive ? 1 : 0 }}
transition={{ duration: 0.5 }}
/>
);
};
const AudioAnalyzer: React.FC<{ volumeLevel: number; isSessionActive: boolean }> = ({ volumeLevel, isSessionActive }) => {
const [audioData, setAudioData] = useState<Uint8Array>(new Uint8Array(128));
useEffect(() => {
if (!isSessionActive) {
setAudioData(new Uint8Array(128));
return;
}
const updateAudioData = () => {
const dataArray = new Uint8Array(128);
for (let i = 0; i < dataArray.length; i++) {
const variability = (Math.random() - 0.5) * 0.5; // Reduced variability for less noise
dataArray[i] = Math.min(Math.max(128 + volumeLevel * variability * 128, 0), 255);
}
setAudioData(dataArray);
};
const tick = () => {
updateAudioData();
animationFrameId = requestAnimationFrame(tick);
};
let animationFrameId = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(animationFrameId);
};
}, [volumeLevel, isSessionActive]);
return <AudioVisualizer audioData={audioData} isSessionActive={isSessionActive} />;
};
const MinimalComponent: React.FC = () => {
const { volumeLevel, isSessionActive, toggleCall } = useVapi();
const [showVisualizer, setShowVisualizer] = useState(false);
const handleToggleCall = () => {
toggleCall();
setShowVisualizer(!isSessionActive);
};
return (
<div className="flex flex-col items-center justify-center h-full">
<div className="flex items-center justify-center">
<motion.button
key="callButton"
onClick={handleToggleCall}
className="p-2 rounded-xl bg-secondary"
whileTap={{ scale: 0.9 }}
whileHover={{ scale: 1.1 }}
initial={{ x: 0 }}
animate={{ x: showVisualizer ? -10 : 0 }}
transition={{ duration: 0.3 }}
style={{ zIndex: 10, position: 'relative' }}
>
{isSessionActive ? <MicOff size={20} /> : <Mic size={20} />}
</motion.button>
<AnimatePresence>
{showVisualizer && (
<motion.div
className="rounded-4xl"
initial={{ width: 0, opacity: 0 }}
animate={{ width: '100%', opacity: 1 }}
exit={{ width: 0, opacity: 0 }}
transition={{ duration: 0.3 }}
style={{ marginLeft: '10px' }}
>
<AudioAnalyzer volumeLevel={volumeLevel} isSessionActive={isSessionActive} />
</motion.div>
)}
</AnimatePresence>
</div>
</div>
);
};
const VapiDemoComponent: React.FC = () => {
const { volumeLevel, isSessionActive, conversation, toggleCall, code } = useVapi();
const [showSandpack, setShowSandpack] = useState(true);
const handleToggleSandpack = () => {
setShowSandpack((prev) => !prev);
};
return (
<Layout>
<div className="flex justify-between items-center p-4">
<h1 className="text-xl font-bold">Coding Assistant</h1>
<div>
<MinimalComponent />
</div>
</div>
{code && (
<div className="mt-4">
<SandpackComponent code={code} />
</div>
)}
<Transcriber conversation={conversation} />
</Layout>
);
};
export default VapiDemoComponent;