From 25d8ac6593fb21e896772cbed4f4c16291aee88a Mon Sep 17 00:00:00 2001 From: jaredvu Date: Fri, 16 Feb 2024 14:21:42 -0800 Subject: [PATCH] Add hashstring fallback --- src/App.tsx | 21 ++++++++++++++----- .../{addressUtils.ts => addressUtils.spec.ts} | 0 src/lib/__test__/urlUtils.spec.ts | 14 +++++++++++++ src/lib/urlUtils.ts | 13 ++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) rename src/lib/__test__/{addressUtils.ts => addressUtils.spec.ts} (100%) create mode 100644 src/lib/__test__/urlUtils.spec.ts create mode 100644 src/lib/urlUtils.ts diff --git a/src/App.tsx b/src/App.tsx index d1093e5..da1af89 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,5 @@ -import { lazy, Suspense } from 'react'; -import { Navigate, Route, Routes } from 'react-router-dom'; +import { lazy, Suspense, useMemo } from 'react'; +import { Navigate, Route, Routes, useLocation } from 'react-router-dom'; import styled, { AnyStyledComponent, css } from 'styled-components'; import { WagmiConfig } from 'wagmi'; import { QueryClient, QueryClientProvider } from 'react-query'; @@ -26,6 +26,7 @@ import { RestrictionProvider } from '@/hooks/useRestrictions'; import { SubaccountProvider } from '@/hooks/useSubaccount'; import { GuardedMobileRoute } from '@/components/GuardedMobileRoute'; +import { LoadingSpace } from '@/components/Loading/LoadingSpinner'; import { HeaderDesktop } from '@/layout/Header/HeaderDesktop'; import { FooterDesktop } from '@/layout/Footer/FooterDesktop'; @@ -34,12 +35,12 @@ import { NotificationsToastArea } from '@/layout/NotificationsToastArea'; import { DialogManager } from '@/layout/DialogManager'; import { GlobalCommandDialog } from '@/views/dialogs/GlobalCommandDialog'; +import { parseHash } from '@/lib/urlUtils'; import { config } from '@/lib/wagmi'; import { breakpoints } from '@/styles'; import { GlobalStyle } from '@/styles/globalStyle'; import { layoutMixins } from '@/styles/layoutMixins'; -import { LoadingSpace } from './components/Loading/LoadingSpinner'; import '@/styles/constants.css'; import '@/styles/fonts.css'; @@ -68,6 +69,14 @@ const Content = () => { const isShowingHeader = isNotTablet; const isShowingFooter = useShouldShowFooter(); const { chainTokenLabel } = useTokenConfigs(); + const location = useLocation(); + + const pathFromHash = useMemo(() => { + if (location.hash === '') { + return ''; + } + return parseHash(location.hash); + }, [location.hash]); return ( <> @@ -102,8 +111,10 @@ const Content = () => { } /> } /> - - } /> + } + /> diff --git a/src/lib/__test__/addressUtils.ts b/src/lib/__test__/addressUtils.spec.ts similarity index 100% rename from src/lib/__test__/addressUtils.ts rename to src/lib/__test__/addressUtils.spec.ts diff --git a/src/lib/__test__/urlUtils.spec.ts b/src/lib/__test__/urlUtils.spec.ts new file mode 100644 index 0000000..aaee71a --- /dev/null +++ b/src/lib/__test__/urlUtils.spec.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from 'vitest'; + +import { parseHash } from '@/lib/urlUtils'; + +describe('parseHash', () => { + it('returns the path separated from hash', () => { + const hash = '#/markets'; + expect(parseHash(hash)).toEqual('/markets'); + }); + it('returns the path and query string separated from hash', () => { + const hash = '#/markets?displayinitializingmarkets=true'; + expect(parseHash(hash)).toEqual('/markets?displayinitializingmarkets=true'); + }); +}); diff --git a/src/lib/urlUtils.ts b/src/lib/urlUtils.ts new file mode 100644 index 0000000..39015f6 --- /dev/null +++ b/src/lib/urlUtils.ts @@ -0,0 +1,13 @@ +/** + * @param hash location.hash + * @returns path and query string if hash parameter is not empty + */ +export const parseHash = (hash: string) => { + if (!hash || hash.length === 0) return ''; + + // Remove '#' and split by '?' + const [path, queryString] = hash.substring(1).split('?'); + + // Reconstruct path and query string + return `${path}${queryString ? `?${queryString}` : ''}`; +};