Skip to content

Commit

Permalink
fix: retore form state, chromium
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Jun 29, 2023
1 parent d3978f0 commit be3204d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 });
});
}
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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]
);
Expand Down
40 changes: 23 additions & 17 deletions src/background/messaging/internal-methods/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions src/shared/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export enum ExternalMethods {

export enum InternalMethods {
RequestDerivedStxAccounts = 'RequestDerivedStxAccounts',
GetActiveFormState = 'GetActiveFormState',
SetActiveFormState = 'SetActiveFormState',
ClearActiveFormState = 'ClearActiveFormState',
ShareInMemoryKeyToBackground = 'ShareInMemoryKeyToBackground',
RequestInMemoryKeys = 'RequestInMemoryKeys',
RemoveInMemoryKeys = 'RemoveInMemoryKeys',
Expand Down
15 changes: 0 additions & 15 deletions src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -41,9 +29,6 @@ type OriginatingTabClosed = BackgroundMessage<

export type BackgroundMessages =
| RequestDerivedStxAccounts
| GetActiveFormState
| SetActiveFormState
| ClearActiveFormState
| FirefoxShareInMemoryKeyToBackground
| FirefoxRequestInMemoryKeys
| FirefoxRemoveInMemoryKeys
Expand Down

0 comments on commit be3204d

Please sign in to comment.