mirror of
https://github.com/He4eT/elseifplayer.git
synced 2026-05-04 17:07:22 +00:00
42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
import {
|
|
useCallback, useEffect, useState,
|
|
} from 'preact/hooks'
|
|
|
|
const windowLocation = () =>
|
|
window.location.hash.replace('#', '') || '/'
|
|
|
|
export const buildPlayLinkHref = ({ url }) =>
|
|
`/#/play/${encodeURIComponent(url)}`
|
|
|
|
export const extractView = (location) => {
|
|
if (location === '/') return 'home'
|
|
|
|
const currentView = location.split('/').filter(Boolean)[0]
|
|
|
|
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]
|
|
}
|