forked from ephemeraHQ/xmtp-inbox-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddressInputController.tsx
120 lines (114 loc) · 4.43 KB
/
AddressInputController.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
import { useEffect } from "react";
import { useConversation, useConsent } from "@xmtp/react-sdk";
import { AddressInput } from "../component-library/components/AddressInput/AddressInput";
import { getRecipientInputSubtext, shortAddress } from "../helpers";
import useWindowSize from "../hooks/useWindowSize";
import { useXmtpStore } from "../store/xmtp";
import { useAddressInput } from "../hooks/useAddressInput";
export const AddressInputController = () => {
// XMTP State
const recipientAddress = useXmtpStore((s) => s.recipientAddress);
const recipientAvatar = useXmtpStore((s) => s.recipientAvatar);
const recipientState = useXmtpStore((s) => s.recipientState);
const recipientOnNetwork = useXmtpStore((s) => s.recipientOnNetwork);
const recipientInput = useXmtpStore((s) => s.recipientInput);
const recipientName = useXmtpStore((s) => s.recipientName);
const conversationTopic = useXmtpStore((s) => s.conversationTopic);
const resetRecipient = useXmtpStore((s) => s.resetRecipient);
const loadingConversations = useXmtpStore((s) => s.loadingConversations);
const setRecipientInput = useXmtpStore((s) => s.setRecipientInput);
const setStartedFirstMessage = useXmtpStore((s) => s.setStartedFirstMessage);
const setConversationTopic = useXmtpStore((s) => s.setConversationTopic);
const changedConsentCount = useXmtpStore((s) => s.changedConsentCount);
const setChangedConsentCount = useXmtpStore((s) => s.setChangedConsentCount);
const activeTab = useXmtpStore((s) => s.activeTab);
const { getCachedByPeerAddress, getCachedByTopic } = useConversation();
const { deny, allow } = useConsent();
// manage address input state
useAddressInput();
const size = useWindowSize();
useEffect(() => {
const selectConversation = async () => {
// if there's a valid network address, look for an existing conversation
if (recipientAddress && recipientOnNetwork) {
let updateSelectedConversation = true;
// if there's an existing conversation topic, check if it has the same
// peer address as the recipient
if (conversationTopic) {
const convo = await getCachedByTopic(conversationTopic);
// if the peer address is the same, do not attempt to update the
// select conversation
if (convo?.peerAddress === recipientAddress) {
updateSelectedConversation = false;
}
}
// if we're updated the selected conversation, look for a conversation
// with the recipient's address. if present, select that conversation.
if (updateSelectedConversation) {
const existing = await getCachedByPeerAddress(recipientAddress);
if (existing && conversationTopic !== existing.topic) {
setConversationTopic(existing.topic);
}
}
}
};
void selectConversation();
}, [
conversationTopic,
getCachedByPeerAddress,
getCachedByTopic,
recipientAddress,
recipientOnNetwork,
setConversationTopic,
]);
return (
<AddressInput
isError={recipientState === "invalid" || recipientState === "error"}
subtext={
!loadingConversations
? getRecipientInputSubtext(
recipientInput,
recipientAddress,
recipientState,
recipientOnNetwork,
)
: ""
}
resolvedAddress={{
displayAddress:
recipientName ??
(size[0] < 700
? recipientAddress
? shortAddress(recipientAddress)
: ""
: recipientAddress ?? ""),
walletAddress: recipientName
? recipientAddress ?? undefined
: undefined,
}}
onChange={setRecipientInput}
isLoading={recipientState === "loading"}
value={recipientInput}
avatarUrlProps={{
url: recipientAvatar || "",
isLoading: recipientState === "loading",
address: recipientAddress ?? undefined,
}}
onLeftIconClick={() => {
resetRecipient();
setStartedFirstMessage(false);
setConversationTopic("");
}}
onRightIconClick={() => {
if (activeTab === "messages") {
void deny([recipientAddress]);
setChangedConsentCount(changedConsentCount + 1);
} else if (activeTab === "blocked") {
void allow([recipientAddress]);
setChangedConsentCount(changedConsentCount + 1);
}
}}
activeTab={activeTab}
/>
);
};