Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max index limit to messages to avoid replaying interjection replies #12

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions client/src/components/Output.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Output({
const [currentTextMessage, setCurrentTextMessage] = useState(null);
const [currentAudioMessage, setCurrentAudioMessage] = useState(null);
const [stopAudio, setStopAudio] = useState(false);
const [totalInterjections, setTotalInterjections] = useState(0);

useEffect(() => {
if (interjectionReplyRecieved) {
Expand All @@ -29,6 +30,8 @@ function Output({
setCurrentTextMessage(textMessages[textMessages.length - 1]);
setCurrentAudioMessage(audioMessages[audioMessages.length - 1]);

setTotalInterjections((prev) => prev + 1);

onResetInterjectionReply();
}
}, [interjectionReplyRecieved]);
Expand Down Expand Up @@ -90,17 +93,24 @@ function Output({
// Set isReady to true in the parent component to render the controls (for skipping forward et.c.)
onIsReady();

setCurrentTextMessage((prev) => textMessage);
setCurrentAudioMessage((prev) => audioMessage);
setCurrentTextMessage(() => textMessage);
setCurrentAudioMessage(() => audioMessage);
}
}

function proceedToNextMessage() {
// Reset the current message contents
setCurrentTextMessage(() => null);
setCurrentAudioMessage(() => null);

console.log("Current index: ", currentMessageIndex);
setCurrentMessageIndex((prev) => prev + 1);
// Update the index to the next message, ensuring it doesn't exceed the available range
// considering interjections that may reduce the number of messages to show
setCurrentMessageIndex((prev) => {
// Calculate the maximum index allowed based on the length of the messages and total interjections
const maxIndex = textMessages.length - totalInterjections - 1;
// Increment the index if it's within the bounds, otherwise keep it at the maximum allowed index
return prev < maxIndex ? prev + 1 : maxIndex;
});
}

function handleOnFinishedPlaying() {
Expand Down