From d1f8ca2cde9fb39e43787097e79989b0a225ab79 Mon Sep 17 00:00:00 2001 From: Simon ZHANG <102216594+userMeh@users.noreply.github.com> Date: Tue, 23 Jul 2024 19:30:17 +0200 Subject: [PATCH] fix: to fix (#289) * fix: to fix * fix: some fix * test: console.log websocket payload --- apps/api/src/handlers/messages.ts | 28 +------------------ .../(auth)/(members)/members/chat/page.tsx | 6 ++-- apps/client/app/lib/type/Messages.ts | 12 -------- 3 files changed, 5 insertions(+), 41 deletions(-) diff --git a/apps/api/src/handlers/messages.ts b/apps/api/src/handlers/messages.ts index f347d282..cfed28a0 100644 --- a/apps/api/src/handlers/messages.ts +++ b/apps/api/src/handlers/messages.ts @@ -14,33 +14,7 @@ export const messages = new OpenAPIHono<{ Variables: Variables }>({ supabase .channel('realtime_messages') .on('postgres_changes', { event: '*', schema: 'public', table: 'MESSAGES' }, async (payload) => { - const dataMessage = { - new: { - ...payload.new, - name: '', - }, - old: { - ...payload.old, - name: '', - }, - event: payload.eventType, - }; - if (payload.eventType !== 'DELETE') { - const { data, error } = await supabase - .from('USERS') - .select('username') - .eq('id', (payload.new as { id_sender: number }).id_sender) - .single(); - - if (error || !data) { - console.error(error); - return; - } - - dataMessage.new.name = data.username; - } - - io.emit('receivedMessage', dataMessage); + io.emit('receivedMessage', payload.new); }) .subscribe(); diff --git a/apps/client/app/(auth)/(members)/members/chat/page.tsx b/apps/client/app/(auth)/(members)/members/chat/page.tsx index 017d9cd4..d45a283b 100644 --- a/apps/client/app/(auth)/(members)/members/chat/page.tsx +++ b/apps/client/app/(auth)/(members)/members/chat/page.tsx @@ -1,5 +1,5 @@ 'use client'; -import type { Message, SocketMessage } from '@/app/lib/type/Messages'; +import type { Message } from '@/app/lib/type/Messages'; import { deleteMessage, getMessages, sendMessage, updateMessage } from '@/app/lib/utils/messages'; import { zodResolver } from '@hookform/resolvers/zod'; import { Button } from '@repo/ui/components/ui/button'; @@ -44,11 +44,13 @@ export default function ChatView() { fetchMessages(); const socket = io(process.env.NEXT_PUBLIC_SOCKET_ENDPOINT || ''); - socket.on('receivedMessage', (payload: SocketMessage) => { + socket.on('receivedMessage', (payload) => { + console.log(payload); fetchMessages(); }); return () => { + socket.off('receivedMessage'); socket.disconnect(); }; }, []); diff --git a/apps/client/app/lib/type/Messages.ts b/apps/client/app/lib/type/Messages.ts index 70d850ed..fede8119 100644 --- a/apps/client/app/lib/type/Messages.ts +++ b/apps/client/app/lib/type/Messages.ts @@ -6,15 +6,3 @@ export type Message = { updated_at: string | null; name: string; }; - -export type SocketMessage = { - new: Message | undefined; - old: - | Message - | { - id: number; - name: string; - } - | undefined; - event: 'INSERT' | 'UPDATE' | 'DELETE'; -};