Skip to content

Commit

Permalink
Fix node url removal
Browse files Browse the repository at this point in the history
Reintroduce remove URL state function & make the appStore persist
  • Loading branch information
grctest committed Jul 24, 2023
1 parent 295768e commit f80407b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 61 deletions.
4 changes: 0 additions & 4 deletions src/components/beet/BeetLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ import { appStore, beetStore, identitiesStore } from '../../lib/states';
export default function BeetLink(properties) {
const { t, i18n } = useTranslation();
const environment = appStore((state) => state.environment);
const setMode = appStore((state) => state.setMode);

const link = beetStore((state) => state.link);
const setConnection = beetStore((state) => state.setConnection);
const setAuthenticated = beetStore((state) => state.setAuthenticated);

const identity = beetStore((state) => state.identity);
const setIdentities = identitiesStore((state) => state.setIdentities);

const [inProgress, setInProgress] = useState(false);
/*
* After connection attempt to link app to Beet client
Expand Down
4 changes: 0 additions & 4 deletions src/components/beet/GetAccount.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ export default function GetAccount(properties) {
assetName = "TEST";
relevantChain = 'BTS_TEST';
titleName = "Bitshares (Testnet)";
} else if (env === 'tusc' || params.env === 'tusc') {
assetName = "TUSC";
relevantChain = 'TUSC';
titleName = "TUSC";
}

useEffect(() => {
Expand Down
137 changes: 84 additions & 53 deletions src/lib/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,60 +121,90 @@ const identitiesStore = create(
);

/**
* NFT_Viewer related
* Global app settings
*/
const appStore = create((set, get) => ({
nodes: {
bitshares: config.bitshares.nodeList.map((node) => node.url),
bitshares_testnet: config.bitshares_testnet.nodeList.map((node) => node.url),
tusc: config.tusc.nodeList.map((node) => node.url),
},
environment: null,
setEnvironment: (env) => set({ environment: env }),
replaceNodes: (env, nodes) => {
if (env === 'bitshares') {
set(async (state) => ({
nodes: { ...state.nodes, bitshares: nodes },
}));
} else if (env === 'bitshares_testnet') {
set(async (state) => ({
nodes: { ...state.nodes, bitshares_testnet: nodes },
}));
} else if (env === 'tusc') {
set(async (state) => ({
nodes: { ...state.nodes, tusc: nodes },
}));
}
},
changeURL: (env) => {
/**
* The current node url isn't healthy anymore
* shift it to the back of the queue
* Replaces nodeFailureCallback
*/
console.log('Changing primary node');
const nodesToChange = get().nodes[env];
nodesToChange.push(nodesToChange.shift()); // Moving misbehaving node to end

if (env === 'bitshares') {
set(async (state) => ({
nodes: { ...state.nodes, bitshares: nodesToChange },
}));
} else if (env === 'bitshares_testnet') {
set(async (state) => ({
nodes: { ...state.nodes, bitshares_testnet: nodesToChange },
}));
} else if (env === 'tusc') {
set(async (state) => ({
nodes: { ...state.nodes, tusc: nodesToChange },
}));
}
},
reset: () => set({
environment: null,
nodes: null,
}),
}));
const appStore = create(
persist(
(set, get) => ({
environment: null,
nodes: {
bitshares: config.bitshares.nodeList.map((node) => node.url),
bitshares_testnet: config.bitshares_testnet.nodeList.map((node) => node.url),
},
setEnvironment: (env) => set({ environment: env }),
setNodes: async () => {
const env = get().environment;
if (!env) {
console.log('No env set');
return;
}

let response;
try {
response = await testNodes(env === 'production' ? 'BTS' : 'BTS_TEST');
} catch (error) {
console.log(error);
}

if (response) {
set({ nodes: await response });
}
},
changeURL: (env) => {
/**
* The current node url isn't healthy anymore
* shift it to the back of the queue
* Replaces nodeFailureCallback
*/
console.log('Changing primary node');
const nodesToChange = get().nodes[env];
nodesToChange.push(nodesToChange.shift()); // Moving misbehaving node to end

if (env === 'bitshares') {
set(async (state) => ({
nodes: { ...state.nodes, bitshares: nodesToChange },
}));
} else if (env === 'bitshares_testnet') {
set(async (state) => ({
nodes: { ...state.nodes, bitshares_testnet: nodesToChange },
}));
} else if (env === 'tusc') {
set(async (state) => ({
nodes: { ...state.nodes, tusc: nodesToChange },
}));
}
},
removeURL: (env, url) => {
let nodesToChange = get().nodes[env];
nodesToChange = nodesToChange.filter((x) => x !== url);

if (env === 'bitshares') {
set((state) => ({
nodes: { ...state.nodes, bitshares: nodesToChange },
}));
} else if (env === 'bitshares_testnet') {
set((state) => ({
nodes: { ...state.nodes, bitshares_testnet: nodesToChange },
}));
} else if (env === 'tusc') {
set((state) => ({
nodes: { ...state.nodes, tusc: nodesToChange },
}));
}
},
reset: () => set({
environment: null,
nodes: {
bitshares: config.bitshares.nodeList.map((node) => node.url),
bitshares_testnet: config.bitshares_testnet.nodeList.map((node) => node.url),
},
}),
}),
{
name: 'nodeStorage',
},
),
);

/**
* Temporary store for the NFT Issuance Tool
Expand All @@ -183,6 +213,7 @@ const tempStore = create(
(set, get) => ({
account: "",
asset: null,
assets: null,
asset_images: null,
initialValues: null,
changing_images: false,
Expand Down

0 comments on commit f80407b

Please sign in to comment.