2022-06-06 14:17:51 +00:00
|
|
|
import { ChatsStackParamList } from '../../../stacks/types';
|
|
|
|
import Navigation from '../../navigation/appNavigation';
|
|
|
|
import { IOmnichannelRoom, SubscriptionType, IVisitor, TSubscriptionModel, ISubscription } from '../../../definitions';
|
|
|
|
import { getRoomTitle, getUidDirectMessage } from './helpers';
|
|
|
|
import { Services } from '../../services';
|
2022-02-25 16:43:42 +00:00
|
|
|
|
2022-03-02 14:49:43 +00:00
|
|
|
interface IGoRoomItem {
|
2022-02-25 16:43:42 +00:00
|
|
|
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-03-07 21:16:20 +00:00
|
|
|
export type TGoRoomItem = IGoRoomItem | TSubscriptionModel | ISubscription | IOmnichannelRoomVisitor;
|
2022-03-02 14:49:43 +00:00
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const navigate = ({
|
|
|
|
item,
|
|
|
|
isMasterDetail,
|
|
|
|
...props
|
|
|
|
}: {
|
2022-03-02 14:49:43 +00:00
|
|
|
item: TGoRoomItem;
|
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,
|
2022-04-28 20:37:25 +00:00
|
|
|
name: getRoomTitle(item),
|
2020-05-13 19:02:57 +00:00
|
|
|
t: item.t,
|
|
|
|
prid: item.prid,
|
|
|
|
room: item,
|
|
|
|
visitor: item.visitor,
|
2022-04-28 20:37:25 +00:00
|
|
|
roomUserId: getUidDirectMessage(item),
|
2020-06-15 14:00:46 +00:00
|
|
|
...props
|
2020-05-13 19:02:57 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-07 21:16:20 +00:00
|
|
|
interface IOmnichannelRoomVisitor extends IOmnichannelRoom {
|
|
|
|
// this visitor came from ee/omnichannel/views/QueueListView
|
|
|
|
visitor: IVisitor;
|
|
|
|
}
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
export const goRoom = async ({
|
|
|
|
item,
|
|
|
|
isMasterDetail = false,
|
|
|
|
...props
|
|
|
|
}: {
|
2022-03-02 14:49:43 +00:00
|
|
|
item: TGoRoomItem;
|
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-03-02 14:49:43 +00:00
|
|
|
if (!('id' in item) && 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-04-28 20:37:25 +00:00
|
|
|
const result = await Services.createDirectMessage(username as string);
|
2022-02-25 16:43:42 +00:00
|
|
|
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
|
|
|
};
|