explorer: Add developer setting to enable customUrl param (#13697)

This commit is contained in:
Justin Starry
2020-11-19 22:03:20 +08:00
committed by GitHub
parent 110acd20dc
commit 83799356dd
3 changed files with 64 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import React from "react"; import React, { ChangeEvent } from "react";
import { Link, useHistory, useLocation } from "react-router-dom"; import { Link, useHistory, useLocation } from "react-router-dom";
import { useDebounceCallback } from "@react-hook/debounce"; import { useDebounceCallback } from "@react-hook/debounce";
import { Location } from "history"; import { Location } from "history";
@ -20,6 +20,15 @@ import { useQuery } from "utils/url";
export function ClusterModal() { export function ClusterModal() {
const [show, setShow] = useClusterModal(); const [show, setShow] = useClusterModal();
const onClose = () => setShow(false); const onClose = () => setShow(false);
const enableCustomUrl = localStorage.getItem("enableCustomUrl") !== null;
const onToggleCustomUrlFeature = (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.checked) {
localStorage.setItem("enableCustomUrl", "");
} else {
localStorage.removeItem("enableCustomUrl");
}
};
return ( return (
<> <>
<div <div
@ -34,8 +43,31 @@ export function ClusterModal() {
</span> </span>
<h2 className="text-center mb-4 mt-4">Choose a Cluster</h2> <h2 className="text-center mb-4 mt-4">Choose a Cluster</h2>
<ClusterToggle /> <ClusterToggle />
<hr />
<h2 className="text-center mb-4 mt-4">Developer Settings</h2>
<div className="d-flex justify-content-between">
<span className="mr-3">Enable custom url param</span>
<div className="custom-control custom-switch d-inline">
<input
type="checkbox"
defaultChecked={enableCustomUrl}
className="custom-control-input"
id="cardToggle"
onChange={onToggleCustomUrlFeature}
/>
<label
className="custom-control-label"
htmlFor="cardToggle"
></label>
</div>
</div>
<p className="text-muted font-size-sm mt-3">
Enable this setting to easily connect to a custom cluster via
the "customUrl" url param.
</p>
</div> </div>
</div> </div>
</div> </div>
@ -59,7 +91,10 @@ function CustomClusterInput({ activeSuffix, active }: InputProps) {
active ? `${prefix}-${activeSuffix}` : ""; active ? `${prefix}-${activeSuffix}` : "";
const clusterLocation = (location: Location) => { const clusterLocation = (location: Location) => {
if (customUrl.length > 0) query.set("cluster", "custom"); if (customUrl.length > 0) {
query.set("cluster", "custom");
query.set("customUrl", customUrl);
}
return { return {
...location, ...location,
search: query.toString(), search: query.toString(),
@ -70,6 +105,7 @@ function CustomClusterInput({ activeSuffix, active }: InputProps) {
updateCustomUrl(url); updateCustomUrl(url);
if (url.length > 0) { if (url.length > 0) {
query.set("cluster", "custom"); query.set("cluster", "custom");
query.set("customUrl", url);
history.push({ ...location, search: query.toString() }); history.push({ ...location, search: query.toString() });
} }
}, 500); }, 500);

View File

@ -134,22 +134,34 @@ export function ClusterProvider({ children }: ClusterProviderProps) {
const [showModal, setShowModal] = React.useState(false); const [showModal, setShowModal] = React.useState(false);
const query = useQuery(); const query = useQuery();
const cluster = parseQuery(query); const cluster = parseQuery(query);
const enableCustomUrl = localStorage.getItem("enableCustomUrl") !== null;
const customUrl = enableCustomUrl
? query.get("customUrl") || ""
: state.customUrl;
const history = useHistory(); const history = useHistory();
const location = useLocation(); const location = useLocation();
// Remove customUrl param if dev setting is disabled
React.useEffect(() => {
if (!enableCustomUrl) {
query.delete("customUrl");
history.push({ ...location, search: query.toString() });
}
}, [enableCustomUrl]); // eslint-disable-line react-hooks/exhaustive-deps
// Reconnect to cluster when params change // Reconnect to cluster when params change
React.useEffect(() => { React.useEffect(() => {
if (cluster === Cluster.Custom) { if (cluster === Cluster.Custom) {
// Remove cluster param if custom url has not been set // Remove cluster param if custom url has not been set
if (state.customUrl.length === 0) { if (customUrl.length === 0) {
query.delete("cluster"); query.delete("cluster");
history.push({ ...location, search: query.toString() }); history.push({ ...location, search: query.toString() });
return; return;
} }
} }
updateCluster(dispatch, cluster, state.customUrl); updateCluster(dispatch, cluster, customUrl);
}, [cluster, state.customUrl]); // eslint-disable-line react-hooks/exhaustive-deps }, [cluster, customUrl]); // eslint-disable-line react-hooks/exhaustive-deps
return ( return (
<StateContext.Provider value={state}> <StateContext.Provider value={state}>

View File

@ -7,24 +7,24 @@ export function useQuery() {
export const clusterPath = (pathname: string) => { export const clusterPath = (pathname: string) => {
return (location: Location) => ({ return (location: Location) => ({
...pickCluster(location), ...pickClusterParams(location),
pathname, pathname,
}); });
}; };
export function pickCluster(location: Location): Location { export function pickClusterParams(location: Location): Location {
const cluster = new URLSearchParams(location.search).get("cluster"); const urlParams = new URLSearchParams(location.search);
const cluster = urlParams.get("cluster");
const customUrl = urlParams.get("customUrl");
let search = ""; // Pick the params we care about
if (cluster) { const newParams = new URLSearchParams();
const params = new URLSearchParams(); if (cluster) newParams.set("cluster", cluster);
params.set("cluster", cluster); if (customUrl) newParams.set("customUrl", customUrl);
search = params.toString();
}
return { return {
...location, ...location,
search, search: newParams.toString(),
}; };
} }