code refactoring

This commit is contained in:
sairaj mote 2023-03-10 03:03:47 +05:30
parent 720c6ff857
commit 82d1f6b888

View File

@ -1741,8 +1741,8 @@
getRef('contacts').classList.add('hide-on-mobile') getRef('contacts').classList.add('hide-on-mobile')
getRef('main_navbar').classList.add('hide-on-mobile') getRef('main_navbar').classList.add('hide-on-mobile')
} else { } else {
if (activeChat.floID && !floGlobals.isMobileView) { if (activeChat.address && !floGlobals.isMobileView) {
history.replaceState(null, null, `#/chat_page/messages?floId=${activeChat.floID}`); history.replaceState(null, null, `#/chat_page/messages?floId=${activeChat.address}`);
} else { } else {
history.replaceState(null, null, '#/chat_page'); history.replaceState(null, null, '#/chat_page');
getRef('chat_view').classList.add('hidden') getRef('chat_view').classList.add('hidden')
@ -2104,7 +2104,7 @@
if (document.visibilityState === "hidden") { if (document.visibilityState === "hidden") {
// code if page is hidden // code if page is hidden
} else { } else {
if (activeChat.floID) { if (activeChat.address) {
if (!chatScrollInfo.isScrolledUp) { if (!chatScrollInfo.isScrolledUp) {
scrollToBottom() scrollToBottom()
} }
@ -2532,7 +2532,7 @@
} }
}).catch(error => console.error(error)) }).catch(error => console.error(error))
if (prepend) { if (prepend) {
activeChat.floID = floID activeChat.address = floID
getRef('chats_list').querySelectorAll('.active').forEach(child => child.classList.remove('active')) getRef('chats_list').querySelectorAll('.active').forEach(child => child.classList.remove('active'))
} }
} }
@ -2546,7 +2546,7 @@
<div class="initial flex align-center"> <div class="initial flex align-center">
${initial} ${initial}
</div> </div>
${type === 'pipeline' ? html`<div class="tag">${tag} Multisig</div>` : ''} ${type === 'pipeline' ? html`<div class="tag">${tag} Multisig ${messenger.pipeline[floID].disabled ? ' - Completed' : ''}</div>` : ''}
<h4 class="name">${name}</h4> <h4 class="name">${name}</h4>
${type === 'contact' ? html`<p class="contact__flo-address wrap-around">${floID}</p>` : ''} ${type === 'contact' ? html`<p class="contact__flo-address wrap-around">${floID}</p>` : ''}
</div> </div>
@ -2664,10 +2664,13 @@
</p>` </p>`
break; break;
case 'BROADCAST': case 'BROADCAST':
let redirectTo = `https://live.blockcypher.com/btc/tx/`
if (messenger.pipeline[activeChat.address].model === 'flo_multisig')
redirectTo = `https://flosight.ranchimall.net/tx/`
return html.node`<div class="pipeline-event pipeline-event--signed event-card flex align-center"> return html.node`<div class="pipeline-event pipeline-event--signed event-card flex align-center">
<svg class="icon margin-right-0-5" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><g><rect fill="none" height="24" width="24"/></g><g><path d="M23,12l-2.44-2.79l0.34-3.69l-3.61-0.82L15.4,1.5L12,2.96L8.6,1.5L6.71,4.69L3.1,5.5L3.44,9.2L1,12l2.44,2.79l-0.34,3.7 l3.61,0.82L8.6,22.5l3.4-1.47l3.4,1.46l1.89-3.19l3.61-0.82l-0.34-3.69L23,12z M10.09,16.72l-3.8-3.81l1.48-1.48l2.32,2.33 l5.85-5.87l1.48,1.48L10.09,16.72z"/></g></svg> <svg class="icon margin-right-0-5" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><g><rect fill="none" height="24" width="24"/></g><g><path d="M23,12l-2.44-2.79l0.34-3.69l-3.61-0.82L15.4,1.5L12,2.96L8.6,1.5L6.71,4.69L3.1,5.5L3.44,9.2L1,12l2.44,2.79l-0.34,3.7 l3.61,0.82L8.6,22.5l3.4-1.47l3.4,1.46l1.89-3.19l3.61-0.82l-0.34-3.69L23,12z M10.09,16.72l-3.8-3.81l1.48-1.48l2.32,2.33 l5.85-5.87l1.48,1.48L10.09,16.72z"/></g></svg>
<h4>Transaction approved</h4> <h4>Transaction approved</h4>
<a class="button--small margin-left-0-5" href="${`https://www.blockchain.com/btc/tx/${msg.txid}`}" target="_blank">View on blockchain</a> <a class="button--small margin-left-0-5" href="${`${redirectTo}${msg.txid}`}" target="_blank">View on blockchain</a>
</div>` </div>`
break; break;
} }
@ -2983,10 +2986,9 @@
fill: 'forwards' fill: 'forwards'
} }
for (let messageId in messagesData) { for (let messageId in messagesData) {
console.log(messageId, messagesData[messageId])
const { category, floID, time, message, sender, groupID, admin, name, pipeID, unconfirmed } = messagesData[messageId] const { category, floID, time, message, sender, groupID, admin, name, pipeID, unconfirmed } = messagesData[messageId]
// code to run if a chat is opened // code to run if a chat is opened
if (activeChat && activeChat.floID === (floID || groupID || pipeID)) { if (activeChat && activeChat.address === (floID || groupID || pipeID)) {
if (!sentByMe && sender && sender === floDapps.user.id) { if (!sentByMe && sender && sender === floDapps.user.id) {
// if message is sent by me, then dont add it to the chat // if message is sent by me, then dont add it to the chat
} else { } else {
@ -2997,7 +2999,7 @@
scrollToBottom() scrollToBottom()
} }
// remove encryption badge if it exists // remove encryption badge if it exists
if (!groupID && floGlobals.pubKeys[activeChat.floID] && floID !== floDapps.user.id) { if (!groupID && floGlobals.pubKeys[activeChat.address] && floID !== floDapps.user.id) {
document.getElementById('warn_no_encryption')?.remove() document.getElementById('warn_no_encryption')?.remove()
} }
} }
@ -3055,7 +3057,7 @@
if (chatCard.querySelector('.time')) if (chatCard.querySelector('.time'))
chatCard.querySelector('.time').textContent = getFormattedTime(time, 'relative') chatCard.querySelector('.time').textContent = getFormattedTime(time, 'relative')
if (activeChat.floID === (floID || groupID || pipeID)) { if (activeChat.address === (floID || groupID || pipeID)) {
if (chatScrollInfo.isScrolledUp) if (chatScrollInfo.isScrolledUp)
getRef('scroll_to_bottom').classList.add('new-message') getRef('scroll_to_bottom').classList.add('new-message')
else { else {
@ -3226,7 +3228,7 @@
}) })
floGlobals.uiState = {} floGlobals.uiState = {}
getRef('chat_details_button').addEventListener('click', e => { getRef('chat_details_button').addEventListener('click', e => {
floGlobals.activeFloID = activeChat.floID floGlobals.activeFloID = activeChat.address
openPopup('contact_details_popup') openPopup('contact_details_popup')
}) })
@ -3646,7 +3648,7 @@
async function sendMessage() { async function sendMessage() {
if (!floGlobals.isMobileView) if (!floGlobals.isMobileView)
getRef('type_message').focusIn() getRef('type_message').focusIn()
let receiver = activeChat.floID let receiver = activeChat.address
let message = getRef('type_message').value.trim(); let message = getRef('type_message').value.trim();
getRef('type_message').value = '' getRef('type_message').value = ''
if (message === '') return if (message === '') return
@ -3716,7 +3718,7 @@
if (popupStack.items.find(elem => elem.popup.id === 'new_message_popup')) { if (popupStack.items.find(elem => elem.popup.id === 'new_message_popup')) {
renderContactList() renderContactList()
} }
if (activeChat.floID === floID) if (activeChat.address === floID)
updateChatHeaderName(name) updateChatHeaderName(name)
}).catch(error => notify(error, "error")); }).catch(error => notify(error, "error"));
} }
@ -3772,10 +3774,10 @@
} }
function scrollToBottom(smooth = false) { function scrollToBottom(smooth = false) {
if (activeChat.floID) { if (activeChat.address) {
messenger.removeMark(activeChat.floID, 'unread') messenger.removeMark(activeChat.address, 'unread')
if (getChatCard(activeChat.floID)) if (getChatCard(activeChat.address))
getChatCard(activeChat.floID).classList.remove('unread') getChatCard(activeChat.address).classList.remove('unread')
} }
getRef('scroll_to_bottom').classList.remove('new-message') getRef('scroll_to_bottom').classList.remove('new-message')
getRef('messages_container').scrollTo({ top: getRef('messages_container').scrollHeight, behavior: smooth ? 'smooth' : undefined }) getRef('messages_container').scrollTo({ top: getRef('messages_container').scrollHeight, behavior: smooth ? 'smooth' : undefined })
@ -3822,13 +3824,13 @@
renderedDates = {} renderedDates = {}
// save typed message from previous chat // save typed message from previous chat
if (getRef('type_message').value.trim() !== '') { if (getRef('type_message').value.trim() !== '') {
floGlobals.typedMessages[activeChat.floID] = getRef('type_message').value floGlobals.typedMessages[activeChat.address] = getRef('type_message').value
} else { } else {
delete floGlobals.typedMessages[activeChat.floID] delete floGlobals.typedMessages[activeChat.address]
} }
// restore typed message if any // restore typed message if any
getRef('type_message').value = floGlobals.typedMessages[floID] || '' getRef('type_message').value = floGlobals.typedMessages[floID] || ''
activeChat.floID = floID activeChat.address = floID
activeChat.type = getFloIdType(floID) activeChat.type = getFloIdType(floID)
updateChatHeaderName(getContactName(floID)); updateChatHeaderName(getContactName(floID));
const chatCard = getChatCard(floID); const chatCard = getChatCard(floID);
@ -3888,7 +3890,6 @@
details = await btcOperator.parseTransaction(floGlobals.pipelineTxHex) details = await btcOperator.parseTransaction(floGlobals.pipelineTxHex)
break; break;
} }
console.log(details)
const { inputs, outputs, fee } = details const { inputs, outputs, fee } = details
const { s: signsDone, r: minSignsRequired, t: totalMembers } = inputs[0]?.signed || {} const { s: signsDone, r: minSignsRequired, t: totalMembers } = inputs[0]?.signed || {}
const pendingSigns = minSignsRequired - signsDone const pendingSigns = minSignsRequired - signsDone
@ -4152,12 +4153,12 @@
function updateChatCards({ name, floID }) { function updateChatCards({ name, floID }) {
const type = getFloIdType(floID) const type = getFloIdType(floID)
if (activeChat.floID && activeChat.floID === clickedContact.floID) { if (activeChat.address && activeChat.address === clickedContact.floID) {
updateChatHeaderName(name) updateChatHeaderName(name)
} }
if (type === 'plain') { if (type === 'plain') {
getRef('contact_initial').textContent = name.charAt(0) getRef('contact_initial').textContent = name.charAt(0)
if (activeChat.floID && activeChat.floID === clickedContact.floID) { if (activeChat.address && activeChat.address === clickedContact.floID) {
getRef('receiver_initial').textContent = name.charAt(0) getRef('receiver_initial').textContent = name.charAt(0)
} }
document.querySelectorAll(`.contact[data-flo-address="${floID}"]`).forEach(contact => { document.querySelectorAll(`.contact[data-flo-address="${floID}"]`).forEach(contact => {