import { AppState } from '@/ui_models/app_state'; import { toDirective, useAutorunValue } from './utils'; import { useRef, useState } from 'preact/hooks'; import { WebApplication } from '@/ui_models/application'; import VisuallyHidden from '@reach/visually-hidden'; import { Disclosure, DisclosureButton, DisclosurePanel, } from '@reach/disclosure'; import { FocusEvent } from 'react'; import { Switch } from './Switch'; import TuneIcon from '../../icons/ic_tune.svg'; type Props = { appState: AppState; application: WebApplication; }; function SearchOptions({ appState }: Props) { const { searchOptions } = appState; const { includeProtectedContents, includeArchived, includeTrashed, } = useAutorunValue( () => ({ includeProtectedContents: searchOptions.includeProtectedContents, includeArchived: searchOptions.includeArchived, includeTrashed: searchOptions.includeTrashed, }), [searchOptions] ); const [ togglingIncludeProtectedContents, setTogglingIncludeProtectedContents, ] = useState(false); async function toggleIncludeProtectedContents() { setTogglingIncludeProtectedContents(true); try { await searchOptions.toggleIncludeProtectedContents(); } finally { setTogglingIncludeProtectedContents(false); } } const [open, setOpen] = useState(false); const [optionsPanelTop, setOptionsPanelTop] = useState(0); const buttonRef = useRef(); const panelRef = useRef(); function closeOnBlur(event: FocusEvent) { if ( !togglingIncludeProtectedContents && !panelRef.current.contains(event.relatedTarget as Node) ) { setOpen(false); } } return ( { const { height } = buttonRef.current.getBoundingClientRect(); setOptionsPanelTop(height); setOpen((prevOpen) => !prevOpen); }} > Search options

Include protected contents

Include archived notes

Include trashed notes

); } export const SearchOptionsDirective = toDirective(SearchOptions);