diff --git a/src/message/messageActionSheet.js b/src/message/messageActionSheet.js index 264ab114e37..bcb0ff03968 100644 --- a/src/message/messageActionSheet.js +++ b/src/message/messageActionSheet.js @@ -321,6 +321,22 @@ export const constructNonHeaderActionButtons = ({ } }; +function makeButtonCallback( + buttonList: Button[], + args: Args, +) { + return buttonIndex => { + (async () => { + const pressedButton: Button = buttonList[buttonIndex]; + try { + await pressedButton(args); + } catch (err) { + Alert.alert(args._(pressedButton.errorMessage), err.message); + } + })(); + }; +} + export const showMessageActionSheet = ({ showActionSheetWithOptions, callbacks, @@ -345,27 +361,17 @@ export const showMessageActionSheet = ({ narrow: Narrow, |}): void => { const buttonList = constructNonHeaderActionButtons({ backgroundData, message, narrow }); - const callback = buttonIndex => { - (async () => { - const pressedButton: Button = buttonList[buttonIndex]; - try { - await pressedButton({ - ...backgroundData, - ...callbacks, - message, - narrow, - }); - } catch (err) { - Alert.alert(callbacks._(pressedButton.errorMessage), err.message); - } - })(); - }; showActionSheetWithOptions( { options: buttonList.map(button => callbacks._(button.title)), cancelButtonIndex: buttonList.length - 1, }, - callback, + makeButtonCallback(buttonList, { + ...backgroundData, + ...callbacks, + message, + narrow, + }), ); }; @@ -397,27 +403,17 @@ export const showHeaderActionSheet = ({ stream, topic, }); - const callback = buttonIndex => { - (async () => { - const pressedButton: Button = buttonList[buttonIndex]; - try { - await pressedButton({ - ...backgroundData, - ...callbacks, - stream, - topic, - }); - } catch (err) { - Alert.alert(callbacks._(pressedButton.errorMessage), err.message); - } - })(); - }; showActionSheetWithOptions( { title: `#${stream} > ${topic}`, options: buttonList.map(button => callbacks._(button.title)), cancelButtonIndex: buttonList.length - 1, }, - callback, + makeButtonCallback(buttonList, { + ...backgroundData, + ...callbacks, + stream, + topic, + }), ); };