2022-01-12 12:54:04 +00:00
|
|
|
import { ChatsStackParamList } from '../stacks/types';
|
2020-05-13 19:02:57 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
2022-01-12 12:54:04 +00:00
|
|
|
import { ISubscription, SubscriptionType } from '../definitions/ISubscription';
|
2020-05-13 19:02:57 +00:00
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const navigate = ({
|
|
|
|
item,
|
|
|
|
isMasterDetail,
|
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
item: IItem;
|
|
|
|
isMasterDetail: boolean;
|
|
|
|
navigationMethod?: () => ChatsStackParamList;
|
|
|
|
}) => {
|
2021-04-07 18:31:25 +00:00
|
|
|
let navigationMethod = props.navigationMethod ?? Navigation.navigate;
|
2020-06-15 14:00:46 +00:00
|
|
|
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigationMethod = Navigation.replace;
|
|
|
|
}
|
|
|
|
|
|
|
|
navigationMethod('RoomView', {
|
2020-05-13 19:02:57 +00:00
|
|
|
rid: item.rid,
|
|
|
|
name: RocketChat.getRoomTitle(item),
|
|
|
|
t: item.t,
|
|
|
|
prid: item.prid,
|
|
|
|
room: item,
|
|
|
|
visitor: item.visitor,
|
2020-06-15 14:00:46 +00:00
|
|
|
roomUserId: RocketChat.getUidDirectMessage(item),
|
|
|
|
...props
|
2020-05-13 19:02:57 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
interface IItem extends Partial<ISubscription> {
|
2022-02-01 13:39:09 +00:00
|
|
|
search?: boolean; // comes from spotlight
|
|
|
|
username?: string;
|
2022-01-12 12:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const goRoom = async ({
|
|
|
|
item,
|
|
|
|
isMasterDetail = false,
|
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
item: IItem;
|
|
|
|
isMasterDetail: boolean;
|
|
|
|
navigationMethod?: any;
|
|
|
|
jumpToMessageId?: string;
|
|
|
|
}): Promise<void> => {
|
2022-02-01 13:39:09 +00:00
|
|
|
if (item.t === SubscriptionType.DIRECT && item?.search) {
|
2020-05-13 19:02:57 +00:00
|
|
|
// if user is using the search we need first to join/create room
|
|
|
|
try {
|
|
|
|
const { username } = item;
|
|
|
|
const result = await RocketChat.createDirectMessage(username);
|
|
|
|
if (result.success) {
|
|
|
|
return navigate({
|
2020-06-15 14:00:46 +00:00
|
|
|
item: {
|
|
|
|
rid: result.room._id,
|
2022-02-01 13:39:09 +00:00
|
|
|
name: username,
|
2022-01-12 12:54:04 +00:00
|
|
|
t: SubscriptionType.DIRECT
|
2020-06-15 14:00:46 +00:00
|
|
|
},
|
|
|
|
isMasterDetail,
|
|
|
|
...props
|
2020-05-13 19:02:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
|
|
|
|
return navigate({ item, isMasterDetail, ...props });
|
2020-05-13 19:02:57 +00:00
|
|
|
};
|