2020-06-17 19:18:31 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
import QtQuick.Controls 2.13
|
|
|
|
import QtQuick.Layouts 1.13
|
2020-05-28 12:56:43 +00:00
|
|
|
import "../../../../imports"
|
|
|
|
import "../../../../shared"
|
2020-11-13 15:28:27 +00:00
|
|
|
import "../../../../shared/status"
|
2020-05-28 12:56:43 +00:00
|
|
|
import "./"
|
|
|
|
|
2020-05-29 16:27:50 +00:00
|
|
|
ModalPopup {
|
2020-06-22 15:51:15 +00:00
|
|
|
property string validationError: ""
|
|
|
|
|
2020-06-25 13:26:58 +00:00
|
|
|
property string pubKey : "";
|
|
|
|
property string ensUsername : "";
|
2020-11-13 15:28:27 +00:00
|
|
|
|
2020-06-22 15:51:15 +00:00
|
|
|
function validate() {
|
2020-06-25 13:26:58 +00:00
|
|
|
if (!Utils.isChatKey(chatKey.text) && !Utils.isValidETHNamePrefix(chatKey.text)) {
|
2021-02-08 12:21:23 +00:00
|
|
|
validationError = qsTr("Enter a valid chat key or ENS username");
|
|
|
|
pubKey = ""
|
2020-07-07 19:03:23 +00:00
|
|
|
ensUsername.text = "";
|
2020-10-14 07:41:09 +00:00
|
|
|
} else if (profileModel.profile.pubKey === chatKey.text) {
|
|
|
|
validationError = qsTr("Can't chat with yourself");
|
2020-06-22 15:51:15 +00:00
|
|
|
} else {
|
|
|
|
validationError = ""
|
|
|
|
}
|
|
|
|
return validationError === ""
|
|
|
|
}
|
|
|
|
|
2020-07-11 21:56:28 +00:00
|
|
|
property var resolveENS: Backpressure.debounce(popup, 500, function (ensName){
|
2021-02-08 12:21:23 +00:00
|
|
|
noContactsRect.visible = false
|
|
|
|
searchResults.loading = true
|
|
|
|
searchResults.showProfileNotFoundMessage = false
|
2020-07-07 19:03:23 +00:00
|
|
|
chatsModel.resolveENS(ensName)
|
2020-07-11 21:56:28 +00:00
|
|
|
});
|
2020-07-07 19:03:23 +00:00
|
|
|
|
2020-06-25 13:26:58 +00:00
|
|
|
function onKeyReleased(){
|
2021-02-08 12:21:23 +00:00
|
|
|
searchResults.pubKey = ""
|
2020-07-07 19:03:23 +00:00
|
|
|
if (!validate()) {
|
2021-02-08 12:21:23 +00:00
|
|
|
searchResults.showProfileNotFoundMessage = false
|
|
|
|
noContactsRect.visible = false
|
2020-07-07 19:03:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-22 15:51:15 +00:00
|
|
|
|
2020-07-07 19:03:23 +00:00
|
|
|
chatKey.text = chatKey.text.trim();
|
|
|
|
|
2021-02-08 12:21:23 +00:00
|
|
|
if (Utils.isChatKey(chatKey.text)){
|
2020-06-25 13:26:58 +00:00
|
|
|
pubKey = chatKey.text;
|
2021-02-08 12:21:23 +00:00
|
|
|
if (!profileModel.contacts.isAdded(pubKey)) {
|
|
|
|
searchResults.username = utilsModel.generateAlias(pubKey)
|
|
|
|
searchResults.userAlias = Utils.compactAddress(pubKey, 4)
|
|
|
|
searchResults.pubKey = pubKey
|
|
|
|
}
|
|
|
|
noContactsRect.visible = false
|
2020-06-16 18:04:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-07 19:03:23 +00:00
|
|
|
|
|
|
|
Qt.callLater(resolveENS, chatKey.text)
|
2020-06-25 13:26:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 12:21:23 +00:00
|
|
|
function validateAndJoin(pk, ensName) {
|
|
|
|
if (!validate() || pk.trim() === "" || validationError !== "") return;
|
|
|
|
doJoin(pk, ensName)
|
|
|
|
}
|
|
|
|
function doJoin(pk, ensName) {
|
|
|
|
if(Utils.isChatKey(pk)){
|
|
|
|
chatsModel.joinChat(pk, Constants.chatTypeOneToOne);
|
2020-11-17 14:37:00 +00:00
|
|
|
} else {
|
2021-02-08 12:21:23 +00:00
|
|
|
chatsModel.joinChatWithENS(pk, ensName);
|
2020-11-17 14:37:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 16:24:21 +00:00
|
|
|
popup.close();
|
|
|
|
}
|
|
|
|
|
2020-05-28 12:56:43 +00:00
|
|
|
id: popup
|
2020-07-06 20:39:55 +00:00
|
|
|
//% "New chat"
|
|
|
|
title: qsTrId("new-chat")
|
2020-05-29 16:27:50 +00:00
|
|
|
|
2020-05-29 18:38:11 +00:00
|
|
|
onOpened: {
|
|
|
|
chatKey.text = "";
|
2020-06-25 13:26:58 +00:00
|
|
|
pubKey = "";
|
2020-07-07 19:03:23 +00:00
|
|
|
ensUsername.text = "";
|
2020-05-29 18:38:11 +00:00
|
|
|
chatKey.forceActiveFocus(Qt.MouseFocusReason)
|
2021-02-08 12:21:23 +00:00
|
|
|
existingContacts.visible = profileModel.contacts.list.hasAddedContacts()
|
|
|
|
noContactsRect.visible = !existingContacts.visible
|
2020-05-29 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 16:24:21 +00:00
|
|
|
Input {
|
|
|
|
id: chatKey
|
2020-07-06 20:39:55 +00:00
|
|
|
//% "Enter ENS username or chat key"
|
|
|
|
placeholderText: qsTrId("enter-contact-code")
|
2021-02-08 12:21:23 +00:00
|
|
|
Keys.onEnterPressed: validateAndJoin(popup.pubKey, chatKey.text)
|
|
|
|
Keys.onReturnPressed: validateAndJoin(popup.pubKey, chatKey.text)
|
2020-06-25 13:26:58 +00:00
|
|
|
Keys.onReleased: {
|
|
|
|
onKeyReleased();
|
2020-06-22 15:51:15 +00:00
|
|
|
}
|
2021-02-08 12:21:23 +00:00
|
|
|
textField.anchors.rightMargin: clearBtn.width + Style.current.padding + 2
|
2020-07-07 19:03:23 +00:00
|
|
|
|
|
|
|
Connections {
|
|
|
|
target: chatsModel
|
|
|
|
onEnsWasResolved: {
|
|
|
|
if(chatKey.text == ""){
|
2021-02-08 12:21:23 +00:00
|
|
|
ensUsername.text = "";
|
2020-07-07 19:03:23 +00:00
|
|
|
pubKey = "";
|
|
|
|
} else if(resolvedPubKey == ""){
|
2021-02-08 12:21:23 +00:00
|
|
|
ensUsername.text = "";
|
|
|
|
searchResults.pubKey = pubKey = "";
|
|
|
|
searchResults.showProfileNotFoundMessage = true
|
2020-07-07 19:03:23 +00:00
|
|
|
} else {
|
2020-11-17 14:37:00 +00:00
|
|
|
if (profileModel.profile.pubKey === resolvedPubKey) {
|
2021-02-08 12:21:23 +00:00
|
|
|
popup.validationError = qsTr("Can't chat with yourself");
|
2020-11-17 14:37:00 +00:00
|
|
|
} else {
|
2021-02-08 12:21:23 +00:00
|
|
|
searchResults.username = chatsModel.formatENSUsername(chatKey.text)
|
|
|
|
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;
|
2020-11-17 14:37:00 +00:00
|
|
|
}
|
2021-02-08 12:21:23 +00:00
|
|
|
searchResults.showProfileNotFoundMessage = false
|
2020-07-07 19:03:23 +00:00
|
|
|
}
|
2021-02-08 12:21:23 +00:00
|
|
|
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 = popup.pubKey = ""
|
|
|
|
noContactsRect.visible = false
|
2020-07-07 19:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 16:27:50 +00:00
|
|
|
}
|
2021-02-08 12:21:23 +00:00
|
|
|
|
2020-07-13 18:45:54 +00:00
|
|
|
StyledText {
|
2021-02-08 12:21:23 +00:00
|
|
|
id: validationErrorMessage
|
|
|
|
text: popup.validationError
|
|
|
|
visible: popup.validationError !== ""
|
|
|
|
font.pixelSize: 13
|
|
|
|
color: Style.current.danger
|
2020-06-25 13:26:58 +00:00
|
|
|
anchors.top: chatKey.bottom
|
2021-02-08 12:21:23 +00:00
|
|
|
anchors.topMargin: Style.current.smallPadding
|
|
|
|
anchors.horizontalCenter: parent.horizontalCenter
|
2020-06-25 13:26:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 12:21:23 +00:00
|
|
|
PrivateChatPopupExistingContacts {
|
|
|
|
id: existingContacts
|
|
|
|
anchors.topMargin: this.height > 0 ? Style.current.xlPadding : 0
|
|
|
|
anchors.top: chatKey.bottom
|
|
|
|
filterText: chatKey.text
|
|
|
|
onContactClicked: function (contact) {
|
|
|
|
doJoin(contact.pubKey, profileModel.contacts.addedContacts.userName(contact.pubKey, contact.name))
|
|
|
|
}
|
|
|
|
expanded: !searchResults.loading && popup.pubKey === "" && !searchResults.showProfileNotFoundMessage
|
|
|
|
}
|
2020-08-10 02:14:21 +00:00
|
|
|
|
2021-02-08 12:21:23 +00:00
|
|
|
PrivateChatPopupSearchResults {
|
|
|
|
id: searchResults
|
|
|
|
anchors.top: existingContacts.visible ? existingContacts.bottom : chatKey.bottom
|
|
|
|
anchors.topMargin: Style.current.padding
|
|
|
|
hasExistingContacts: existingContacts.visible
|
|
|
|
loading: false
|
2021-02-01 12:06:21 +00:00
|
|
|
|
2021-02-08 12:21:23 +00:00
|
|
|
onResultClicked: validateAndJoin(popup.pubKey, chatKey.text)
|
|
|
|
onAddToContactsButtonClicked: profileModel.contacts.addContact(popup.pubKey)
|
2020-06-16 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 12:21:23 +00:00
|
|
|
NoFriendsRectangle {
|
|
|
|
id: noContactsRect
|
|
|
|
anchors.top: chatKey.bottom
|
|
|
|
anchors.topMargin: Style.current.xlPadding * 3
|
|
|
|
anchors.horizontalCenter: parent.horizontalCenter
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
2020-11-16 20:56:10 +00:00
|
|
|
}
|
2021-02-08 12:21:23 +00:00
|
|
|
|
2020-05-28 12:56:43 +00:00
|
|
|
}
|
2020-06-16 18:04:56 +00:00
|
|
|
|
|
|
|
/*##^##
|
|
|
|
Designer {
|
|
|
|
D{i:0;height:300;width:300}
|
|
|
|
}
|
|
|
|
##^##*/
|