Skip to content

Commit

Permalink
Merge pull request #12 from Nonhuman-Nonsense/albin
Browse files Browse the repository at this point in the history
Add max index limit to messages to avoid replaying interjection replies
  • Loading branch information
albin-karlsson authored Apr 26, 2024
2 parents f51c5fb + d597423 commit 10aa2fd
Showing 1 changed file with 14 additions and 4 deletions.
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

0 comments on commit 10aa2fd

Please sign in to comment.