Skip to content

Commit

Permalink
chore: debug window close util
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Oct 19, 2023
1 parent e437710 commit 77056e7
Show file tree
Hide file tree
Showing 28 changed files with 73 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ module.exports = {
property: 'open',
message: avoidWindowOpenMsg,
},
{
object: 'window',
property: 'close',
message: 'Use `closeWindow` utility helper',
},
],
'@typescript-eslint/no-floating-promises': ['warn'],
'@typescript-eslint/no-unnecessary-type-assertion': ['warn'],
Expand Down
3 changes: 2 additions & 1 deletion src/app/common/rpc-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMemo } from 'react';
import { RpcErrorCode } from '@btckit/types';

import { WalletMethodMap, makeRpcErrorResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';

import { useDefaultRequestParams } from './hooks/use-default-request-search-params';
import { initialSearchParams } from './initial-search-params';
Expand Down Expand Up @@ -33,5 +34,5 @@ export function useRejectIfLedgerWallet(request: keyof WalletMethodMap) {
},
})
);
window.close();
closeWindow();
}
4 changes: 3 additions & 1 deletion src/app/components/generic-error/generic-error.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ReactNode } from 'react';

import { closeWindow } from '@shared/utils';

import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { Header } from '@app/components/header';

Expand All @@ -12,7 +14,7 @@ interface GenericErrorProps {
title: string;
}
export function GenericError(props: GenericErrorProps) {
const { body, helpTextList, onClose = () => window.close(), title } = props;
const { body, helpTextList, onClose = () => closeWindow(), title } = props;

useRouteHeader(<Header hideActions />);

Expand Down
6 changes: 4 additions & 2 deletions src/app/features/container/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useEffect } from 'react';
import { Toaster } from 'react-hot-toast';
import { Outlet, useLocation } from 'react-router-dom';

import { closeWindow } from '@shared/utils';

import { useInitalizeAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { LoadingSpinner } from '@app/components/loading-spinner';
Expand All @@ -21,8 +23,8 @@ export function Container() {
const analytics = useAnalytics();
const hasStateRehydrated = useHasStateRehydrated();

useOnWalletLock(() => window.close());
useOnSignOut(() => window.close());
useOnWalletLock(() => closeWindow());
useOnSignOut(() => closeWindow());

useRestoreFormState();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useNavigate } from 'react-router-dom';

import { RouteUrls } from '@shared/route-urls';
import { closeWindow } from '@shared/utils';

import { doesBrowserSupportWebUsbApi, whenPageMode } from '@app/common/utils';
import { openIndexPageInNewTab } from '@app/common/utils/open-in-new-tab';
Expand All @@ -18,7 +19,7 @@ export function ConnectLedgerStart() {
},
popup() {
void openIndexPageInNewTab(url);
window.close();
closeWindow();
},
});

Expand Down
3 changes: 2 additions & 1 deletion src/app/features/psbt-signer/psbt-signer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useNavigate } from 'react-router-dom';
import * as btc from '@scure/btc-signer';

import { RouteUrls } from '@shared/route-urls';
import { closeWindow } from '@shared/utils';

