import { AppState } from '@/ui_models/app_state'; import { Icon } from './Icon'; import { Switch } from './Switch'; import { observer } from 'mobx-react-lite'; import { useRef, useState, useEffect } from 'preact/hooks'; import { Disclosure, DisclosureButton, DisclosurePanel, } from '@reach/disclosure'; import { SNNote } from '@standardnotes/snjs/dist/@types'; type Props = { appState: AppState; closeOnBlur: (event: { relatedTarget: EventTarget | null }) => void; onSubmenuChange?: (submenuOpen: boolean) => void; }; const MAX_TAGS_MENU_HEIGHT = 320; export const NotesOptions = observer( ({ appState, closeOnBlur, onSubmenuChange }: Props) => { const [tagsMenuOpen, setTagsMenuOpen] = useState(false); const [tagsMenuPosition, setTagsMenuPosition] = useState({ top: 0, right: 0, }); const toggleOn = (condition: (note: SNNote) => boolean) => { const notesMatchingAttribute = notes.filter(condition); const notesNotMatchingAttribute = notes.filter((note) => !condition(note)); return (notesMatchingAttribute.length > notesNotMatchingAttribute.length); }; const notes = Object.values(appState.notes.selectedNotes); const hidePreviews = toggleOn(note => note.hidePreview); const locked = toggleOn(note => note.locked); const archived = notes.some((note) => note.archived); const unarchived = notes.some((note) => !note.archived); const trashed = notes.some((note) => note.trashed); const notTrashed = notes.some((note) => !note.trashed); const pinned = notes.some((note) => note.pinned); const unpinned = notes.some((note) => !note.pinned); const tagsButtonRef = useRef(); const iconClass = 'fill-current color-neutral mr-2'; const buttonClass = 'flex items-center border-0 focus:inner-ring-info ' + 'cursor-pointer hover:bg-contrast color-text bg-transparent px-3 ' + 'text-left'; useEffect(() => { if (onSubmenuChange) { onSubmenuChange(tagsMenuOpen); } }, [tagsMenuOpen, onSubmenuChange]); return ( <> { appState.notes.setLockSelectedNotes(!locked); }} > Prevent editing { appState.notes.setHideSelectedNotePreviews(!hidePreviews); }} > Show preview
{appState.tags.tagsCount > 0 && ( { const buttonRect = tagsButtonRef.current.getBoundingClientRect(); const { offsetTop, offsetWidth } = tagsButtonRef.current; setTagsMenuPosition({ top: offsetTop, right: buttonRect.right + MAX_TAGS_MENU_HEIGHT > document.body.clientWidth ? offsetWidth : -offsetWidth, }); setTagsMenuOpen(!tagsMenuOpen); }} > { if (event.key === 'Escape') { setTagsMenuOpen(false); } }} onBlur={closeOnBlur} ref={tagsButtonRef} className={`${buttonClass} py-1.5 justify-between`} >
{'Add tag'}
{ if (event.key === 'Escape') { setTagsMenuOpen(false); tagsButtonRef.current.focus(); } }} style={{ ...tagsMenuPosition, }} className="sn-dropdown sn-dropdown-anchor-right flex flex-col py-2 max-h-80 overflow-y-scroll" > {appState.tags.tags.map((tag) => ( ))}
)} {unpinned && ( )} {pinned && ( )} {unarchived && ( )} {archived && ( )} {notTrashed && ( )} {trashed && ( )} {appState.selectedTag?.isTrashTag && ( )} ); } );