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