From 99d83af3ba8d1bbe88dfed9027defb77ea0fe923 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Thu, 28 Apr 2022 00:49:27 +0530 Subject: [PATCH] feat: sort files by name (#1001) --- .../AttachedFilesButton.tsx | 46 +++++-------------- .../AttachedFilesPopover.tsx | 12 ++--- .../Components/Files/FilePreviewModal.tsx | 14 ++---- .../Files/FilePreviewModalProvider.tsx | 7 +-- .../Preferences/Panes/Account/index.tsx | 4 +- .../Device/DesktopDeviceInterface.ts | 4 +- .../Device/DesktopWebCommunication.ts | 5 +- .../javascripts/Services/DesktopManager.ts | 5 +- app/assets/javascripts/Services/IOService.ts | 17 ++----- package.json | 2 +- yarn.lock | 44 +++++++++--------- 11 files changed, 52 insertions(+), 108 deletions(-) diff --git a/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesButton.tsx b/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesButton.tsx index ec3f17918..b4ced4d06 100644 --- a/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesButton.tsx +++ b/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesButton.tsx @@ -8,7 +8,7 @@ import { FunctionComponent } from 'preact' import { useCallback, useEffect, useRef, useState } from 'preact/hooks' import { Icon } from '@/Components/Icon' import { useCloseOnBlur } from '@/Hooks/useCloseOnBlur' -import { ChallengeReason, ContentType, SNFile } from '@standardnotes/snjs' +import { ChallengeReason, CollectionSort, ContentType, SNFile } from '@standardnotes/snjs' import { confirmDialog } from '@/Services/AlertService' import { addToast, dismissToast, ToastType } from '@standardnotes/stylekit' import { StreamingFileReader } from '@standardnotes/filepicker' @@ -39,8 +39,7 @@ const removeDragOverlay = () => { } const isHandlingFileDrag = (event: DragEvent) => - event.dataTransfer?.items && - Array.from(event.dataTransfer.items).some((item) => item.kind === 'file') + event.dataTransfer?.items && Array.from(event.dataTransfer.items).some((item) => item.kind === 'file') export const AttachedFilesButton: FunctionComponent = observer( ({ application, appState, onClickPreprocessing }) => { @@ -66,17 +65,11 @@ export const AttachedFilesButton: FunctionComponent = observer( const attachedFilesCount = attachedFiles.length useEffect(() => { + application.items.setDisplayOptions(ContentType.File, CollectionSort.Title, 'dsc') + const unregisterFileStream = application.streamItems(ContentType.File, () => { - setAllFiles( - application.items - .getItems(ContentType.File) - .sort((a, b) => (a.created_at < b.created_at ? 1 : -1)), - ) - setAttachedFiles( - application.items - .getFilesForNote(note) - .sort((a, b) => (a.created_at < b.created_at ? 1 : -1)), - ) + setAllFiles(application.items.getDisplayableItems(ContentType.File)) + setAttachedFiles(application.items.getFilesForNote(note)) }) return () => { @@ -162,14 +155,8 @@ export const AttachedFilesButton: FunctionComponent = observer( return isProtected } - const authorizeProtectedActionForFile = async ( - file: SNFile, - challengeReason: ChallengeReason, - ) => { - const authorizedFiles = await application.protections.authorizeProtectedActionForFiles( - [file], - challengeReason, - ) + const authorizeProtectedActionForFile = async (file: SNFile, challengeReason: ChallengeReason) => { + const authorizedFiles = await application.protections.authorizeProtectedActionForFiles([file], challengeReason) const isAuthorized = authorizedFiles.length > 0 && authorizedFiles.includes(file) return isAuthorized } @@ -179,16 +166,12 @@ export const AttachedFilesButton: FunctionComponent = observer( } const handleFileAction = async (action: PopoverFileItemAction) => { - const file = - action.type !== PopoverFileItemActionType.RenameFile ? action.payload : action.payload.file + const file = action.type !== PopoverFileItemActionType.RenameFile ? action.payload : action.payload.file let isAuthorizedForAction = true if (file.protected && action.type !== PopoverFileItemActionType.ToggleFileProtection) { keepMenuOpen(true) - isAuthorizedForAction = await authorizeProtectedActionForFile( - file, - ChallengeReason.AccessProtectedFile, - ) + isAuthorizedForAction = await authorizeProtectedActionForFile(file, ChallengeReason.AccessProtectedFile) keepMenuOpen(false) buttonRef.current?.focus() } @@ -219,10 +202,7 @@ export const AttachedFilesButton: FunctionComponent = observer( await renameFile(file, action.payload.name) break case PopoverFileItemActionType.PreviewFile: - filePreviewModal.activate( - file, - currentTab === PopoverTabs.AllFiles ? allFiles : attachedFiles, - ) + filePreviewModal.activate(file, currentTab === PopoverTabs.AllFiles ? allFiles : attachedFiles) break } @@ -353,9 +333,7 @@ export const AttachedFilesButton: FunctionComponent = observer( } }} ref={buttonRef} - className={`sn-icon-button border-contrast ${ - attachedFilesCount > 0 ? 'py-1 px-3' : '' - }`} + className={`sn-icon-button border-contrast ${attachedFilesCount > 0 ? 'py-1 px-3' : ''}`} onBlur={closeOnBlur} > Attached files diff --git a/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesPopover.tsx b/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesPopover.tsx index d5e9d9bed..15221a5ad 100644 --- a/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesPopover.tsx +++ b/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesPopover.tsx @@ -47,9 +47,7 @@ export const AttachedFilesPopover: FunctionComponent = observer( const filteredList = searchQuery.length > 0 - ? filesList.filter( - (file) => file.name.toLowerCase().indexOf(searchQuery.toLowerCase()) !== -1, - ) + ? filesList.filter((file) => file.name.toLowerCase().indexOf(searchQuery.toLowerCase()) !== -1) : filesList const handleAttachFilesClick = async () => { @@ -78,9 +76,7 @@ export const AttachedFilesPopover: FunctionComponent = observer(