import { FOCUSABLE_BUT_NOT_TABBABLE } from '@/constants'; import { WebApplication } from '@/ui_models/application'; import { AppState } from '@/ui_models/app_state'; import { ContentType, SNFile, SNNote } from '@standardnotes/snjs'; import { FilesIllustration } from '@standardnotes/stylekit'; import { observer } from 'mobx-react-lite'; import { FunctionComponent } from 'preact'; import { StateUpdater, useEffect, useRef, useState } from 'preact/hooks'; import { Button } from '../Button'; import { Icon } from '../Icon'; import { PopoverFileItem } from './PopoverFileItem'; import { PopoverFileItemAction, PopoverFileItemActionType, } from './PopoverFileItemAction'; export enum PopoverTabs { AttachedFiles, AllFiles, } type Props = { application: WebApplication; appState: AppState; currentTab: PopoverTabs; closeOnBlur: (event: { relatedTarget: EventTarget | null }) => void; handleFileAction: (action: PopoverFileItemAction) => Promise; isDraggingFiles: boolean; note: SNNote; setCurrentTab: StateUpdater; }; export const AttachedFilesPopover: FunctionComponent = observer( ({ application, appState, currentTab, closeOnBlur, handleFileAction, isDraggingFiles, note, setCurrentTab, }) => { const [attachedFiles, setAttachedFiles] = useState([]); const [allFiles, setAllFiles] = useState([]); const [searchQuery, setSearchQuery] = useState(''); const searchInputRef = useRef(null); const filesList = currentTab === PopoverTabs.AttachedFiles ? attachedFiles : allFiles; const filteredList = searchQuery.length > 0 ? filesList.filter( (file) => file.name.toLowerCase().indexOf(searchQuery.toLowerCase()) !== -1 ) : filesList; useEffect(() => { const unregisterFileStream = application.streamItems( ContentType.File, () => { setAttachedFiles( application.items .getFilesForNote(note) .sort((a, b) => (a.created_at < b.created_at ? 1 : -1)) ); setAllFiles( application.items .getItems(ContentType.File) .sort((a, b) => a.created_at < b.created_at ? 1 : -1 ) as SNFile[] ); } ); return () => { unregisterFileStream(); }; }, [application, note]); const handleAttachFilesClick = async () => { const uploadedFiles = await appState.files.uploadNewFile(); if (!uploadedFiles) { return; } if (currentTab === PopoverTabs.AttachedFiles) { uploadedFiles.forEach((file) => { handleFileAction({ type: PopoverFileItemActionType.AttachFileToNote, payload: file, }); }); } }; return (
{filteredList.length > 0 || searchQuery.length > 0 ? (
{ setSearchQuery((e.target as HTMLInputElement).value); }} onBlur={closeOnBlur} ref={searchInputRef} /> {searchQuery.length > 0 && ( )}
) : null} {filteredList.length > 0 ? ( filteredList .filter((file) => !file.deleted) .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 && ( )}
); } );