Skip to content

Commit

Permalink
Open url fragment
Browse files Browse the repository at this point in the history
taken from cinnyapp#411.
Removed space selector, as it prevents code from being executed after it
  • Loading branch information
aceArt-GmbH committed Jan 17, 2024
1 parent cdf8723 commit fbb4e2b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/app/templates/auth/Auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function Homeserver({ onChange }) {
}, [hs]);

useEffect(async () => {
const link = window.location.href;
const link = window.location.href.split("#")[0];
const configFileUrl = `${link}${link[link.length - 1] === '/' ? '' : '/'}config.json`;
try {
const result = await (await fetch(configFileUrl, { method: 'GET' })).json();
Expand Down
6 changes: 6 additions & 0 deletions src/app/templates/client/Client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { MatrixClientProvider } from '../../hooks/useMatrixClient';
import { ClientContent } from './ClientContent';
import { useSetting } from '../../state/hooks/settings';
import { settingsAtom } from '../../state/settings';
import { destructUrlHandling, handleUriFragmentChange } from '../../../util/uriFragments';

function SystemEmojiFeature() {
const [twitterEmoji] = useSetting(settingsAtom, 'twitterEmoji');
Expand Down Expand Up @@ -80,8 +81,13 @@ function Client() {
initHotkeys();
initRoomListListener(initMatrix.roomList);
changeLoading(false);
handleUriFragmentChange();
});
initMatrix.init();

return () => {
destructUrlHandling();
};
}, []);

if (isLoading) {
Expand Down
78 changes: 78 additions & 0 deletions src/util/uriFragments.js
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));
}

0 comments on commit fbb4e2b

Please sign in to comment.