1
1
import React , { useRef } from 'react' ;
2
2
import type { ValueOf } from 'type-fest' ;
3
3
import type { FileObject } from '@components/AttachmentModal' ;
4
- import * as Browser from '@libs/Browser' ;
4
+ import { isMobileChrome } from '@libs/Browser' ;
5
5
import Visibility from '@libs/Visibility' ;
6
6
import CONST from '@src/CONST' ;
7
7
import type AttachmentPickerProps from './types' ;
@@ -10,7 +10,7 @@ import type AttachmentPickerProps from './types';
10
10
* Returns acceptable FileTypes based on ATTACHMENT_PICKER_TYPE
11
11
*/
12
12
function getAcceptableFileTypes ( type : string ) : string | undefined {
13
- if ( type !== CONST . ATTACHMENT_PICKER_TYPE . IMAGE || Browser . isMobileChrome ( ) ) {
13
+ if ( type !== CONST . ATTACHMENT_PICKER_TYPE . IMAGE || isMobileChrome ( ) ) {
14
14
return ;
15
15
}
16
16
@@ -47,14 +47,15 @@ function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE, a
47
47
const fileInput = useRef < HTMLInputElement > ( null ) ;
48
48
const onPicked = useRef < ( files : FileObject [ ] ) => void > ( ( ) => { } ) ;
49
49
const onCanceled = useRef < ( ) => void > ( ( ) => { } ) ;
50
-
50
+ const isPickingRef = useRef ( false ) ;
51
51
return (
52
52
< >
53
53
< input
54
54
hidden
55
55
type = "file"
56
56
ref = { fileInput }
57
57
onChange = { ( e ) => {
58
+ isPickingRef . current = false ;
58
59
if ( ! e . target . files ) {
59
60
return ;
60
61
}
@@ -82,6 +83,7 @@ function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE, a
82
83
fileInput . current . addEventListener (
83
84
'cancel' ,
84
85
( ) => {
86
+ isPickingRef . current = false ;
85
87
// For Android Chrome, the cancel event happens before the page is visible on physical devices,
86
88
// which makes it unreliable for us to show the keyboard, while on emulators it happens after the page is visible.
87
89
// So here we can delay calling the onCanceled.current function based on visibility in order to reliably show the keyboard.
@@ -103,6 +105,10 @@ function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE, a
103
105
{ /* eslint-disable-next-line react-compiler/react-compiler */ }
104
106
{ children ( {
105
107
openPicker : ( { onPicked : newOnPicked , onCanceled : newOnCanceled = ( ) => { } } ) => {
108
+ if ( isPickingRef . current ) {
109
+ return ;
110
+ }
111
+ isPickingRef . current = true ;
106
112
onPicked . current = newOnPicked ;
107
113
fileInput . current ?. click ( ) ;
108
114
onCanceled . current = newOnCanceled ;
0 commit comments