1 | import PropTypes from 'prop-types';
|
2 | import React, { useState, useMemo } from 'react';
|
3 | import { Text, StyleSheet, View, TouchableOpacity, } from 'react-native';
|
4 | import { useCallbackOne } from 'use-memo-one';
|
5 | import Color from './Color';
|
6 | import { StylePropType } from './utils';
|
7 | import { warning } from './logging';
|
8 | const styles = StyleSheet.create({
|
9 | container: {
|
10 | flexDirection: 'row',
|
11 | flexWrap: 'wrap',
|
12 | maxWidth: 300,
|
13 | },
|
14 | quickReply: {
|
15 | justifyContent: 'center',
|
16 | alignItems: 'center',
|
17 | borderWidth: 1,
|
18 | maxWidth: 200,
|
19 | paddingVertical: 7,
|
20 | paddingHorizontal: 12,
|
21 | minHeight: 50,
|
22 | borderRadius: 13,
|
23 | margin: 3,
|
24 | },
|
25 | quickReplyText: {
|
26 | overflow: 'visible',
|
27 | },
|
28 | sendLink: {
|
29 | borderWidth: 0,
|
30 | },
|
31 | sendLinkText: {
|
32 | color: Color.defaultBlue,
|
33 | fontWeight: '600',
|
34 | fontSize: 17,
|
35 | },
|
36 | });
|
37 | const sameReply = (currentReply) => (reply) => currentReply.value === reply.value;
|
38 | const diffReply = (currentReply) => (reply) => currentReply.value !== reply.value;
|
39 | export function QuickReplies({ currentMessage, nextMessage, color = Color.peterRiver, quickReplyStyle, quickReplyTextStyle, onQuickReply, sendText = 'Send', renderQuickReplySend, }) {
|
40 | const { type } = currentMessage.quickReplies;
|
41 | const [replies, setReplies] = useState([]);
|
42 | const shouldComponentDisplay = useMemo(() => {
|
43 | const hasReplies = !!currentMessage && !!currentMessage.quickReplies;
|
44 | const hasNext = !!nextMessage && !!nextMessage._id;
|
45 | const keepIt = currentMessage.quickReplies.keepIt;
|
46 | if (hasReplies && !hasNext) {
|
47 | return true;
|
48 | }
|
49 | if (hasReplies && hasNext && keepIt) {
|
50 | return true;
|
51 | }
|
52 | return false;
|
53 | }, [currentMessage, nextMessage]);
|
54 | const handlePress = useCallbackOne((reply) => () => {
|
55 | if (currentMessage) {
|
56 | const { type } = currentMessage.quickReplies;
|
57 | switch (type) {
|
58 | case 'radio': {
|
59 | handleSend([reply])();
|
60 | return;
|
61 | }
|
62 | case 'checkbox': {
|
63 | if (replies.find(sameReply(reply))) {
|
64 | setReplies(replies.filter(diffReply(reply)));
|
65 | }
|
66 | else {
|
67 | setReplies([...replies, reply]);
|
68 | }
|
69 | return;
|
70 | }
|
71 | default: {
|
72 | warning(`onQuickReply unknown type: ${type}`);
|
73 | return;
|
74 | }
|
75 | }
|
76 | }
|
77 | }, [replies, currentMessage]);
|
78 | const handleSend = (repliesData) => () => {
|
79 | onQuickReply === null || onQuickReply === void 0 ? void 0 : onQuickReply(repliesData.map((reply) => ({
|
80 | ...reply,
|
81 | messageId: currentMessage._id,
|
82 | })));
|
83 | };
|
84 | if (!shouldComponentDisplay) {
|
85 | return null;
|
86 | }
|
87 | return (<View style={styles.container}>
|
88 | {currentMessage.quickReplies.values.map((reply, index) => {
|
89 | const selected = type === 'checkbox' && replies.find(sameReply(reply));
|
90 | return (<TouchableOpacity onPress={handlePress(reply)} style={[
|
91 | styles.quickReply,
|
92 | quickReplyStyle,
|
93 | { borderColor: color },
|
94 | selected && { backgroundColor: color },
|
95 | ]} key={`${reply.value}-${index}`}>
|
96 | <Text numberOfLines={10} ellipsizeMode={'tail'} style={[
|
97 | styles.quickReplyText,
|
98 | { color: selected ? Color.white : color },
|
99 | quickReplyTextStyle,
|
100 | ]}>
|
101 | {reply.title}
|
102 | </Text>
|
103 | </TouchableOpacity>);
|
104 | })}
|
105 | {replies.length > 0 && (<TouchableOpacity style={[styles.quickReply, styles.sendLink]} onPress={handleSend(replies)}>
|
106 | {(renderQuickReplySend === null || renderQuickReplySend === void 0 ? void 0 : renderQuickReplySend()) || (<Text style={styles.sendLinkText}>{sendText}</Text>)}
|
107 | </TouchableOpacity>)}
|
108 | </View>);
|
109 | }
|
110 | QuickReplies.propTypes = {
|
111 | currentMessage: PropTypes.object.isRequired,
|
112 | onQuickReply: PropTypes.func,
|
113 | color: PropTypes.string,
|
114 | sendText: PropTypes.string,
|
115 | renderQuickReplySend: PropTypes.func,
|
116 | quickReplyStyle: StylePropType,
|
117 | };
|
118 |
|
\ | No newline at end of file |