status-desktop/ui/app/AppLayouts/Chat/components/PrivateChatPopup.qml

197 lines
6.8 KiB
QML
Raw Normal View History

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"
import "../../../../shared/status"
2020-05-28 12:56:43 +00:00
import "./"
2020-05-29 16:27:50 +00:00
ModalPopup {
property string validationError: ""
2020-06-25 13:26:58 +00:00
property string pubKey : "";
property string ensUsername : "";
function validate() {
2020-06-25 13:26:58 +00:00
if (!Utils.isChatKey(chatKey.text) && !Utils.isValidETHNamePrefix(chatKey.text)) {
2021-02-18 16:36:05 +00:00
//% "Enter a valid chat key or ENS username"
validationError = qsTrId("enter-a-valid-chat-key-or-ens-username");
pubKey = ""
ensUsername.text = "";
} else if (profileModel.profile.pubKey === chatKey.text) {
2021-02-18 16:36:05 +00:00
//% "Can't chat with yourself"
validationError = qsTrId("can-t-chat-with-yourself");
} else {
validationError = ""
}
return validationError === ""
}
2020-07-11 21:56:28 +00:00
property var resolveENS: Backpressure.debounce(popup, 500, function (ensName){
noContactsRect.visible = false
searchResults.loading = true
searchResults.showProfileNotFoundMessage = false
chatsModel.resolveENS(ensName)
2020-07-11 21:56:28 +00:00
});
2020-06-25 13:26:58 +00:00
function onKeyReleased(){
searchResults.pubKey = ""
if (!validate()) {
searchResults.showProfileNotFoundMessage = false
noContactsRect.visible = false
return;
}
chatKey.text = chatKey.text.trim();
if (Utils.isChatKey(chatKey.text)){
2020-06-25 13:26:58 +00:00
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)
2020-06-25 13:26:58 +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);
} else {
chatsModel.joinChatWithENS(pk, ensName);
}
popup.close();
}
2020-05-28 12:56:43 +00:00
id: popup
//% "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 = "";
ensUsername.text = "";
2020-05-29 18:38:11 +00:00
chatKey.forceActiveFocus(Qt.MouseFocusReason)
existingContacts.visible = profileModel.contacts.list.hasAddedContacts()
noContactsRect.visible = !existingContacts.visible
2020-05-29 18:38:11 +00:00
}
Input {
id: chatKey
//% "Enter ENS username or chat key"
placeholderText: qsTrId("enter-contact-code")
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();
}
textField.anchors.rightMargin: clearBtn.width + Style.current.padding + 2
Connections {
target: chatsModel
onEnsWasResolved: {
if(chatKey.text == ""){
ensUsername.text = "";
pubKey = "";
} else if(resolvedPubKey == ""){
ensUsername.text = "";
searchResults.pubKey = pubKey = "";
searchResults.showProfileNotFoundMessage = true
} else {
if (profileModel.profile.pubKey === resolvedPubKey) {
2021-02-18 16:36:05 +00:00
//% "Can't chat with yourself"
popup.validationError = qsTrId("can-t-chat-with-yourself");
} else {
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;
}
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 = popup.pubKey = ""
noContactsRect.visible = false
}
}
2020-05-29 16:27:50 +00:00
}
StyledText {
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
anchors.topMargin: Style.current.smallPadding
anchors.horizontalCenter: parent.horizontalCenter
2020-06-25 13:26:58 +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
}
PrivateChatPopupSearchResults {
id: searchResults
anchors.top: existingContacts.visible ? existingContacts.bottom : chatKey.bottom
anchors.topMargin: Style.current.padding
hasExistingContacts: existingContacts.visible
loading: false
onResultClicked: validateAndJoin(popup.pubKey, chatKey.text)
onAddToContactsButtonClicked: profileModel.contacts.addContact(popup.pubKey)
}
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
}
2020-05-28 12:56:43 +00:00
}
/*##^##
Designer {
D{i:0;height:300;width:300}
}
##^##*/