status-desktop/ui/app/AppLayouts/Wallet/services/dapps/BCDappsProvider.qml

95 lines
2.6 KiB
QML
Raw Normal View History

import QtQuick 2.15
import AppLayouts.Wallet.services.dapps 1.0
import StatusQ.Core.Utils 0.1
import shared.stores 1.0
import utils 1.0
DAppsModel {
id: root
required property WalletConnectSDKBase bcSDK
readonly property int connectorId: Constants.StatusConnect
readonly property bool enabled: bcSDK.enabled
signal connected(string pairingId, string topic, string dAppUrl)
signal disconnected(string topic, string dAppUrl)
Connections {
target: root.bcSDK
enabled: root.enabled
function onSessionDelete(topic, err) {
const dapp = root.getByTopic(topic)
if (!dapp) {
console.warn("DApp not found for topic - cannot delete session", topic)
return
}
root.remove(topic)
root.disconnected(topic, dapp.url)
}
function onApproveSessionResult(proposalId, session, error) {
if (error) {
console.warn("Failed to approve session", error)
return
}
const dapp = d.sessionToDApp(session)
root.append(dapp)
root.connected(proposalId, dapp.topic, dapp.url)
}
}
QtObject {
id: d
function sessionToDApp(session) {
const dapp = session.peer.metadata
if (!!dapp.icons && dapp.icons.length > 0) {
dapp.iconUrl = dapp.icons[0]
} else {
dapp.iconUrl = ""
}
const accounts = DAppsHelpers.getAccountsInSession(session)
dapp.accountAddresses = accounts.map(account => ({address: account}))
dapp.topic = session.topic
dapp.rawSessions = [session]
dapp.connectorId = root.connectorId
return dapp
}
function getPersistedDapps() {
if (!root.enabled) {
return []
}
let dapps = []
root.bcSDK.getActiveSessions((allSessions) => {
if (!allSessions) {
return
}
for (const sessionID in allSessions) {
const session = allSessions[sessionID]
const dapp = sessionToDApp(session)
dapps.push(dapp)
}
})
return dapps
}
function resetModel() {
root.clear()
const dapps = d.getPersistedDapps()
for (let i = 0; i < dapps.length; i++) {
root.append(dapps[i])
}
}
}
Component.onCompleted: {
d.resetModel()
}
}