@@ -6,15 +6,16 @@ import * as NavigationService from '../nav/NavigationService';
6
6
import type {
7
7
Auth ,
8
8
Dispatch ,
9
+ FlagsState ,
9
10
GetText ,
10
11
Message ,
12
+ MuteState ,
11
13
Narrow ,
12
14
Outbox ,
13
15
Subscription ,
14
16
User ,
15
17
EditMessage ,
16
18
} from '../types' ;
17
- import type { BackgroundData } from '../webview/MessageList' ;
18
19
import {
19
20
getNarrowForReply ,
20
21
isPmNarrow ,
@@ -241,16 +242,12 @@ type ButtonCode = $Keys<typeof allButtonsRaw>;
241
242
242
243
const allButtons : { | [ ButtonCode ] : ButtonDescription | } = allButtonsRaw ;
243
244
244
- type ConstructSheetParams < MsgType : Message | Outbox = Message | Outbox > = { |
245
- backgroundData : BackgroundData ,
246
- message : MsgType ,
247
- narrow : Narrow ,
248
- | } ;
249
-
250
- export const constructHeaderActionButtons = ( {
251
- backgroundData : { mute, subscriptions, ownUser } ,
252
- message,
253
- } : ConstructSheetParams < > ) : ButtonCode [ ] => {
245
+ export const constructHeaderActionButtons = (
246
+ mute : MuteState ,
247
+ subscriptions : Subscription [ ] ,
248
+ ownUser : User ,
249
+ message : Message | Outbox ,
250
+ ) : ButtonCode [ ] => {
254
251
const buttons : ButtonCode [ ] = [ ] ;
255
252
if ( message . type === 'stream' ) {
256
253
if ( ownUser . is_admin ) {
@@ -273,11 +270,7 @@ export const constructHeaderActionButtons = ({
273
270
return buttons ;
274
271
} ;
275
272
276
- export const constructOutboxActionButtons = ( {
277
- backgroundData,
278
- message,
279
- narrow,
280
- } : ConstructSheetParams < Outbox > ): ButtonCode[] => {
273
+ export const constructOutboxActionButtons = ( message : Outbox , narrow : Narrow ) : ButtonCode [ ] => {
281
274
const buttons = [ ] ;
282
275
buttons . push ( 'copyToClipboard' ) ;
283
276
buttons . push ( 'shareMessage' ) ;
@@ -289,11 +282,12 @@ export const constructOutboxActionButtons = ({
289
282
const messageNotDeleted = ( message : Message | Outbox ) : boolean =>
290
283
message . content !== '<p>(deleted)</p>' ;
291
284
292
- export const constructMessageActionButtons = ({
293
- backgroundData : { ownUser, flags } ,
294
- message ,
295
- narrow ,
296
- } : ConstructSheetParams< Message > ): ButtonCode[] => {
285
+ export const constructMessageActionButtons = (
286
+ ownUser : User ,
287
+ flags : FlagsState ,
288
+ message : Message ,
289
+ narrow : Narrow ,
290
+ ) : ButtonCode [ ] => {
297
291
const buttons = [ ] ;
298
292
if ( messageNotDeleted ( message ) ) {
299
293
buttons . push ( 'addReaction' ) ;
@@ -327,15 +321,16 @@ export const constructMessageActionButtons = ({
327
321
return buttons ;
328
322
} ;
329
323
330
- export const constructNonHeaderActionButtons = ( {
331
- backgroundData,
332
- message,
333
- narrow,
334
- } : ConstructSheetParams < > ): ButtonCode[] => {
324
+ export const constructNonHeaderActionButtons = (
325
+ ownUser : User ,
326
+ flags : FlagsState ,
327
+ message : Message | Outbox ,
328
+ narrow : Narrow ,
329
+ ) : ButtonCode [ ] => {
335
330
if ( message . isOutbox ) {
336
- return constructOutboxActionButtons ( { backgroundData , message, narrow } ) ;
331
+ return constructOutboxActionButtons ( message , narrow ) ;
337
332
} else {
338
- return constructMessageActionButtons ( { backgroundData , message, narrow } ) ;
333
+ return constructMessageActionButtons ( ownUser , flags , message , narrow ) ;
339
334
}
340
335
} ;
341
336
@@ -352,46 +347,83 @@ const getActionSheetTitle = (message: Message | Outbox, ownUser: User): string =
352
347
}
353
348
} ;
354
349
355
- /** Invoke the given callback to show an appropriate action sheet. */
356
- export const showActionSheet = (
357
- isHeader : boolean ,
350
+ const makeButtonCallback = (
351
+ optionCodes : ButtonCode [ ] ,
352
+ auth : Auth ,
353
+ subscriptions : Subscription [ ] ,
354
+ ownUser : User ,
355
+ flags : FlagsState ,
356
+ message : Message | Outbox ,
357
+ callbacks : { |
358
+ dispatch : Dispatch ,
359
+ startEditMessage : ( editMessage : EditMessage ) => void ,
360
+ _ : GetText ,
361
+ | } ,
362
+ ) => buttonIndex => {
363
+ ( async ( ) => {
364
+ const pressedButton : ButtonDescription = allButtons [ optionCodes [ buttonIndex ] ] ;
365
+ try {
366
+ await pressedButton ( {
367
+ auth,
368
+ subscriptions,
369
+ ownUser,
370
+ flags,
371
+ message,
372
+ ...callbacks ,
373
+ } ) ;
374
+ } catch ( err ) {
375
+ Alert . alert ( callbacks . _ ( pressedButton . errorMessage ) , err . message ) ;
376
+ }
377
+ } ) ( ) ;
378
+ } ;
379
+
380
+ export const showMessageActionSheet = (
358
381
showActionSheetWithOptions : ShowActionSheetWithOptions ,
359
382
callbacks : { |
360
383
dispatch : Dispatch ,
361
384
startEditMessage : ( editMessage : EditMessage ) => void ,
362
385
_ : GetText ,
363
386
| } ,
364
- params: ConstructSheetParams< > ,
387
+ auth : Auth ,
388
+ subscriptions : Subscription [ ] ,
389
+ ownUser : User ,
390
+ flags : FlagsState ,
391
+ message : Message | Outbox ,
392
+ narrow : Narrow ,
393
+ ) : void => {
394
+ const optionCodes = constructNonHeaderActionButtons ( ownUser , flags , message , narrow ) ;
395
+ showActionSheetWithOptions (
396
+ {
397
+ options : optionCodes . map ( code => callbacks . _ ( allButtons [ code ] . title ) ) ,
398
+ cancelButtonIndex : optionCodes . length - 1 ,
399
+ } ,
400
+ makeButtonCallback ( optionCodes , auth , subscriptions , ownUser , flags , message , callbacks ) ,
401
+ ) ;
402
+ } ;
403
+
404
+ export const showHeaderActionSheet = (
405
+ showActionSheetWithOptions : ShowActionSheetWithOptions ,
406
+ callbacks : { |
407
+ dispatch : Dispatch ,
408
+ _ : GetText ,
409
+ | } ,
410
+ auth : Auth ,
411
+ mute : MuteState ,
412
+ subscriptions : Subscription [ ] ,
413
+ ownUser : User ,
414
+ flags : FlagsState ,
415
+ message : Message | Outbox ,
365
416
) : void => {
366
- const optionCodes = isHeader
367
- ? constructHeaderActionButtons ( params )
368
- : constructNonHeaderActionButtons ( params ) ;
369
- const callback = buttonIndex => {
370
- ( async ( ) => {
371
- const pressedButton : ButtonDescription = allButtons [ optionCodes [ buttonIndex ] ] ;
372
- try {
373
- await pressedButton ( {
374
- subscriptions : params . backgroundData . subscriptions ,
375
- auth : params . backgroundData . auth ,
376
- ownUser : params . backgroundData . ownUser ,
377
- ...params ,
378
- ...callbacks ,
379
- } ) ;
380
- } catch ( err ) {
381
- Alert . alert ( callbacks . _ ( pressedButton . errorMessage ) , err . message ) ;
382
- }
383
- } ) ( ) ;
384
- } ;
417
+ const optionCodes = constructHeaderActionButtons ( mute , subscriptions , ownUser , message ) ;
385
418
showActionSheetWithOptions (
386
419
{
387
- ...( isHeader
388
- ? {
389
- title : getActionSheetTitle ( params . message , params . backgroundData . ownUser ) ,
390
- }
391
- : { } ) ,
420
+ title : getActionSheetTitle ( message , ownUser ) ,
392
421
options : optionCodes . map ( code => callbacks . _ ( allButtons [ code ] . title ) ) ,
393
422
cancelButtonIndex : optionCodes . length - 1 ,
394
423
} ,
395
- callback ,
424
+ makeButtonCallback ( optionCodes , auth , subscriptions , ownUser , flags , message , {
425
+ startEditMessage : _ => { } ,
426
+ ...callbacks ,
427
+ } ) ,
396
428
) ;
397
429
} ;
0 commit comments