Skip to content

Commit

Permalink
fix(network): safely parse initial selected network
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed Oct 30, 2024
1 parent 77644e7 commit 8f0e2de
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/deploy-web/mvm.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"dependencies": {
"@akashnetwork/env-loader": "1.0.1",
"@akashnetwork/http-sdk": "1.0.7",
"@akashnetwork/network-store": "1.0.0",
"@akashnetwork/network-store": "1.0.1",
"@akashnetwork/ui": "1.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion apps/stats-web/mvm.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"@akashnetwork/network-store": "1.0.0",
"@akashnetwork/network-store": "1.0.1",
"@akashnetwork/ui": "1.0.0"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/network-store/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@akashnetwork/network-store",
"version": "1.0.0",
"version": "1.0.1",
"description": "Package containing browser storage for Akash Network",
"keywords": [],
"license": "Apache-2.0",
Expand Down
16 changes: 13 additions & 3 deletions packages/network-store/src/network.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getDefaultStore, useAtom } from "jotai";
import { atomWithStorage } from "jotai/utils";
import cloneDeep from "lodash/cloneDeep";

import { INITIAL_NETWORKS_CONFIG, MAINNET_ID, SANDBOX_ID } from "./network.config";
import { INITIAL_NETWORKS_CONFIG } from "./network.config";
import type { Network } from "./network.type";

interface NetworkStoreOptions {
Expand All @@ -19,6 +19,8 @@ interface NetworksStore {
data: Network[];
}

const networkIds = INITIAL_NETWORKS_CONFIG.map(({ id }) => id);

class NetworkStoreVersionsInitError extends Error {
errors: Error[];
constructor(errors: { network: Network; error: Error }[]) {
Expand Down Expand Up @@ -84,13 +86,21 @@ export class NetworkStore {
}
const raw = localStorage.getItem(this.STORAGE_KEY);

if (networkIds.some(id => id === raw)) {
return raw;
}

if (!raw) {
return this.options.defaultNetworkId;
}

const parsed = JSON.parse(raw);
try {
const parsed = JSON.parse(raw);

return [MAINNET_ID, SANDBOX_ID].includes(parsed) ? parsed : this.options.defaultNetworkId;
return networkIds.includes(parsed) ? parsed : this.options.defaultNetworkId;
} catch (error) {
return this.options.defaultNetworkId;
}
}

private async initiateNetworks() {
Expand Down

0 comments on commit 8f0e2de

Please sign in to comment.