// @ts-nocheck import MaterialIcons from '@expo/vector-icons/MaterialIcons' import * as Linking from 'expo-linking' import AppLoading from 'expo-app-loading' import * as SplashScreen from 'expo-splash-screen'; import React, { Component,useCallback,useEffect } from 'react' import { StyleSheet, View, Text, Platform, Alert,KeyboardAvoidingView } from 'react-native' import { Bubble, GiftedChat, SystemMessage, IMessage, Send, SendProps, } from 'react-native-gifted-chat' import AccessoryBar from './chat/AccesoryBar' import CustomActions from './chat/CustomActions' import CustomView from './chat/CustomView' //import messagesData from './chat/data/messages' //import earlierMessages from './chat/data/earlierMessages' import { NavBar } from './chat/navbar' import ChatState from './state'; import AsyncStorage from '@react-native-async-storage/async-storage'; // import { IUser } from '../../context/interfaces'; const styles = StyleSheet.create({ container: { flex: 1 }, }) const filterBotMessages = (message : any) => !message.system && message.user && message.user._id && message.user._id === 2 const findStep = (step : any) => (message : any) => message._id === step interface ChatUser { _id:number; name:string; } interface IProps { title:string; user:any; chatUser:ChatUser; messages:any[]; onAddMessage:any; onRefresh(id:number):Promise; refreshData:number; } export default function(props:IProps) { return } class ChatApp extends Component { chatUser = { // _id: 1, // name: 'Developer', } otherUser = { _id: 2, name: 'Chofer', //avatar:'https://i.ibb.co/n6Wc32n/chofer.jpg' avatar:"https://cdn-icons-png.flaticon.com/512/4634/4634905.png" //avatar:'https://facebook.github.io/react/img/logo_og.png', } user:any; _isMounted = false constructor(props) { super(props); console.disableYellowBox = true; this.state = { inverted: false, step: 0, messages: [], loadEarlier: true, typingText: null, isLoadingEarlier: false, appIsReady: false, isTyping: false, interval:-1 }; this.user = this.props.user; this.chatUser = this.props.chatUser; this.chatUser.avatar = this.chatUser._id == 2 ? "https://image.pngaaa.com/97/4824097-small.png" : "https://toppng.com/uploads/preview/user-account-management-logo-user-icon-11562867145a56rus2zwu.png"; } componentDidMount() { this._isMounted = true; let self = this; // init with only system messages this.setState({ //...this.state, messages: this.props.messages, appIsReady: true, isTyping: false, }) } addMessages(messagesToAdd) { this.setState({ //...this.state, messages : [...messages,...messagesToAdd] }) } componentWillUnmount() { try { if (this.state.interval!= -1) { clearInterval(this.state.interval); } } catch (e) { } this._isMounted = false } onLoadEarlier = () => { } onSend = (messages : any[] = [], refresh:boolean=false) => { if (this.state) { const step = this.state.step + 1; let sentMessages = messages!= null && messages.length > 0 ? [{ ...messages[0], sent: true, received: true,createdAt:new Date() }] : []; if (refresh && messages != null && messages.length > 0) { sentMessages=[]; for (var i=0;i { return { messages: GiftedChat.append( previousState.messages, sentMessages, Platform.OS !== 'web', ), step, } }) } } } botSend = (step = 0) => { const newMessage = (messagesData as IMessage[]) .reverse() .find(findStep(step)) if (newMessage) { this.setState((previousState: any) => ({ messages: GiftedChat.append( previousState.messages, [newMessage], Platform.OS !== 'web', ), })) } } parsePatterns = (_linkStyle: any) => { return [ { pattern: /#(\w+)/, style: { textDecorationLine: 'underline', color: 'darkorange' }, onPress: () => Linking.openURL('http://gifted.chat'), }, ] } renderCustomView(props:any) { return } onReceive = (text: string) => { this.setState((previousState: any) => { return { messages: GiftedChat.append( previousState.messages as any, [ { _id: Math.round(Math.random() * 1000000), text, createdAt: new Date(), user: otherUser, }, ], Platform.OS !== 'web', ), } }) } onSendFromUser = (messages: IMessage[] = []) => { const createdAt = new Date() const messagesToUpload = messages.map(message => ({ ...message, user: this.chatUser, createdAt, _id: Math.round(Math.random() * 1000000), })) this.onSend(messagesToUpload) } setIsTyping = () => { if (this.state) { this.setState({ ...this.state, isTyping: !this.state.isTyping, }) } else { this.setState({ ...this.state, isTyping: false, }) } } refreshMessages = () => { this.props.onRefresh(this.props.refreshData).then(e => { this.onSend(e.data,true) }) } renderAccessory = () => ( ) renderCustomActions = (props:any) => Platform.OS === 'web' ? null : ( ) renderBubble = (props: any) => { return } renderSystemMessage = (props:any) => { return ( ) } getUniqueId() { return Math.round(Math.random() * 1000000); } onQuickReply = (replies:any) => { const createdAt = new Date() if (replies.length === 1) { this.onSend([ { createdAt, _id: this.getUniqueId(), text: replies[0].title, user:this.chatUser, }, ]) } else if (replies.length > 1) { this.onSend([ { createdAt, _id: this.getUniqueId(), text: replies.map((reply:any) => reply.title).join(', '), user:this.chatUser, }, ]) } else { console.warn('replies param is not set correctly') } } renderQuickReplySend = () => {' custom send =>'} renderSend = (props: SendProps) => ( ) async showSplash() { try { // Keep the splash screen visible while we fetch resources await SplashScreen.preventAutoHideAsync(); } catch (e) { console.warn(e); } finally { // Tell the application to render this.setState({...self.state,appIsReady:true}) } } async hideSplash() { if (this.state && this.state.appIsReady) { await SplashScreen.hideAsync(); } } render() { if (!this.state || !this.state.appIsReady) { this.showSplash(); } return ( {} // Alert.alert(JSON.stringify(userPress)) } onPressAvatar={(e) => {} //Alert.alert('short press') } onPress={() => { //Alert.alert('Bubble pressed') }} onQuickReply={this.onQuickReply} keyboardShouldPersistTaps='never' renderAccessory={Platform.OS === 'web' ? null : this.renderAccessory} //renderActions={this.renderCustomActions} renderBubble={this.renderBubble} renderSystemMessage={this.renderSystemMessage} renderCustomView={this.renderCustomView} renderSend={this.renderSend} quickReplyStyle={{ borderRadius: 2 }} quickReplyTextStyle={{ fontWeight: '200', }} renderQuickReplySend={this.renderQuickReplySend} inverted={Platform.OS !== 'web'} timeTextStyle={{ left: { color: 'red' }, right: { color: 'yellow' } }} isTyping={this.state && this.state.isTyping} infiniteScroll /> { Platform.OS === 'ios' && } ) } }