File size: 7,019 Bytes
00373d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e49fcf4
5ed1690
e49fcf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ed1690
 
e49fcf4
 
 
 
 
 
 
 
 
 
 
5ed1690
e49fcf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ed1690
e49fcf4
 
 
 
 
 
5ed1690
 
 
 
 
 
 
 
 
e49fcf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ed1690
 
 
 
 
 
 
 
e49fcf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ed1690
e49fcf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// /$$$$$$  /$$$$$$ /$$    /$$ /$$$$$$$$       /$$      /$$ /$$$$$$$$        /$$$$$$           /$$$$$  /$$$$$$  /$$$$$$$ 
// /$$__  $$|_  $$_/| $$   | $$| $$_____/      | $$$    /$$$| $$_____/       /$$__  $$         |__  $$ /$$__  $$| $$__  $$
// | $$  \__/  | $$  | $$   | $$| $$            | $$$$  /$$$$| $$            | $$  \ $$            | $$| $$  \ $$| $$  \ $$
// | $$ /$$$$  | $$  |  $$ / $$/| $$$$$         | $$ $$/$$ $$| $$$$$         | $$$$$$$$            | $$| $$  | $$| $$$$$$$ 
// | $$|_  $$  | $$   \  $$ $$/ | $$__/         | $$  $$$| $$| $$__/         | $$__  $$       /$$  | $$| $$  | $$| $$__  $$
// | $$  \ $$  | $$    \  $$$/  | $$            | $$\  $ | $$| $$            | $$  | $$      | $$  | $$| $$  | $$| $$  \ $$
// |  $$$$$$/ /$$$$$$   \  $/   | $$$$$$$$      | $$ \/  | $$| $$$$$$$$      | $$  | $$      |  $$$$$$/|  $$$$$$/| $$$$$$$/
// \______/ |______/    \_/    |________/      |__/     |__/|________/      |__/  |__/       \______/  \______/ |_______/ 
//
// Hi, I'm Roland and i'm looking for a job.
// Resume in /public/resume.pdf
// roland.vrignon@roland.com
// https://www.linkedin.com/in/roland-vrignon/
//


'use client';
import { FC, useState, useEffect, Dispatch, SetStateAction, useRef } from 'react';
import Image from 'next/image';

interface Message {
  content: string;
  role: 'lawyer' | 'judge';
  requiredWords?: string[];
}


interface Chat {
  messages: Message[];
}

interface LawyerSceneProps {
  language: 'fr' | 'en' | 'es';
  chat: Chat;
  setNextScene: () => void;
  currentQuestion: string;
  round: number;
  setRound: Dispatch<SetStateAction<number>>;
}

const LawyerScene: FC<LawyerSceneProps> = ({
  language,
  chat,
  setNextScene,
  currentQuestion,
  round,
  setRound,
}) => {
  const [visible, setVisible] = useState(false);
  const [buttonEnabled, setButtonEnabled] = useState(false);
  const [countdown, setCountdown] = useState(3);

  const [question, setQuestion] = useState('');
  const [answer, setAnswer] = useState('');

  const audioRef = useRef<HTMLAudioElement | null>(null);

  useEffect(() => {
    const lastJudgeMessage = chat.messages.filter((message: Message) => message.role === 'judge').slice(-1)[0]?.content;
    const lastLawyerMessage = chat.messages.filter((message: Message) => message.role === 'lawyer').slice(-1)[0]?.content;
    setQuestion(lastJudgeMessage || '');
    setAnswer(lastLawyerMessage || '');
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []); // Only run once on mount

  useEffect(() => {
    const playAudio = async () => {
      try {
        if (!answer) return;

        const response = await fetch('/api/voice', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            language,
            text: answer,
            role: 'lawyer'
          })
        });

        if (!response.ok) {
          throw new Error('Failed to generate audio');
        }

        const audioBlob = await response.blob();
        const audioUrl = URL.createObjectURL(audioBlob);
        const audio = new Audio(audioUrl);
        audioRef.current = audio;
        audio.play();
      } catch (error) {
        console.error('Error playing audio:', error);
      }
    };

    playAudio();

    return () => {
      if (audioRef.current) {
        audioRef.current.pause();
        audioRef.current = null;
      }
    };
  }, [answer, language]);

  useEffect(() => {
    setVisible(true);
    setRound(round + 1);
    const timer = setInterval(() => {
      setCountdown((prev) => {
        if (prev <= 1) {
          setButtonEnabled(true);
          clearInterval(timer);
          return 0;
        }
        return prev - 1;
      });
    }, 1000);

    return () => clearInterval(timer);
  }, []);

  const handleNextScene = () => {
    if (audioRef.current) {
      audioRef.current.pause();
      audioRef.current = null;
    }
    setNextScene();
  };

  return (
    <div className="relative w-screen h-screen">
      {/* Image de fond */}
      <Image
        src="https://ik.imagekit.io/z0tzxea0wgx/MistralGameJam/DD_attorney1_k5DWtEzcV.png?updatedAt=1737835883169"
        alt="Background"
        fill
        className="object-cover"
        priority
      />

      {/* Contenu avec overlay noir */}
      <div className="absolute inset-0">
        <div className="flex flex-col items-center justify-end h-full p-8">
          <div className={`flex flex-col items-center justify-center space-y-8 transition-opacity duration-1000 ${
            visible ? 'opacity-100' : 'opacity-0'
          }`}>
            {/* Question du juge */}
            <div className="bg-black/60 border border-black border-8 p-6 mb-8 w-[80%]">
              <h2 className="text-2xl font-bold mb-4 roboto-slab text-white">
                {language === 'fr' ? 'Question du juge' : language === 'en' ? 'Judge\'s question' : 'Pregunta del juez'}:
              </h2>
              <p className="text-xl text-white roboto-slab">
                {question}
              </p>
            </div>

            {/* Réponse de l'avocat avec flèche */}
            <div className="relative bg-black/60 border border-black border-8 p-6 mb-8 w-[80%]">
              <h2 className="text-2xl font-bold mb-4 roboto-slab text-white">
                {language === 'fr' ? 'Votre réponse' : language === 'en' ? 'Your answer' : 'Tu respuesta'}:
              </h2>
              <p className="text-xl text-white roboto-slab whitespace-pre-wrap mb-8">
                {answer}
              </p>

              {/* Flèche à droite */}
              <div className="absolute bottom-5 right-5">
                <button
                  onClick={handleNextScene}
                  disabled={!buttonEnabled || (currentQuestion === "" && round < 3)}
                  className={`text-white transition-colors ${
                    buttonEnabled && (currentQuestion !== "" || round === 3)
                      ? 'hover:text-blue-400 cursor-pointer'
                      : 'text-gray-600 cursor-not-allowed'
                  }`}
                  aria-label="Continuer"
                >
                  {!buttonEnabled ? (
                    <span className="text-2xl">{countdown}s</span>
                  ) : (
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      className="h-12 w-12"
                      fill="none"
                      viewBox="0 0 24 24"
                      stroke="currentColor"
                    >
                      <path
                        strokeLinecap="round"
                        strokeLinejoin="round"
                        strokeWidth={2}
                        d="M14 5l7 7m0 0l-7 7m7-7H3"
                      />
                    </svg>
                  )}
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

export default LawyerScene;