fix: corrige gravação de áudio na página de história

- Remove campos não utilizados (class_id e school_id) da tabela story_recordings
- Simplifica o componente AudioRecorder para usar apenas campos necessários
- Atualiza interface StoryRecording no types/database.ts
- Corrige erro de constraint na inserção de gravações
This commit is contained in:
Lucas Santana 2024-12-20 16:00:47 -03:00
parent 9ecf46a9ac
commit 5573274ad4
3 changed files with 20 additions and 12 deletions

View File

@ -5,12 +5,10 @@ import { supabase } from '../../lib/supabase';
interface AudioRecorderProps { interface AudioRecorderProps {
storyId: string; storyId: string;
studentId: string; studentId: string;
classId: string;
schoolId: string;
onAudioUploaded: (audioUrl: string) => void; onAudioUploaded: (audioUrl: string) => void;
} }
export function AudioRecorder({ storyId, studentId, classId, schoolId, onAudioUploaded }: AudioRecorderProps) { export function AudioRecorder({ storyId, studentId, onAudioUploaded }: AudioRecorderProps) {
const [isRecording, setIsRecording] = useState(false); const [isRecording, setIsRecording] = useState(false);
const [audioBlob, setAudioBlob] = useState<Blob | null>(null); const [audioBlob, setAudioBlob] = useState<Blob | null>(null);
const [isUploading, setIsUploading] = useState(false); const [isUploading, setIsUploading] = useState(false);
@ -56,15 +54,21 @@ export function AudioRecorder({ storyId, studentId, classId, schoolId, onAudioUp
const uploadAudio = async () => { const uploadAudio = async () => {
if (!audioBlob) return; if (!audioBlob) return;
const { data: { session } } = await supabase.auth.getSession();
if (!session?.user) {
setError('Usuário não autenticado');
return;
}
setIsUploading(true); setIsUploading(true);
setError(null); setError(null);
try { try {
// Criar nome único para o arquivo usando uma estrutura de pastas organizada // Criar nome único para o arquivo
const timestamp = new Date().getTime(); const timestamp = new Date().getTime();
const filePath = `${studentId}/${classId}/${storyId}/${timestamp}.webm`; const filePath = `${studentId}/${storyId}/${timestamp}.webm`;
// Upload do arquivo para o Supabase Storage // Upload do arquivo
const { data, error: uploadError } = await supabase.storage const { data, error: uploadError } = await supabase.storage
.from('recordings') .from('recordings')
.upload(filePath, audioBlob, { .upload(filePath, audioBlob, {
@ -74,19 +78,17 @@ export function AudioRecorder({ storyId, studentId, classId, schoolId, onAudioUp
if (uploadError) throw uploadError; if (uploadError) throw uploadError;
// Obter URL pública do arquivo // Obter URL pública
const { data: { publicUrl } } = supabase.storage const { data: { publicUrl } } = supabase.storage
.from('recordings') .from('recordings')
.getPublicUrl(filePath); .getPublicUrl(filePath);
// Salvar referência no banco com todas as relações // Salvar referência no banco apenas com os campos necessários
const { error: dbError } = await supabase const { error: dbError } = await supabase
.from('story_recordings') .from('story_recordings')
.insert({ .insert({
story_id: storyId, story_id: storyId,
student_id: studentId, student_id: studentId,
class_id: classId,
school_id: schoolId,
audio_url: publicUrl, audio_url: publicUrl,
status: 'pending_analysis' status: 'pending_analysis'
}); });

View File

@ -129,8 +129,6 @@ export function StoryPage() {
<AudioRecorder <AudioRecorder
storyId={story.id} storyId={story.id}
studentId={story.student_id} studentId={story.student_id}
schoolId={story.school_id}
classId={story.class_id}
onAudioUploaded={(audioUrl) => { onAudioUploaded={(audioUrl) => {
console.log('Áudio gravado:', audioUrl); console.log('Áudio gravado:', audioUrl);
}} }}

View File

@ -129,4 +129,12 @@ export interface StudentWithStories extends Student {
// Atualizando ClassWithStudents para incluir histórias dos alunos // Atualizando ClassWithStudents para incluir histórias dos alunos
export interface ClassWithStudentsAndStories extends Class { export interface ClassWithStudentsAndStories extends Class {
students: StudentWithStories[]; students: StudentWithStories[];
}
interface StoryRecording {
id: string;
student_id: string;
story_id: string;
audio_url: string;
created_at: string;
} }