import React , { useContext } from 'react' ; import { ScreenContext , useTranslationFunction } from '../../contexts' ; import { openLeaveChatDialog , openDeleteChatDialog , openBlockFirstContactOfChatDialog , openEncryptionInfoDialog , openViewGroupDialog , openViewProfileDialog , setChatVisibility , openMuteChatDialog , unMuteChat , } from '../helpers/ChatMethods' ; import { ChatListItemType } from '../../../shared/shared-types' ; import { C } from 'deltachat-node/dist/constants' ; import { DeltaBackend } from '../../delta-remote' ; import { ContextMenuItem } from '../ContextMenu' ; import MailingListProfile from '../dialogs/MessageListProfile' ; function archiveStateMenu ( chat , tx , isTheSelectedChat ) { const archive = { label : tx ( 'menu_archive_chat' ) , action : ( ) => setChatVisibility ( chat . id , C . DC_CHAT_VISIBILITY_ARCHIVED , isTheSelectedChat ) , } ; const unArchive = { label : tx ( 'menu_unarchive_chat' ) , action : ( ) => setChatVisibility ( chat . id , C . DC_CHAT_VISIBILITY_NORMAL , isTheSelectedChat ) , } ; const pin = { label : tx ( 'pin_chat' ) , action : ( ) => setChatVisibility ( chat . id , C . DC_CHAT_VISIBILITY_PINNED , chat . archived ) , } ; const unPin = { label : tx ( 'unpin_chat' ) , action : ( ) => setChatVisibility ( chat . id , C . DC_CHAT_VISIBILITY_NORMAL ) , } ; if ( chat . pinned ) { return [ unPin , archive ] ; } else if ( chat . archived ) { return [ pin , unArchive ] ; } else { return [ pin , archive ] ; } } export function useChatListContextMenu ( ) { const screenContext = useContext ( ScreenContext ) ; const tx = useTranslationFunction ( ) ; return ( event , chatListItem , selectedChatId ) => { const onDeleteChat = ( ) => openDeleteChatDialog ( screenContext , chatListItem , selectedChatId ) ; const onEncrInfo = ( ) => openEncryptionInfoDialog ( screenContext , chatListItem ) ; const onViewGroup = async ( ) => { const fullChat = await DeltaBackend . call ( 'chatList.getFullChatById' , chatListItem . id ) ; openViewGroupDialog ( screenContext , fullChat ) ; } ; const onViewProfile = async ( ) => { const fullChat = await DeltaBackend . call ( 'chatList.getFullChatById' , chatListItem . id ) ; if ( fullChat . type !== C . DC_CHAT_TYPE_MAILINGLIST ) { openViewProfileDialog ( screenContext , fullChat . contactIds [ 0 ] ) ; } else { screenContext . openDialog ( MailingListProfile , { chat : fullChat , } ) ; } } ; const onLeaveGroup = ( ) => openLeaveChatDialog ( screenContext , chatListItem . id ) ; const onBlockContact = ( ) => openBlockFirstContactOfChatDialog ( screenContext , chatListItem ) ; const onMuteChat = ( ) => openMuteChatDialog ( screenContext , chatListItem . id ) ; const onUnmuteChat = ( ) => unMuteChat ( chatListItem . id ) ; const menu = chatListItem ? [ ... archiveStateMenu ( chatListItem , tx , selectedChatId === chatListItem . id ) , ! chatListItem . muted ? { label : tx ( 'menu_mute' ) , action : onMuteChat , } : { label : tx ( 'menu_unmute' ) , action : onUnmuteChat , } , chatListItem . isGroup && chatListItem . selfInGroup && { label : tx ( 'menu_edit_group' ) , action : onViewGroup , } , ! chatListItem . isGroup && { label : tx ( 'menu_view_profile' ) , action : onViewProfile , } , ! chatListItem . isDeviceTalk && ! chatListItem . isSelfTalk && { label : tx ( 'encryption_info_desktop' ) , action : onEncrInfo , } , chatListItem . isGroup && chatListItem . selfInGroup && { label : tx ( 'menu_leave_group' ) , action : onLeaveGroup , } , ! chatListItem . isGroup && ! ( chatListItem . isSelfTalk || chatListItem . isDeviceTalk ) && { label : tx ( 'menu_block_contact' ) , action : onBlockContact , } , { label : tx ( 'menu_delete_chat' ) , action : onDeleteChat , } , ] : [ ] ; const [ cursorX , cursorY ] = [ event . clientX , event . clientY ] ; event . preventDefault ( ) ; screenContext . openContextMenu ( { cursorX , cursorY , items : menu , } ) ; } ; }