Improve code quality

This commit is contained in:
C0ffeeCode 2022-05-12 10:46:16 +02:00
parent 473d97312e
commit 7f892ca499
No known key found for this signature in database
GPG key ID: 211B3EF9F31E56FE

View file

@ -5,40 +5,34 @@ import {
import navigation from '../client/state/navigation';
import cons from '../client/state/cons';
async function joinRoom(roomAlias) {
openJoinAlias(roomAlias);
}
const listeners = [];
export function handleUriFragmentChange() {
if (window.location.hash === ''
|| window.location.hash === '#'
|| window.location.hash === '#/') return;
if (!window.location.hash.startsWith('/#')) return;
// Room must be selected AFTER client finished loading
const a = window.location.hash.split('/');
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
if (a[1] === 'join') {
joinRoom(a[2]);
if (pieces[1] === 'join') {
openJoinAlias(pieces[2]);
return;
}
// /<room|home|spaceid>...
if (a.length >= 2) {
if (a[1] === 'room' || a[1][0] !== '!') a[1] = cons.tabs.HOME;
if (pieces.length >= 2) {
if (pieces[1] === 'room' || pieces[1][0] !== '!') pieces[1] = cons.tabs.HOME;
selectSpace(a[1]);
selectSpace(pieces[1]);
}
// /<room|spaceid>/<roomid?>/<eventid?>
if (a.length >= 3) {
if (a[2][0] !== '!') selectRoom(null);
else if (a[3] && a[3][0] === '$') selectRoom(a[2], a[3]);
else selectRoom(a[2]);
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]);
}
}
@ -46,25 +40,25 @@ listeners.push(
window.addEventListener('hashchange', handleUriFragmentChange),
navigation.on(cons.events.navigation.ROOM_SELECTED, (selectedRoom, _previousRoom, eventId) => {
const a = window.location.hash.split('/');
if (!eventId) a.length = 3;
else a[3] = eventId;
if (!selectRoom) a.length = 2;
else a[2] = selectedRoom;
if (a[1] === 'join') a[1] = cons.tabs.HOME;
window.location.hash = a.join('/');
const pieces = window.location.hash.split('/');
if (!eventId) pieces.length = 3;
else pieces[3] = eventId;
if (!selectRoom) 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 strp = window.location.hash.split('/');
strp[1] = spaceSelected ?? 'room';
window.location.hash = strp.join('/');
const pieces = window.location.hash.split('/');
pieces[1] = spaceSelected ?? 'room';
window.location.hash = pieces.join('/');
}),
navigation.on(cons.actions.navigation.OPEN_NAVIGATION, () => {
const h = window.location.hash.split('/');
h.length = 2;
window.location.hash = h.join('/');
const pieces = window.location.hash.split('/');
pieces.length = 2;
window.location.hash = pieces.join('/');
}),
);