Skip to content

refactor: separate routing #1673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/extension-base/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const START_WITH_PATH = [
'/derivefs/'
] as const;

const ROOT_PATH = [
'/authorize',
'/signing',
'/metadata'
] as const;

const EXTENSION_PREFIX = 'POLKAGATE';

const PORT_PREFIX = `${EXTENSION_PREFIX || 'unknown'}-${process.env['PORT_PREFIX'] || 'unknown'}`;
Expand All @@ -40,5 +46,6 @@ export {
PHISHING_PAGE_REDIRECT,
PORT_CONTENT,
PORT_EXTENSION,
ROOT_PATH, // Added for PolkaGate
START_WITH_PATH// Added for PolkaGate
};
2 changes: 1 addition & 1 deletion packages/extension-polkagate/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export * from './contexts';
export { default as Convictions } from './Convictions';
export { default as CopyAddressButton } from './CopyAddressButton';
export { default as DisplayInfo } from './DisplayInfo';
export { default as ErrorBoundary } from './ErrorBoundary';
export { default as ErrorBoundary } from '../../../extension-ui/src/Popup/components/ErrorBoundary';
export { default as FormatBalance } from './FormatBalance';
export { default as FormatBalance2 } from './FormatBalance2';
export { default as FormatPrice } from './FormatPrice';
Expand Down
7 changes: 4 additions & 3 deletions packages/extension-polkagate/src/components/translate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
// SPDX-License-Identifier: Apache-2.0
// @ts-nocheck

import { useTranslation as useTranslationBase, UseTranslationResponse, withTranslation } from 'react-i18next';
import type { UseTranslationResponse } from 'react-i18next';

export function useTranslation(): UseTranslationResponse<string, undefined> {
import { useTranslation as useTranslationBase, withTranslation } from 'react-i18next';

export function useTranslation (): UseTranslationResponse<string, undefined> {
return useTranslationBase();
}

Expand Down
7 changes: 3 additions & 4 deletions packages/extension-polkagate/src/hooks/useFullscreen.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
// SPDX-License-Identifier: Apache-2.0
// @ts-nocheck

import { useEffect } from 'react';
import { useLayoutEffect } from 'react';

export default function useFullscreen(): void {
useEffect(() => {
export default function useFullscreen (): void {
useLayoutEffect(() => {
/** to change app width to full screen */
const root = document.getElementById('root');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import type { WithTranslation } from 'react-i18next';
import { Grid, Typography } from '@mui/material';
import React from 'react';

import HeaderBrand from '../partials/HeaderBrand';
import { EXTENSION_NAME } from '../util/constants';
import translate from './translate';
import { PButton } from '.';
import { PButton } from '@polkadot/extension-polkagate/src/components';
import translate from '@polkadot/extension-polkagate/src/components/translate';
import AlertBox from '@polkadot/extension-polkagate/src/partials/AlertBox';
import HeaderBrand from '@polkadot/extension-polkagate/src/partials/HeaderBrand';
import { EXTENSION_NAME } from '@polkadot/extension-polkagate/src/util/constants';

interface Props extends WithTranslation {
children: React.ReactNode;
Expand All @@ -26,6 +27,26 @@ interface State {

// NOTE: This is the only way to do an error boundary, via extend
class ErrorBoundary extends React.Component<Props> {
private isExtensionPopup: boolean;

constructor (props: Props) {
super(props);

// Initialize extension detection in constructor
this.isExtensionPopup = false;

if (chrome?.extension?.getViews) {
const extensionViews = chrome.extension.getViews({ type: 'popup' });
const isPopupOpenedByExtension = extensionViews.includes(window);

if (isPopupOpenedByExtension) {
this.isExtensionPopup = true;
}
} else {
this.isExtensionPopup = window.innerWidth <= 357 && window.innerHeight <= 621;
}
}

public override state: State = { error: null };

public static getDerivedStateFromError (error: Error): Partial<State> {
Expand Down Expand Up @@ -73,10 +94,12 @@ class ErrorBoundary extends React.Component<Props> {
_onClick={this.#goHome}
text={t<string>('Back to home')}
/>
{/* </ButtonArea> */}
</>
)
: children;
: <>
{children}
{!this.isExtensionPopup && <AlertBox />}
</>;
}
}

Expand Down
20 changes: 20 additions & 0 deletions packages/extension-ui/src/Popup/components/RouteWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
// SPDX-License-Identifier: Apache-2.0

import React from 'react';

import ErrorBoundary from './ErrorBoundary';

interface RouteWrapperProps {
component: React.ComponentType;
trigger?: string;
props?: Record<string, unknown>;
}

const RouteWrapper = ({ component: Component, props, trigger }: RouteWrapperProps) => (
<ErrorBoundary trigger={trigger}>
<Component {...props} />
</ErrorBoundary>
);

export default React.memo(RouteWrapper);
Loading
Loading