diff --git a/apps/trading/pages/markets/[marketId].page.tsx b/apps/trading/pages/markets/[marketId].page.tsx index 38da85814..fd8d9428e 100644 --- a/apps/trading/pages/markets/[marketId].page.tsx +++ b/apps/trading/pages/markets/[marketId].page.tsx @@ -6,6 +6,7 @@ import React, { Children, isValidElement, ReactNode, + useCallback, useEffect, useState, } from 'react'; @@ -131,6 +132,25 @@ interface GridTabsProps { const GridTabs = ({ children, group }: GridTabsProps) => { const { query, asPath, replace } = useRouter(); + const [activeTab, setActiveTab] = useState(() => { + if (query[group]) { + return query[group]; + } + + // Default to first tab + return children[0].props.name; + }); + + // Using replace inside an effect causes a render loop. Seems like its not using useCallback + // eslint-disable-next-line + const safeReplace = useCallback((path: string) => replace(path), []); + + useEffect(() => { + const [url, queryString] = asPath.split('?'); + const searchParams = new URLSearchParams(queryString); + searchParams.set(group, activeTab as string); + safeReplace(`${url}?${searchParams.toString()}`); + }, [activeTab, group, asPath, safeReplace]); return (
@@ -138,7 +158,7 @@ const GridTabs = ({ children, group }: GridTabsProps) => {
{Children.map(children, (child) => { if (!isValidElement(child)) return null; - const isActive = query[group] === child.props.name; + const isActive = activeTab === child.props.name; const buttonClass = classNames( 'py-4', 'px-12', @@ -152,12 +172,7 @@ const GridTabs = ({ children, group }: GridTabsProps) => { return (