2024-05-31 19:49:46 +05:30
|
|
|
import { useCallback } from 'react';
|
2024-07-30 17:48:59 +05:30
|
|
|
import { NavigateOptions, useNavigate } from 'react-router-dom';
|
2024-05-31 19:49:46 +05:30
|
|
|
import { useAtomValue } from 'jotai';
|
|
|
|
|
import { getCanonicalAliasOrRoomId } from '../utils/matrix';
|
|
|
|
|
import {
|
|
|
|
|
getDirectRoomPath,
|
|
|
|
|
getHomeRoomPath,
|
|
|
|
|
getSpacePath,
|
|
|
|
|
getSpaceRoomPath,
|
|
|
|
|
} from '../pages/pathUtils';
|
|
|
|
|
import { useMatrixClient } from './useMatrixClient';
|
2025-07-11 16:33:55 +05:30
|
|
|
import { getOrphanParents, guessPerfectParent } from '../utils/room';
|
2024-05-31 19:49:46 +05:30
|
|
|
import { roomToParentsAtom } from '../state/room/roomToParents';
|
|
|
|
|
import { mDirectAtom } from '../state/mDirectList';
|
2024-07-30 17:48:59 +05:30
|
|
|
import { useSelectedSpace } from './router/useSelectedSpace';
|
2025-06-10 10:14:17 +05:30
|
|
|
import { settingsAtom } from '../state/settings';
|
|
|
|
|
import { useSetting } from '../state/hooks/settings';
|
2024-05-31 19:49:46 +05:30
|
|
|
|
|
|
|
|
export const useRoomNavigate = () => {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const mx = useMatrixClient();
|
|
|
|
|
const roomToParents = useAtomValue(roomToParentsAtom);
|
|
|
|
|
const mDirects = useAtomValue(mDirectAtom);
|
2024-07-30 17:48:59 +05:30
|
|
|
const spaceSelectedId = useSelectedSpace();
|
2025-06-10 10:14:17 +05:30
|
|
|
const [developerTools] = useSetting(settingsAtom, 'developerTools');
|
2024-05-31 19:49:46 +05:30
|
|
|
|
|
|
|
|
const navigateSpace = useCallback(
|
|
|
|
|
(roomId: string) => {
|
|
|
|
|
const roomIdOrAlias = getCanonicalAliasOrRoomId(mx, roomId);
|
|
|
|
|
navigate(getSpacePath(roomIdOrAlias));
|
|
|
|
|
},
|
|
|
|
|
[mx, navigate]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const navigateRoom = useCallback(
|
2024-07-30 17:48:59 +05:30
|
|
|
(roomId: string, eventId?: string, opts?: NavigateOptions) => {
|
2024-05-31 19:49:46 +05:30
|
|
|
const roomIdOrAlias = getCanonicalAliasOrRoomId(mx, roomId);
|
2025-06-10 10:14:17 +05:30
|
|
|
const openSpaceTimeline = developerTools && spaceSelectedId === roomId;
|
2024-05-31 19:49:46 +05:30
|
|
|
|
2025-06-10 10:14:17 +05:30
|
|
|
const orphanParents = openSpaceTimeline ? [roomId] : getOrphanParents(roomToParents, roomId);
|
2024-05-31 19:49:46 +05:30
|
|
|
if (orphanParents.length > 0) {
|
2025-07-11 16:33:55 +05:30
|
|
|
let parentSpace: string;
|
|
|
|
|
if (spaceSelectedId && orphanParents.includes(spaceSelectedId)) {
|
|
|
|
|
parentSpace = spaceSelectedId;
|
|
|
|
|
} else {
|
|
|
|
|
parentSpace = guessPerfectParent(mx, roomId, orphanParents) ?? orphanParents[0];
|
2025-06-10 10:14:17 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-11 16:33:55 +05:30
|
|
|
const pSpaceIdOrAlias = getCanonicalAliasOrRoomId(mx, parentSpace);
|
|
|
|
|
|
|
|
|
|
navigate(
|
|
|
|
|
getSpaceRoomPath(pSpaceIdOrAlias, openSpaceTimeline ? roomId : roomIdOrAlias, eventId),
|
|
|
|
|
opts
|
|
|
|
|
);
|
2024-05-31 19:49:46 +05:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mDirects.has(roomId)) {
|
2024-07-30 17:48:59 +05:30
|
|
|
navigate(getDirectRoomPath(roomIdOrAlias, eventId), opts);
|
2024-05-31 19:49:46 +05:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 17:48:59 +05:30
|
|
|
navigate(getHomeRoomPath(roomIdOrAlias, eventId), opts);
|
2024-05-31 19:49:46 +05:30
|
|
|
},
|
2025-06-10 10:14:17 +05:30
|
|
|
[mx, navigate, spaceSelectedId, roomToParents, mDirects, developerTools]
|
2024-05-31 19:49:46 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
navigateSpace,
|
|
|
|
|
navigateRoom,
|
|
|
|
|
};
|
|
|
|
|
};
|