-
Notifications
You must be signed in to change notification settings - Fork 1
/
Council.jsx
148 lines (135 loc) · 4.06 KB
/
Council.jsx
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
import React, { useState, useEffect, useRef } from "react";
import io from "socket.io-client";
import globalOptions from "../global-options.json";
import FoodItem from "./FoodItem";
import Overlay from "./Overlay";
import About from "./About";
import Topics from "./Topics";
import Contact from "./Contact";
import Share from "./Share";
import ResetWarning from "./ResetWarning";
import Navbar from "./Navbar";
import Output from "./Output";
import useWindowSize from "../hooks/useWindowSize";
function Council({ options }) {
const { foods, humanName, topic } = options;
const [activeOverlay, setActiveOverlay] = useState("");
const { width: screenWidth } = useWindowSize();
const [conversation, setConversation] = useState([]); // State to store conversation updates
const [audioBuffers, setAudioBuffers] = useState([]); // To store multiple ArrayBuffers
const socketRef = useRef(null); // Using useRef to persist socket instance
const foodsContainerStyle = {
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: "70%",
display: "flex",
justifyContent: "space-around",
alignItems: "center",
};
useEffect(() => {
socketRef.current = io();
// Send initial data to start the conversation
let promptsAndOptions = {
options: {
...globalOptions,
humanName,
raiseHandPrompt: false,
neverMindPrompt: false,
},
name: "New room",
topic,
characters: foods,
};
socketRef.current.emit("start_conversation", promptsAndOptions);
// Listen for conversation text updates
socketRef.current.on("conversation_update", (message) => {
setConversation((prev) => [...prev, message]);
});
// Listen for audio updates
socketRef.current.on("audio_update", (audioData) => {
setAudioBuffers((prevBuffers) => [...prevBuffers, audioData.audio]);
});
return () => {
socketRef.current.disconnect();
};
}, []);
function displayResetWarning() {
setActiveOverlay("reset");
}
// Function to handle overlay content based on navbar clicks
const displayOverlay = (section) => {
setActiveOverlay(section); // Update state to control overlay content
};
function removeOverlay() {
setActiveOverlay("");
}
// Conditional rendering of overlay content based on activeOverlay state
const renderOverlayContent = () => {
switch (activeOverlay) {
case "about":
return <About />;
case "settings":
return (
<Topics
currentTopic={options.topic}
onReset={options.onReset}
onCancel={removeOverlay}
/>
);
case "contact":
return <Contact />;
case "share":
return <Share />;
case "reset":
return (
<ResetWarning
onReset={() => options.onReset()}
onCancel={removeOverlay}
/>
);
default:
return null; // No overlay content if no section is active
}
};
return (
<div style={{ height: "100%", width: "100%" }}>
<div className="wrapper">
{activeOverlay === "" && (
<div
className="text-container"
style={{ justifyContent: "end" }}
>
<Output
conversation={conversation}
audioBuffers={audioBuffers}
/>
</div>
)}
<div style={foodsContainerStyle}>
{foods.map((food, index) => (
<FoodItem
key={food.name}
food={food}
index={index}
total={foods.length}
screenWidth={screenWidth}
/>
))}
</div>
<Overlay isActive={activeOverlay !== ""}>
{renderOverlayContent()}
</Overlay>
<Navbar
topic={options.topic}
activeOverlay={activeOverlay}
onDisplayOverlay={displayOverlay}
onRemoveOverlay={removeOverlay}
onDisplayResetWarning={displayResetWarning}
/>
</div>
</div>
);
}
export default Council;