2022-06-20 10:59:51 -07:00
import React from 'react' ;
import { defineMessages , FormattedMessage , useIntl } from 'react-intl' ;
import { cancelReplyCompose } from 'soapbox/actions/compose' ;
import { openModal , closeModal } from 'soapbox/actions/modals' ;
2022-08-30 13:51:13 -07:00
import { checkComposeContent } from 'soapbox/components/modal_root' ;
2022-06-20 10:59:51 -07:00
import { Modal } from 'soapbox/components/ui' ;
2022-09-14 11:01:00 -07:00
import { useAppDispatch , useCompose } from 'soapbox/hooks' ;
2022-06-20 10:59:51 -07:00
2022-09-10 14:52:06 -07:00
import ComposeForm from '../../compose/components/compose-form' ;
2022-06-20 10:59:51 -07:00
const messages = defineMessages ( {
close : { id : 'lightbox.close' , defaultMessage : 'Close' } ,
confirm : { id : 'confirmations.delete.confirm' , defaultMessage : 'Delete' } ,
2022-08-03 15:09:47 -07:00
cancelEditing : { id : 'confirmations.cancel_editing.confirm' , defaultMessage : 'Cancel editing' } ,
2022-06-20 10:59:51 -07:00
} ) ;
interface IComposeModal {
onClose : ( type ? : string ) = > void ,
}
const ComposeModal : React.FC < IComposeModal > = ( { onClose } ) = > {
const intl = useIntl ( ) ;
const dispatch = useAppDispatch ( ) ;
2022-09-14 11:01:00 -07:00
const compose = useCompose ( 'compose-modal' ) ;
2022-09-10 14:52:06 -07:00
const { id : statusId , privacy , in_reply_to : inReplyTo , quote } = compose ! ;
2022-06-20 10:59:51 -07:00
const onClickClose = ( ) = > {
2022-10-01 06:37:54 -07:00
if ( checkComposeContent ( compose ) ) {
2022-06-20 10:59:51 -07:00
dispatch ( openModal ( 'CONFIRM' , {
2022-07-09 09:20:02 -07:00
icon : require ( '@tabler/icons/trash.svg' ) ,
2022-08-03 14:55:14 -07:00
heading : statusId
2022-08-03 15:09:47 -07:00
? < FormattedMessage id = 'confirmations.cancel_editing.heading' defaultMessage = 'Cancel post editing' / >
2022-08-03 14:55:14 -07:00
: < FormattedMessage id = 'confirmations.delete.heading' defaultMessage = 'Delete post' / > ,
message : statusId
2022-08-03 15:09:47 -07:00
? < FormattedMessage id = 'confirmations.cancel_editing.message' defaultMessage = 'Are you sure you want to cancel editing this post? All changes will be lost.' / >
2022-08-03 14:55:14 -07:00
: < FormattedMessage id = 'confirmations.delete.message' defaultMessage = 'Are you sure you want to delete this post?' / > ,
2022-08-03 15:09:47 -07:00
confirm : intl.formatMessage ( statusId ? messages.cancelEditing : messages.confirm ) ,
2022-06-20 10:59:51 -07:00
onConfirm : ( ) = > {
dispatch ( closeModal ( 'COMPOSE' ) ) ;
dispatch ( cancelReplyCompose ( ) ) ;
} ,
} ) ) ;
} else {
onClose ( 'COMPOSE' ) ;
}
} ;
const renderTitle = ( ) = > {
if ( statusId ) {
return < FormattedMessage id = 'navigation_bar.compose_edit' defaultMessage = 'Edit post' / > ;
} else if ( privacy === 'direct' ) {
return < FormattedMessage id = 'navigation_bar.compose_direct' defaultMessage = 'Direct message' / > ;
} else if ( inReplyTo ) {
return < FormattedMessage id = 'navigation_bar.compose_reply' defaultMessage = 'Reply to post' / > ;
} else if ( quote ) {
return < FormattedMessage id = 'navigation_bar.compose_quote' defaultMessage = 'Quote post' / > ;
} else {
return < FormattedMessage id = 'navigation_bar.compose' defaultMessage = 'Compose new post' / > ;
}
} ;
return (
< Modal
title = { renderTitle ( ) }
onClose = { onClickClose }
>
2022-09-10 14:52:06 -07:00
< ComposeForm id = 'compose-modal' / >
2022-06-20 10:59:51 -07:00
< / Modal >
) ;
} ;
export default ComposeModal ;