2021-03-31 19:14:09 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
import QtQuick.Controls 2.13
|
|
|
|
import QtGraphicalEffects 1.13
|
|
|
|
import "../imports"
|
|
|
|
import "../shared/status"
|
|
|
|
|
|
|
|
Item {
|
|
|
|
property string validationError: ""
|
|
|
|
property string successMessage: ""
|
|
|
|
property alias chatKey: chatKey
|
|
|
|
property alias existingContacts: existingContacts
|
|
|
|
property alias noContactsRect: noContactsRect
|
|
|
|
property string pubKey : ""
|
2021-04-13 17:49:24 +00:00
|
|
|
property int searchResultsWidth : 0
|
|
|
|
property alias loading : searchResults.loading
|
2021-03-31 19:14:09 +00:00
|
|
|
property string ensUsername : ""
|
|
|
|
property bool showCheckbox: false
|
2021-04-13 17:49:24 +00:00
|
|
|
property bool showContactList: true
|
2021-08-04 12:34:06 +00:00
|
|
|
property bool showSearch: true
|
2021-04-13 17:49:24 +00:00
|
|
|
signal userClicked(bool isContact, string pubKey, string ensName, string address)
|
2021-03-31 19:14:09 +00:00
|
|
|
property var pubKeys: ([])
|
2021-05-17 09:56:55 +00:00
|
|
|
property bool hideCommunityMembers: false
|
2021-07-13 09:57:11 +00:00
|
|
|
property bool addContactEnabled: true
|
2021-03-31 19:14:09 +00:00
|
|
|
|
|
|
|
id: root
|
2021-07-15 11:12:46 +00:00
|
|
|
height: childrenRect.height + 24
|
2021-03-31 19:14:09 +00:00
|
|
|
|
|
|
|
property var resolveENS: Backpressure.debounce(root, 500, function (ensName) {
|
|
|
|
noContactsRect.visible = false
|
|
|
|
searchResults.loading = true
|
|
|
|
searchResults.showProfileNotFoundMessage = false
|
2021-06-17 13:51:59 +00:00
|
|
|
chatsModel.ensView.resolveENS(ensName)
|
2021-03-31 19:14:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function validate() {
|
|
|
|
if (!Utils.isChatKey(chatKey.text) && !Utils.isValidETHNamePrefix(chatKey.text)) {
|
2021-09-17 08:49:58 +00:00
|
|
|
root.validationError = qsTr("Enter a valid chat key or ENS username");
|
2021-03-31 19:14:09 +00:00
|
|
|
pubKey = ""
|
|
|
|
ensUsername = "";
|
|
|
|
} else if (profileModel.profile.pubKey === chatKey.text) {
|
|
|
|
//% "Can't chat with yourself"
|
2021-07-13 16:05:44 +00:00
|
|
|
root.validationError = qsTrId("can-t-chat-with-yourself");
|
2021-03-31 19:14:09 +00:00
|
|
|
} else {
|
2021-07-13 16:05:44 +00:00
|
|
|
root.validationError = "";
|
2021-03-31 19:14:09 +00:00
|
|
|
}
|
2021-07-13 16:05:44 +00:00
|
|
|
return root.validationError === "";
|
2021-03-31 19:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Input {
|
|
|
|
id: chatKey
|
|
|
|
//% "Enter ENS username or chat key"
|
|
|
|
placeholderText: qsTrId("enter-contact-code")
|
2021-08-04 12:34:06 +00:00
|
|
|
visible: showSearch
|
2021-03-31 19:14:09 +00:00
|
|
|
Keys.onReleased: {
|
2021-07-13 16:05:44 +00:00
|
|
|
successMessage = "";
|
|
|
|
searchResults.pubKey = "";
|
|
|
|
if (chatKey.text !== "") {
|
|
|
|
if (!validate()) {
|
|
|
|
searchResults.showProfileNotFoundMessage = false;
|
|
|
|
noContactsRect.visible = false;
|
|
|
|
return;
|
|
|
|
}
|
2021-03-31 19:14:09 +00:00
|
|
|
|
2021-07-13 16:05:44 +00:00
|
|
|
chatKey.text = chatKey.text.trim();
|
2021-03-31 19:14:09 +00:00
|
|
|
|
2021-07-13 16:05:44 +00:00
|
|
|
if (Utils.isChatKey(chatKey.text)) {
|
|
|
|
pubKey = chatKey.text;
|
|
|
|
if (!profileModel.contacts.isAdded(pubKey)) {
|
|
|
|
searchResults.username = utilsModel.generateAlias(pubKey);
|
|
|
|
searchResults.userAlias = Utils.compactAddress(pubKey, 4);
|
|
|
|
searchResults.pubKey = pubKey
|
|
|
|
}
|
|
|
|
noContactsRect.visible = false;
|
|
|
|
return;
|
2021-03-31 19:14:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 16:05:44 +00:00
|
|
|
Qt.callLater(resolveENS, chatKey.text);
|
|
|
|
} else {
|
|
|
|
root.validationError = "";
|
|
|
|
}
|
2021-03-31 19:14:09 +00:00
|
|
|
}
|
|
|
|
textField.anchors.rightMargin: clearBtn.width + Style.current.padding + 2
|
|
|
|
|
|
|
|
Connections {
|
2021-06-17 13:51:59 +00:00
|
|
|
target: chatsModel.ensView
|
2021-03-31 19:14:09 +00:00
|
|
|
onEnsWasResolved: {
|
2021-07-13 16:05:44 +00:00
|
|
|
if (chatKey.text == "") {
|
2021-03-31 19:14:09 +00:00
|
|
|
ensUsername.text = "";
|
|
|
|
pubKey = "";
|
|
|
|
} else if(resolvedPubKey == ""){
|
|
|
|
ensUsername.text = "";
|
|
|
|
searchResults.pubKey = pubKey = "";
|
2021-04-13 17:49:24 +00:00
|
|
|
searchResults.address = "";
|
2021-03-31 19:14:09 +00:00
|
|
|
searchResults.showProfileNotFoundMessage = true
|
|
|
|
} else {
|
|
|
|
if (profileModel.profile.pubKey === resolvedPubKey) {
|
|
|
|
//% "Can't chat with yourself"
|
2021-07-13 16:05:44 +00:00
|
|
|
root.validationError = qsTrId("can-t-chat-with-yourself");
|
2021-03-31 19:14:09 +00:00
|
|
|
} else {
|
2021-06-17 13:51:59 +00:00
|
|
|
searchResults.username = chatsModel.ensView.formatENSUsername(chatKey.text)
|
2021-03-31 19:14:09 +00:00
|
|
|
let userAlias = utilsModel.generateAlias(resolvedPubKey)
|
|
|
|
userAlias = userAlias.length > 20 ? userAlias.substring(0, 19) + "..." : userAlias
|
|
|
|
searchResults.userAlias = userAlias + " • " + Utils.compactAddress(resolvedPubKey, 4)
|
|
|
|
searchResults.pubKey = pubKey = resolvedPubKey;
|
2021-04-13 17:49:24 +00:00
|
|
|
searchResults.address = resolvedAddress;
|
2021-03-31 19:14:09 +00:00
|
|
|
}
|
|
|
|
searchResults.showProfileNotFoundMessage = false
|
|
|
|
}
|
|
|
|
searchResults.loading = false;
|
|
|
|
noContactsRect.visible = pubKey === "" && ensUsername.text === "" && !profileModel.contacts.list.hasAddedContacts() && !profileNotFoundMessage.visible
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusIconButton {
|
|
|
|
id: clearBtn
|
|
|
|
icon.name: "close-icon"
|
|
|
|
type: "secondary"
|
|
|
|
visible: chatKey.text !== ""
|
|
|
|
icon.width: 14
|
|
|
|
icon.height: 14
|
|
|
|
width: 14
|
|
|
|
height: 14
|
|
|
|
anchors.right: parent.right
|
|
|
|
anchors.rightMargin: Style.current.padding
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
onClicked: {
|
2021-07-13 16:05:44 +00:00
|
|
|
chatKey.text = "";
|
|
|
|
chatKey.forceActiveFocus(Qt.MouseFocusReason);
|
|
|
|
searchResults.showProfileNotFoundMessage = false;
|
|
|
|
searchResults.pubKey = pubKey = "";
|
|
|
|
noContactsRect.visible = false;
|
|
|
|
searchResults.loading = false;
|
|
|
|
root.validationError = "";
|
2021-03-31 19:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StyledText {
|
|
|
|
id: message
|
2021-07-13 16:05:44 +00:00
|
|
|
text: root.validationError || successMessage
|
|
|
|
visible: root.validationError !== "" || successMessage !== ""
|
2021-03-31 19:14:09 +00:00
|
|
|
font.pixelSize: 13
|
2021-07-13 16:05:44 +00:00
|
|
|
color: !!root.validationError ? Style.current.danger : Style.current.success
|
2021-03-31 19:14:09 +00:00
|
|
|
anchors.top: chatKey.bottom
|
|
|
|
anchors.topMargin: Style.current.smallPadding
|
|
|
|
anchors.horizontalCenter: parent.horizontalCenter
|
|
|
|
}
|
|
|
|
|
|
|
|
ExistingContacts {
|
|
|
|
id: existingContacts
|
2021-04-13 17:49:24 +00:00
|
|
|
visible: showContactList
|
2021-05-17 09:56:55 +00:00
|
|
|
hideCommunityMembers: root.hideCommunityMembers
|
2021-06-30 12:49:07 +00:00
|
|
|
anchors.topMargin: this.height > 0 ? Style.current.halfPadding : 0
|
2021-08-04 12:34:06 +00:00
|
|
|
anchors.top: {
|
|
|
|
if (message.visible) {
|
|
|
|
return message.bottom
|
|
|
|
}
|
|
|
|
if (chatKey.visible) {
|
|
|
|
return chatKey.bottom
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 19:14:09 +00:00
|
|
|
showCheckbox: root.showCheckbox
|
|
|
|
filterText: chatKey.text
|
|
|
|
pubKeys: root.pubKeys
|
|
|
|
onContactClicked: function (contact) {
|
|
|
|
if (!contact || typeof contact === "string") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const index = root.pubKeys.indexOf(contact.pubKey)
|
|
|
|
const pubKeysCopy = Object.assign([], root.pubKeys)
|
|
|
|
if (index === -1) {
|
|
|
|
pubKeysCopy.push(contact.pubKey)
|
|
|
|
} else {
|
|
|
|
pubKeysCopy.splice(index, 1)
|
|
|
|
}
|
|
|
|
root.pubKeys = pubKeysCopy
|
|
|
|
|
2021-04-13 17:49:24 +00:00
|
|
|
userClicked(true, contact.pubKey, profileModel.contacts.addedContacts.userName(contact.pubKey, contact.name), contact.address)
|
2021-03-31 19:14:09 +00:00
|
|
|
}
|
|
|
|
expanded: !searchResults.loading && pubKey === "" && !searchResults.showProfileNotFoundMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchResults {
|
|
|
|
id: searchResults
|
2021-06-30 12:49:07 +00:00
|
|
|
anchors.top: existingContacts.visible ? existingContacts.bottom :
|
|
|
|
message.visible? message.bottom : chatKey.bottom
|
|
|
|
anchors.topMargin: Style.current.halfPadding
|
2021-03-31 19:14:09 +00:00
|
|
|
hasExistingContacts: existingContacts.visible
|
|
|
|
loading: false
|
2021-04-13 17:49:24 +00:00
|
|
|
width: searchResultsWidth > 0 ? searchResultsWidth : parent.width
|
2021-07-13 09:57:11 +00:00
|
|
|
addContactEnabled: root.addContactEnabled
|
2021-03-31 19:14:09 +00:00
|
|
|
onResultClicked: {
|
|
|
|
if (!validate()) {
|
|
|
|
return
|
|
|
|
}
|
2021-04-13 17:49:24 +00:00
|
|
|
userClicked(false, pubKey, chatKey.text, searchResults.address)
|
2021-03-31 19:14:09 +00:00
|
|
|
}
|
|
|
|
onAddToContactsButtonClicked: profileModel.contacts.addContact(pubKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
NoFriendsRectangle {
|
|
|
|
id: noContactsRect
|
2021-04-13 17:49:24 +00:00
|
|
|
visible: showContactList
|
2021-03-31 19:14:09 +00:00
|
|
|
anchors.top: chatKey.bottom
|
|
|
|
anchors.topMargin: Style.current.xlPadding * 3
|
|
|
|
anchors.horizontalCenter: parent.horizontalCenter
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
}
|
|
|
|
}
|