mirror of
https://github.com/Qortal/chrome-extension.git
synced 2025-02-11 17:55:49 +00:00
reset recoils on logout
This commit is contained in:
parent
0cc2bc240e
commit
203226d3d9
37
src/App.tsx
37
src/App.tsx
@ -100,6 +100,8 @@ import { Settings } from "./components/Group/Settings";
|
||||
import { MainAvatar } from "./components/MainAvatar";
|
||||
import { useRetrieveDataLocalStorage } from "./useRetrieveDataLocalStorage";
|
||||
import { useQortalGetSaveSettings } from "./useQortalGetSaveSettings";
|
||||
import { useResetRecoilState } from "recoil";
|
||||
import { canSaveSettingToQdnAtom, oldPinnedAppsAtom, settingsLocalLastUpdatedAtom, settingsQDNLastUpdatedAtom, sortablePinnedAppsAtom } from "./atoms/global";
|
||||
|
||||
type extStates =
|
||||
| "not-authenticated"
|
||||
@ -326,6 +328,21 @@ function App() {
|
||||
const qortalRequestCheckbox1Ref = useRef(null);
|
||||
useRetrieveDataLocalStorage()
|
||||
useQortalGetSaveSettings(userInfo?.name)
|
||||
|
||||
//resets for recoil
|
||||
const resetAtomSortablePinnedAppsAtom = useResetRecoilState(sortablePinnedAppsAtom);
|
||||
const resetAtomCanSaveSettingToQdnAtom = useResetRecoilState(canSaveSettingToQdnAtom);
|
||||
const resetAtomSettingsQDNLastUpdatedAtom = useResetRecoilState(settingsQDNLastUpdatedAtom);
|
||||
const resetAtomSettingsLocalLastUpdatedAtom = useResetRecoilState(settingsLocalLastUpdatedAtom);
|
||||
const resetAtomOldPinnedAppsAtom = useResetRecoilState(oldPinnedAppsAtom);
|
||||
|
||||
const resetAllRecoil = () => {
|
||||
resetAtomSortablePinnedAppsAtom();
|
||||
resetAtomCanSaveSettingToQdnAtom();
|
||||
resetAtomSettingsQDNLastUpdatedAtom();
|
||||
resetAtomSettingsLocalLastUpdatedAtom();
|
||||
resetAtomOldPinnedAppsAtom();
|
||||
};
|
||||
useEffect(() => {
|
||||
if (!isMobile) return;
|
||||
// Function to set the height of the app to the viewport height
|
||||
@ -595,6 +612,20 @@ function App() {
|
||||
}
|
||||
}
|
||||
};
|
||||
const qortalRequestPermissonFromExtension = async (message, sender, sendResponse) => {
|
||||
if (message.action === "QORTAL_REQUEST_PERMISSION" && isMainWindow) {
|
||||
try {
|
||||
console.log("payloadbefore", message.payload);
|
||||
|
||||
await show('do you accept?');
|
||||
|
||||
sendResponse({ accepted: true });
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
sendResponse({ accepted: false });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
// Listen for messages from the background script
|
||||
@ -667,7 +698,12 @@ function App() {
|
||||
console.log("isMainWindow", isMainWindow, window?.location?.href);
|
||||
return true; // Return true to indicate an async response is coming
|
||||
}
|
||||
if (message.action === "QORTAL_REQUEST_PERMISSION" && isMainWindow && message?.isFromExtension) {
|
||||
qortalRequestPermissonFromExtension(message, sender, sendResponse);
|
||||
return true;
|
||||
}
|
||||
if (message.action === "QORTAL_REQUEST_PERMISSION" && isMainWindow) {
|
||||
|
||||
return;
|
||||
}
|
||||
};
|
||||
@ -1014,6 +1050,7 @@ function App() {
|
||||
setConfirmUseOfLocal(false);
|
||||
setTxList([]);
|
||||
setMemberGroups([]);
|
||||
resetAllRecoil()
|
||||
};
|
||||
|
||||
function roundUpToDecimals(number, decimals = 8) {
|
||||
|
@ -19,4 +19,9 @@ export const settingsQDNLastUpdatedAtom = atom({
|
||||
export const settingsLocalLastUpdatedAtom = atom({
|
||||
key: 'settingsLocalLastUpdatedAtom',
|
||||
default: 0,
|
||||
});
|
||||
|
||||
export const oldPinnedAppsAtom = atom({
|
||||
key: 'oldPinnedAppsAtom',
|
||||
default: [],
|
||||
});
|
@ -205,9 +205,12 @@ export const AppsLibrary = ({ availableQapps, setMode, myName, hasPublishApp, i
|
||||
width: "60px",
|
||||
}}
|
||||
onClick={()=> {
|
||||
executeEvent("addTab", {
|
||||
data: qapp
|
||||
})
|
||||
// executeEvent("addTab", {
|
||||
// data: qapp
|
||||
// })
|
||||
executeEvent("selectedAppInfo", {
|
||||
data: qapp,
|
||||
});
|
||||
}}
|
||||
>
|
||||
<AppCircleContainer>
|
||||
|
@ -344,7 +344,7 @@ export const useQortalMessageListener = (frameWindow) => {
|
||||
if (UIQortalRequests.includes(event.data.action)) {
|
||||
console.log('event?.data', event?.data);
|
||||
sendMessageToRuntime(
|
||||
{ action: event.data.action, type: 'qortalRequest', payload: event.data },
|
||||
{ action: event.data.action, type: 'qortalRequest', payload: event.data, isExtension: true },
|
||||
event.ports[0]
|
||||
);
|
||||
} else if (
|
||||
@ -368,7 +368,7 @@ export const useQortalMessageListener = (frameWindow) => {
|
||||
console.log('data after', data)
|
||||
if (data) {
|
||||
sendMessageToRuntime(
|
||||
{ action: event.data.action, type: 'qortalRequest', payload: data },
|
||||
{ action: event.data.action, type: 'qortalRequest', payload: data, isExtension: true },
|
||||
event.ports[0]
|
||||
);
|
||||
} else {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import React, { useContext, useMemo, useState } from 'react'
|
||||
import { useRecoilState } from 'recoil';
|
||||
import isEqual from 'lodash/isEqual'; // Import deep comparison utility
|
||||
import { canSaveSettingToQdnAtom, settingsLocalLastUpdatedAtom, settingsQDNLastUpdatedAtom, sortablePinnedAppsAtom } from '../../atoms/global';
|
||||
import { canSaveSettingToQdnAtom, oldPinnedAppsAtom, settingsLocalLastUpdatedAtom, settingsQDNLastUpdatedAtom, sortablePinnedAppsAtom } from '../../atoms/global';
|
||||
import { ButtonBase } from '@mui/material';
|
||||
import { objectToBase64 } from '../../qdn/encryption/group-encryption';
|
||||
import { MyContext } from '../../App';
|
||||
@ -17,7 +17,7 @@ export const Save = () => {
|
||||
const [openSnack, setOpenSnack] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [infoSnack, setInfoSnack] = useState(null);
|
||||
const [oldPinnedApps, setOldPinnedApps] = useState(pinnedApps)
|
||||
const [oldPinnedApps, setOldPinnedApps] = useRecoilState(oldPinnedAppsAtom)
|
||||
console.log('oldpin', {oldPinnedApps, pinnedApps}, settingsQdnLastUpdated, settingsLocalLastUpdated, settingsQdnLastUpdated < settingsLocalLastUpdated,)
|
||||
const { show } = useContext(MyContext);
|
||||
|
||||
@ -40,7 +40,7 @@ export const Save = () => {
|
||||
}
|
||||
console.log('!isEqual(oldChanges, newChanges)', !isEqual(oldChanges, newChanges))
|
||||
if(settingsQdnLastUpdated === -100) return false
|
||||
return !isEqual(oldChanges, newChanges) || settingsQdnLastUpdated < settingsLocalLastUpdated
|
||||
return !isEqual(oldChanges, newChanges) && settingsQdnLastUpdated < settingsLocalLastUpdated
|
||||
}, [oldPinnedApps, pinnedApps, settingsQdnLastUpdated, settingsLocalLastUpdated])
|
||||
|
||||
const saveToQdn = async ()=> {
|
||||
|
@ -63,6 +63,8 @@ function getLocalStorage(key) {
|
||||
|
||||
chrome?.runtime?.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request) {
|
||||
console.log('rquest', request)
|
||||
const isFromExtension = request?.isExtension
|
||||
switch (request.action) {
|
||||
case "GET_USER_ACCOUNT": {
|
||||
getUserAccount()
|
||||
@ -169,7 +171,7 @@ chrome?.runtime?.onMessage.addListener((request, sender, sendResponse) => {
|
||||
case "VOTE_ON_POLL": {
|
||||
const data = request.payload;
|
||||
|
||||
voteOnPoll(data)
|
||||
voteOnPoll(data, isFromExtension)
|
||||
.then((res) => {
|
||||
sendResponse(res);
|
||||
})
|
||||
|
@ -139,7 +139,7 @@ const _deployAt = async (
|
||||
}
|
||||
};
|
||||
|
||||
const _voteOnPoll = async (pollName, optionIndex, optionName) => {
|
||||
const _voteOnPoll = async ({pollName, optionIndex, optionName}, isFromExtension) => {
|
||||
const fee = await getFee("VOTE_ON_POLL");
|
||||
|
||||
const resPermission = await getUserPermission({
|
||||
@ -147,7 +147,7 @@ const _voteOnPoll = async (pollName, optionIndex, optionName) => {
|
||||
text2: `Poll: ${pollName}`,
|
||||
text3: `Option: ${optionName}`,
|
||||
fee: fee.fee,
|
||||
});
|
||||
}, isFromExtension);
|
||||
const { accepted } = resPermission;
|
||||
|
||||
if (accepted) {
|
||||
@ -207,7 +207,22 @@ function sendToSaveFilePicker(data, sender) {
|
||||
});
|
||||
}
|
||||
|
||||
async function getUserPermission(payload: any) {
|
||||
async function responseFromExtension() {
|
||||
return new Promise((resolve) => {
|
||||
|
||||
// Send message to the content script to check focus
|
||||
chrome.runtime.sendMessage({ action: "QORTAL_REQUEST_PERMISSION", payloa }, (response) => {
|
||||
|
||||
if (chrome.runtime.lastError) {
|
||||
resolve(false); // Error occurred, assume not focused
|
||||
} else {
|
||||
resolve(response); // Resolve based on the response
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function getUserPermission(payload: any, isFromExtension?: boolean) {
|
||||
function waitForWindowReady(windowId) {
|
||||
return new Promise((resolve) => {
|
||||
const checkInterval = setInterval(() => {
|
||||
@ -224,6 +239,33 @@ async function getUserPermission(payload: any) {
|
||||
});
|
||||
}
|
||||
|
||||
console.log('isFromExtension', isFromExtension)
|
||||
if(isFromExtension){
|
||||
|
||||
|
||||
return new Promise((resolve) => {
|
||||
// Set a timeout for 1 second
|
||||
const timeout = setTimeout(() => {
|
||||
resolve(false);
|
||||
}, 30000);
|
||||
|
||||
// Send message to the content script to check focus
|
||||
chrome.runtime.sendMessage(
|
||||
{ action: "QORTAL_REQUEST_PERMISSION", payload, isFromExtension },
|
||||
(response) => {
|
||||
console.log("permission response", response);
|
||||
if (response === undefined) return;
|
||||
clearTimeout(timeout); // Clear the timeout if we get a response
|
||||
|
||||
if (chrome.runtime.lastError) {
|
||||
resolve(false); // Error occurred, assume not focused
|
||||
} else {
|
||||
resolve(response); // Resolve based on the response
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
await new Promise((res) => {
|
||||
const popupUrl = chrome.runtime.getURL("index.html?secondary=true");
|
||||
console.log("popupUrl", popupUrl);
|
||||
@ -294,7 +336,7 @@ async function getUserPermission(payload: any) {
|
||||
return new Promise((resolve) => {
|
||||
// Set a timeout for 1 second
|
||||
const timeout = setTimeout(() => {
|
||||
resolve(false); // No response within 10 second, assume not focused
|
||||
resolve(false);
|
||||
}, 30000);
|
||||
|
||||
// Send message to the content script to check focus
|
||||
@ -909,7 +951,7 @@ export const publishMultipleQDNResources = async (data: any, sender) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
export const voteOnPoll = async (data) => {
|
||||
export const voteOnPoll = async (data, isFromExtension) => {
|
||||
const requiredFields = ["pollName", "optionIndex"];
|
||||
const missingFields: string[] = [];
|
||||
requiredFields.forEach((field) => {
|
||||
@ -941,7 +983,7 @@ export const voteOnPoll = async (data) => {
|
||||
}
|
||||
try {
|
||||
const optionName = pollInfo.pollOptions[optionIndex].optionName;
|
||||
const resVoteOnPoll = await _voteOnPoll(pollName, optionIndex, optionName);
|
||||
const resVoteOnPoll = await _voteOnPoll({pollName, optionIndex, optionName}, isFromExtension);
|
||||
return resVoteOnPoll;
|
||||
} catch (error) {
|
||||
throw new Error(error?.message || "Failed to vote on the poll.");
|
||||
|
@ -1,6 +1,6 @@
|
||||
import React, { useCallback, useEffect } from 'react'
|
||||
import { useRecoilState, useSetRecoilState } from 'recoil';
|
||||
import { canSaveSettingToQdnAtom, settingsLocalLastUpdatedAtom, settingsQDNLastUpdatedAtom, sortablePinnedAppsAtom } from './atoms/global';
|
||||
import { canSaveSettingToQdnAtom, oldPinnedAppsAtom, settingsLocalLastUpdatedAtom, settingsQDNLastUpdatedAtom, sortablePinnedAppsAtom } from './atoms/global';
|
||||
import { getArbitraryEndpointReact, getBaseApiReact } from './App';
|
||||
import { decryptResource } from './components/Group/Group';
|
||||
import { base64ToUint8Array, uint8ArrayToObject } from './backgroundFunctions/encryption';
|
||||
@ -58,13 +58,19 @@ export const useQortalGetSaveSettings = (myName) => {
|
||||
const setCanSave = useSetRecoilState(canSaveSettingToQdnAtom);
|
||||
const setSettingsQDNLastUpdated = useSetRecoilState(settingsQDNLastUpdatedAtom);
|
||||
const [settingsLocalLastUpdated] = useRecoilState(settingsLocalLastUpdatedAtom);
|
||||
const [oldPinnedApps, setOldPinnedApps] = useRecoilState(oldPinnedAppsAtom)
|
||||
|
||||
const getSavedSettings = useCallback(async (myName, settingsLocalLastUpdated)=> {
|
||||
try {
|
||||
const {hasPublishRecord, timestamp} = await getPublishRecord(myName)
|
||||
if(hasPublishRecord){
|
||||
const settings = await getPublish(myName)
|
||||
console.log('settings', settings, timestamp, settingsLocalLastUpdated )
|
||||
if(settings?.sortablePinnedApps && timestamp > settingsLocalLastUpdated){
|
||||
setSortablePinnedApps(settings.sortablePinnedApps)
|
||||
setOldPinnedApps(settings.sortablePinnedApps)
|
||||
setSettingsQDNLastUpdated(timestamp || 0)
|
||||
} else if(settings?.sortablePinnedApps){
|
||||
setSettingsQDNLastUpdated(timestamp || 0)
|
||||
}
|
||||
if(!settings){
|
||||
|
Loading…
x
Reference in New Issue
Block a user