Skip to content

Commit

Permalink
feat: add voi testnet block explorer (#38)
Browse files Browse the repository at this point in the history
* feat: add voi testnet block explorer

* fix: settings now stores block explorers for each network
  • Loading branch information
kieranroneill authored Dec 5, 2023
1 parent 99c8959 commit b7498d1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 24 deletions.
14 changes: 13 additions & 1 deletion src/extension/config/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,19 @@ const networks: INetwork[] = [
],
canonicalName: 'Voi',
chakraTheme: 'voi',
explorers: [],
explorers: [
{
accountPath: '/account',
applicationPath: '/application',
assetPath: '/asset',
baseUrl: 'https://voi.observer/explorer',
blockPath: '/block',
canonicalName: 'Voi Observer',
groupPath: '/group',
id: 'voi-observer',
transactionPath: '/transaction',
},
],
genesisId: 'voitest-v1',
genesisHash: 'IXnoWtviVVJW5LGivNFc0Dq14V3kqaXuK2u5OQrdVZo=',
indexers: [
Expand Down
6 changes: 4 additions & 2 deletions src/extension/features/settings/thunks/setSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {

// utils
import {
convertGenesisHashToHex,
selectDefaultNetwork,
selectNetworkFromSettings,
} from '@extension/utils';
Expand All @@ -48,8 +49,9 @@ const setSettings: AsyncThunk<
) {
selectedNetwork = selectDefaultNetwork(networks);

settings.general.preferredBlockExplorerId =
selectedNetwork.explorers[0]?.id || null;
settings.general.preferredBlockExplorerIds[
convertGenesisHashToHex(selectedNetwork.genesisHash).toUpperCase()
] = selectedNetwork.explorers[0]?.id || null;
settings.general.selectedNetworkGenesisHash = selectedNetwork.genesisHash;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function createDefaultSettings(networks: INetwork[]): ISettings {
theme: 'light',
},
general: {
preferredBlockExplorerId: defaultNetwork.explorers[0]?.id || null,
preferredBlockExplorerIds: {},
selectedNetworkGenesisHash: defaultNetwork.genesisHash,
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/extension/features/settings/utils/getInitialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function getInitialState(): ISettingsState {
},
fetching: false,
general: {
preferredBlockExplorerId: null,
preferredBlockExplorerIds: {},
selectedNetworkGenesisHash: null,
},
saving: false,
Expand Down
45 changes: 29 additions & 16 deletions src/extension/pages/GeneralSettingsPage/GeneralSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
INetwork,
ISettings,
} from '@extension/types';
import { convertGenesisHashToHex } from '@extension/utils';

const GeneralSettingsPage: FC = () => {
const { t } = useTranslation();
Expand All @@ -45,21 +46,30 @@ const GeneralSettingsPage: FC = () => {
const handlePreferredBlockExplorerChange = (
event: ChangeEvent<HTMLSelectElement>
) => {
const explorer: IExplorer | null =
selectedNetwork?.explorers.find(
(value) => value.id === event.target.value
) || null;
let explorer: IExplorer | null;

if (explorer) {
dispatch(
setSettings({
...settings,
general: {
...settings.general,
preferredBlockExplorerId: explorer.id,
},
})
);
if (selectedNetwork) {
explorer =
selectedNetwork?.explorers.find(
(value) => value.id === event.target.value
) || null;

if (explorer) {
dispatch(
setSettings({
...settings,
general: {
...settings.general,
preferredBlockExplorerIds: {
...settings.general.preferredBlockExplorerIds,
[convertGenesisHashToHex(
selectedNetwork.genesisHash
).toUpperCase()]: explorer.id,
},
},
})
);
}
}
};

Expand All @@ -82,8 +92,11 @@ const GeneralSettingsPage: FC = () => {
value: value.id,
}))}
value={
settings.general.preferredBlockExplorerId ||
selectedNetwork.explorers[0].id
settings.general.preferredBlockExplorerIds[
convertGenesisHashToHex(
selectedNetwork.genesisHash
).toUpperCase()
] || selectedNetwork.explorers[0].id
}
/>
)}
Expand Down
11 changes: 9 additions & 2 deletions src/extension/selectors/useSelectPreferredBlockExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useSelector } from 'react-redux';

// types
import { IExplorer, IMainRootState, INetwork } from '@extension/types';
import { convertGenesisHashToHex } from '@extension/utils';

/**
* Gets the currently preferred block explorer from the settings.
Expand All @@ -22,8 +23,14 @@ export default function useSelectPreferredBlockExplorer(): IExplorer | null {

return (
selectedNetwork.explorers.find(
(value) => value.id === state.settings.general.preferredBlockExplorerId
) || null
(value) =>
value.id ===
state.settings.general.preferredBlockExplorerIds[
convertGenesisHashToHex(selectedNetwork.genesisHash).toUpperCase()
]
) ||
selectedNetwork.explorers[0] ||
null
);
});
}
2 changes: 1 addition & 1 deletion src/extension/types/IGeneralSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface IGeneralSettings {
preferredBlockExplorerId: string | null;
preferredBlockExplorerIds: Record<string, string | null>;
selectedNetworkGenesisHash: string | null;
}

Expand Down

0 comments on commit b7498d1

Please sign in to comment.