rolexx commited on
Commit
9942bf5
·
1 Parent(s): 655b40c
src/components/court/Court.tsx CHANGED
@@ -23,6 +23,12 @@ const CourtScene: FC<CourtSceneProps> = ({
23
  try {
24
  if (!currentQuestion) return;
25
 
 
 
 
 
 
 
26
  const response = await fetch('/api/voice', {
27
  method: 'POST',
28
  headers: {
@@ -43,15 +49,13 @@ const CourtScene: FC<CourtSceneProps> = ({
43
  const audioUrl = URL.createObjectURL(audioBlob);
44
  const audio = new Audio(audioUrl);
45
  audioRef.current = audio;
46
- audio.play();
47
  } catch (error) {
48
  console.error('Error playing audio:', error);
49
  }
50
  };
51
 
52
- if (currentQuestion) {
53
- playAudio();
54
- }
55
 
56
  return () => {
57
  if (audioRef.current) {
@@ -59,7 +63,8 @@ const CourtScene: FC<CourtSceneProps> = ({
59
  audioRef.current = null;
60
  }
61
  };
62
- }, [currentQuestion, reaction]);
 
63
 
64
  const handleNextScene = () => {
65
  if (audioRef.current) {
 
23
  try {
24
  if (!currentQuestion) return;
25
 
26
+ // Cleanup previous audio if exists
27
+ if (audioRef.current) {
28
+ audioRef.current.pause();
29
+ audioRef.current = null;
30
+ }
31
+
32
  const response = await fetch('/api/voice', {
33
  method: 'POST',
34
  headers: {
 
49
  const audioUrl = URL.createObjectURL(audioBlob);
50
  const audio = new Audio(audioUrl);
51
  audioRef.current = audio;
52
+ await audio.play();
53
  } catch (error) {
54
  console.error('Error playing audio:', error);
55
  }
56
  };
57
 
58
+ playAudio();
 
 
59
 
60
  return () => {
61
  if (audioRef.current) {
 
63
  audioRef.current = null;
64
  }
65
  };
66
+ // eslint-disable-next-line react-hooks/exhaustive-deps
67
+ }, [currentQuestion]);
68
 
69
  const handleNextScene = () => {
70
  if (audioRef.current) {
src/components/end/End.tsx CHANGED
@@ -1,5 +1,5 @@
1
  'use client';
2
- import { FC, useEffect, useState } from 'react';
3
  import Image from 'next/image';
4
 
5
  interface Verdict {
@@ -20,14 +20,66 @@ const EndScene: FC<EndSceneProps> = ({
20
  verdict
21
  }) => {
22
  const [isLoading, setIsLoading] = useState(true);
 
23
 
24
  useEffect(() => {
25
  if (verdict) {
26
- console.log('End verdict:', verdict)
27
  setIsLoading(false);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  }
 
 
 
 
 
 
 
 
29
  }, [verdict]);
30
 
 
 
 
 
 
 
 
 
31
  const getLoadingText = () => {
32
  if (language === 'fr') {
33
  return 'Le juge délibère...';
@@ -77,7 +129,7 @@ const EndScene: FC<EndSceneProps> = ({
77
  priority
78
  />
79
  <div className="absolute inset-0 flex items-center justify-center">
80
- <div className="bg-black/60 border-8 border-black w-[600px] aspect-square p-8 m-8 flex flex-col items-center justify-center">
81
  {isLoading ? (
82
  <div className="text-center">
83
  <h1 className="text-4xl text-white roboto-slab mb-8">
@@ -113,7 +165,7 @@ const EndScene: FC<EndSceneProps> = ({
113
  </p>
114
 
115
  <button
116
- onClick={setNextScene}
117
  className="px-12 py-6 text-2xl font-bold text-white bg-sky-500 hover:bg-blue-700 transition-colors roboto-slab mt-auto"
118
  >
119
  {language === 'fr' ? 'Rejouer' : language === 'en' ? 'Try again' : 'Jugar de nuevo'}
 
1
  'use client';
2
+ import { FC, useEffect, useState, useRef } from 'react';
3
  import Image from 'next/image';
4
 
5
  interface Verdict {
 
20
  verdict
21
  }) => {
22
  const [isLoading, setIsLoading] = useState(true);
23
+ const audioRef = useRef<HTMLAudioElement | null>(null);
24
 
25
  useEffect(() => {
26
  if (verdict) {
27
+ console.log('End verdict:', verdict);
28
  setIsLoading(false);
29
+
30
+ const playVerdict = async () => {
31
+ try {
32
+ if (audioRef.current) {
33
+ audioRef.current.pause();
34
+ audioRef.current = null;
35
+ }
36
+
37
+ const response = await fetch('/api/voice', {
38
+ method: 'POST',
39
+ headers: {
40
+ 'Content-Type': 'application/json',
41
+ },
42
+ body: JSON.stringify({
43
+ language,
44
+ text: verdict.argument,
45
+ role: 'judge'
46
+ })
47
+ });
48
+
49
+ if (!response.ok) {
50
+ throw new Error('Failed to generate audio');
51
+ }
52
+
53
+ const audioBlob = await response.blob();
54
+ const audioUrl = URL.createObjectURL(audioBlob);
55
+ const audio = new Audio(audioUrl);
56
+ audioRef.current = audio;
57
+ await audio.play();
58
+ } catch (error) {
59
+ console.error('Error playing verdict audio:', error);
60
+ }
61
+ };
62
+
63
+ playVerdict();
64
  }
65
+
66
+ return () => {
67
+ if (audioRef.current) {
68
+ audioRef.current.pause();
69
+ audioRef.current = null;
70
+ }
71
+ };
72
+ // eslint-disable-next-line react-hooks/exhaustive-deps
73
  }, [verdict]);
74
 
75
+ const handleNextScene = () => {
76
+ if (audioRef.current) {
77
+ audioRef.current.pause();
78
+ audioRef.current = null;
79
+ }
80
+ setNextScene();
81
+ };
82
+
83
  const getLoadingText = () => {
84
  if (language === 'fr') {
85
  return 'Le juge délibère...';
 
129
  priority
130
  />
131
  <div className="absolute inset-0 flex items-center justify-center">
132
+ <div className="bg-black/60 border-8 border-black w-[600px] p-10 m-8 flex flex-col items-center justify-center">
133
  {isLoading ? (
134
  <div className="text-center">
135
  <h1 className="text-4xl text-white roboto-slab mb-8">
 
165
  </p>
166
 
167
  <button
168
+ onClick={handleNextScene}
169
  className="px-12 py-6 text-2xl font-bold text-white bg-sky-500 hover:bg-blue-700 transition-colors roboto-slab mt-auto"
170
  >
171
  {language === 'fr' ? 'Rejouer' : language === 'en' ? 'Try again' : 'Jugar de nuevo'}