just another day

This commit is contained in:
2024-10-25 16:57:25 -05:00
parent 0e46c630a6
commit e84597883b
11 changed files with 882 additions and 1401 deletions

View File

@ -10,7 +10,7 @@ const TabLayout = () => {
<Tabs
screenOptions={{
tabBarActiveTintColor: Colors[scheme].tint,
//headerShown: false
headerShown: false,
headerStyle: {
backgroundColor: Colors[scheme].background,
},
@ -37,6 +37,7 @@ const TabLayout = () => {
<Tabs.Screen
name='messages'
options={{
headerShown: true,
title: 'Messages',
tabBarIcon: ({ color, focused }) => (
<TabBarIcon name={focused ? 'chatbubbles' : 'chatbubbles-outline'} color={color} />

View File

@ -1,7 +1,7 @@
import React, { useCallback, useEffect, useReducer, useState, useRef } from 'react';
import React, { useCallback, useEffect, useReducer, useState } from 'react';
import { io } from 'socket.io-client';
import { ThemedText, ThemedView } from '@/components/theme/Theme';
import { Alert, Linking, Platform, StyleSheet, ActivityIndicator } from 'react-native';
import { Alert, Linking, Platform, StyleSheet } from 'react-native';
import { MaterialIcons } from '@expo/vector-icons';
import {
GiftedChat,
@ -11,7 +11,6 @@ import {
SystemMessage,
} from 'react-native-gifted-chat';
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';
import AccessoryBar from '@/components/chat/AccessoryBar';
import CustomActions from '@/components/chat/CustomActions';
import CustomView from '@/components/chat/CustomView';
import * as Clipboard from 'expo-clipboard';
@ -93,44 +92,34 @@ const MessagesScreen = () => {
} catch (error) {
console.error('Error initializing users:', error);
Alert.alert('Error', 'Failed to initialize users. Please try again.');
dispatch({ type: ActionKind.LOAD_EARLIER_MESSAGES, payload: [] });
}
};
initialize();
}, []);
useEffect(() => {
if (!user || !partner) return;
if (!user || !partner) {
console.log('User or partner not found');
return;
}
const socket = io(process.env.EXPO_PUBLIC_WEBSOCKET_URL as string, {
transports: ['websocket'],
});
socket.on('connect', () => {
//console.log('Connected to WebSocket server');
console.log('Connected to WebSocket server');
socket.emit('join', user.id);
});
socket.on('connect_error', (error) => {
console.error('Error connecting to WebSocket server:', error);
});
socket.on('message', async (newMessage) => {
console.log('New message received:', newMessage);
const formattedMessage = formatMessages([newMessage]);
const initialMessages = await getMessages(user.id, 20, 0);
if (!initialMessages) return;
const formattedMessages = formatMessages(initialMessages);
dispatch({
type: ActionKind.SEND_MESSAGE,
payload: GiftedChat.append(state.messages, formattedMessage),
payload: formattedMessages,
});
if (user && partner) {
dispatch({ type: ActionKind.LOAD_EARLIER_START });
const initialMessages = await getMessages(user.id, 20, 0);
console.log('initial messages: ', initialMessages);
if (initialMessages) {
const formattedMessages = formatMessages(initialMessages);
console.log('formatted messages: ', formattedMessages);
dispatch({
type: ActionKind.LOAD_EARLIER_MESSAGES,
payload: formattedMessages,
});
}
} else console.log('user or partner not initialized');
});
return () => {
socket.disconnect();
@ -141,7 +130,7 @@ const MessagesScreen = () => {
const fetchMessages = async () => {
if (user && partner) {
dispatch({ type: ActionKind.LOAD_EARLIER_START });
const initialMessages = await getMessages(user.id, 20, 0);
const initialMessages = await getMessages(user.id);
if (initialMessages) {
const formattedMessages = formatMessages(initialMessages);
dispatch({ type: ActionKind.LOAD_EARLIER_MESSAGES, payload: formattedMessages });
@ -189,8 +178,7 @@ const MessagesScreen = () => {
message: messageToSend,
},
};
//sendPushNotification(partner.pushToken, notificationMessage);
sendPushNotification(user.pushToken, notificationMessage);
sendPushNotification(partner.pushToken, notificationMessage);
// Add the message with a tempId immediately to the state
const tempFormattedMessages = formatMessages([messageToSend]);
@ -203,9 +191,12 @@ const MessagesScreen = () => {
try {
// Send the message to the server
const sentMessage = await sendMessage(messageToSend);
if (!user || isNaN(user.id)) {
console.error('User not found');
throw new Error('User not found');
}
// Fetch the latest messages from the server to ensure consistency
const updatedMessages = await getMessages(user?.id ?? 0);
const updatedMessages = await getMessages(user.id);
if (updatedMessages) {
const formattedMessages = formatMessages(updatedMessages);
dispatch({ type: ActionKind.SEND_MESSAGE, payload: formattedMessages });
@ -311,15 +302,6 @@ const MessagesScreen = () => {
dispatch({ type: ActionKind.SET_IS_TYPING, payload: isTyping });
}, [dispatch]);
const renderAccessory = useCallback(() => {
return (
<AccessoryBar
onSend={onSend}
isTyping={() => setIsTyping(!state.isTyping)}
/>
);
}, [onSend, setIsTyping, state.isTyping]);
const renderCustomActions = useCallback(
props =>
Platform.OS === 'web' ? null : (
@ -374,7 +356,6 @@ const MessagesScreen = () => {
fontWeight: '200',
}}
renderQuickReplySend={renderQuickReplySend}
renderAccessory={renderAccessory}
renderActions={renderCustomActions}
renderSystemMessage={renderSystemMessage}
renderCustomView={renderCustomView}