fix: menu should remain open when showing trash alert

This commit is contained in:
Antonella Sgarlatta
2021-05-07 15:41:00 -03:00
parent 8a9dc14d81
commit 9906cd2a20
4 changed files with 28 additions and 9 deletions

View File

@@ -2,7 +2,7 @@ import { AppState } from '@/ui_models/app_state';
import { Icon, IconType } from './Icon';
import { Switch } from './Switch';
import { observer } from 'mobx-react-lite';
import { useRef, useState } from 'preact/hooks';
import { useEffect, useRef, useState } from 'preact/hooks';
import {
Disclosure,
DisclosureButton,
@@ -12,16 +12,18 @@ import {
type Props = {
appState: AppState;
closeOnBlur: (event: { relatedTarget: EventTarget | null }) => void;
blurLocked: boolean;
setLockCloseOnBlur: (lock: boolean) => void;
};
export const NotesOptions = observer(
({ appState, closeOnBlur, setLockCloseOnBlur }: Props) => {
({ appState, closeOnBlur, blurLocked, setLockCloseOnBlur }: Props) => {
const [tagsMenuOpen, setTagsMenuOpen] = useState(false);
const [tagsMenuPosition, setTagsMenuPosition] = useState({
top: 0,
right: 0,
});
const [shouldTrashNotes, setShouldTrashNotes] = useState(false);
const notes = Object.values(appState.notes.selectedNotes);
const hidePreviews = !notes.some((note) => !note.hidePreview);
@@ -39,6 +41,23 @@ export const NotesOptions = observer(
'cursor-pointer hover:bg-contrast color-text bg-transparent px-3 ' +
'text-left';
useEffect(() => {
const openTrashAlert = async () => {
if (shouldTrashNotes && blurLocked) {
await appState.notes.setTrashSelectedNotes(!trashed, trashButtonRef);
setShouldTrashNotes(false);
setLockCloseOnBlur(false);
}
};
openTrashAlert();
}, [
appState.notes,
blurLocked,
setLockCloseOnBlur,
shouldTrashNotes,
trashed,
]);
return (
<>
<Switch
@@ -167,12 +186,8 @@ export const NotesOptions = observer(
onBlur={closeOnBlur}
className={`${buttonClass} py-1.5`}
onClick={async () => {
setShouldTrashNotes(true);
setLockCloseOnBlur(true);
await appState.notes.setTrashSelectedNotes(
!trashed,
trashButtonRef
);
setLockCloseOnBlur(false);
}}
>
<Icon type={IconType.Trash} className={iconClass} />