mirror of
https://github.com/He4eT/elseifplayer.git
synced 2026-05-05 01:17:22 +00:00
Rearrange components
This commit is contained in:
parent
39e8b8a526
commit
daa26965cd
16 changed files with 14 additions and 11 deletions
33
src/routing.js
Normal file
33
src/routing.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import {
|
||||
useState, useEffect, useCallback,
|
||||
} from 'preact/hooks'
|
||||
|
||||
export const useHashLocation = () => {
|
||||
const currentLoc = () =>
|
||||
window.location.hash.replace('#', '') || '/'
|
||||
|
||||
const [loc, setLoc] = useState(currentLoc())
|
||||
|
||||
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)}`
|
||||
|
||||
export const extractView = (location) => {
|
||||
const currentView = location.split('/').filter(Boolean)[0]
|
||||
return currentView || ''
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue