mirror of
https://github.com/He4eT/elseifplayer.git
synced 2026-05-04 17:07:22 +00:00
routing: ignore repeats
This commit is contained in:
parent
54512df52c
commit
e0a90005e6
2 changed files with 30 additions and 24 deletions
|
|
@ -20,7 +20,7 @@ import '~/src/style/base.css'
|
||||||
|
|
||||||
function App () {
|
function App () {
|
||||||
const themeEngine = useThemeEngine()
|
const themeEngine = useThemeEngine()
|
||||||
const [location] = useHashLocation()
|
const [currentLocation] = useHashLocation()
|
||||||
|
|
||||||
const playerView = (themeEngine, singleWindow) =>
|
const playerView = (themeEngine, singleWindow) =>
|
||||||
function view (params) {
|
function view (params) {
|
||||||
|
|
@ -35,7 +35,7 @@ function App () {
|
||||||
<Router hook={useHashLocation}>
|
<Router hook={useHashLocation}>
|
||||||
<div className={[
|
<div className={[
|
||||||
'app',
|
'app',
|
||||||
extractView(location),
|
extractView(currentLocation),
|
||||||
themeEngine.currentTheme].join(' ')}>
|
themeEngine.currentTheme].join(' ')}>
|
||||||
|
|
||||||
<Switch>
|
<Switch>
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,39 @@
|
||||||
import {
|
import {
|
||||||
useState, useEffect, useCallback,
|
useCallback, useEffect, useState,
|
||||||
} from 'preact/hooks'
|
} from 'preact/hooks'
|
||||||
|
|
||||||
export const useHashLocation = () => {
|
const windowLocation = () =>
|
||||||
const currentLoc = () =>
|
|
||||||
window.location.hash.replace('#', '') || '/'
|
window.location.hash.replace('#', '') || '/'
|
||||||
|
|
||||||
const [loc, setLoc] = useState(currentLoc())
|
export const buildPlayLinkHref = ({url}) =>
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const handler = () => {
|
|
||||||
setLoc(currentLoc())
|
|
||||||
window.scrollTo(0, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
handler()
|
|
||||||
window.addEventListener('hashchange', handler)
|
|
||||||
return () => window.removeEventListener('hashchange', handler)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const navigate = useCallback((to) =>
|
|
||||||
(window.location.hash = to.replace('#/', '')), [])
|
|
||||||
return [loc, navigate]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const buildPlayLinkHref = ({ url }) =>
|
|
||||||
`/#/play/${encodeURIComponent(url)}`
|
`/#/play/${encodeURIComponent(url)}`
|
||||||
|
|
||||||
export const extractView = (location) => {
|
export const extractView = (location) => {
|
||||||
const currentView = location.split('/').filter(Boolean)[0]
|
const currentView = location.split('/').filter(Boolean)[0]
|
||||||
return currentView || ''
|
return currentView || ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const useHashLocation = () => {
|
||||||
|
const [currentLocation, setCurrentLocation] =
|
||||||
|
useState(windowLocation())
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onHashChange = () => {
|
||||||
|
let newLocation = windowLocation()
|
||||||
|
if (newLocation !== currentLocation) {
|
||||||
|
setCurrentLocation(newLocation)
|
||||||
|
window.scrollTo(0, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onHashChange()
|
||||||
|
window.addEventListener('hashchange', onHashChange)
|
||||||
|
return () => window.removeEventListener('hashchange', onHashChange)
|
||||||
|
}, [currentLocation, setCurrentLocation])
|
||||||
|
|
||||||
|
const navigate = useCallback((to) => {
|
||||||
|
window.location.hash = to.replace('#/', '')
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return [currentLocation, navigate]
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue