mirror of
https://github.com/zadam/trilium.git
synced 2025-10-21 07:38:53 +02:00
fix(react/dialogs): events triggering even when modal is hidden
This commit is contained in:
parent
48eebbe2fe
commit
11f6462a31
@ -46,7 +46,7 @@ function BulkActionComponent() {
|
|||||||
row.type === "label" && row.name === "action" && row.noteId === "_bulkAction")) {
|
row.type === "label" && row.name === "action" && row.noteId === "_bulkAction")) {
|
||||||
refreshExistingActions();
|
refreshExistingActions();
|
||||||
}
|
}
|
||||||
});
|
}, shown);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
|
import { useContext, useEffect, useRef } from "preact/hooks";
|
||||||
import { EventData, EventNames } from "../../components/app_context";
|
import { EventData, EventNames } from "../../components/app_context";
|
||||||
import { ParentComponent } from "./ReactBasicWidget";
|
import { ParentComponent } from "./ReactBasicWidget";
|
||||||
import SpacedUpdate from "../../services/spaced_update";
|
import SpacedUpdate from "../../services/spaced_update";
|
||||||
|
|
||||||
export default function useTriliumEvent<T extends EventNames>(eventName: T, handler: (data: EventData<T>) => void) {
|
/**
|
||||||
|
* Allows a React component to react to Trilium events (e.g. `entitiesReloaded`). When the desired event is triggered, the handler is invoked with the event parameters.
|
||||||
|
*
|
||||||
|
* Under the hood, it works by altering the parent (Trilium) component of the React element to introduce the corresponding event.
|
||||||
|
*
|
||||||
|
* @param eventName the name of the Trilium event to listen for.
|
||||||
|
* @param handler the handler to be invoked when the event is triggered.
|
||||||
|
* @param enabled determines whether the event should be listened to or not. Useful to conditionally limit the listener based on a state (e.g. a modal being displayed).
|
||||||
|
*/
|
||||||
|
export default function useTriliumEvent<T extends EventNames>(eventName: T, handler: (data: EventData<T>) => void, enabled = true) {
|
||||||
const parentWidget = useContext(ParentComponent);
|
const parentWidget = useContext(ParentComponent);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!parentWidget) {
|
if (!parentWidget || !enabled) {
|
||||||
console.warn("useTriliumEvent: No widget context found");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,11 +33,11 @@ export default function useTriliumEvent<T extends EventNames>(eventName: T, hand
|
|||||||
handler(data);
|
handler(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cleanup: restore original handler on unmount
|
// Cleanup: restore original handler on unmount or when disabled
|
||||||
return () => {
|
return () => {
|
||||||
parentWidget[handlerName] = originalHandler;
|
parentWidget[handlerName] = originalHandler;
|
||||||
};
|
};
|
||||||
}, [parentWidget]);
|
}, [parentWidget, enabled, eventName, handler]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useSpacedUpdate(callback: () => Promise<void>, interval = 1000) {
|
export function useSpacedUpdate(callback: () => Promise<void>, interval = 1000) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user