Skip to content

Commit

Permalink
fix: Propagate URL changes to react state (#5188)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Behar <[email protected]>
  • Loading branch information
simster7 committed Mar 2, 2021
1 parent db65775 commit d9d831c
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Loading} from '../../../shared/components/loading';
import {Context} from '../../../shared/context';
import {historyUrl} from '../../../shared/history';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {Utils} from '../../../shared/utils';
import {SubmitWorkflowPanel} from '../../../workflows/components/submit-workflow-panel';
import {ClusterWorkflowTemplateEditor} from '../cluster-workflow-template-editor';
Expand All @@ -30,6 +31,14 @@ export const ClusterWorkflowTemplateDetails = ({history, location, match}: Route
const [template, setTemplate] = useState<ClusterWorkflowTemplate>();
const [edited, setEdited] = useState(false);

useEffect(
useQueryParams(history, p => {
setSidePanel(p.get('sidePanel') === 'true');
setTab(p.get('tab'));
}),
[history]
);

useEffect(() => setEdited(true), [template]);
useEffect(() => {
history.push(historyUrl('cluster-workflow-templates/{name}', {name, sidePanel, tab}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Loading} from '../../../shared/components/loading';
import {Context} from '../../../shared/context';
import {historyUrl} from '../../../shared/history';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {WidgetGallery} from '../../../widgets/widget-gallery';
import {CronWorkflowEditor} from '../cron-workflow-editor';

Expand All @@ -28,6 +29,14 @@ export const CronWorkflowDetails = ({match, location, history}: RouteComponentPr
const [edited, setEdited] = useState(false);
const [error, setError] = useState<Error>();

useEffect(
useQueryParams(history, p => {
setSidePanel(p.get('sidePanel'));
setTab(p.get('tab'));
}),
[history]
);

useEffect(
() =>
history.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {historyUrl} from '../../../shared/history';
import {ListWatch} from '../../../shared/list-watch';
import {RetryObservable} from '../../../shared/retry-observable';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {EventsPanel} from '../../../workflows/components/events-panel';
import {FullHeightLogsViewer} from '../../../workflows/components/workflow-logs-viewer/full-height-logs-viewer';
import {buildGraph} from './build-graph';
Expand All @@ -42,6 +43,18 @@ export const EventFlowPage = ({history, location, match}: RouteComponentProps<an
const [expanded, setExpanded] = useState(queryParams.get('expanded') === 'true');
const [selectedNode, setSelectedNode] = useState<Node>(queryParams.get('selectedNode'));
const [tab, setTab] = useState<Node>(queryParams.get('tab'));

useEffect(
useQueryParams(history, p => {
setShowFlow(p.get('showFlow') === 'true');
setShowWorkflows(p.get('showWorkflows') !== 'false');
setExpanded(p.get('expanded') === 'true');
setSelectedNode(p.get('selectedNode'));
setTab(p.get('tab'));
}),
[history]
);

useEffect(
() =>
history.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {Loading} from '../../../shared/components/loading';
import {Context} from '../../../shared/context';
import {historyUrl} from '../../../shared/history';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {EventsPanel} from '../../../workflows/components/events-panel';
import {EventSourceEditor} from '../event-source-editor';
import {EventSourceLogsViewer} from '../event-source-log-viewer';
Expand All @@ -26,6 +27,14 @@ export const EventSourceDetails = ({history, location, match}: RouteComponentPro
const [tab, setTab] = useState<string>(queryParams.get('tab'));
const [selectedNode, setSelectedNode] = useState<string>(queryParams.get('selectedNode'));

useEffect(
useQueryParams(history, p => {
setTab(p.get('tab'));
setSelectedNode(p.get('selectedNode'));
}),
[history]
);

useEffect(
() =>
history.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {Context} from '../../../shared/context';
import {Footnote} from '../../../shared/footnote';
import {historyUrl} from '../../../shared/history';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {EventsPanel} from '../../../workflows/components/events-panel';
import {EventSourceCreator} from '../event-source-creator';
import {EventSourceLogsViewer} from '../event-source-log-viewer';
Expand All @@ -34,6 +35,16 @@ export const EventSourceList = ({match, location, history}: RouteComponentProps<
const [sidePanel, setSidePanel] = useState(queryParams.get('sidePanel') === 'true');
const [selectedNode, setSelectedNode] = useState<Node>(queryParams.get('selectedNode'));
const [tab, setTab] = useState<Node>(queryParams.get('tab'));

useEffect(
useQueryParams(history, p => {
setSidePanel(p.get('sidePanel') === 'true');
setSelectedNode(p.get('selectedNode'));
setTab(p.get('tab'));
}),
[history]
);

useEffect(
() =>
history.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {Loading} from '../../../shared/components/loading';
import {Context} from '../../../shared/context';
import {historyUrl} from '../../../shared/history';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {SensorEditor} from '../sensor-editor';
import {SensorSidePanel} from '../sensor-side-panel';

Expand All @@ -30,6 +31,14 @@ export const SensorDetails = ({match, location, history}: RouteComponentProps<an
const [selectedLogNode, setSelectedLogNode] = useState<Node>(queryParams.get('selectedLogNode'));
const [error, setError] = useState<Error>();

useEffect(
useQueryParams(history, p => {
setTab(p.get('tab'));
setSelectedLogNode(p.get('selectedLogNode'));
}),
[history]
);

useEffect(
() =>
history.push(
Expand Down
10 changes: 10 additions & 0 deletions ui/src/app/sensors/components/sensor-list/sensor-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {Context} from '../../../shared/context';
import {Footnote} from '../../../shared/footnote';
import {historyUrl} from '../../../shared/history';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {SensorCreator} from '../sensor-creator';
import {SensorSidePanel} from '../sensor-side-panel';
import {Utils as EventsUtils} from '../utils';
Expand All @@ -32,6 +33,15 @@ export const SensorList = ({match, location, history}: RouteComponentProps<any>)
const [namespace, setNamespace] = useState(match.params.namespace || '');
const [sidePanel, setSidePanel] = useState(queryParams.get('sidePanel') === 'true');
const [selectedNode, setSelectedNode] = useState<Node>(queryParams.get('selectedNode'));

useEffect(
useQueryParams(history, p => {
setSidePanel(p.get('sidePanel') === 'true');
setSelectedNode(p.get('selectedNode'));
}),
[history]
);

useEffect(
() =>
history.push(
Expand Down
10 changes: 10 additions & 0 deletions ui/src/app/shared/use-query-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {History} from 'history';

export function useQueryParams(history: History, set: (p: URLSearchParams) => void): () => void {
return () => {
history.listen(newLocation => {
const newQueryParams = new URLSearchParams(newLocation.search);
set(newQueryParams);
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {Context} from '../../../shared/context';
import {Footnote} from '../../../shared/footnote';
import {historyUrl} from '../../../shared/history';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {ID} from './id';

const introductionText = (
Expand All @@ -34,6 +35,14 @@ export const WorkflowEventBindings = ({match, location, history}: RouteComponent
// state for URL and query parameters
const [namespace, setNamespace] = useState(match.params.namespace || '');
const [selectedWorkflowEventBinding, setSelectedWorkflowEventBinding] = useState(queryParams.get('selectedWorkflowEventBinding'));

useEffect(
useQueryParams(history, p => {
setSelectedWorkflowEventBinding(p.get('selectedWorkflowEventBinding'));
}),
[history]
);

useEffect(
() =>
history.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Loading} from '../../../shared/components/loading';
import {Context} from '../../../shared/context';
import {historyUrl} from '../../../shared/history';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {WidgetGallery} from '../../../widgets/widget-gallery';
import {SubmitWorkflowPanel} from '../../../workflows/components/submit-workflow-panel';
import {WorkflowTemplateEditor} from '../workflow-template-editor';
Expand All @@ -25,6 +26,14 @@ export const WorkflowTemplateDetails = ({history, location, match}: RouteCompone
const [sidePanel, setSidePanel] = useState(queryParams.get('sidePanel'));
const [tab, setTab] = useState<string>(queryParams.get('tab'));

useEffect(
useQueryParams(history, p => {
setSidePanel(p.get('sidePanel'));
setTab(p.get('tab'));
}),
[history]
);

useEffect(
() =>
history.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {Context} from '../../../shared/context';
import {Footnote} from '../../../shared/footnote';
import {historyUrl} from '../../../shared/history';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import {WorkflowTemplateCreator} from '../workflow-template-creator';

require('./workflow-template-list.scss');
Expand All @@ -29,6 +30,14 @@ export const WorkflowTemplateList = ({match, location, history}: RouteComponentP
// state for URL and query parameters
const [namespace, setNamespace] = useState(match.params.namespace || '');
const [sidePanel, setSidePanel] = useState(queryParams.get('sidePanel') === 'true');

useEffect(
useQueryParams(history, p => {
setSidePanel(p.get('sidePanel') === 'true');
}),
[history]
);

useEffect(
() =>
history.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {Context} from '../../../shared/context';
import {historyUrl} from '../../../shared/history';
import {RetryWatch} from '../../../shared/retry-watch';
import {services} from '../../../shared/services';
import {useQueryParams} from '../../../shared/use-query-params';
import * as Operations from '../../../shared/workflow-operations-map';
import {WorkflowOperations} from '../../../shared/workflow-operations-map';
import {WidgetGallery} from '../../../widgets/widget-gallery';
Expand Down Expand Up @@ -46,6 +47,15 @@ export const WorkflowDetails = ({history, location, match}: RouteComponentProps<
const [nodeId, setNodeId] = useState(queryParams.get('nodeId'));
const [sidePanel, setSidePanel] = useState(queryParams.get('sidePanel'));

useEffect(
useQueryParams(history, p => {
setTab(p.get('tab') || 'workflow');
setNodeId(p.get('nodeId'));
setSidePanel(p.get('sidePanel'));
}),
[history]
);

useEffect(() => {
history.push(historyUrl('workflows/{namespace}/{name}', {namespace, name, tab, nodeId, sidePanel}));
}, [namespace, name, tab, nodeId, sidePanel]);
Expand Down

0 comments on commit d9d831c

Please sign in to comment.