* feat: hide note contents if the protection expires when the protected note is open and wasn't edited for a while * feat: handle session expiration for opened protected note for both plain advanced editors * fix: if after canceling session expiry modal only one unprotected note stays selected, show its contents in the editor * refactor: handle session expiration for opened protected note (move the logic to web client) * feat: handle the case of selecting "Don't remember" option in session expiry dialog * test (WIP): add unit tests for protecting opened note after the session has expired * test: add remaining unit tests * refactor: move the opened note protection logic to "editor_view" * refactor: reviewer comments - don't rely on user signed-in/out status to require authentication for protected note - remove unnecessary async/awaits - better wording on ui * refactor: reviewer's comments: - use snjs method to check if "Don't remember" option is selected in authentication modal - move the constant to snjs - fix eslint error * refactor: avoid `any` type for `appEvent` payload * test: add unit tests * chore: update function name * refactor: use simpler protection session event types * refactor: protected access terminology * refactor: start counting idle timer after every edit (instead of counting by interval in spite of edits) * test: unit tests * style: don't give extra brightness to the "View Note"/"Authenticate" button on hover/focus * chore: bump snjs version Co-authored-by: Mo Bitar <me@bitar.io>
119 lines
3.3 KiB
TypeScript
119 lines
3.3 KiB
TypeScript
import { AppState } from '@/ui_models/app_state';
|
|
import { Icon } from './Icon';
|
|
import { toDirective, useCloseOnBlur } from './utils';
|
|
import { useEffect, 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 { Switch } from './Switch';
|
|
import { observer } from 'mobx-react-lite';
|
|
|
|
type Props = {
|
|
appState: AppState;
|
|
application: WebApplication;
|
|
};
|
|
|
|
const SearchOptions = observer(({ appState }: Props) => {
|
|
const { searchOptions } = appState;
|
|
|
|
const {
|
|
includeProtectedContents,
|
|
includeArchived,
|
|
includeTrashed,
|
|
} = searchOptions;
|
|
|
|
const [open, setOpen] = useState(false);
|
|
const [position, setPosition] = useState({
|
|
top: 0,
|
|
right: 0,
|
|
});
|
|
const [maxWidth, setMaxWidth] = useState<number | 'auto'>('auto');
|
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
const panelRef = useRef<HTMLDivElement>(null);
|
|
const [closeOnBlur, setLockCloseOnBlur] = useCloseOnBlur(panelRef as any, setOpen);
|
|
|
|
async function toggleIncludeProtectedContents() {
|
|
setLockCloseOnBlur(true);
|
|
try {
|
|
await searchOptions.toggleIncludeProtectedContents();
|
|
} finally {
|
|
setLockCloseOnBlur(false);
|
|
}
|
|
}
|
|
|
|
const updateWidthAndPosition = () => {
|
|
const rect = buttonRef.current!.getBoundingClientRect();
|
|
setMaxWidth(rect.right - 16);
|
|
setPosition({
|
|
top: rect.bottom,
|
|
right: document.body.clientWidth - rect.right,
|
|
});
|
|
};
|
|
|
|
useEffect(() => {
|
|
window.addEventListener('resize', updateWidthAndPosition);
|
|
return () => {
|
|
window.removeEventListener('resize', updateWidthAndPosition);
|
|
};
|
|
}, []);
|
|
|
|
return (
|
|
<Disclosure
|
|
open={open}
|
|
onChange={() => {
|
|
updateWidthAndPosition();
|
|
setOpen(!open);
|
|
}}
|
|
>
|
|
<DisclosureButton
|
|
ref={buttonRef}
|
|
onBlur={closeOnBlur}
|
|
className="border-0 p-0 bg-transparent cursor-pointer color-neutral hover:color-info"
|
|
>
|
|
<VisuallyHidden>Search options</VisuallyHidden>
|
|
<Icon type="tune" className="block" />
|
|
</DisclosureButton>
|
|
<DisclosurePanel
|
|
ref={panelRef}
|
|
style={{
|
|
...position,
|
|
maxWidth,
|
|
}}
|
|
className="sn-dropdown sn-dropdown--animated w-80 fixed grid gap-2 py-2"
|
|
onBlur={closeOnBlur}
|
|
>
|
|
<Switch
|
|
className="h-10"
|
|
checked={includeProtectedContents}
|
|
onChange={toggleIncludeProtectedContents}
|
|
onBlur={closeOnBlur}
|
|
>
|
|
<p className="capitalize">Include protected contents</p>
|
|
</Switch>
|
|
<Switch
|
|
className="h-10"
|
|
checked={includeArchived}
|
|
onChange={searchOptions.toggleIncludeArchived}
|
|
onBlur={closeOnBlur}
|
|
>
|
|
<p className="capitalize">Include archived notes</p>
|
|
</Switch>
|
|
<Switch
|
|
className="h-10"
|
|
checked={includeTrashed}
|
|
onChange={searchOptions.toggleIncludeTrashed}
|
|
onBlur={closeOnBlur}
|
|
>
|
|
<p className="capitalize">Include trashed notes</p>
|
|
</Switch>
|
|
</DisclosurePanel>
|
|
</Disclosure>
|
|
);
|
|
});
|
|
|
|
export const SearchOptionsDirective = toDirective<Props>(SearchOptions);
|