import React, { useState, useMemo, useCallback } from 'react' import { StyleSheet, View, StyleProp, ViewStyle, TextStyle, } from 'react-native' import { Text } from 'react-native-gesture-handler' import { Color } from './Color' import { TouchableOpacity } from './components/TouchableOpacity' import { warning } from './logging' import { IMessage, Reply } from './Models' import stylesCommon from './styles' const styles = StyleSheet.create({ container: { flexDirection: 'row', flexWrap: 'wrap', maxWidth: 300, }, quickReply: { borderWidth: 1, maxWidth: 200, paddingVertical: 7, paddingHorizontal: 12, minHeight: 50, borderRadius: 13, margin: 3, }, quickReplyText: { overflow: 'visible', }, sendLink: { borderWidth: 0, }, sendLinkText: { color: Color.defaultBlue, fontWeight: '600', fontSize: 17, }, }) export interface QuickRepliesProps { nextMessage?: TMessage currentMessage: TMessage color?: string sendText?: string quickReplyStyle?: StyleProp quickReplyTextStyle?: StyleProp quickReplyContainerStyle?: StyleProp onQuickReply?(reply: Reply[]): void renderQuickReplySend?(): React.ReactNode } const sameReply = (currentReply: Reply) => (reply: Reply) => currentReply.value === reply.value const diffReply = (currentReply: Reply) => (reply: Reply) => currentReply.value !== reply.value export function QuickReplies ({ currentMessage, nextMessage, color = Color.peterRiver, quickReplyStyle, quickReplyTextStyle, quickReplyContainerStyle, onQuickReply, sendText = 'Send', renderQuickReplySend, }: QuickRepliesProps) { const { type } = currentMessage!.quickReplies! const [replies, setReplies] = useState([]) const shouldComponentDisplay = useMemo(() => { const hasReplies = !!currentMessage && !!currentMessage!.quickReplies const hasNext = !!nextMessage && !!nextMessage!._id const keepIt = currentMessage!.quickReplies!.keepIt if (hasReplies && !hasNext) return true if (hasReplies && hasNext && keepIt) return true return false }, [currentMessage, nextMessage]) const handleSend = useCallback((repliesData: Reply[]) => () => { onQuickReply?.( repliesData.map((reply: Reply) => ({ ...reply, messageId: currentMessage!._id, })) ) }, [onQuickReply, currentMessage]) const handlePress = useCallback( (reply: Reply) => () => { if (currentMessage) { const { type } = currentMessage.quickReplies! switch (type) { case 'radio': { handleSend([reply])() return } case 'checkbox': { if (replies.find(sameReply(reply))) setReplies(replies.filter(diffReply(reply))) else setReplies([...replies, reply]) return } default: { warning(`onQuickReply unknown type: ${type}`) } } } }, [replies, currentMessage, handleSend] ) const renderSendButton = useMemo(() => { if (!replies.length) return null return ( {renderQuickReplySend?.() || ( {sendText} )} ) }, [replies, handleSend, renderQuickReplySend, sendText]) if (!shouldComponentDisplay) return null return ( {currentMessage!.quickReplies!.values.map( (reply: Reply, index: number) => { const selected = type === 'checkbox' && replies.find(sameReply(reply)) return ( {reply.title} ) } )} {renderSendButton} ) }