From c54a918cb7ed75852c56a7a51e7abd63c1d4c1ec Mon Sep 17 00:00:00 2001 From: Christian Laino Date: Wed, 21 Jul 2021 13:02:56 -0300 Subject: [PATCH] fix scrollbars on windows + stuff (#121) * fixed horizontal scroll on incoming action * fixed login redirect * fix resizing on firefox * rejected default status --- source/Background/Controller.js | 256 ++++++++++-------- source/Background/errors.js | 6 +- source/ContentScript/index.js | 3 - .../components/AppConnection/index.jsx | 8 +- .../components/Transfer/components/Data.jsx | 12 +- .../Transfer/components/Details.jsx | 12 +- 6 files changed, 166 insertions(+), 131 deletions(-) diff --git a/source/Background/Controller.js b/source/Background/Controller.js index b497b800..08c1aad8 100644 --- a/source/Background/Controller.js +++ b/source/Background/Controller.js @@ -74,68 +74,76 @@ backgroundController.exposeController('isConnected', async (opts, url) => secure }); })); -backgroundController.exposeController('requestConnect', async (opts, domainUrl, name, icon) => secureController(opts.callback, async () => { - const { message, sender } = opts; - storage.get('apps', (response) => { - const apps = { - ...response.apps, - [domainUrl]: { +backgroundController.exposeController( + 'requestConnect', + async (opts, domainUrl, name, icon) => secureController(opts.callback, async () => { + const { message, sender } = opts; + storage.get('apps', (response) => { + const apps = { + ...response.apps, + [domainUrl]: { + url: domainUrl, + name, + status: CONNECTION_STATUS.pending, + icon, + }, + }; + + storage.set({ apps }); + }); + + const url = qs.stringifyUrl({ + url: 'notification.html', + query: { + callId: message.data.data.id, + portId: sender.id, url: domainUrl, - name, - status: CONNECTION_STATUS.pending, icon, + type: 'connect', }, - }; - - storage.set({ apps }); - }); - - const url = qs.stringifyUrl({ - url: 'notification.html', - query: { - callId: message.data.data.id, - portId: sender.id, - url: domainUrl, - icon, - type: 'connect', - }, - }); + }); - const height = keyring?.isUnlocked - ? SIZES.appConnectHeight - : SIZES.loginHeight; + const height = keyring?.isUnlocked + ? SIZES.appConnectHeight + : SIZES.loginHeight; - extension.windows.create({ - url, - type: 'popup', - width: SIZES.width, - height, - }); -})); + extension.windows.create({ + url, + type: 'popup', + width: SIZES.width, + height, + }); + }), +); -backgroundController.exposeController('handleAppConnect', async (opts, url, status, callId, portId) => secureController(opts.callback, async () => { - const { callback } = opts; +backgroundController.exposeController( + 'handleAppConnect', + async (opts, url, status, callId, portId) => secureController(opts.callback, async () => { + const { callback } = opts; - storage.get('apps', (response) => { - const apps = response.apps || {}; + storage.get('apps', (response) => { + const apps = response.apps || {}; - const newApps = Object.keys(apps).reduce((obj, key) => { - const newObj = { ...obj }; - newObj[key] = apps[key]; - if (key === url) { - newObj[key].status = status; - newObj[key].date = new Date().toISOString(); - } + const newApps = Object.keys(apps).reduce((obj, key) => { + const newObj = { ...obj }; + newObj[key] = apps[key]; + if (key === url) { + newObj[key].status = status || CONNECTION_STATUS.rejected; + newObj[key].date = new Date().toISOString(); + } - return newObj; - }, {}); + return newObj; + }, {}); - storage.set({ apps: newApps }); - }); + storage.set({ apps: newApps }); + }); - callback(null, true); - callback(null, status === CONNECTION_STATUS.accepted, [{ portId, callId }]); -})); + callback(null, true); + callback(null, status === CONNECTION_STATUS.accepted, [ + { portId, callId }, + ]); + }), +); const requestBalance = async (accountId, callback) => { const getBalance = getKeyringHandler(HANDLER_TYPES.GET_BALANCE, keyring); @@ -147,37 +155,42 @@ const requestBalance = async (accountId, callback) => { } }; -backgroundController.exposeController('requestBalance', async (opts, metadata, accountId) => secureController(opts.callback, async () => { - const { callback, message, sender } = opts; - - storage.get('apps', async (state) => { - if (state?.apps?.[metadata.url]?.status === CONNECTION_STATUS.accepted) { - if (!keyring.isUnlocked) { - const url = qs.stringifyUrl({ - url: 'notification.html', - query: { - callId: message.data.data.id, - portId: sender.id, - type: 'balance', - argsJson: accountId, - metadataJson: JSON.stringify(metadata), - }, - }); +backgroundController.exposeController( + 'requestBalance', + async (opts, metadata, accountId) => secureController(opts.callback, async () => { + const { callback, message, sender } = opts; - extension.windows.create({ - url, - type: 'popup', - width: SIZES.width, - height: SIZES.loginHeight, - }); + storage.get('apps', async (state) => { + if ( + state?.apps?.[metadata.url]?.status === CONNECTION_STATUS.accepted + ) { + if (!keyring.isUnlocked) { + const url = qs.stringifyUrl({ + url: 'notification.html', + query: { + callId: message.data.data.id, + portId: sender.id, + type: 'balance', + argsJson: accountId, + metadataJson: JSON.stringify(metadata), + }, + }); + + extension.windows.create({ + url, + type: 'popup', + width: SIZES.width, + height: SIZES.loginHeight, + }); + } else { + requestBalance(accountId, callback); + } } else { - requestBalance(accountId, callback); + callback(ERRORS.CONNECTION_ERROR, null); } - } else { - callback(ERRORS.CONNECTION_ERROR, null); - } - }); -})); + }); + }), +); backgroundController.exposeController( 'handleRequestBalance', @@ -205,45 +218,50 @@ backgroundController.exposeController( }, ); -backgroundController.exposeController('requestTransfer', async (opts, metadata, args) => secureController(opts.callback, async () => { - const { message, sender, callback } = opts; - - const { id: callId } = message.data.data; - const { id: portId } = sender; - storage.get('apps', async (state) => { - if (state?.apps?.[metadata.url]?.status === CONNECTION_STATUS.accepted) { - const argsError = validateTransferArgs(args); - if (argsError) { - callback(argsError, null); - return; +backgroundController.exposeController( + 'requestTransfer', + async (opts, metadata, args) => secureController(opts.callback, async () => { + const { message, sender, callback } = opts; + + const { id: callId } = message.data.data; + const { id: portId } = sender; + storage.get('apps', async (state) => { + if ( + state?.apps?.[metadata.url]?.status === CONNECTION_STATUS.accepted + ) { + const argsError = validateTransferArgs(args); + if (argsError) { + callback(argsError, null); + return; + } + const url = qs.stringifyUrl({ + url: 'notification.html', + query: { + callId, + portId, + metadataJson: JSON.stringify(metadata), + argsJson: JSON.stringify(args), + type: 'transfer', + }, + }); + + const height = keyring?.isUnlocked + ? SIZES.detailHeightSmall + : SIZES.loginHeight; + extension.windows.create({ + url, + type: 'popup', + width: SIZES.width, + height, + top: 65, + left: metadata.pageWidth - SIZES.width, + }); + } else { + callback(ERRORS.CONNECTION_ERROR, null); } - const url = qs.stringifyUrl({ - url: 'notification.html', - query: { - callId, - portId, - metadataJson: JSON.stringify(metadata), - argsJson: JSON.stringify(args), - type: 'transfer', - }, - }); - - const height = keyring?.isUnlocked - ? SIZES.detailHeightSmall - : SIZES.loginHeight; - extension.windows.create({ - url, - type: 'popup', - width: SIZES.width, - height, - top: 65, - left: metadata.pageWidth - SIZES.width, - }); - } else { - callback(ERRORS.CONNECTION_ERROR, null); - } - }); -})); + }); + }), +); backgroundController.exposeController( 'handleRequestTransfer', @@ -254,9 +272,7 @@ backgroundController.exposeController( // Answer this callback no matter if the transfer succeeds or not. callback(null, true); if (transfer?.status === 'declined') { - callback(ERRORS.TRANSACTION_REJECTED, null, [ - { portId, callId }, - ]); + callback(ERRORS.TRANSACTION_REJECTED, null, [{ portId, callId }]); } else { const getBalance = getKeyringHandler(HANDLER_TYPES.GET_BALANCE, keyring); const sendICP = getKeyringHandler(HANDLER_TYPES.SEND_ICP, keyring); diff --git a/source/Background/errors.js b/source/Background/errors.js index 004af634..834b8bc1 100644 --- a/source/Background/errors.js +++ b/source/Background/errors.js @@ -1,5 +1,9 @@ export default { - CONNECTION_ERROR: { code: 401, message: 'You are not connected. You must call window.ic.plug.requestConnect() and have the user accept the popup before you call this method.' }, + CONNECTION_ERROR: { + code: 401, + message: + 'You are not connected. You must call window.ic.plug.requestConnect() and have the user accept the popup before you call this method.', + }, BALANCE_ERROR: { code: 400, message: 'Insufficient balance' }, TRANSACTION_REJECTED: { code: 401, message: 'The transactions was rejected' }, INITIALIZED_ERROR: { code: 403, message: 'Plug must be initialized.' }, diff --git a/source/ContentScript/index.js b/source/ContentScript/index.js index 90e9895d..e04a8eed 100644 --- a/source/ContentScript/index.js +++ b/source/ContentScript/index.js @@ -17,7 +17,4 @@ serverRPC.start(); injectScript(null, INPAGE_SCRIPT); -/* eslint-disable no-console */ -console.log('helloworld from content script'); - export {}; diff --git a/source/Pages/Notification/components/AppConnection/index.jsx b/source/Pages/Notification/components/AppConnection/index.jsx index 39d551f5..14a4979d 100644 --- a/source/Pages/Notification/components/AppConnection/index.jsx +++ b/source/Pages/Notification/components/AppConnection/index.jsx @@ -13,6 +13,7 @@ import { import { CONNECTION_STATUS } from '@shared/constants/connectionStatus'; import store from '@redux/store'; import { Layout } from '@components'; +import extension from 'extensionizer'; import initConfig from '../../../../locales'; import SIZES from '../Transfer/constants'; import useStyles from './styles'; @@ -50,7 +51,12 @@ const AppConnection = () => { // onClickHandler(CONNECTION_STATUS.rejected); // }; - window.resizeTo(SIZES.width, SIZES.appConnectHeight); + extension.windows.update( + extension.windows.WINDOW_ID_CURRENT, + { + height: SIZES.appConnectHeight, + }, + ); return ( diff --git a/source/Pages/Notification/components/Transfer/components/Data.jsx b/source/Pages/Notification/components/Transfer/components/Data.jsx index e8368b4a..3a619992 100644 --- a/source/Pages/Notification/components/Transfer/components/Data.jsx +++ b/source/Pages/Notification/components/Transfer/components/Data.jsx @@ -1,15 +1,21 @@ import React from 'react'; import PropTypes from 'prop-types'; import { FormItem } from '@ui'; +import extension from 'extensionizer'; import useStyles from '../styles'; import SIZES from '../constants'; const Data = ({ data, principalId }) => { const classes = useStyles(); - window.resizeTo(SIZES.width, principalId - ? SIZES.dataHeightBig - : SIZES.dataHeightSmall); + extension.windows.update( + extension.windows.WINDOW_ID_CURRENT, + { + height: principalId + ? SIZES.dataHeightBig + : SIZES.dataHeightSmall, + }, + ); return (
diff --git a/source/Pages/Notification/components/Transfer/components/Details.jsx b/source/Pages/Notification/components/Transfer/components/Details.jsx index 902e6178..c5d331aa 100644 --- a/source/Pages/Notification/components/Transfer/components/Details.jsx +++ b/source/Pages/Notification/components/Transfer/components/Details.jsx @@ -6,6 +6,7 @@ import { Typography } from '@material-ui/core'; import { CURRENCIES } from '@shared/constants/currencies'; import { E8S_PER_ICP } from '@background/Keyring'; import NumberFormat from 'react-number-format'; +import extension from 'extensionizer'; import useStyles from '../styles'; import SIZES from '../constants'; @@ -19,9 +20,14 @@ const Details = ({ const amount = e8s / E8S_PER_ICP; const value = (amount * icpPrice); - window.resizeTo(SIZES.width, requestCount > 1 - ? SIZES.detailsHeightBig - : SIZES.detailHeightSmall); + extension.windows.update( + extension.windows.WINDOW_ID_CURRENT, + { + height: requestCount > 1 + ? SIZES.detailsHeightBig + : SIZES.detailHeightSmall, + }, + ); return (