changed qortalrequest messaging

This commit is contained in:
2024-10-30 09:37:23 +02:00
parent 4f91924947
commit eb8049c12d
11 changed files with 674 additions and 652 deletions

View File

@@ -3,6 +3,8 @@ import FileSaver from 'file-saver';
import { executeEvent } from '../../utils/events';
import { useSetRecoilState } from 'recoil';
import { navigationControllerAtom } from '../../atoms/global';
class Semaphore {
constructor(count) {
this.count = count
@@ -58,7 +60,7 @@ const fileToBase64 = (file) => new Promise(async (resolve, reject) => {
}
})
function openIndexedDB() {
export function openIndexedDB() {
return new Promise((resolve, reject) => {
const request = indexedDB.open("fileStorageDB", 1);
@@ -79,58 +81,7 @@ function openIndexedDB() {
});
}
async function handleGetFileFromIndexedDB(fileId, sendResponse) {
try {
const db = await openIndexedDB();
const transaction = db.transaction(["files"], "readonly");
const objectStore = transaction.objectStore("files");
const getRequest = objectStore.get(fileId);
getRequest.onsuccess = async function (event) {
if (getRequest.result) {
const file = getRequest.result.data;
try {
const base64String = await fileToBase64(file);
// Create a new transaction to delete the file
const deleteTransaction = db.transaction(["files"], "readwrite");
const deleteObjectStore = deleteTransaction.objectStore("files");
const deleteRequest = deleteObjectStore.delete(fileId);
deleteRequest.onsuccess = function () {
try {
sendResponse({ result: base64String });
} catch (error) {
console.log('error', error)
}
};
deleteRequest.onerror = function () {
console.error(`Error deleting file with ID ${fileId} from IndexedDB`);
sendResponse({ result: null, error: "Failed to delete file from IndexedDB" });
};
} catch (error) {
console.error("Error converting file to Base64:", error);
sendResponse({ result: null, error: "Failed to convert file to Base64" });
}
} else {
console.error(`File with ID ${fileId} not found in IndexedDB`);
sendResponse({ result: null, error: "File not found in IndexedDB" });
}
};
getRequest.onerror = function () {
console.error(`Error retrieving file with ID ${fileId} from IndexedDB`);
sendResponse({ result: null, error: "Error retrieving file from IndexedDB" });
};
} catch (error) {
console.error("Error opening IndexedDB:", error);
sendResponse({ result: null, error: "Error opening IndexedDB" });
}
}
const UIQortalRequests = [
'GET_USER_ACCOUNT', 'DECRYPT_DATA', 'SEND_COIN', 'GET_LIST_ITEMS',
@@ -215,7 +166,7 @@ const UIQortalRequests = [
}
}
const showSaveFilePicker = async (data) => {
export const showSaveFilePicker = async (data) => {
let blob
let fileName
try {
@@ -368,7 +319,7 @@ isDOMContentLoaded: false
if (event?.data?.requestedHandler !== 'UI') return;
const sendMessageToRuntime = (message, eventPort) => {
window.sendMessage(message.action, message, 60000, message.isFromExtension)
window.sendMessage(message.action, message.payload, 60000, message.isExtension)
.then((response) => {
if (response.error) {
eventPort.postMessage({
@@ -402,6 +353,7 @@ isDOMContentLoaded: false
) {
let data;
try {
console.log('storeFilesInIndexedDB', structuredClone(event.data))
data = await storeFilesInIndexedDB(event.data);
} catch (error) {
console.error('Error storing files in IndexedDB:', error);
@@ -457,7 +409,9 @@ isDOMContentLoaded: false
name: event?.data?.payload?.name
} }, '*'
);
}
}
};
// Add the listener for messages coming from the frameWindow
@@ -471,17 +425,7 @@ isDOMContentLoaded: false
}, []); // Empty dependency array to run once when the component mounts
// TODO
// chrome.runtime?.onMessage.addListener( function (message, sender, sendResponse) {
// if(message.action === "SHOW_SAVE_FILE_PICKER"){
// showSaveFilePicker(message?.data)
// }
// else if (message.action === "getFileFromIndexedDB") {
// handleGetFileFromIndexedDB(message.fileId, sendResponse);
// return true; // Keep the message channel open for async response
// }
// });
return {path, history, resetHistory, changeCurrentIndex}
};

View File

@@ -1,6 +1,7 @@
import React, { useEffect } from 'react';
import DOMPurify from 'dompurify';
import './styles.css'; // Ensure this CSS file is imported
import { Browser } from '@capacitor/browser';
export const MessageDisplay = ({ htmlContent , isReply}) => {
@@ -28,25 +29,15 @@ export const MessageDisplay = ({ htmlContent , isReply}) => {
});
// Function to handle link clicks
const handleClick = (e) => {
const handleClick = async (e) => {
e.preventDefault();
// Ensure we are targeting an <a> element
const target = e.target.closest('a');
if (target) {
const href = target.getAttribute('href');
// TODO
// if (chrome && chrome.tabs) {
// chrome.tabs.create({ url: href }, (tab) => {
// if (chrome.runtime.lastError) {
// console.error('Error opening tab:', chrome.runtime.lastError);
// } else {
// console.log('Tab opened successfully:', tab);
// }
// });
// } else {
// console.error('chrome.tabs API is not available.');
// }
await Browser.open({ url: href });
} else {
console.error('No <a> tag found or href is null.');
}

View File

@@ -9,6 +9,7 @@ import { Home, Groups, Message, ShowChart } from "@mui/icons-material";
import Box from "@mui/material/Box";
import BottomLogo from "../../assets/svgs/BottomLogo5.svg";
import LogoSelected from "../../assets/svgs/LogoSelected.svg";
import { Browser } from '@capacitor/browser';
import { CustomSvg } from "../../common/CustomSvg";
import { WalletIcon } from "../../assets/Icons/WalletIcon";
@@ -183,10 +184,10 @@ export const MobileFooter = ({
}}
/>
<BottomNavigationAction
onClick={() => {
// TODO
// chrome.tabs.create({ url: "https://www.qort.trade"});
onClick={async () => {
await Browser.open({ url: 'https://www.qort.trade' });
}}
icon={
<IconWrapper label="Trading" color="rgba(250, 250, 250, 0.5)">
<TradingIcon color="rgba(250, 250, 250, 0.5)" />