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';
|
|
|
|
|
import { getOrphanParents } from '../utils/room';
|
|
|
|
|
import { roomToParentsAtom } from '../state/room/roomToParents';
|
|
|
|
|
import { mDirectAtom } from '../state/mDirectList';
|
2024-07-30 17:48:59 +05:30
|
|
|
import { useSelectedSpace } from './router/useSelectedSpace';
|
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();
|
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);
|
|
|
|
|
|
|
|
|
|
const orphanParents = getOrphanParents(roomToParents, roomId);
|
|
|
|
|
if (orphanParents.length > 0) {
|
2024-07-30 17:48:59 +05:30
|
|
|
const pSpaceIdOrAlias = getCanonicalAliasOrRoomId(
|
|
|
|
|
mx,
|
|
|
|
|
spaceSelectedId && orphanParents.includes(spaceSelectedId)
|
|
|
|
|
? spaceSelectedId
|
|
|
|
|
: orphanParents[0]
|
|
|
|
|
);
|
|
|
|
|
navigate(getSpaceRoomPath(pSpaceIdOrAlias, 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
|
|
|
},
|
2024-07-30 17:48:59 +05:30
|
|
|
[mx, navigate, spaceSelectedId, roomToParents, mDirects]
|
2024-05-31 19:49:46 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
navigateSpace,
|
|
|
|
|
navigateRoom,
|
|
|
|
|
};
|
|
|
|
|
};
|