1
0
Fork 0
forked from Mirrors/gomuks

web/mainscreen: handle url fragment change

This commit is contained in:
Tulir Asokan 2025-01-10 01:58:55 +02:00
parent 40e7d63453
commit 5ab60cb816

View file

@ -234,8 +234,11 @@ class ContextFields implements MainScreenContextFields {
const SYNC_ERROR_HIDE_DELAY = 30 * 1000 const SYNC_ERROR_HIDE_DELAY = 30 * 1000
const handleURLHash = (client: Client, context: MainScreenContextFields) => { const handleURLHash = (client: Client, context: MainScreenContextFields, hashOnly = false) => {
if (!location.hash.startsWith("#/uri/")) { if (!location.hash.startsWith("#/uri/")) {
if (hashOnly) {
return null
}
if (location.search) { if (location.search) {
const currentETag = ( const currentETag = (
document.querySelector("meta[name=gomuks-frontend-etag]") as HTMLMetaElement document.querySelector("meta[name=gomuks-frontend-etag]") as HTMLMetaElement
@ -261,7 +264,7 @@ const handleURLHash = (client: Client, context: MainScreenContextFields) => {
const uri = parseMatrixURI(decodedURI) const uri = parseMatrixURI(decodedURI)
if (!uri) { if (!uri) {
console.error("Invalid matrix URI", decodedURI) console.error("Invalid matrix URI", decodedURI)
return history.state return hashOnly ? null : history.state
} }
console.log("Handling URI", uri) console.log("Handling URI", uri)
const newURL = new URL(location.href) const newURL = new URL(location.href)
@ -295,8 +298,9 @@ const handleURLHash = (client: Client, context: MainScreenContextFields) => {
return null return null
} else { } else {
console.error("Invalid matrix URI", uri) console.error("Invalid matrix URI", uri)
history.replaceState(history.state, "", newURL.toString())
} }
return history.state return hashOnly ? null : history.state
} }
type ActiveRoomType = [RoomStateStore | RoomPreviewProps | null, RoomStateStore | RoomPreviewProps | null] type ActiveRoomType = [RoomStateStore | RoomPreviewProps | null, RoomStateStore | RoomPreviewProps | null]
@ -327,7 +331,7 @@ const MainScreen = () => {
) )
useEffect(() => { useEffect(() => {
window.mainScreenContext = context window.mainScreenContext = context
const listener = (evt: PopStateEvent) => { const listener = (evt: Pick<PopStateEvent, "state" | "hasUAVisualTransition">) => {
skipNextTransitionRef.current = evt.hasUAVisualTransition skipNextTransitionRef.current = evt.hasUAVisualTransition
const roomID = evt.state?.room_id ?? null const roomID = evt.state?.room_id ?? null
const spaceID = evt.state?.space_id ?? undefined const spaceID = evt.state?.space_id ?? undefined
@ -342,6 +346,13 @@ const MainScreen = () => {
} }
context.setRightPanel(evt.state?.right_panel ?? null, false) context.setRightPanel(evt.state?.right_panel ?? null, false)
} }
const hashListener = () => {
const state = handleURLHash(client, context, true)
if (state !== null) {
listener({ state, hasUAVisualTransition: false })
}
}
window.addEventListener("hashchange", hashListener)
window.addEventListener("popstate", listener) window.addEventListener("popstate", listener)
const initHandle = () => { const initHandle = () => {
const state = handleURLHash(client, context) const state = handleURLHash(client, context)
@ -355,6 +366,7 @@ const MainScreen = () => {
} }
return () => { return () => {
window.removeEventListener("popstate", listener) window.removeEventListener("popstate", listener)
window.removeEventListener("hashchange", hashListener)
cancel() cancel()
} }
}, [context, client]) }, [context, client])