forked from cinnyapp/cinny
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
taken from cinnyapp#411. Removed space selector, as it prevents code from being executed after it
- Loading branch information
1 parent
cdf8723
commit fbb4e2b
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { openJoinAlias, selectRoom } from '../client/action/navigation'; | ||
|
||
import cons from '../client/state/cons'; | ||
import navigation from '../client/state/navigation'; | ||
|
||
const listeners = []; | ||
|
||
/** | ||
* Old `window.location.hash` value | ||
* @type {string} | ||
*/ | ||
let oldHash; | ||
|
||
export function handleUriFragmentChange() { | ||
// If there is no fragment, there is no need to proceed here | ||
if (!window.location.hash.startsWith('/#') && !window.location.hash.startsWith('#')) return; | ||
|
||
// Avoid falling into loops of self-triggering | ||
// by detecting hashchange events which are not actually a change | ||
if (window.location.hash === oldHash) { | ||
return; | ||
} | ||
oldHash = window.location.hash; | ||
|
||
// Room must be selected AFTER client finished loading | ||
const pieces = window.location.hash.split('/'); | ||
// a[0] always is # | ||
// if no trailing '/' would be used for hash we would have to remove it | ||
// relevant array items start at index 1 | ||
|
||
// /join/<room> | ||
if (pieces[1] === 'join') { | ||
openJoinAlias(pieces[2]); | ||
return; | ||
} | ||
|
||
// /<room|spaceid>/<roomid?>/<eventid?> | ||
if (pieces.length >= 3) { | ||
if (pieces[2][0] !== '!') selectRoom(null); | ||
else if (pieces[3] && pieces[3][0] === '$') selectRoom(pieces[2], pieces[3]); | ||
else selectRoom(pieces[2]); | ||
} | ||
} | ||
|
||
listeners.push( | ||
window.addEventListener('hashchange', handleUriFragmentChange), | ||
|
||
navigation.on(cons.events.navigation.ROOM_SELECTED, (selectedRoom, _previousRoom, eventId) => { | ||
const pieces = window.location.hash.split('/'); | ||
|
||
if (_previousRoom === selectedRoom) { | ||
return; | ||
} | ||
|
||
if (!eventId) pieces.length = 3; | ||
else pieces[3] = eventId; | ||
if (!selectedRoom) pieces.length = 2; | ||
else pieces[2] = selectedRoom; | ||
if (pieces[1] === 'join') pieces[1] = cons.tabs.HOME; | ||
window.location.hash = pieces.join('/'); | ||
}), | ||
|
||
navigation.on(cons.events.navigation.SPACE_SELECTED, (spaceSelected) => { | ||
const pieces = window.location.hash.split('/'); | ||
pieces[1] = spaceSelected ?? 'room'; | ||
window.location.hash = pieces.join('/'); | ||
}), | ||
|
||
navigation.on(cons.actions.navigation.OPEN_NAVIGATION, () => { | ||
const pieces = window.location.hash.split('/'); | ||
pieces.length = 2; | ||
window.location.hash = pieces.join('/'); | ||
}) | ||
); | ||
|
||
export function destructUrlHandling() { | ||
listeners.forEach((l) => navigation.removeListener(l)); | ||
} |