Skip to content

Commit

Permalink
Merge pull request #63 from AudiusProject/jowlee-hash-router
Browse files Browse the repository at this point in the history
Hash Routing
  • Loading branch information
jowlee authored Feb 19, 2021
2 parents 893ec42 + 4eb16d4 commit 0fe90c5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'connected-react-router'
import { Switch, Route } from 'react-router'
import { createBrowserHistory } from 'history'
import { createHashHistory } from 'history'

import Header from 'components/Header'
import Home from 'containers/Home'
Expand All @@ -26,7 +26,7 @@ import Proposal from 'containers/Proposal'
import { createStyles } from 'utils/mobile'

const styles = createStyles({ desktopStyles, mobileStyles })
const history = createBrowserHistory()
const history = createHashHistory()
const store = createStore(history)

const Root = () => (
Expand Down
3 changes: 3 additions & 0 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import desktopStyles from './Header.module.css'
import mobileStyles from './HeaderMobile.module.css'
import { createStyles } from 'utils/mobile'
import MobileNav from 'components/MobileNav'
import useRerouteLegacy from 'hooks/useRerouteLegacy'

const styles = createStyles({ desktopStyles, mobileStyles })

Expand All @@ -23,6 +24,8 @@ const Header: React.FC<HeaderProps> = ({ className }) => {
const isMobile = useIsMobile()
const [showMobileNav, setShowMobileNav] = useState(false)

useRerouteLegacy()

return (
<div className={clsx(styles.container, { [className!]: !!className })}>
{isMobile && (
Expand Down
5 changes: 4 additions & 1 deletion src/components/Nav/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AppState } from 'store/types'
import styles from './Nav.module.css'
import * as routes from 'utils/routes'
import { Button, ButtonType } from '@audius/stems'
import { useLocation } from 'react-router-dom'

const navRoutes = [
{ matchParams: { path: routes.HOME, exact: true }, text: 'OVERVIEW' },
Expand All @@ -32,7 +33,9 @@ const NavButton = (props: NavButtonProps) => {
pushRoute,
matchParams: { path }
} = props
const isActiveRoute = !!matchPath(window.location.pathname, props.matchParams)
const location = useLocation()

const isActiveRoute = !!matchPath(location.pathname, props.matchParams)
const onButtonClick = useCallback(() => pushRoute(path), [path, pushRoute])

return (
Expand Down
22 changes: 22 additions & 0 deletions src/hooks/useRerouteLegacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect } from 'react'
import { useLocation } from 'react-router-dom'

/**
* NOTE: updated the route from the legacy url structure to hash routing.
* ie. /services/service-providers => /#/services/service-providers
*
* NOTE: When switching to being served from ipfs, this needs to be updated for the
* client to be loaded via ipns
*/

// -------------------------------- Hooks --------------------------------
export const useRerouteLegacy = () => {
const location = useLocation()
useEffect(() => {
if (window.location.pathname !== '/') {
window.history.replaceState({}, '', `/#${window.location.pathname}`)
}
}, [location])
}

export default useRerouteLegacy

0 comments on commit 0fe90c5

Please sign in to comment.