From 7e86b2b6f800c2609c5d4ccf62ea3060fefa3f00 Mon Sep 17 00:00:00 2001 From: Florian Date: Sun, 18 Feb 2024 13:23:11 +0100 Subject: [PATCH] Cleanup code (#356) --- .vscode/settings.json | 2 +- packages/extension/cypress/e2e/delete_account.cy.ts | 13 +++++++------ packages/extension/cypress/e2e/offers.cy.ts | 1 + .../extension/cypress/e2e/submit_raw_json_tx.cy.ts | 2 +- .../atoms/KeyValueDisplay/KeyValueDisplay.tsx | 4 +--- .../TransactionDisplay/TransactionDisplay.tsx | 7 +------ .../TransactionHeader/TransactionHeader.tsx | 2 +- .../extension/src/components/pages/Login/Login.tsx | 12 +++++------- .../components/pages/SignMessage/SignMessage.tsx | 3 ++- .../StepperView/TransactionsDisplay.tsx | 1 + .../src/contexts/BrowserContext/BrowserContext.tsx | 11 +++-------- packages/extension/src/utils/NFTViewer.ts | 3 ++- packages/extension/src/utils/login.ts | 4 +++- packages/extension/src/utils/storageChromeLocal.ts | 6 ++---- 14 files changed, 31 insertions(+), 40 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index ca5cc4118..09d7e9e24 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,7 +13,7 @@ "[json]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, - "cSpell.words": ["gemwallet", "Trustline", "xrpl"], + "cSpell.words": ["deepcode", "gemwallet", "testid", "Trustline", "xrpl"], "[typescriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode" } diff --git a/packages/extension/cypress/e2e/delete_account.cy.ts b/packages/extension/cypress/e2e/delete_account.cy.ts index 3f1d5bc69..3d0051667 100644 --- a/packages/extension/cypress/e2e/delete_account.cy.ts +++ b/packages/extension/cypress/e2e/delete_account.cy.ts @@ -3,6 +3,7 @@ import { Chain, XRPLNetwork } from '@gemwallet/constants'; describe('Delete account', () => { + // deepcode ignore NoHardcodedPasswords: const PASSWORD = 'SECRET_PASSWORD'; beforeEach(() => { @@ -98,21 +99,21 @@ describe('Delete account', () => { const navigate = (url: string, password: string) => { cy.visit(url, { onBeforeLoad(win) { - (win as any).chrome = (win as any).chrome || {}; - (win as any).chrome.runtime = { - sendMessage(message, cb) {} + win['chrome'] = win['chrome'] || {}; + win['chrome'].runtime = { + sendMessage() {} }; - (win as any).chrome.storage = { + win['chrome'].storage = { local: { - get(key, cb) {}, + get() {}, set(obj, cb) { if (cb) cb(); } } }; - cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({}); + cy.stub(win['chrome'].runtime, 'sendMessage').resolves({}); } }); diff --git a/packages/extension/cypress/e2e/offers.cy.ts b/packages/extension/cypress/e2e/offers.cy.ts index 825fcb0ae..e274338cf 100644 --- a/packages/extension/cypress/e2e/offers.cy.ts +++ b/packages/extension/cypress/e2e/offers.cy.ts @@ -290,6 +290,7 @@ describe('Offers', () => { cy.contains('Sequence') .next() .invoke('text') + // deepcode ignore PromiseNotCaughtGeneral: No need to have a catch here as this is a test .then((sequence) => { cy.wrap(sequence).as('sequence'); }); diff --git a/packages/extension/cypress/e2e/submit_raw_json_tx.cy.ts b/packages/extension/cypress/e2e/submit_raw_json_tx.cy.ts index 880ae93f1..d638be677 100644 --- a/packages/extension/cypress/e2e/submit_raw_json_tx.cy.ts +++ b/packages/extension/cypress/e2e/submit_raw_json_tx.cy.ts @@ -2,8 +2,8 @@ import { Chain, XRPLNetwork } from '@gemwallet/constants'; import { navigate } from '../utils/navigation'; -// deepcode ignore NoHardcodedPasswords: password used for testing purposes const SUBMIT_RAW_TRANSACTION_PATH = 'http://localhost:3000/build-transaction?transaction=buildRaw'; +// deepcode ignore NoHardcodedPasswords: password used for testing purposes const PASSWORD = 'SECRET_PASSWORD'; beforeEach(() => { diff --git a/packages/extension/src/components/atoms/KeyValueDisplay/KeyValueDisplay.tsx b/packages/extension/src/components/atoms/KeyValueDisplay/KeyValueDisplay.tsx index 0bd8425ae..932955958 100644 --- a/packages/extension/src/components/atoms/KeyValueDisplay/KeyValueDisplay.tsx +++ b/packages/extension/src/components/atoms/KeyValueDisplay/KeyValueDisplay.tsx @@ -1,12 +1,10 @@ import { FC } from 'react'; -import { Paper, Tooltip, Typography, TypographyProps } from '@mui/material'; +import { Paper, Tooltip, Typography } from '@mui/material'; interface KeyValueDisplayProps { keyName: string; value: string; - keyTypographyProps?: TypographyProps; - valueTypographyProps?: TypographyProps; hasTooltip?: boolean; useLegacy?: boolean; } diff --git a/packages/extension/src/components/molecules/TransactionDisplay/TransactionDisplay.tsx b/packages/extension/src/components/molecules/TransactionDisplay/TransactionDisplay.tsx index 2eeda16db..a6dafc0da 100644 --- a/packages/extension/src/components/molecules/TransactionDisplay/TransactionDisplay.tsx +++ b/packages/extension/src/components/molecules/TransactionDisplay/TransactionDisplay.tsx @@ -1,6 +1,5 @@ import { FC } from 'react'; -import { TypographyProps } from '@mui/material'; import { convertHexToString, Currency } from 'xrpl'; import { Amount, Memo, Signer } from 'xrpl/dist/npm/models/common'; import { GlobalFlags } from 'xrpl/dist/npm/models/transactions/common'; @@ -176,12 +175,11 @@ export const TransactionDisplay: FC = ({ const renderAmount = (params: { title: string; value: Amount; - valueTypographyProps?: TypographyProps; useLegacy: boolean; hasMultipleAmounts?: boolean; mainToken?: string; }) => { - const { title, value, valueTypographyProps, useLegacy } = params; + const { title, value, useLegacy } = params; const res = parseAmountObject(value, mainToken); if (hasMultipleAmounts) { const formattedValue = res.issuer @@ -194,7 +192,6 @@ export const TransactionDisplay: FC = ({ @@ -206,14 +203,12 @@ export const TransactionDisplay: FC = ({ {res.issuer ? ( diff --git a/packages/extension/src/components/molecules/TransactionHeader/TransactionHeader.tsx b/packages/extension/src/components/molecules/TransactionHeader/TransactionHeader.tsx index 25e41a1c1..f73f6f2fc 100644 --- a/packages/extension/src/components/molecules/TransactionHeader/TransactionHeader.tsx +++ b/packages/extension/src/components/molecules/TransactionHeader/TransactionHeader.tsx @@ -6,7 +6,7 @@ import { SECONDARY_GRAY } from '../../../constants'; export interface TransactionHeaderProps { title: string; - favicon?: string | undefined; + favicon?: string; url?: string | null; } diff --git a/packages/extension/src/components/pages/Login/Login.tsx b/packages/extension/src/components/pages/Login/Login.tsx index 0dcd06451..785b52b66 100644 --- a/packages/extension/src/components/pages/Login/Login.tsx +++ b/packages/extension/src/components/pages/Login/Login.tsx @@ -154,13 +154,11 @@ export const Login: FC = () => { useEffect(() => { const loadTimerData = async () => { const storedTimerData = await loadFromChromeLocalStorage('disabledLoginTimer'); - if (storedTimerData !== null) { - if (storedTimerData > Date.now()) { - setDisableLogin(true); - setPasswordError('Too many attempts, please try again later'); - } else { - setDisableLogin(false); - } + if (typeof storedTimerData === 'number' && storedTimerData > Date.now()) { + setDisableLogin(true); + setPasswordError('Too many attempts, please try again later'); + } else { + setDisableLogin(false); } }; loadTimerData(); diff --git a/packages/extension/src/components/pages/SignMessage/SignMessage.tsx b/packages/extension/src/components/pages/SignMessage/SignMessage.tsx index 5e092a742..39a3d1098 100644 --- a/packages/extension/src/components/pages/SignMessage/SignMessage.tsx +++ b/packages/extension/src/components/pages/SignMessage/SignMessage.tsx @@ -48,7 +48,7 @@ export const SignMessage: FC = () => { return { id: Number(urlParams.get('id')) || 0, url, - favicon: favicon || undefined + favicon: favicon ?? undefined }; }, []); @@ -178,6 +178,7 @@ export const SignMessage: FC = () => { = ({ const { ID, ...txWithoutID } = tx; return ( handleToggleExpand(key)} diff --git a/packages/extension/src/contexts/BrowserContext/BrowserContext.tsx b/packages/extension/src/contexts/BrowserContext/BrowserContext.tsx index a6da97630..59eddb2cb 100644 --- a/packages/extension/src/contexts/BrowserContext/BrowserContext.tsx +++ b/packages/extension/src/contexts/BrowserContext/BrowserContext.tsx @@ -4,12 +4,11 @@ import * as Sentry from '@sentry/react'; export interface CloseProps { windowId: number; - callback?: Function; } interface ContextType { window?: chrome.windows.Window; - closeExtension: ({ windowId, callback }: CloseProps) => void; + closeExtension: ({ windowId }: CloseProps) => void; } const BrowserContext = createContext({ @@ -34,14 +33,10 @@ const BrowserProvider: FC = ({ children }) => { getCurrentWindow(); }, [getCurrentWindow]); - const closeExtension = useCallback(({ windowId, callback }: CloseProps) => { + const closeExtension = useCallback(({ windowId }: CloseProps) => { if (chrome?.windows) { chrome.storage.local.remove('currentWindowId'); - chrome.windows.remove(windowId).then(() => { - if (callback) { - callback(); - } - }); + chrome.windows.remove(windowId); } }, []); diff --git a/packages/extension/src/utils/NFTViewer.ts b/packages/extension/src/utils/NFTViewer.ts index 89663fdda..25fe1e536 100644 --- a/packages/extension/src/utils/NFTViewer.ts +++ b/packages/extension/src/utils/NFTViewer.ts @@ -1,6 +1,7 @@ import { NFTData } from '@gemwallet/constants'; import { IPFSResolverPrefix } from '@gemwallet/constants/src/xrpl/nft.constant'; +// eslint-disable-next-line @typescript-eslint/no-explicit-any export const parseImage = (NFTData: any, URL: string): string => { if (NFTData.image) { return replaceIPFS(NFTData.image); @@ -13,7 +14,7 @@ export const parseImage = (NFTData: any, URL: string): string => { return URL.replace('.json', '.png'); }; -export const parseJSON = async (URL: any, NFTokenID: string): Promise => { +export const parseJSON = async (URL: string, NFTokenID: string): Promise => { const NFTData = await fetch(URL) .then((res) => res.json()) .catch(() => { diff --git a/packages/extension/src/utils/login.ts b/packages/extension/src/utils/login.ts index bf05eed44..947e1ccff 100644 --- a/packages/extension/src/utils/login.ts +++ b/packages/extension/src/utils/login.ts @@ -10,7 +10,9 @@ export const saveRememberSessionState = (checked: boolean) => { export const loadRememberSessionState = async (): Promise => { try { const storedState = await loadFromChromeLocalStorage(STORAGE_REMEMBER_SESSION); - return storedState ? JSON.parse(storedState) === true : false; + return storedState + ? typeof storedState === 'string' && JSON.parse(storedState) === true + : false; } catch (e) { Sentry.captureException(e); return false; diff --git a/packages/extension/src/utils/storageChromeLocal.ts b/packages/extension/src/utils/storageChromeLocal.ts index e08f36f05..63213f829 100644 --- a/packages/extension/src/utils/storageChromeLocal.ts +++ b/packages/extension/src/utils/storageChromeLocal.ts @@ -2,6 +2,7 @@ * Manages the storage in Chrome local storage */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any export const saveInChromeLocalStorage = (key: string, value: any): Promise => { return new Promise((resolve, reject) => { if (process.env.NODE_ENV === 'production') { @@ -18,10 +19,7 @@ export const saveInChromeLocalStorage = (key: string, value: any): Promise }); }; -export const loadFromChromeLocalStorage = ( - key: string, - deleteAfterLoad: boolean = false -): Promise => { +export const loadFromChromeLocalStorage = (key: string, deleteAfterLoad: boolean = false) => { return new Promise((resolve, reject) => { if (process.env.NODE_ENV === 'production') { chrome.storage.local.get(key, (result) => {