mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-02-23 13:43:07 +01:00
move room navigate hook in global hooks
This commit is contained in:
parent
23d823b0d3
commit
a2b0e4ce2e
3 changed files with 23 additions and 11 deletions
|
@ -1,16 +1,23 @@
|
|||
import { useCallback } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { getCanonicalAliasOrRoomId } from '../../../utils/matrix';
|
||||
import { getHomeRoomPath, getSpacePath, getSpaceRoomPath } from '../../pathUtils';
|
||||
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
||||
import { getOrphanParents } from '../../../utils/room';
|
||||
import { roomToParentsAtom } from '../../../state/room/roomToParents';
|
||||
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';
|
||||
|
||||
export const useRoomNavigate = () => {
|
||||
const navigate = useNavigate();
|
||||
const mx = useMatrixClient();
|
||||
const roomToParents = useAtomValue(roomToParentsAtom);
|
||||
const mDirects = useAtomValue(mDirectAtom);
|
||||
|
||||
const navigateSpace = useCallback(
|
||||
(roomId: string) => {
|
||||
|
@ -21,19 +28,24 @@ export const useRoomNavigate = () => {
|
|||
);
|
||||
|
||||
const navigateRoom = useCallback(
|
||||
(roomId: string) => {
|
||||
(roomId: string, eventId?: string) => {
|
||||
const roomIdOrAlias = getCanonicalAliasOrRoomId(mx, roomId);
|
||||
|
||||
const orphanParents = getOrphanParents(roomToParents, roomId);
|
||||
if (orphanParents.length > 0) {
|
||||
const pSpaceIdOrAlias = getCanonicalAliasOrRoomId(mx, orphanParents[0]);
|
||||
navigate(getSpaceRoomPath(pSpaceIdOrAlias, roomIdOrAlias));
|
||||
navigate(getSpaceRoomPath(pSpaceIdOrAlias, roomIdOrAlias, eventId));
|
||||
return;
|
||||
}
|
||||
|
||||
navigate(getHomeRoomPath(roomIdOrAlias));
|
||||
if (mDirects.has(roomId)) {
|
||||
navigate(getDirectRoomPath(roomIdOrAlias, eventId));
|
||||
return;
|
||||
}
|
||||
|
||||
navigate(getHomeRoomPath(roomIdOrAlias, eventId));
|
||||
},
|
||||
[mx, navigate, roomToParents]
|
||||
[mx, navigate, roomToParents, mDirects]
|
||||
);
|
||||
|
||||
return {
|
|
@ -14,7 +14,7 @@ import {
|
|||
} from '../../../components/page';
|
||||
import { RoomTopicViewer } from '../../../components/room-topic-viewer';
|
||||
import * as css from './style.css';
|
||||
import { useRoomNavigate } from './hooks';
|
||||
import { useRoomNavigate } from '../../../hooks/useRoomNavigate';
|
||||
|
||||
export function FeaturedRooms() {
|
||||
const { featuredCommunities } = useClientConfig();
|
||||
|
|
|
@ -38,7 +38,7 @@ import { ExploreServerPathSearchParams } from '../../paths';
|
|||
import { getExploreServerPath, withSearchParam } from '../../pathUtils';
|
||||
import * as css from './style.css';
|
||||
import { allRoomsAtom } from '../../../state/room-list/roomList';
|
||||
import { useRoomNavigate } from './hooks';
|
||||
import { useRoomNavigate } from '../../../hooks/useRoomNavigate';
|
||||
import { getMxIdServer } from '../../../utils/matrix';
|
||||
|
||||
const getServerSearchParams = (searchParams: URLSearchParams): ExploreServerPathSearchParams => ({
|
||||
|
|
Loading…
Reference in a new issue