From 53dfc211ecc3bb839bb607d9c46ba419bf072c9d Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 23 Feb 2021 14:53:22 -0800 Subject: [PATCH] explorer: detect if localstorage is available (#15499) * feat: detect if localstorage is available * feat: do not show developer settings in cluster modal if localStorage is disabled --- explorer/src/components/ClusterModal.tsx | 54 +++++++++++++----------- explorer/src/providers/cluster.tsx | 5 ++- explorer/src/utils/index.tsx | 11 +++++ 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/explorer/src/components/ClusterModal.tsx b/explorer/src/components/ClusterModal.tsx index d2abe5f52e..9d05cac39a 100644 --- a/explorer/src/components/ClusterModal.tsx +++ b/explorer/src/components/ClusterModal.tsx @@ -13,14 +13,16 @@ import { useClusterModal, useUpdateCustomUrl, } from "providers/cluster"; -import { assertUnreachable } from "../utils"; +import { assertUnreachable, localStorageIsAvailable } from "../utils"; import { Overlay } from "./common/Overlay"; import { useQuery } from "utils/url"; export function ClusterModal() { const [show, setShow] = useClusterModal(); const onClose = () => setShow(false); - const enableCustomUrl = localStorage.getItem("enableCustomUrl") !== null; + const showDeveloperSettings = localStorageIsAvailable(); + const enableCustomUrl = + showDeveloperSettings && localStorage.getItem("enableCustomUrl") !== null; const onToggleCustomUrlFeature = (e: ChangeEvent) => { if (e.target.checked) { localStorage.setItem("enableCustomUrl", ""); @@ -45,29 +47,33 @@ export function ClusterModal() {

Choose a Cluster

-
+ {showDeveloperSettings && ( + <> +
-

Developer Settings

-
- Enable custom url param -
- - -
-
-

- Enable this setting to easily connect to a custom cluster via - the "customUrl" url param. -

+

Developer Settings

+
+ Enable custom url param +
+ + +
+
+

+ Enable this setting to easily connect to a custom cluster + via the "customUrl" url param. +

+ + )} diff --git a/explorer/src/providers/cluster.tsx b/explorer/src/providers/cluster.tsx index c333b43db3..396fbba59d 100644 --- a/explorer/src/providers/cluster.tsx +++ b/explorer/src/providers/cluster.tsx @@ -3,6 +3,7 @@ import { clusterApiUrl, Connection } from "@solana/web3.js"; import { useQuery } from "../utils/url"; import { useHistory, useLocation } from "react-router-dom"; import { reportError } from "utils/sentry"; +import { localStorageIsAvailable } from "utils"; export enum ClusterStatus { Connected, @@ -134,7 +135,9 @@ export function ClusterProvider({ children }: ClusterProviderProps) { const [showModal, setShowModal] = React.useState(false); const query = useQuery(); const cluster = parseQuery(query); - const enableCustomUrl = localStorage.getItem("enableCustomUrl") !== null; + const enableCustomUrl = + localStorageIsAvailable() && + localStorage.getItem("enableCustomUrl") !== null; const customUrl = enableCustomUrl ? query.get("customUrl") || "" : state.customUrl; diff --git a/explorer/src/utils/index.tsx b/explorer/src/utils/index.tsx index d4f7f01138..5649d49586 100644 --- a/explorer/src/utils/index.tsx +++ b/explorer/src/utils/index.tsx @@ -99,3 +99,14 @@ export function wrap(input: string, length: number): string { } return result.join("\n"); } + +export function localStorageIsAvailable() { + const test = "test"; + try { + localStorage.setItem(test, test); + localStorage.removeItem(test); + return true; + } catch (e) { + return false; + } +}