import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { SignPsbtArgs } from '@app/common/psbt/requests';
Expand Down Expand Up @@ -64,7 +65,7 @@ export function PsbtSigner(props: PsbtSignerProps) {

useRouteHeader(<PopupHeader displayAddresssBalanceOf="all" />);

useOnOriginTabClose(() => window.close());
useOnOriginTabClose(() => closeWindow());

const psbtRaw = useMemo(() => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { truncateMiddle } from '@stacks/ui-utils';
import { HStack } from 'leather-styles/jsx';

import { RouteUrls } from '@shared/route-urls';
import { closeWindow } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useDrawers } from '@app/common/hooks/use-drawers';
Expand All @@ -28,7 +29,7 @@ function InsufficientFundsActionButtons({ eventName }: InsufficientFundsActionBu

const onGetStx = () => {
void analytics.track(eventName);
window.close();
closeWindow();
void chrome.tabs.create({ url: 'index.html#/fund' });
};

Expand Down
4 changes: 3 additions & 1 deletion src/app/pages/choose-account/choose-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Outlet } from 'react-router-dom';

import { Flex, Stack, styled } from 'leather-styles/jsx';

import { closeWindow } from '@shared/utils';

import { useCancelAuthRequest } from '@app/common/authentication/use-cancel-auth-request';
import { useAppDetails } from '@app/common/hooks/auth/use-app-details';
import { useRouteHeader } from '@app/common/hooks/use-route-header';
Expand All @@ -17,7 +19,7 @@ export const ChooseAccount = memo(() => {
const cancelAuthentication = useCancelAuthRequest();

useRouteHeader(<></>);
useOnOriginTabClose(() => window.close());
useOnOriginTabClose(() => closeWindow());

const handleUnmount = async () => cancelAuthentication();

Expand Down
5 changes: 3 additions & 2 deletions src/app/pages/onboarding/welcome/welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from 'react';
import { Outlet, useNavigate } from 'react-router-dom';

import { RouteUrls } from '@shared/route-urls';
import { closeWindow } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useOnboardingState } from '@app/common/hooks/auth/use-onboarding-state';
Expand All @@ -27,7 +28,7 @@ export function WelcomePage() {
const startOnboarding = useCallback(async () => {
if (isPopupMode()) {
openIndexPageInNewTab(RouteUrls.Onboarding);
window.close();
closeWindow();
return;
}
setIsGeneratingWallet(true);
Expand All @@ -52,7 +53,7 @@ export function WelcomePage() {
},
popup() {
void openIndexPageInNewTab(url);
window.close();
closeWindow();
},
});

Expand Down
4 changes: 3 additions & 1 deletion src/app/pages/rpc-get-addresses/rpc-get-addresses.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { closeWindow } from '@shared/utils';

import { GetAddressesLayout } from './components/get-addresses.layout';
import { useGetAddresses } from './use-request-accounts';

export function RpcGetAddresses() {
const { origin, onUserApproveGetAddresses } = useGetAddresses();

if (origin === null) {
window.close();
closeWindow();
throw new Error('Origin is null');
}

Expand Down
3 changes: 2 additions & 1 deletion src/app/pages/rpc-get-addresses/use-request-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { bytesToHex } from '@stacks/common';
import { ecdsaPublicKeyToSchnorr } from '@shared/crypto/bitcoin/bitcoin.utils';
import { logger } from '@shared/logger';
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useRpcRequestParams } from '@app/common/rpc-helpers';
Expand Down Expand Up @@ -67,7 +68,7 @@ export function useGetAddresses() {
},
})
);
window.close();
closeWindow();
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState } from 'react';
import { Outlet, useOutletContext } from 'react-router-dom';

import { BtcFeeType } from '@shared/models/fees/bitcoin-fees.model';
import { closeWindow } from '@shared/utils';

import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { PopupHeader } from '@app/features/current-account/popup-header';
Expand All @@ -25,7 +26,7 @@ export function RpcSendTransferContainer() {
useRouteHeader(<PopupHeader displayAddresssBalanceOf="all" />);

if (origin === null) {
window.close();
closeWindow();
throw new Error('Origin is null');
}

Expand Down
5 changes: 0 additions & 5 deletions src/app/pages/rpc-send-transfer/rpc-send-transfer-summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ export function RpcSendTransferSummary() {

const { onCopy } = useClipboard(txId);

// TODO: Force close window?
// useOnMount(() => {
// setTimeout(() => window.close(), timeOut);
// });

function onClickLink() {
void analytics.track('view_rpc_send_transfer_confirmation', { symbol: 'BTC' });
handleOpenTxLink(txLink);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { truncateMiddle } from '@stacks/ui-utils';

import { closeWindow } from '@shared/utils';

import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { useRejectIfLedgerWallet } from '@app/common/rpc-helpers';
import { Disclaimer } from '@app/components/disclaimer';
Expand Down Expand Up @@ -40,7 +42,7 @@ function RpcSignBip322Message() {
const { chain } = useCurrentNetwork();

if (origin === null) {
window.close();
closeWindow();
throw new Error('Origin is null');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { deriveAddressIndexZeroFromAccount } from '@shared/crypto/bitcoin/bitcoin.utils';
import { logger } from '@shared/logger';
import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-search-params';
Expand Down Expand Up @@ -75,7 +76,7 @@ function useSignBip322MessageFactory({ address, signPsbt }: SignBip322MessageFac
},
})
);
window.close();
closeWindow();
},
async onUserApproveBip322MessageSigningRequest() {
setIsLoading(true);
Expand Down Expand Up @@ -106,7 +107,7 @@ function useSignBip322MessageFactory({ address, signPsbt }: SignBip322MessageFac
void analytics.track('user_approved_message_signing', { origin });

await allowTimeForUserToReadToast();
window.close();
closeWindow();
},
};
}
Expand Down
5 changes: 0 additions & 5 deletions src/app/pages/rpc-sign-psbt/rpc-sign-psbt-summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ export function RpcSignPsbtSummary() {

const { onCopy } = useClipboard(txId);

// TODO: Force close window?
// useOnMount(() => {
// setTimeout(() => window.close(), timeOut);
// });

function onClickLink() {
void analytics.track('view_rpc_sign_and_broadcast_psbt_confirmation', { symbol: 'BTC' });
handleOpenTxLink(txLink);
Expand Down
5 changes: 3 additions & 2 deletions src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { bytesToHex } from '@stacks/common';
import { Money } from '@shared/models/money.model';
import { RouteUrls } from '@shared/route-urls';
import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { sumMoney } from '@app/common/money/calculate-money';
Expand Down Expand Up @@ -124,7 +125,7 @@ export function useRpcSignPsbt() {
return;
}

window.close();
closeWindow();
},
onCancel() {
chrome.tabs.sendMessage(
Expand All @@ -137,7 +138,7 @@ export function useRpcSignPsbt() {
},
})
);
window.close();
closeWindow();
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { bytesToHex } from '@stacks/common';
import { MultiSigSpendingCondition, deserializeTransaction } from '@stacks/transactions';

