fix: retore form state, chromium

This commit is contained in:
kyranjamie
2023-06-05 15:30:48 +02:00
committed by kyranjamie
parent d3978f0115
commit be3204d7e7
5 changed files with 34 additions and 56 deletions

View File

@@ -1,6 +1,5 @@
import { useNavigate } from 'react-router-dom';
import { InternalMethods } from '@shared/message-types';
import { getActiveTab } from '@shared/utils/get-active-tab';
import { useOnMount } from '@app/common/hooks/use-on-mount';
@@ -23,14 +22,12 @@ void run();
export function useRestoreFormState() {
const navigate = useNavigate();
useOnMount(() => {
if (!isPopupMode() || !currentTabId) return;
chrome.runtime.sendMessage(
{ method: InternalMethods.GetActiveFormState, payload: { tabId: currentTabId } },
persistedState => {
if (!persistedState || !persistedState.symbol) return;
navigate('send/' + persistedState.symbol, { state: persistedState });
}
);
useOnMount(async () => {
if (!isPopupMode() || !currentTabId || !chrome.storage.session) return;
const key = 'form-state-' + currentTabId.toString();
const state = await chrome.storage.session.get('form-state-' + currentTabId.toString());
const persistedState = state[key];
if (!persistedState || !persistedState.symbol) return;
navigate('send/' + persistedState.symbol, { state: persistedState });
});
}

View File

@@ -1,7 +1,6 @@
import { useEffect } from 'react';
import { useAsync } from 'react-async-hook';
import { InternalMethods } from '@shared/message-types';
import { getActiveTab } from '@shared/utils/get-active-tab';
import { isPopupMode } from '@app/common/utils';
@@ -10,23 +9,17 @@ export function useUpdatePersistedSendFormValues() {
const activeTab = useAsync(getActiveTab, []).result;
function onFormStateChange(state: { recipient: string; amount: string | number }) {
if (!isPopupMode()) return;
if (!activeTab) return;
if (!isPopupMode() || !activeTab || !chrome.storage.session) return;
chrome.runtime.sendMessage({
method: InternalMethods.SetActiveFormState,
payload: { tabId: activeTab.id, ...state },
});
chrome.storage.session.set({ ['form-state-' + activeTab.toString()]: state });
}
useEffect(
() => () => {
if (!isPopupMode()) return;
if (!activeTab) return;
chrome.runtime.sendMessage({
method: InternalMethods.SetActiveFormState,
payload: { tabId: activeTab.id },
});
if (!chrome.storage.session) return;
chrome.storage.session.remove('form-state-' + activeTab.toString());
},
[activeTab]
);

View File

@@ -10,10 +10,16 @@ function validateMessagesAreFromExtension(sender: chrome.runtime.MessageSender)
const inMemoryKeys = new Map();
const inMemoryFormState = new Map<number, object>();
function makeFormStateKey(tabId: number) {
return 'form-state-' + tabId.toString();
}
async function removeFormState(tabId: number) {
return chrome.storage.session.remove(makeFormStateKey(tabId));
}
// Remove any persisted form state when a tab is closed
chrome.tabs.onRemoved.addListener(tabId => inMemoryFormState.delete(tabId));
chrome.tabs.onRemoved.addListener(tabId => removeFormState(tabId));
export async function internalBackgroundMessageHandler(
message: BackgroundMessages,
@@ -49,23 +55,23 @@ export async function internalBackgroundMessageHandler(
break;
}
case InternalMethods.GetActiveFormState: {
sendResponse(inMemoryFormState.get(message.payload.tabId));
break;
}
// case InternalMethods.GetActiveFormState: {
// sendResponse(await chrome.storage.session.get(makeFormStateKey(message.payload.tabId)));
// break;
// }
case InternalMethods.SetActiveFormState: {
const { tabId, ...state } = message.payload;
inMemoryFormState.set(tabId, state);
sendResponse();
break;
}
// case InternalMethods.SetActiveFormState: {
// const { tabId, ...state } = message.payload;
// await chrome.storage.session.set({ [makeFormStateKey(tabId)]: state });
// sendResponse();
// break;
// }
case InternalMethods.ClearActiveFormState: {
inMemoryFormState.delete(message.payload.tabId);
sendResponse();
break;
}
// case InternalMethods.ClearActiveFormState: {
// await removeFormState(message.payload.tabId);
// sendResponse();
// break;
// }
case InternalMethods.RemoveInMemoryKeys: {
inMemoryKeys.clear();

View File

@@ -27,9 +27,6 @@ export enum ExternalMethods {
export enum InternalMethods {
RequestDerivedStxAccounts = 'RequestDerivedStxAccounts',
GetActiveFormState = 'GetActiveFormState',
SetActiveFormState = 'SetActiveFormState',
ClearActiveFormState = 'ClearActiveFormState',
ShareInMemoryKeyToBackground = 'ShareInMemoryKeyToBackground',
RequestInMemoryKeys = 'RequestInMemoryKeys',
RemoveInMemoryKeys = 'RemoveInMemoryKeys',

View File

@@ -13,18 +13,6 @@ export type RequestDerivedStxAccounts = BackgroundMessage<
{ secretKey: string; highestAccountIndex: number }
>;
type GetActiveFormState = BackgroundMessage<InternalMethods.GetActiveFormState, { tabId: number }>;
type SetActiveFormState = BackgroundMessage<
InternalMethods.SetActiveFormState,
{ tabId: number; symbol: string; amount?: string; recipient?: string }
>;
type ClearActiveFormState = BackgroundMessage<
InternalMethods.ClearActiveFormState,
{ tabId: number }
>;
type FirefoxShareInMemoryKeyToBackground = BackgroundMessage<
InternalMethods.ShareInMemoryKeyToBackground,
{ secretKey: string; keyId: string }
@@ -41,9 +29,6 @@ type OriginatingTabClosed = BackgroundMessage<
export type BackgroundMessages =
| RequestDerivedStxAccounts
| GetActiveFormState
| SetActiveFormState
| ClearActiveFormState
| FirefoxShareInMemoryKeyToBackground
| FirefoxRequestInMemoryKeys
| FirefoxRemoveInMemoryKeys