feat: add error handling modal to ChatWindow and improve call initiation logic

- Introduced ErrorModal in ChatWindow to display error messages related to call initiation issues.
- Enhanced error handling during call initiation to provide user-friendly feedback based on different error scenarios.
- Refactored backend queries to streamline the retrieval of active calls, ensuring accurate status checks.
- Updated function names for clarity and consistency in the backend call management logic.
This commit is contained in:
2025-11-21 16:57:21 -03:00
parent 8fc3cf08c4
commit 9d2f6e7c79
4 changed files with 78 additions and 31 deletions

View File

@@ -10,6 +10,7 @@
import ScheduleMessageModal from './ScheduleMessageModal.svelte';
import SalaReuniaoManager from './SalaReuniaoManager.svelte';
import CallWindow from '../call/CallWindow.svelte';
import ErrorModal from '../ErrorModal.svelte';
import { getAvatarUrl } from '$lib/utils/avatarGenerator';
import { browser } from '$app/environment';
import {
@@ -41,6 +42,12 @@
let showNotificacaoModal = $state(false);
let iniciandoChamada = $state(false);
let chamadaAtiva = $state<Id<'chamadas'> | null>(null);
// Estados para modal de erro
let showErrorModal = $state(false);
let errorTitle = $state('Erro');
let errorMessage = $state('');
let errorDetails = $state<string | undefined>(undefined);
const chamadaAtivaQuery = useQuery(api.chamadas.obterChamadaAtiva, {
conversaId: conversaId as Id<'conversas'>
@@ -139,7 +146,10 @@
// Funções para chamadas
async function iniciarChamada(tipo: 'audio' | 'video'): Promise<void> {
if (chamadaAtual) {
alert('Já existe uma chamada ativa nesta conversa');
errorTitle = 'Chamada Ativa';
errorMessage = 'Já existe uma chamada ativa nesta conversa';
errorDetails = undefined;
showErrorModal = true;
return;
}
@@ -155,12 +165,36 @@
chamadaAtiva = chamadaId;
} catch (error) {
console.error('Erro ao iniciar chamada:', error);
const errorMessage = error instanceof Error ? error.message : 'Erro ao iniciar chamada';
alert(errorMessage);
// Tratar diferentes tipos de erro
if (error instanceof Error) {
errorTitle = 'Erro ao Iniciar Chamada';
// Verificar se é erro do Convex sobre função não encontrada
if (error.message.includes('Could not find public function')) {
errorMessage = 'O servidor de chamadas não está sincronizado. Por favor, aguarde alguns segundos e tente novamente.';
errorDetails = `Erro técnico: ${error.message}\n\nSugestão: Verifique se o backend Convex está rodando corretamente.`;
} else {
errorMessage = error.message || 'Erro ao iniciar chamada';
errorDetails = error.stack;
}
} else {
errorTitle = 'Erro ao Iniciar Chamada';
errorMessage = 'Ocorreu um erro desconhecido ao iniciar a chamada. Por favor, tente novamente.';
errorDetails = String(error);
}
showErrorModal = true;
} finally {
iniciandoChamada = false;
}
}
function fecharErrorModal(): void {
showErrorModal = false;
errorMessage = '';
errorDetails = undefined;
}
function fecharChamada(): void {
chamadaAtiva = null;
@@ -592,3 +626,12 @@
</form>
</dialog>
{/if}
<!-- Modal de Erro -->
<ErrorModal
open={showErrorModal}
title={errorTitle}
message={errorMessage}
details={errorDetails}
onClose={fecharErrorModal}
/>