-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathChatPlaceholder.tsx
161 lines (156 loc) · 5.05 KB
/
ChatPlaceholder.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { actionSheetColors, textPrimaryColor } from "@styles/colors";
import { translate } from "i18n-js";
import {
Keyboard,
Platform,
StyleSheet,
Text,
TouchableWithoutFeedback,
useColorScheme,
View,
} from "react-native";
import {
currentAccount,
useProfilesStore,
useRecommendationsStore,
useSettingsStore,
} from "../../../data/store/accountsStore";
import { useConversationContext } from "../../../utils/conversation";
import { sendMessage } from "../../../utils/message";
import { getProfile, getProfileData } from "../../../utils/profile";
import { conversationName } from "../../../utils/str";
import { consentToPeersOnProtocol } from "../../../utils/xmtpRN/conversations";
import ActivityIndicator from "../../ActivityIndicator/ActivityIndicator";
import Button from "../../Button/Button";
import { Recommendation } from "../../Recommendations/Recommendation";
import { showActionSheetWithOptions } from "../../StateHandlers/ActionSheetStateHandler";
type Props = {
messagesCount: number;
};
export default function ChatPlaceholder({ messagesCount }: Props) {
const conversation = useConversationContext("conversation");
const isBlockedPeer = useConversationContext("isBlockedPeer");
const onReadyToFocus = useConversationContext("onReadyToFocus");
const colorScheme = useColorScheme();
const styles = useStyles();
const setPeersStatus = useSettingsStore((s) => s.setPeersStatus);
const recommendationData = useRecommendationsStore((s) =>
conversation?.peerAddress ? s.frens[conversation.peerAddress] : undefined
);
const peerSocials = useProfilesStore((s) =>
conversation?.peerAddress
? getProfile(conversation.peerAddress, s.profiles)?.socials
: undefined
);
const profileData = getProfileData(recommendationData, peerSocials);
return (
<TouchableWithoutFeedback
onPress={() => {
Keyboard.dismiss();
}}
>
<View
onLayout={() => {
if (conversation && !isBlockedPeer && messagesCount === 0) {
onReadyToFocus();
}
}}
style={styles.chatPlaceholder}
>
{!conversation && (
<View>
<ActivityIndicator style={{ marginBottom: 20 }} />
<Text style={styles.chatPlaceholderText}>
Opening your conversation
</Text>
</View>
)}
{conversation && isBlockedPeer && (
<View>
<Text style={styles.chatPlaceholderText}>This user is blocked</Text>
<Button
variant="secondary"
picto="lock.open"
title="Unblock"
style={styles.cta}
onPress={() => {
showActionSheetWithOptions(
{
options: ["Unblock", "Cancel"],
cancelButtonIndex: 1,
destructiveButtonIndex: isBlockedPeer ? undefined : 0,
title: translate("if_you_unblock_contact"),
...actionSheetColors(colorScheme),
},
(selectedIndex?: number) => {
if (selectedIndex === 0 && conversation?.peerAddress) {
const { peerAddress } = conversation;
consentToPeersOnProtocol(
currentAccount(),
[peerAddress],
"allow"
);
setPeersStatus({ [peerAddress]: "consented" });
}
}
);
}}
/>
</View>
)}
{conversation && !isBlockedPeer && messagesCount === 0 && (
<View>
{profileData && !conversation.isGroup ? (
<Recommendation
address={conversation.peerAddress}
recommendationData={profileData}
embedInChat
isVisible
/>
) : (
<Text style={styles.chatPlaceholderText}>
This is the beginning of your{"\n"}conversation with{" "}
{conversation ? conversationName(conversation) : ""}
</Text>
)}
<Button
variant="secondary"
picto="hand.wave"
title="Say hi"
style={styles.cta}
onPress={() => {
sendMessage({
conversation,
content: "👋",
contentType: "xmtp.org/text:1.0",
});
}}
/>
</View>
)}
</View>
</TouchableWithoutFeedback>
);
}
const useStyles = () => {
const colorScheme = useColorScheme();
return StyleSheet.create({
chatPlaceholder: {
flex: 1,
justifyContent: "center",
},
chatPlaceholderContent: {
paddingVertical: 20,
flex: 1,
},
chatPlaceholderText: {
textAlign: "center",
fontSize: Platform.OS === "android" ? 16 : 17,
color: textPrimaryColor(colorScheme),
},
cta: {
alignSelf: "center",
marginTop: 20,
},
});
};