Skip to content
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

fix: switch rpc in url should not auto use local fork #10308

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 20 additions & 20 deletions packages/apps/src/Endpoints/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function loadAffinities (groups: Group[]): Record<string, string> {

function isSwitchDisabled (hasUrlChanged: boolean, apiUrl: string, isUrlValid: boolean): boolean {
if (!hasUrlChanged) {
if (store.get('isLocalFork')) {
if (store.get('localFork') === apiUrl) {
return false;
} else {
return true;
Expand All @@ -136,7 +136,7 @@ function isSwitchDisabled (hasUrlChanged: boolean, apiUrl: string, isUrlValid: b

function isLocalForkDisabled (hasUrlChanged: boolean, apiUrl: string, isUrlValid: boolean): boolean {
if (!hasUrlChanged) {
if (store.get('isLocalFork')) {
if (store.get('localFork') === apiUrl) {
return true;
} else {
return false;
Expand Down Expand Up @@ -243,7 +243,7 @@ function Endpoints ({ className = '', offset, onClose }: Props): React.ReactElem

const _onApply = useCallback(
(): void => {
store.set('isLocalFork', false);
store.set('localFork', '');
settings.set({ ...(settings.get()), apiUrl });
window.location.assign(`${window.location.origin}${window.location.pathname}?rpc=${encodeURIComponent(apiUrl)}${window.location.hash}`);

Expand All @@ -258,7 +258,7 @@ function Endpoints ({ className = '', offset, onClose }: Props): React.ReactElem

const _onLocalFork = useCallback(
(): void => {
store.set('isLocalFork', true);
store.set('localFork', apiUrl);
settings.set({ ...(settings.get()), apiUrl });
window.location.assign(`${window.location.origin}${window.location.pathname}?rpc=${encodeURIComponent(apiUrl)}${window.location.hash}`);

Expand Down Expand Up @@ -296,27 +296,27 @@ function Endpoints ({ className = '', offset, onClose }: Props): React.ReactElem

return (
<StyledSidebar
button={
<Button
icon='sync'
isDisabled={canSwitch}
label={t('Switch')}
onClick={_onApply}
/>
buttons={
<>
<Button
icon='code-fork'
isDisabled={canLocalFork}
label={t('Fork Locally')}
onClick={_onLocalFork}
tooltip='fork-locally-btn'
/>
<Button
icon='sync'
isDisabled={canSwitch}
label={t('Switch')}
onClick={_onApply}
/>
</>
}
className={className}
offset={offset}
onClose={onClose}
position='left'
secondaryButton={
<Button
icon='code-fork'
isDisabled={canLocalFork}
label={t('Fork Locally')}
onClick={_onLocalFork}
tooltip='fork-locally-btn'
/>
}
sidebarRef={sidebarRef}
>
{groups.map((group, index): React.ReactNode => (
Expand Down
4 changes: 3 additions & 1 deletion packages/apps/src/overlays/LocalFork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import React from 'react';
import store from 'store';

import { settings } from '@polkadot/ui-settings';

import { useTranslation } from '../translate.js';
import BaseOverlay from './Base.js';

Expand All @@ -14,7 +16,7 @@ interface Props {
function LocalFork ({ className }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();

if (store.get('isLocalFork')) {
if (store.get('localFork') === settings.get().apiUrl) {
return (
<BaseOverlay
className={className}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-api/src/Api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ async function createApi (apiUrl: string, signer: ApiSigner, onError: (error: un
try {
if (isLight) {
provider = await getLightProvider(apiUrl.replace('light://', ''));
} else if (store.get('isLocalFork')) {
} else if (store.get('localFork') === apiUrl) {
provider = await ChopsticksProvider.fromEndpoint(apiUrl);
await setStorage(provider.chain, {
System: {
Expand Down Expand Up @@ -321,7 +321,7 @@ export function ApiCtxRoot ({ apiUrl, children, isElectron, store: keyringStore
.catch(onError);
});

if (store.get('isLocalFork')) {
if (store.get('localFork') === apiUrl) {
statics.api.connect()
.catch(onError);
}
Expand Down
8 changes: 3 additions & 5 deletions packages/react-components/src/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import Button from './Button/index.js';
import { styled } from './styled.js';

interface Props {
button?: React.ReactNode;
secondaryButton?: React.ReactNode;
buttons?: React.ReactNode;
children: React.ReactNode;
className?: string;
dataTestId?: string;
Expand All @@ -18,16 +17,15 @@ interface Props {
sidebarRef: React.RefObject<HTMLDivElement>;
}

function Sidebar ({ button, children, className = '', dataTestId = '', onClose, position, secondaryButton, sidebarRef }: Props): React.ReactElement<Props> {
function Sidebar ({ buttons, children, className = '', dataTestId = '', onClose, position, sidebarRef }: Props): React.ReactElement<Props> {
return (
<StyledDiv
className={`${className} ui--Sidebar ${position}Position`}
data-testid={dataTestId}
ref={sidebarRef}
>
<Button.Group className='ui--Sidebar-buttons'>
{button}
{secondaryButton}
{buttons}
<Button
dataTestId='close-sidebar-button'
icon='times'
Expand Down
Loading