refactor: rename testnet localStorage helpers

This commit is contained in:
Ben Kremer 2022-02-10 16:53:46 +01:00
parent c058d500d0
commit bb744c48c5
2 changed files with 11 additions and 8 deletions

View File

@ -7,7 +7,7 @@ import Column from "./components/Column";
import Header from "./components/Header"; import Header from "./components/Header";
import Modal from "./components/Modal"; import Modal from "./components/Modal";
import { DEFAULT_MAIN_CHAINS, DEFAULT_TEST_CHAINS } from "./constants"; import { DEFAULT_MAIN_CHAINS, DEFAULT_TEST_CHAINS } from "./constants";
import { AccountAction, setInitialStateTestnet, getInitialStateTestnet } from "./helpers"; import { AccountAction, getLocalStorageTestnetFlag, setLocaleStorageTestnetFlag } from "./helpers";
import Toggle from "./components/Toggle"; import Toggle from "./components/Toggle";
import RequestModal from "./modals/RequestModal"; import RequestModal from "./modals/RequestModal";
import PairingModal from "./modals/PairingModal"; import PairingModal from "./modals/PairingModal";
@ -26,7 +26,7 @@ import { useWalletConnectClient } from "./contexts/ClientContext";
import { useJsonRpc } from "./contexts/JsonRpcContext"; import { useJsonRpc } from "./contexts/JsonRpcContext";
export default function App() { export default function App() {
const [isTestnet, setIsTestnet] = useState(getInitialStateTestnet()); const [isTestnet, setIsTestnet] = useState(getLocalStorageTestnetFlag());
const [modal, setModal] = useState(""); const [modal, setModal] = useState("");
@ -35,6 +35,7 @@ export default function App() {
const openPingModal = () => setModal("ping"); const openPingModal = () => setModal("ping");
const openRequestModal = () => setModal("request"); const openRequestModal = () => setModal("request");
// Initialize the WalletConnect client.
const { const {
client, client,
session, session,
@ -48,10 +49,11 @@ export default function App() {
setChains, setChains,
} = useWalletConnectClient(); } = useWalletConnectClient();
// Use `JsonRpcContext` to provide us with relevant RPC methods and states.
const { chainData, ping, ethereumRpc, cosmosRpc, isRpcRequestPending, rpcResult } = useJsonRpc(); const { chainData, ping, ethereumRpc, cosmosRpc, isRpcRequestPending, rpcResult } = useJsonRpc();
useEffect(() => {
// Close the pairing modal after a session is established. // Close the pairing modal after a session is established.
useEffect(() => {
if (session && modal === "pairing") { if (session && modal === "pairing") {
closeModal(); closeModal();
} }
@ -61,9 +63,11 @@ export default function App() {
if (typeof client === "undefined") { if (typeof client === "undefined") {
throw new Error("WalletConnect is not initialized"); throw new Error("WalletConnect is not initialized");
} }
// Suggest existing pairings (if any).
if (client.pairing.topics.length) { if (client.pairing.topics.length) {
return openPairingModal(); return openPairingModal();
} }
// If no existing pairings are available, trigger `WalletConnectClient.connect`.
connect(); connect();
}; };
@ -123,8 +127,7 @@ export default function App() {
const toggleTestnets = () => { const toggleTestnets = () => {
const nextIsTestnetState = !isTestnet; const nextIsTestnetState = !isTestnet;
setIsTestnet(nextIsTestnetState); setIsTestnet(nextIsTestnetState);
// TODO: rename "setLocalStorage..." setLocaleStorageTestnetFlag(nextIsTestnetState);
setInitialStateTestnet(nextIsTestnetState);
}; };
const handleChainSelectionClick = (chainId: string) => { const handleChainSelectionClick = (chainId: string) => {

View File

@ -185,15 +185,15 @@ export const fromWad = (wad: BigNumberish, decimals = 18): string => {
export const LOCALSTORAGE_KEY_TESTNET = "TESTNET"; export const LOCALSTORAGE_KEY_TESTNET = "TESTNET";
export const INITIAL_STATE_TESTNET_DEFAULT = true; export const INITIAL_STATE_TESTNET_DEFAULT = true;
export function setInitialStateTestnet(value: boolean): void { export function setLocaleStorageTestnetFlag(value: boolean): void {
window.localStorage.setItem(LOCALSTORAGE_KEY_TESTNET, `${value}`); window.localStorage.setItem(LOCALSTORAGE_KEY_TESTNET, `${value}`);
} }
export function getInitialStateTestnet(): boolean { export function getLocalStorageTestnetFlag(): boolean {
let value = INITIAL_STATE_TESTNET_DEFAULT; let value = INITIAL_STATE_TESTNET_DEFAULT;
const persisted = window.localStorage.getItem(LOCALSTORAGE_KEY_TESTNET); const persisted = window.localStorage.getItem(LOCALSTORAGE_KEY_TESTNET);
if (!persisted) { if (!persisted) {
setInitialStateTestnet(value); setLocaleStorageTestnetFlag(value);
} else { } else {
value = persisted === "true" ? true : false; value = persisted === "true" ? true : false;
} }