generated from cameronking4/VapiBlocks
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dynamic-island.tsx
151 lines (138 loc) · 4.75 KB
/
dynamic-island.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
"use client";
import React, { useEffect, useState, useRef } from "react";
import {
DynamicIslandProvider,
DynamicIsland,
DynamicContainer,
DynamicDescription,
DynamicDiv,
DynamicTitle,
useDynamicIslandSize,
SIZE_PRESETS
} from "@/components/ui/dynamic-island"; // Adjust import paths as needed
import useWebRTCAudioSession from '@/hooks/use-webrtc'; // Replace useVapi import
import { Button } from "@/components/ui/button";
import { Loader } from "lucide-react"; // Assuming you have these icons available
const DynamicIslandExample = () => {
const { handleStartStopClick: toggleCall, isSessionActive, currentVolume: volumeLevel, conversation } = useWebRTCAudioSession('alloy');
const { setSize } = useDynamicIslandSize();
const [isStartingCall, setIsStartingCall] = useState(false);
const [isEndingCall, setIsEndingCall] = useState(false);
const [timer, setTimer] = useState<NodeJS.Timeout | null>(null);
const [isListening, setIsListening] = useState(false);
const scrollRef = useRef<HTMLDivElement>(null);
const [messages, setMessages] = useState<{role: string, text: string}[]>([]);
useEffect(() => {
if (isSessionActive) {
setIsStartingCall(false);
if (volumeLevel > 0.002) {
setIsListening(false);
if (timer) {
clearTimeout(timer);
setTimer(null);
}
setSize(SIZE_PRESETS.TALL);
} else if (volumeLevel === 0 && !isListening) {
if (!timer) {
const newTimer = setTimeout(() => {
setIsListening(true);
setSize(SIZE_PRESETS.COMPACT);
}, 1500);
setTimer(newTimer);
}
}
if (scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
} else {
setIsEndingCall(false);
setIsListening(false);
if (timer) {
clearTimeout(timer);
setTimer(null);
}
setSize(SIZE_PRESETS.DEFAULT);
}
if(!isSessionActive || isListening){
setSize(SIZE_PRESETS.DEFAULT);
}
}, [isSessionActive, setSize, volumeLevel, timer]);
useEffect(() => {
if (conversation) {
setMessages(conversation);
}
}, [conversation]);
const handleDynamicIslandClick = async () => {
if (isSessionActive) {
setIsEndingCall(true);
await toggleCall();
setIsEndingCall(false);
setSize(SIZE_PRESETS.DEFAULT);
} else {
setIsStartingCall(true);
await toggleCall();
setIsStartingCall(false);
setSize(SIZE_PRESETS.DEFAULT);
}
};
const renderState = () => {
if (isStartingCall || isEndingCall) {
return (
<DynamicContainer className="flex items-center justify-center h-full w-full">
<Loader className="animate-spin h-12 w-12 text-yellow-300" />
<DynamicTitle className="ml-2 text-2xl font-black tracking-tighter text-white my-2 mr-3">
{isStartingCall ? "Starting" : "Ending"}
</DynamicTitle>
</DynamicContainer>
);
}
if (!isSessionActive) {
return (
<DynamicContainer className="flex items-center justify-center h-full w-full">
<DynamicTitle className="text-2xl font-black tracking-tighter text-white my-2">Start Call</DynamicTitle>
</DynamicContainer>
);
}
if (isListening) {
return (
<DynamicContainer className="flex flex-col items-center justify-center h-full w-full">
<DynamicTitle className="text-2xl font-black tracking-tighter text-white my-2">Listening...</DynamicTitle>
</DynamicContainer>
);
}
return (
<DynamicContainer className="flex flex-col h-full w-full px-4 py-2">
<div ref={scrollRef} className="h-full overflow-hidden">
<DynamicDiv className="flex justify-center w-full h-full">
<div className="size-full bg-cyan-300 rounded-2xl">
<DynamicDescription className="w-full h-full px-3 py-2 items-center justify-center text-center text-white text-sm leading-snug tracking-tight overflow-y-auto">
{conversation?.length > 0 &&
conversation
.filter(m => m.role === 'assistant')
.slice(-1)[0]?.text
}
</DynamicDescription>
</div>
</DynamicDiv>
</div>
</DynamicContainer>
);
};
return (
<div onClick={handleDynamicIslandClick} className="cursor-pointer">
<DynamicIsland id="vapi-dynamic-island">
{renderState()}
</DynamicIsland>
</div>
);
};
const DynamicIslandExampleWrapper = () => {
return (
<DynamicIslandProvider initialSize={SIZE_PRESETS.DEFAULT}>
<div className="h-full">
<DynamicIslandExample />
</div>
</DynamicIslandProvider>
);
};
export default DynamicIslandExampleWrapper;