Merge branch 'benharri-forks-skip-confirmation-for-empty-post' into main

zio/stable
Paul Frazee 2023-06-27 11:04:33 -05:00
commit 1024f37a54
1 changed files with 28 additions and 23 deletions

View File

@ -82,7 +82,7 @@ export const ComposePost = observer(function ComposePost({
// HACK // HACK
// there's a bug with @mattermost/react-native-paste-input where if the input // there's a bug with @mattermost/react-native-paste-input where if the input
// is focused during unmount, an exception will throw (seems that a blur method isnt implemented) // is focused during unmount, an exception will throw (seems that a blur method isn't implemented)
// manually blurring before closing gets around that // manually blurring before closing gets around that
// -prf // -prf
const hackfixOnClose = useCallback(() => { const hackfixOnClose = useCallback(() => {
@ -90,6 +90,27 @@ export const ComposePost = observer(function ComposePost({
onClose() onClose()
}, [textInput, onClose]) }, [textInput, onClose])
const onPressCancel = useCallback(() => {
if (graphemeLength > 0 || !gallery.isEmpty) {
if (store.shell.activeModals.some(modal => modal.name === 'confirm')) {
store.shell.closeModal()
}
store.shell.openModal({
name: 'confirm',
title: 'Discard draft',
onPressConfirm: hackfixOnClose,
onPressCancel: () => {
store.shell.closeModal()
},
message: "Are you sure you'd like to discard this draft?",
confirmBtnText: 'Discard',
confirmBtnStyle: {backgroundColor: colors.red4},
})
} else {
hackfixOnClose()
}
}, [store, hackfixOnClose, graphemeLength, gallery])
// initial setup // initial setup
useEffect(() => { useEffect(() => {
autocompleteView.setup() autocompleteView.setup()
@ -99,26 +120,10 @@ export const ComposePost = observer(function ComposePost({
const onEscape = useCallback( const onEscape = useCallback(
(e: KeyboardEvent) => { (e: KeyboardEvent) => {
if (e.key === 'Escape') { if (e.key === 'Escape') {
const {shell} = store onPressCancel()
if (shell.activeModals.some(modal => modal.name === 'confirm')) {
store.shell.closeModal()
}
shell.openModal({
name: 'confirm',
title: 'Discard draft',
onPressConfirm: onClose,
onPressCancel: () => {
store.shell.closeModal()
},
message: "Are you sure you'd like to discard this draft?",
confirmBtnText: 'Discard',
confirmBtnStyle: {backgroundColor: colors.red4},
})
} }
}, },
[store, onClose], [onPressCancel],
) )
useEffect(() => { useEffect(() => {
if (isDesktopWeb) { if (isDesktopWeb) {
@ -137,7 +142,7 @@ export const ComposePost = observer(function ComposePost({
const onPhotoPasted = useCallback( const onPhotoPasted = useCallback(
async (uri: string) => { async (uri: string) => {
track('Composer:PastedPhotos') track('Composer:PastedPhotos')
gallery.paste(uri) await gallery.paste(uri)
}, },
[gallery, track], [gallery, track],
) )
@ -187,7 +192,7 @@ export const ComposePost = observer(function ComposePost({
if (replyTo && replyTo.uri) track('Post:Reply') if (replyTo && replyTo.uri) track('Post:Reply')
} }
if (!replyTo) { if (!replyTo) {
store.me.mainFeed.addPostToTop(createdPost.uri) await store.me.mainFeed.addPostToTop(createdPost.uri)
} }
onPost?.() onPost?.()
hackfixOnClose() hackfixOnClose()
@ -227,8 +232,8 @@ export const ComposePost = observer(function ComposePost({
<View style={styles.topbar}> <View style={styles.topbar}>
<TouchableOpacity <TouchableOpacity
testID="composerDiscardButton" testID="composerDiscardButton"
onPress={hackfixOnClose} onPress={onPressCancel}
onAccessibilityEscape={hackfixOnClose} onAccessibilityEscape={onPressCancel}
accessibilityRole="button" accessibilityRole="button"
accessibilityLabel="Discard" accessibilityLabel="Discard"
accessibilityHint="Closes post composer and discards post draft"> accessibilityHint="Closes post composer and discards post draft">