Skip to content

Commit 51744fa

Browse files
authored
Merge pull request #59319 from linhvovan29546/fix/59040-mweb-attechment-picker-shown-twice
fix: mweb attachment picker shown twice
2 parents adbf359 + c8d5c95 commit 51744fa

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/components/AttachmentPicker/index.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {useRef} from 'react';
22
import type {ValueOf} from 'type-fest';
33
import type {FileObject} from '@components/AttachmentModal';
4-
import * as Browser from '@libs/Browser';
4+
import {isMobileChrome} from '@libs/Browser';
55
import Visibility from '@libs/Visibility';
66
import CONST from '@src/CONST';
77
import type AttachmentPickerProps from './types';
@@ -10,7 +10,7 @@ import type AttachmentPickerProps from './types';
1010
* Returns acceptable FileTypes based on ATTACHMENT_PICKER_TYPE
1111
*/
1212
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()) {
1414
return;
1515
}
1616

@@ -47,14 +47,15 @@ function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE, a
4747
const fileInput = useRef<HTMLInputElement>(null);
4848
const onPicked = useRef<(files: FileObject[]) => void>(() => {});
4949
const onCanceled = useRef<() => void>(() => {});
50-
50+
const isPickingRef = useRef(false);
5151
return (
5252
<>
5353
<input
5454
hidden
5555
type="file"
5656
ref={fileInput}
5757
onChange={(e) => {
58+
isPickingRef.current = false;
5859
if (!e.target.files) {
5960
return;
6061
}
@@ -82,6 +83,7 @@ function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE, a
8283
fileInput.current.addEventListener(
8384
'cancel',
8485
() => {
86+
isPickingRef.current = false;
8587
// For Android Chrome, the cancel event happens before the page is visible on physical devices,
8688
// which makes it unreliable for us to show the keyboard, while on emulators it happens after the page is visible.
8789
// 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
103105
{/* eslint-disable-next-line react-compiler/react-compiler */}
104106
{children({
105107
openPicker: ({onPicked: newOnPicked, onCanceled: newOnCanceled = () => {}}) => {
108+
if (isPickingRef.current) {
109+
return;
110+
}
111+
isPickingRef.current = true;
106112
onPicked.current = newOnPicked;
107113
fileInput.current?.click();
108114
onCanceled.current = newOnCanceled;

0 commit comments

Comments
 (0)