From 7f892ca499aa233f10b04f7cb156f434d4e6d873 Mon Sep 17 00:00:00 2001 From: C0ffeeCode Date: Thu, 12 May 2022 10:46:16 +0200 Subject: [PATCH] Improve code quality --- src/util/uriFragments.js | 54 ++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/src/util/uriFragments.js b/src/util/uriFragments.js index 7c8d31a2..c8f61eb7 100644 --- a/src/util/uriFragments.js +++ b/src/util/uriFragments.js @@ -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; } // /... - 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]); } // /// - 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('/'); }), );