Skip to content

Commit

Permalink
Persist tab state in url
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler committed Oct 5, 2023
1 parent 2790157 commit 4417e1f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
5 changes: 4 additions & 1 deletion public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Workflows,
WorkflowDetail,
WorkflowDetailRouterProps,
WorkflowsRouterProps,
} from './pages';

interface Props extends RouteComponentProps {}
Expand Down Expand Up @@ -61,7 +62,9 @@ export const AiFlowDashboardsApp = (props: Props) => {
/>
<Route
path={APP_PATH.WORKFLOWS}
render={(routeProps: RouteComponentProps) => <Workflows />}
render={(routeProps: RouteComponentProps<WorkflowsRouterProps>) => (
<Workflows {...routeProps} />
)}
/>
{/* Defaulting to Overview page */}
<Route
Expand Down
2 changes: 1 addition & 1 deletion public/pages/workflows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

export { Workflows } from './workflows';
export * from './workflows';
46 changes: 42 additions & 4 deletions public/pages/workflows/workflows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import React, { useEffect, useState } from 'react';
import { RouteComponentProps, useLocation } from 'react-router-dom';
import {
EuiPageHeader,
EuiTitle,
Expand All @@ -12,20 +13,55 @@ import {
EuiPageContent,
EuiSpacer,
} from '@elastic/eui';
import queryString from 'query-string';
import { useSelector } from 'react-redux';
import { BREADCRUMBS } from '../../utils';
import { getCore } from '../../services';
import { WorkflowList } from './workflow_list';
import { NewWorkflow } from './new_workflow';
import { AppState } from '../../store';

export interface WorkflowsRouterProps {}

interface WorkflowsProps extends RouteComponentProps<WorkflowsRouterProps> {}

enum WORKFLOWS_TAB {
MANAGE = 'manage',
CREATE = 'create',
}

export function Workflows() {
const [selectedTabId, setSelectedTabId] = useState<WORKFLOWS_TAB>(
WORKFLOWS_TAB.MANAGE
);
const ACTIVE_TAB_PARAM = 'active_tab';

function replaceActiveTab(activeTab: string, props: WorkflowsProps) {
props.history.replace({
...history,
search: queryString.stringify({
[ACTIVE_TAB_PARAM]: activeTab,
}),
});
}

export function Workflows(props: WorkflowsProps) {
const { workflows } = useSelector((state: AppState) => state.workflows);

const tabFromUrl = queryString.parse(useLocation().search)[
ACTIVE_TAB_PARAM
] as WORKFLOWS_TAB;
const [selectedTabId, setSelectedTabId] = useState<WORKFLOWS_TAB>(tabFromUrl);

// If there is no selected tab, default to a tab depending on if user
// has existing created workflows or not.
useEffect(() => {
if (!selectedTabId) {
if (workflows?.length > 0) {
setSelectedTabId(WORKFLOWS_TAB.MANAGE);
replaceActiveTab(WORKFLOWS_TAB.MANAGE, props);
} else {
setSelectedTabId(WORKFLOWS_TAB.CREATE);
replaceActiveTab(WORKFLOWS_TAB.CREATE, props);
}
}
}, [selectedTabId, workflows]);

useEffect(() => {
getCore().chrome.setBreadcrumbs([
Expand All @@ -46,6 +82,7 @@ export function Workflows() {
isSelected: selectedTabId === WORKFLOWS_TAB.MANAGE,
onClick: () => {
setSelectedTabId(WORKFLOWS_TAB.MANAGE);
replaceActiveTab(WORKFLOWS_TAB.MANAGE, props);
},
},
{
Expand All @@ -54,6 +91,7 @@ export function Workflows() {
isSelected: selectedTabId === WORKFLOWS_TAB.CREATE,
onClick: () => {
setSelectedTabId(WORKFLOWS_TAB.CREATE);
replaceActiveTab(WORKFLOWS_TAB.CREATE, props);
},
},
]}
Expand Down

0 comments on commit 4417e1f

Please sign in to comment.