@livelike/react-native
Version:
LiveLike React Native package
132 lines (129 loc) • 4.13 kB
text/typescript
import { useMemo, useState } from 'react';
import {
blockProfile,
deleteMessage,
hasDebugLogger,
reportMessage,
unblockProfile,
} from '@livelike/javascript';
import { useAnalytics } from './useAnalytics';
import { useBannerActions } from './useBannerActions';
import { useChatMessageActions } from './useChatMessageActions';
import { BannerType, IChatMessage } from '../types';
export type UseChatMessageMenuItemActionsArg = {
messageDetails: IChatMessage;
};
export const useChatMessageMenuItemActions = ({
messageDetails,
}: UseChatMessageMenuItemActionsArg) => {
const { addBannerItem } = useBannerActions();
const [blockId, setBlockedId] = useState<null | string>();
const { deleteChatMessage } = useChatMessageActions({
roomId: messageDetails.chat_room_id,
});
const { trackEvent } = useAnalytics();
return useMemo(() => {
const addInfoBanner = (bannerMessage) =>
addBannerItem({ bannerType: BannerType.INFO, bannerMessage });
const addErrorBanner = (bannerMessage) =>
addBannerItem({ bannerType: BannerType.ERROR, bannerMessage });
const deleteMessageApiFn = () =>
deleteMessage({
roomId: messageDetails.chat_room_id,
messageId: messageDetails.id,
})
.then((res) => {
addInfoBanner('Message deleted');
deleteChatMessage({
roomId: messageDetails.chat_room_id,
chatMessage: messageDetails,
});
return res;
})
.catch((error) => {
addErrorBanner('Failed to delete message');
hasDebugLogger() && console.error(error);
return error;
});
const reportMessageApiFn = () =>
reportMessage({
messageId: messageDetails.id,
nickname: messageDetails.sender_nickname,
profileId: messageDetails.sender_id,
roomId: messageDetails.chat_room_id,
})
.then((res) => {
addInfoBanner('Message reported');
trackEvent('Chat Message Reported', {
messageDetails,
reportInfo: res,
});
return res;
})
.catch((error) => {
let errorDetail = 'Failed to report message';
if (error.status === 409) {
errorDetail = 'This message is already reported by you';
}
addErrorBanner(errorDetail);
hasDebugLogger() && console.error(error);
return error;
});
const blockUserApiFn = () =>
blockProfile({
profileId: messageDetails.sender_id,
})
.then((res) => {
setBlockedId(res.id);
addInfoBanner(`${messageDetails.sender_nickname} was blocked`);
trackEvent('Chat User Blocked', {
messageDetails,
blockInfo: res,
});
return res;
})
.catch((error) => {
addErrorBanner(`Failed to block ${messageDetails.sender_nickname}`);
hasDebugLogger() && console.error(error);
return error;
});
const unblockUserApiFn = () =>
unblockProfile({
blockId,
})
.then((res) => {
setBlockedId(null);
addInfoBanner(`${messageDetails.sender_nickname} was unblocked`);
trackEvent('Chat User Unblocked', {
messageDetails,
unblockInfo: res,
});
return res;
})
.catch((error) => {
addErrorBanner(`Unable to unblock ${messageDetails.sender_nickname}`);
hasDebugLogger() && console.error(error);
return error;
});
const blockUnblockApiFn = () => {
if (blockId)
return unblockUserApiFn().then((res) => {
setBlockedId(null);
return res;
});
else
return blockUserApiFn().then((res) => {
setBlockedId(res.id);
return res;
});
};
return {
blockId,
deleteMessageApiFn,
reportMessageApiFn,
blockUserApiFn,
unblockUserApiFn,
blockUnblockApiFn,
};
}, [blockId, messageDetails, addBannerItem, deleteChatMessage, trackEvent]);
};