import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';

import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-search-params';
import { useRejectIfLedgerWallet } from '@app/common/rpc-helpers';
Expand Down Expand Up @@ -66,7 +67,7 @@ export function useRpcSignStacksTransaction() {
},
})
);
window.close();
closeWindow();
},
onCancel() {
chrome.tabs.sendMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isStructuredMessageType,
isUtf8MessageType,
} from '@shared/signature/signature-types';
import { closeWindow } from '@shared/utils';

import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { PopupHeader } from '@app/features/current-account/popup-header';
Expand All @@ -18,7 +19,7 @@ import { SignatureRequestStructuredDataContent } from './components/structured-d

export function StacksMessageSigningRequest() {
useRouteHeader(<PopupHeader />);
useOnOriginTabClose(() => window.close());
useOnOriginTabClose(() => closeWindow());

const { requestToken, messageType, tabId, origin } = useSignatureRequestSearchParams();

Expand Down
3 changes: 2 additions & 1 deletion src/app/pages/unlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Box } from 'leather-styles/jsx';

import { WALLET_ENVIRONMENT } from '@shared/environment';
import { RouteUrls } from '@shared/route-urls';
import { closeWindow } from '@shared/utils';

import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { isFullPageMode, isPopupMode } from '@app/common/utils';
Expand All @@ -24,7 +25,7 @@ export function Unlock() {
useEffect(() => {
if (!hasApprovedNewBrand && isPopupMode() && WALLET_ENVIRONMENT !== 'testing') {
openIndexPageInNewTab('/unlock/we-have-a-new-name');
window.close();
closeWindow();
}
if (!hasApprovedNewBrand && isFullPageMode()) {
navigate('./we-have-a-new-name');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { memo } from 'react';

import { isUndefined } from '@shared/utils';
import { closeWindow, isUndefined } from '@shared/utils';

import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { PopupHeader } from '@app/features/current-account/popup-header';
Expand All @@ -20,7 +20,7 @@ function ProfileUpdateRequestBase() {

useRouteHeader(<PopupHeader />);

useOnOriginTabClose(() => window.close());
useOnOriginTabClose(() => closeWindow());
if (isUndefined(validProfileUpdateRequest) || !validProfileUpdateRequest || !requestToken)
return (
<ProfileUpdateRequestLayout>
Expand Down
3 changes: 2 additions & 1 deletion src/shared/actions/finalize-auth-response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DecodedAuthRequest } from '@shared/models/decoded-auth-request';
import { closeWindow } from '@shared/utils';
import { analytics } from '@shared/utils/analytics';
import { isValidUrl } from '@shared/utils/validate-url';

Expand Down Expand Up @@ -41,5 +42,5 @@ export function finalizeAuthResponse({

const responseMessage = formatAuthResponse({ request: authRequest, response: authResponse });
chrome.tabs.sendMessage(tabId, responseMessage);
window.close();
closeWindow();
}
4 changes: 3 additions & 1 deletion src/shared/actions/finalize-message-signature.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { SignatureData } from '@stacks/connect';

import { closeWindow } from '@shared/utils';

import { formatMessageSigningResponse } from './finalize-message-signature-format';

interface FinalizeMessageSignatureArgs {
Expand All @@ -14,5 +16,5 @@ export function finalizeMessageSignature({
}: FinalizeMessageSignatureArgs) {
const responseMessage = formatMessageSigningResponse({ request: requestPayload, response: data });
chrome.tabs.sendMessage(tabId, responseMessage);
window.close();
closeWindow();
}
Loading

0 comments on commit 77056e7

Please sign in to comment.