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-02-25 16:43:42 +00:00
|
|
|
import { IVisitor, SubscriptionType } from '../definitions/ISubscription';
|
|
|
|
|
|
|
|
export interface IGoRoomItem {
|
|
|
|
search?: boolean; // comes from spotlight
|
|
|
|
username?: string;
|
|
|
|
t?: SubscriptionType;
|
2022-02-25 18:59:39 +00:00
|
|
|
rid?: string;
|
2022-02-25 16:43:42 +00:00
|
|
|
name?: string;
|
|
|
|
prid?: string;
|
|
|
|
visitor?: IVisitor;
|
|
|
|
}
|
2020-05-13 19:02:57 +00:00
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const navigate = ({
|
|
|
|
item,
|
|
|
|
isMasterDetail,
|
|
|
|
...props
|
|
|
|
}: {
|
2022-02-25 16:43:42 +00:00
|
|
|
item: IGoRoomItem;
|
2022-01-12 12:54:04 +00:00
|
|
|
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
|
|
|
export const goRoom = async ({
|
|
|
|
item,
|
|
|
|
isMasterDetail = false,
|
|
|
|
...props
|
|
|
|
}: {
|
2022-02-25 16:43:42 +00:00
|
|
|
item: IGoRoomItem;
|
2022-01-12 12:54:04 +00:00
|
|
|
isMasterDetail: boolean;
|
|
|
|
navigationMethod?: any;
|
|
|
|
jumpToMessageId?: string;
|
2022-02-04 22:15:01 +00:00
|
|
|
usedCannedResponse?: string;
|
2022-01-12 12:54:04 +00:00
|
|
|
}): 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;
|
2022-02-25 16:43:42 +00:00
|
|
|
const result = await RocketChat.createDirectMessage(username as string);
|
|
|
|
if (result.success && result?.room?._id) {
|
2020-05-13 19:02:57 +00:00
|
|
|
return navigate({
|
2020-06-15 14:00:46 +00:00
|
|
|
item: {
|
|
|
|
rid: result.room._id,
|
2022-02-04 22:15:01 +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
|
|
|
};
|