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
|
|
|
|
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
|
|
|
|
width: parent.width
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
//% "Enter a valid chat key or ENS username"
|
|
|
|
validationError = qsTrId("enter-a-valid-chat-key-or-ens-username");
|
|
|
|
pubKey = ""
|
|
|
|
ensUsername = "";
|
|
|
|
} else if (profileModel.profile.pubKey === chatKey.text) {
|
|
|
|
//% "Can't chat with yourself"
|
|
|
|
validationError = qsTrId("can-t-chat-with-yourself");
|
|
|
|
} else {
|
|
|
|
validationError = ""
|
|
|
|
}
|
|
|
|
return validationError === ""
|
|
|
|
}
|
|
|
|
|
|
|
|
Input {
|
|
|
|
id: chatKey
|
|
|
|
//% "Enter ENS username or chat key"
|
|
|
|
placeholderText: qsTrId("enter-contact-code")
|
|
|
|
Keys.onReleased: {
|
2021-06-30 12:49:07 +00:00
|
|
|
successMessage = ""
|
2021-03-31 19:14:09 +00:00
|
|
|
searchResults.pubKey = ""
|
|
|
|
if (!validate()) {
|
|
|
|
searchResults.showProfileNotFoundMessage = false
|
|
|
|
noContactsRect.visible = false
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chatKey.text = chatKey.text.trim();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt.callLater(resolveENS, chatKey.text)
|
|
|
|
}
|
|
|
|
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: {
|
|
|
|
if(chatKey.text == ""){
|
|
|
|
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"
|
|
|
|
validationError = qsTrId("can-t-chat-with-yourself");
|
|
|
|
} 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: {
|
|
|
|
chatKey.text = ""
|
|
|
|
chatKey.forceActiveFocus(Qt.MouseFocusReason)
|
|
|
|
searchResults.showProfileNotFoundMessage = false
|
|
|
|
searchResults.pubKey = pubKey = ""
|
|
|
|
noContactsRect.visible = false
|
|
|
|
searchResults.loading = false
|
2021-04-13 17:49:24 +00:00
|
|
|
validationError = ""
|
2021-03-31 19:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StyledText {
|
|
|
|
id: message
|
|
|
|
text: validationError || successMessage
|
|
|
|
visible: validationError !== "" || successMessage !== ""
|
|
|
|
font.pixelSize: 13
|
|
|
|
color: !!validationError ? Style.current.danger : Style.current.success
|
|
|
|
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
|
|
|
|
anchors.top: message.visible? message.bottom : 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
|
|
|
|
}
|
|
|
|
}
|