From 6cacdd8c5882ac562c73a706aa4237c53a049f5c Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Fri, 2 Dec 2022 21:48:56 +0530 Subject: [PATCH] fix(mobile): Fixes issue where file upload doesn't work with immediate biometrics (#2083) --- .../filepicker/src/Classic/ClassicReader.ts | 23 +++++++++-- .../ContentListView/ContentListView.tsx | 28 +------------- .../LinkedItems/LinkedItemsPanel.tsx | 38 ++++--------------- 3 files changed, 29 insertions(+), 60 deletions(-) diff --git a/packages/filepicker/src/Classic/ClassicReader.ts b/packages/filepicker/src/Classic/ClassicReader.ts index ae2dc365e..65f7b0edd 100644 --- a/packages/filepicker/src/Classic/ClassicReader.ts +++ b/packages/filepicker/src/Classic/ClassicReader.ts @@ -18,16 +18,31 @@ function maximumFileSize(): number { return 50 * 1_000_000 } +const FileInputId = 'classic-reader-file-input' +function createFileInputOrReturnExisting(): HTMLInputElement { + let fileInput = document.getElementById(FileInputId) as HTMLInputElement + if (fileInput) { + return fileInput + } + + fileInput = document.createElement('input') + fileInput.id = FileInputId + fileInput.type = 'file' + fileInput.className = 'absolute top-0 left-0 -z-50 h-px w-px opacity-0' + fileInput.multiple = true + document.body.appendChild(fileInput) + + return fileInput +} + function selectFiles(): Promise { - const input = document.createElement('input') as HTMLInputElement - input.type = 'file' - input.multiple = true + const input = createFileInputOrReturnExisting() return new Promise((resolve) => { input.onchange = async (event) => { const target = event.target as HTMLInputElement const files = [] - for (const file of target.files as FileList) { + for (const file of Array.from(target.files as FileList)) { files.push(file) } resolve(files) diff --git a/packages/web/src/javascripts/Components/ContentListView/ContentListView.tsx b/packages/web/src/javascripts/Components/ContentListView/ContentListView.tsx index be72e8900..df30fcc54 100644 --- a/packages/web/src/javascripts/Components/ContentListView/ContentListView.tsx +++ b/packages/web/src/javascripts/Components/ContentListView/ContentListView.tsx @@ -11,7 +11,7 @@ import { WebApplication } from '@/Application/Application' import { PANEL_NAME_NOTES } from '@/Constants/Constants' import { FileItem, PrefKey, WebAppEvent } from '@standardnotes/snjs' import { observer } from 'mobx-react-lite' -import { forwardRef, useCallback, useEffect, useMemo, useRef } from 'react' +import { forwardRef, useCallback, useEffect, useMemo } from 'react' import ContentList from '@/Components/ContentListView/ContentList' import NoAccountWarning from '@/Components/NoAccountWarning/NoAccountWarning' import { ItemListController } from '@/Controllers/ItemList/ItemListController' @@ -25,7 +25,6 @@ import { ElementIds } from '@/Constants/ElementIDs' import ContentListHeader from './Header/ContentListHeader' import { AppPaneId } from '../Panes/AppPaneMetadata' import { useResponsiveAppPane } from '../Panes/ResponsivePaneProvider' -import { StreamingFileReader } from '@standardnotes/filepicker' import SearchBar from '../SearchBar/SearchBar' import { SearchOptionsController } from '@/Controllers/SearchOptionsController' import { classNames } from '@standardnotes/utils' @@ -88,7 +87,6 @@ const ContentListView = forwardRef( isCurrentNoteTemplate, } = itemListController - const fileInputRef = useRef(null) const innerRef = useForwardedRef(ref) const { addDragTarget, removeDragTarget } = useFileDragNDrop() @@ -172,12 +170,7 @@ const ContentListView = forwardRef( return } - if (StreamingFileReader.available()) { - void filesController.uploadNewFile() - return - } - - fileInputRef.current?.click() + void filesController.uploadNewFile() } else { await createNewNote() toggleAppPane(AppPaneId.Editor) @@ -296,23 +289,6 @@ const ContentListView = forwardRef( >
- { - const files = event.currentTarget.files - - if (!files) { - return - } - - for (let i = 0; i < files.length; i++) { - void filesController.uploadNewFile(files[i]) - } - }} - /> {selectedTag && ( (null) const searchInputRef = useRef(null) const [searchQuery, setSearchQuery] = useState('') const isSearching = !!searchQuery.length @@ -58,33 +57,19 @@ const LinkedItemsPanel = ({ } }, [isOpen]) - const handleFileInputChange: ChangeEventHandler = (event) => { - const files = event.currentTarget.files - - if (!files) { - return - } - - for (let i = 0; i < files.length; i++) { - void filesController.uploadNewFile(files[i]).then((uploadedFiles) => { - if (uploadedFiles) { - void linkItemToSelectedItem(uploadedFiles[0]) - } - }) - } - } - - const selectAndUploadFiles = () => { + const selectAndUploadFiles = async () => { if (!entitledToFiles) { void featuresController.showPremiumAlert(FeatureName.Files) return } - if (!fileInputRef.current) { - return - } + const uploadedFiles = await filesController.uploadNewFile() - fileInputRef.current.click() + if (uploadedFiles && uploadedFiles.length) { + uploadedFiles.forEach((file) => { + void linkItemToSelectedItem(file) + }) + } } return ( @@ -177,13 +162,6 @@ const LinkedItemsPanel = ({
Linked Files
-