mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-02-24 06:03:04 +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 { useCallback } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { useAtomValue } from 'jotai';
|
import { useAtomValue } from 'jotai';
|
||||||
import { getCanonicalAliasOrRoomId } from '../../../utils/matrix';
|
import { getCanonicalAliasOrRoomId } from '../utils/matrix';
|
||||||
import { getHomeRoomPath, getSpacePath, getSpaceRoomPath } from '../../pathUtils';
|
import {
|
||||||
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
getDirectRoomPath,
|
||||||
import { getOrphanParents } from '../../../utils/room';
|
getHomeRoomPath,
|
||||||
import { roomToParentsAtom } from '../../../state/room/roomToParents';
|
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 = () => {
|
export const useRoomNavigate = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const mx = useMatrixClient();
|
const mx = useMatrixClient();
|
||||||
const roomToParents = useAtomValue(roomToParentsAtom);
|
const roomToParents = useAtomValue(roomToParentsAtom);
|
||||||
|
const mDirects = useAtomValue(mDirectAtom);
|
||||||
|
|
||||||
const navigateSpace = useCallback(
|
const navigateSpace = useCallback(
|
||||||
(roomId: string) => {
|
(roomId: string) => {
|
||||||
|
@ -21,19 +28,24 @@ export const useRoomNavigate = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
const navigateRoom = useCallback(
|
const navigateRoom = useCallback(
|
||||||
(roomId: string) => {
|
(roomId: string, eventId?: string) => {
|
||||||
const roomIdOrAlias = getCanonicalAliasOrRoomId(mx, roomId);
|
const roomIdOrAlias = getCanonicalAliasOrRoomId(mx, roomId);
|
||||||
|
|
||||||
const orphanParents = getOrphanParents(roomToParents, roomId);
|
const orphanParents = getOrphanParents(roomToParents, roomId);
|
||||||
if (orphanParents.length > 0) {
|
if (orphanParents.length > 0) {
|
||||||
const pSpaceIdOrAlias = getCanonicalAliasOrRoomId(mx, orphanParents[0]);
|
const pSpaceIdOrAlias = getCanonicalAliasOrRoomId(mx, orphanParents[0]);
|
||||||
navigate(getSpaceRoomPath(pSpaceIdOrAlias, roomIdOrAlias));
|
navigate(getSpaceRoomPath(pSpaceIdOrAlias, roomIdOrAlias, eventId));
|
||||||
return;
|
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 {
|
return {
|
|
@ -14,7 +14,7 @@ import {
|
||||||
} from '../../../components/page';
|
} from '../../../components/page';
|
||||||
import { RoomTopicViewer } from '../../../components/room-topic-viewer';
|
import { RoomTopicViewer } from '../../../components/room-topic-viewer';
|
||||||
import * as css from './style.css';
|
import * as css from './style.css';
|
||||||
import { useRoomNavigate } from './hooks';
|
import { useRoomNavigate } from '../../../hooks/useRoomNavigate';
|
||||||
|
|
||||||
export function FeaturedRooms() {
|
export function FeaturedRooms() {
|
||||||
const { featuredCommunities } = useClientConfig();
|
const { featuredCommunities } = useClientConfig();
|
||||||
|
|
|
@ -38,7 +38,7 @@ import { ExploreServerPathSearchParams } from '../../paths';
|
||||||
import { getExploreServerPath, withSearchParam } from '../../pathUtils';
|
import { getExploreServerPath, withSearchParam } from '../../pathUtils';
|
||||||
import * as css from './style.css';
|
import * as css from './style.css';
|
||||||
import { allRoomsAtom } from '../../../state/room-list/roomList';
|
import { allRoomsAtom } from '../../../state/room-list/roomList';
|
||||||
import { useRoomNavigate } from './hooks';
|
import { useRoomNavigate } from '../../../hooks/useRoomNavigate';
|
||||||
import { getMxIdServer } from '../../../utils/matrix';
|
import { getMxIdServer } from '../../../utils/matrix';
|
||||||
|
|
||||||
const getServerSearchParams = (searchParams: URLSearchParams): ExploreServerPathSearchParams => ({
|
const getServerSearchParams = (searchParams: URLSearchParams): ExploreServerPathSearchParams => ({
|
||||||
|
|
Loading…
Reference in a new issue