From 65852618dc2938b8edef7ee42b0ea50d239c8eb5 Mon Sep 17 00:00:00 2001 From: Mo Date: Fri, 13 May 2022 08:37:27 -0500 Subject: [PATCH] chore: break up file backups into two components --- .../AttachedFilesButton.tsx | 16 +- .../BackupsDropZone.tsx} | 150 +----------------- .../Panes/Backups/Files/FileBackups.tsx | 136 ++++++++++++++++ .../Preferences/Panes/Backups/index.tsx | 2 +- .../javascripts/Utils/DragTypeCheck.tsx | 31 ++++ 5 files changed, 173 insertions(+), 162 deletions(-) rename app/assets/javascripts/Components/Preferences/Panes/Backups/{FileBackups.tsx => Files/BackupsDropZone.tsx} (58%) create mode 100644 app/assets/javascripts/Components/Preferences/Panes/Backups/Files/FileBackups.tsx create mode 100644 app/assets/javascripts/Utils/DragTypeCheck.tsx diff --git a/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesButton.tsx b/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesButton.tsx index 4b9f0f8ed..975c2b724 100644 --- a/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesButton.tsx +++ b/app/assets/javascripts/Components/AttachedFilesPopover/AttachedFilesButton.tsx @@ -16,6 +16,7 @@ import { PopoverFileItemAction, PopoverFileItemActionType } from './PopoverFileI import { AttachedFilesPopover } from './AttachedFilesPopover' import { usePremiumModal } from '@/Hooks/usePremiumModal' import { PopoverTabs } from './PopoverTabs' +import { isHandlingFileDrag } from '@/Utils/DragTypeCheck' type Props = { application: WebApplication @@ -23,21 +24,6 @@ type Props = { onClickPreprocessing?: () => Promise } -const isHandlingFileDrag = (event: DragEvent, application: WebApplication) => { - const items = event.dataTransfer?.items - - if (!items) { - return false - } - - return Array.from(items).some((item) => { - const isFile = item.kind === 'file' - const fileName = item.getAsFile()?.name || '' - const isBackupMetadataFile = application.files.isFileNameFileBackupMetadataFile(fileName) - return isFile && !isBackupMetadataFile - }) -} - export const AttachedFilesButton: FunctionComponent = observer( ({ application, appState, onClickPreprocessing }) => { const premiumModal = usePremiumModal() diff --git a/app/assets/javascripts/Components/Preferences/Panes/Backups/FileBackups.tsx b/app/assets/javascripts/Components/Preferences/Panes/Backups/Files/BackupsDropZone.tsx similarity index 58% rename from app/assets/javascripts/Components/Preferences/Panes/Backups/FileBackups.tsx rename to app/assets/javascripts/Components/Preferences/Panes/Backups/Files/BackupsDropZone.tsx index 5736e53a8..96bacced8 100644 --- a/app/assets/javascripts/Components/Preferences/Panes/Backups/FileBackups.tsx +++ b/app/assets/javascripts/Components/Preferences/Panes/Backups/Files/BackupsDropZone.tsx @@ -1,156 +1,14 @@ -import { WebApplication } from '@/UIModels/Application' -import { observer } from 'mobx-react-lite' -import { - PreferencesGroup, - PreferencesSegment, - Title, - Text, - Subtitle, -} from '@/Components/Preferences/PreferencesComponents' +import { PreferencesSegment, Title, Text, Subtitle } from '@/Components/Preferences/PreferencesComponents' import { useCallback, useEffect, useMemo, useState } from 'preact/hooks' import { Button } from '@/Components/Button/Button' import { FileBackupMetadataFile, FileBackupsConstantsV1, FileContent, FileHandleRead } from '@standardnotes/snjs' -import { Switch } from '@/Components/Switch' import { HorizontalSeparator } from '@/Components/Shared/HorizontalSeparator' -import { EncryptionStatusItem } from '../Security/Encryption' +import { EncryptionStatusItem } from '../../Security/Encryption' import { Icon } from '@/Components/Icon' import { StreamingFileApi } from '@standardnotes/filepicker' import { FunctionComponent } from 'preact' - -type Props = { - application: WebApplication -} - -export const FileBackups = observer(({ application }: Props) => { - const [backupsEnabled, setBackupsEnabled] = useState(false) - const [backupsLocation, setBackupsLocation] = useState('') - const backupsService = useMemo(() => application.fileBackups, [application.fileBackups]) - - if (!backupsService) { - return ( - <> - - - File Backups - Automatically save encrypted backups of files uploaded to any device to this computer. - To enable file backups, use the Standard Notes desktop application. - - - - - - - ) - } - - useEffect(() => { - void backupsService.isFilesBackupsEnabled().then(setBackupsEnabled) - }, [backupsService]) - - useEffect(() => { - if (backupsEnabled) { - void backupsService.getFilesBackupsLocation().then(setBackupsLocation) - } - }, [backupsService, backupsEnabled]) - - const changeBackupsLocation = useCallback(async () => { - await backupsService.changeFilesBackupsLocation() - - setBackupsLocation(await backupsService.getFilesBackupsLocation()) - }, [backupsService]) - - const openBackupsLocation = useCallback(async () => { - await backupsService.openFilesBackupsLocation() - }, [backupsService]) - - const toggleBackups = useCallback(async () => { - if (backupsEnabled) { - await backupsService.disableFilesBackups() - } else { - await backupsService.enableFilesBackups() - } - - setBackupsEnabled(await backupsService.isFilesBackupsEnabled()) - }, [backupsService, backupsEnabled]) - - return ( - <> - - - File Backups - -
-
- - Automatically save encrypted backups of files uploaded on any device to this computer. - -
- -
- - {!backupsEnabled && ( - <> - - File backups are not enabled. Enable to choose where your files are backed up. - - )} -
- - {backupsEnabled && ( - <> - - <> - - Files backups are enabled. When you upload a new file on any device and open this application, files - will be backed up in encrypted form to: - - - ]} - checkmark={false} - /> -
-
- -
- - )} - - - - -
- - ) -}) - -const isHandlingBackupDrag = (event: DragEvent, application: WebApplication) => { - const items = event.dataTransfer?.items - - if (!items) { - return false - } - - return Array.from(items).every((item) => { - const isFile = item.kind === 'file' - const fileName = item.getAsFile()?.name || '' - const isBackupMetadataFile = application.files.isFileNameFileBackupMetadataFile(fileName) - return isFile && isBackupMetadataFile - }) -} +import { Props } from './FileBackups' +import { isHandlingBackupDrag } from '@/Utils/DragTypeCheck' export const BackupsDropZone: FunctionComponent = ({ application }) => { const [droppedFile, setDroppedFile] = useState(undefined) diff --git a/app/assets/javascripts/Components/Preferences/Panes/Backups/Files/FileBackups.tsx b/app/assets/javascripts/Components/Preferences/Panes/Backups/Files/FileBackups.tsx new file mode 100644 index 000000000..ae40b5264 --- /dev/null +++ b/app/assets/javascripts/Components/Preferences/Panes/Backups/Files/FileBackups.tsx @@ -0,0 +1,136 @@ +import { WebApplication } from '@/UIModels/Application' +import { observer } from 'mobx-react-lite' +import { + PreferencesGroup, + PreferencesSegment, + Title, + Text, + Subtitle, +} from '@/Components/Preferences/PreferencesComponents' +import { useCallback, useEffect, useMemo, useState } from 'preact/hooks' +import { Button } from '@/Components/Button/Button' +import { Switch } from '@/Components/Switch' +import { HorizontalSeparator } from '@/Components/Shared/HorizontalSeparator' +import { EncryptionStatusItem } from '../../Security/Encryption' +import { Icon } from '@/Components/Icon' +import { BackupsDropZone } from './BackupsDropZone' + +export type Props = { + application: WebApplication +} + +export const FileBackups = observer(({ application }: Props) => { + const [backupsEnabled, setBackupsEnabled] = useState(false) + const [backupsLocation, setBackupsLocation] = useState('') + const backupsService = useMemo(() => application.fileBackups, [application.fileBackups]) + + if (!backupsService) { + return ( + <> + + + File Backups + Automatically save encrypted backups of files uploaded to any device to this computer. + To enable file backups, use the Standard Notes desktop application. + + + + + + + ) + } + + useEffect(() => { + void backupsService.isFilesBackupsEnabled().then(setBackupsEnabled) + }, [backupsService]) + + useEffect(() => { + if (backupsEnabled) { + void backupsService.getFilesBackupsLocation().then(setBackupsLocation) + } + }, [backupsService, backupsEnabled]) + + const changeBackupsLocation = useCallback(async () => { + await backupsService.changeFilesBackupsLocation() + + setBackupsLocation(await backupsService.getFilesBackupsLocation()) + }, [backupsService]) + + const openBackupsLocation = useCallback(async () => { + await backupsService.openFilesBackupsLocation() + }, [backupsService]) + + const toggleBackups = useCallback(async () => { + if (backupsEnabled) { + await backupsService.disableFilesBackups() + } else { + await backupsService.enableFilesBackups() + } + + setBackupsEnabled(await backupsService.isFilesBackupsEnabled()) + }, [backupsService, backupsEnabled]) + + return ( + <> + + + File Backups + +
+
+ + Automatically save encrypted backups of files uploaded on any device to this computer. + +
+ +
+ + {!backupsEnabled && ( + <> + + File backups are not enabled. Enable to choose where your files are backed up. + + )} +
+ + {backupsEnabled && ( + <> + + <> + + Files backups are enabled. When you upload a new file on any device and open this application, files + will be backed up in encrypted form to: + + + ]} + checkmark={false} + /> +
+
+ +
+ + )} + + + + +
+ + ) +}) diff --git a/app/assets/javascripts/Components/Preferences/Panes/Backups/index.tsx b/app/assets/javascripts/Components/Preferences/Panes/Backups/index.tsx index 8dc7be868..cb1910d96 100644 --- a/app/assets/javascripts/Components/Preferences/Panes/Backups/index.tsx +++ b/app/assets/javascripts/Components/Preferences/Panes/Backups/index.tsx @@ -5,7 +5,7 @@ import { PreferencesPane } from '@/Components/Preferences/PreferencesComponents' import { CloudLink } from './CloudBackups' import { DataBackups } from './DataBackups' import { EmailBackups } from './EmailBackups' -import { FileBackups } from './FileBackups' +import { FileBackups } from './Files/FileBackups' interface Props { appState: AppState diff --git a/app/assets/javascripts/Utils/DragTypeCheck.tsx b/app/assets/javascripts/Utils/DragTypeCheck.tsx new file mode 100644 index 000000000..4ed350455 --- /dev/null +++ b/app/assets/javascripts/Utils/DragTypeCheck.tsx @@ -0,0 +1,31 @@ +import { WebApplication } from '@/UIModels/Application' + +export const isHandlingFileDrag = (event: DragEvent, application: WebApplication) => { + const items = event.dataTransfer?.items + + if (!items) { + return false + } + + return Array.from(items).some((item) => { + const isFile = item.kind === 'file' + const fileName = item.getAsFile()?.name || '' + const isBackupMetadataFile = application.files.isFileNameFileBackupMetadataFile(fileName) + return isFile && !isBackupMetadataFile + }) +} + +export const isHandlingBackupDrag = (event: DragEvent, application: WebApplication) => { + const items = event.dataTransfer?.items + + if (!items) { + return false + } + + return Array.from(items).every((item) => { + const isFile = item.kind === 'file' + const fileName = item.getAsFile()?.name || '' + const isBackupMetadataFile = application.files.isFileNameFileBackupMetadataFile(fileName) + return isFile && isBackupMetadataFile + }) +}