import { ContentType, SNFile } from '@standardnotes/snjs'; import { FilesIllustration } from '@standardnotes/stylekit'; import { observer } from 'mobx-react-lite'; import { FunctionComponent } from 'preact'; import { StateUpdater, useCallback, useEffect, useState } from 'preact/hooks'; import { Button } from '../Button'; import { Icon } from '../Icon'; import { PopoverTabs, PopoverWrapperProps } from './PopoverDragNDropWrapper'; import { PopoverFileItem } from './PopoverFileItem'; import { PopoverFileItemActionType } from './PopoverFileItemAction'; type Props = PopoverWrapperProps & { currentTab: PopoverTabs; setCurrentTab: StateUpdater; }; export const AttachedFilesPopover: FunctionComponent = observer( ({ application, appState, note, fileActionHandler, currentTab, setCurrentTab, }) => { const [attachedFiles, setAttachedFiles] = useState([]); const [allFiles, setAllFiles] = useState([]); const [searchQuery, setSearchQuery] = useState(''); const filesList = currentTab === PopoverTabs.AttachedFiles ? attachedFiles : allFiles; const filteredList = searchQuery.length > 0 ? filesList.filter( (file) => file.nameWithExt.toLowerCase().indexOf(searchQuery) !== -1 ) : filesList; const reloadAttachedFiles = useCallback(() => { setAttachedFiles( application.items .getFilesForNote(note) .sort((a, b) => (a.created_at < b.created_at ? 1 : -1)) ); }, [application.items, note]); const reloadAllFiles = useCallback(() => { setAllFiles( application .getItems(ContentType.File) .sort((a, b) => (a.created_at < b.created_at ? 1 : -1)) as SNFile[] ); }, [application]); useEffect(() => { const unregisterFileStream = application.streamItems( ContentType.File, () => { reloadAttachedFiles(); reloadAllFiles(); } ); return () => { unregisterFileStream(); }; }, [application, reloadAllFiles, reloadAttachedFiles]); const handleAttachFilesClick = async () => { const uploadedFiles = await appState.files.uploadNewFile(); if (!uploadedFiles) { return; } if (currentTab === PopoverTabs.AttachedFiles) { uploadedFiles.forEach((file) => { fileActionHandler({ type: PopoverFileItemActionType.AttachFileToNote, payload: file, }); }); } }; return (
{filteredList.length > 0 || searchQuery.length > 0 ? (
{ setSearchQuery((e.target as HTMLInputElement).value); }} /> {searchQuery.length > 0 && ( )}
) : null} {filteredList.length > 0 ? ( filteredList.map((file: SNFile) => { return ( ); }) ) : (
{searchQuery.length > 0 ? 'No result found' : currentTab === PopoverTabs.AttachedFiles ? 'No files attached to this note' : 'No files found in this account'}
Or drop your files here
)}
{filteredList.length > 0 && ( )}
); } );