From 4417e1f204ed709790179de4e96b8a843ea937ab Mon Sep 17 00:00:00 2001 From: Tyler Ohlsen Date: Thu, 5 Oct 2023 16:31:45 -0700 Subject: [PATCH] Persist tab state in url Signed-off-by: Tyler Ohlsen --- public/app.tsx | 5 ++- public/pages/workflows/index.ts | 2 +- public/pages/workflows/workflows.tsx | 46 +++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/public/app.tsx b/public/app.tsx index 89839bcd..393a0145 100644 --- a/public/app.tsx +++ b/public/app.tsx @@ -12,6 +12,7 @@ import { Workflows, WorkflowDetail, WorkflowDetailRouterProps, + WorkflowsRouterProps, } from './pages'; interface Props extends RouteComponentProps {} @@ -61,7 +62,9 @@ export const AiFlowDashboardsApp = (props: Props) => { /> } + render={(routeProps: RouteComponentProps) => ( + + )} /> {/* Defaulting to Overview page */} {} enum WORKFLOWS_TAB { MANAGE = 'manage', CREATE = 'create', } -export function Workflows() { - const [selectedTabId, setSelectedTabId] = useState( - 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(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([ @@ -46,6 +82,7 @@ export function Workflows() { isSelected: selectedTabId === WORKFLOWS_TAB.MANAGE, onClick: () => { setSelectedTabId(WORKFLOWS_TAB.MANAGE); + replaceActiveTab(WORKFLOWS_TAB.MANAGE, props); }, }, { @@ -54,6 +91,7 @@ export function Workflows() { isSelected: selectedTabId === WORKFLOWS_TAB.CREATE, onClick: () => { setSelectedTabId(WORKFLOWS_TAB.CREATE); + replaceActiveTab(WORKFLOWS_TAB.CREATE, props); }, }, ]}