Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): tie placement-mode listener/toast to state, guard drop data

Elian Doranf1fc14e57ec6
diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsx
index 396759df5e..e27d2e99a3 100644
--- a/apps/client/src/widgets/collections/geomap/index.tsx
+++ b/apps/client/src/widgets/collections/geomap/index.tsx
@@ -68,7 +68,15 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
// geoMapCreateChildNote command: embedded maps share no note context (no distinct ntxId), so a
// broadcast command would arm placement mode on every map at once. The button is this command's
// only trigger, so a direct handler keeps it isolated to the clicked map.
- const startNotePlacement = useCallback(() => {
+ const startNotePlacement = useCallback(() => setState(State.NewNote), []);
+
+ // Placement mode (NewNote) is armed by the button. Tying the instruction toast and the global
+ // Escape-to-cancel listener to the state (rather than the click handler) guarantees both are
+ // torn down on cancel, on completion (map click) and on unmount — otherwise the listener leaks
+ // and a fresh one accumulates on every placement cycle.
+ useEffect(() => {
+ if (state !== State.NewNote) return;
+
toast.showPersistent({
icon: "plus",
id: "geo-new-note",
@@ -76,18 +84,18 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
message: t("geo-map.create-child-note-instruction")
});
- setState(State.NewNote);
-
- const globalKeyListener: (this: Window, ev: KeyboardEvent) => void = (e) => {
+ const globalKeyListener = (e: KeyboardEvent) => {
if (e.key === "Escape") {
setState(State.Normal);
-
- window.removeEventListener("keydown", globalKeyListener);
- toast.closePersistent("geo-new-note");
}
};
window.addEventListener("keydown", globalKeyListener);
- }, []);
+
+ return () => {
+ window.removeEventListener("keydown", globalKeyListener);
+ toast.closePersistent("geo-new-note");
+ };
+ }, [ state ]);
useTriliumEvent("deleteFromMap", ({ noteId }) => {
moveMarker(noteId, null);
@@ -95,7 +103,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
const onClick = useCallback(async (e: LeafletMouseEvent) => {
if (state === State.NewNote) {
- toast.closePersistent("geo-new-note");
+ // Leaving NewNote closes the instruction toast via the placement-mode effect cleanup.
await createNewNote(note, e);
setState(State.Normal);
}
@@ -113,7 +121,9 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
includeArchived,
async callback(treeData, e) {
const api = apiRef.current;
- if (!note || !api || isReadOnly) return [];
+ // treeData is non-empty in practice (useNoteTreeDrag drops empty payloads), but guard
+ // explicitly so the treeData[0] access can't throw.
+ if (!note || !api || isReadOnly || !treeData.length) return [];
const { noteId } = treeData[0];