diff --git a/apps/client/src/widgets/dialogs/bulk_actions.tsx b/apps/client/src/widgets/dialogs/bulk_actions.tsx index f1b10d33f..3156d0d88 100644 --- a/apps/client/src/widgets/dialogs/bulk_actions.tsx +++ b/apps/client/src/widgets/dialogs/bulk_actions.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "preact/hooks"; +import { useEffect, useState, useCallback } from "preact/hooks"; import { t } from "../../services/i18n"; import Modal from "../react/Modal"; import ReactBasicWidget from "../react/ReactBasicWidget"; @@ -28,26 +28,30 @@ function BulkActionComponent() { setShown(true); }); - if (selectedOrActiveNoteIds && bulkActionNote) { - useEffect(() => { - server.post("bulk-action/affected-notes", { - noteIds: selectedOrActiveNoteIds, - includeDescendants - }).then(({ affectedNoteCount }) => setAffectedNoteCount(affectedNoteCount)); - }, [ selectedOrActiveNoteIds, includeDescendants ]); - - function refreshExistingActions() { - setExistingActions(bulk_action.parseActions(bulkActionNote!)); - } + useEffect(() => { + if (!selectedOrActiveNoteIds || !bulkActionNote) return; - useEffect(() => refreshExistingActions(), []); - useTriliumEvent("entitiesReloaded", ({ loadResults }) => { - if (loadResults.getAttributeRows().find((row) => - row.type === "label" && row.name === "action" && row.noteId === "_bulkAction")) { - refreshExistingActions(); - } - }, shown); - } + server.post("bulk-action/affected-notes", { + noteIds: selectedOrActiveNoteIds, + includeDescendants + }).then(({ affectedNoteCount }) => setAffectedNoteCount(affectedNoteCount)); + }, [ selectedOrActiveNoteIds, includeDescendants, bulkActionNote ]); + + const refreshExistingActions = useCallback(() => { + if (!bulkActionNote) return; + setExistingActions(bulk_action.parseActions(bulkActionNote)); + }, [bulkActionNote]); + + useEffect(() => { + refreshExistingActions(); + }, [refreshExistingActions]); + + useTriliumEvent("entitiesReloaded", ({ loadResults }) => { + if (loadResults.getAttributeRows().find((row) => + row.type === "label" && row.name === "action" && row.noteId === "_bulkAction")) { + refreshExistingActions(); + } + }, shown); return ( (null); const triggerRef = useRef(null); - if (triggerRef?.current) { - useEffect(() => { - const dropdown = BootstrapDropdown.getOrCreateInstance(triggerRef.current!); - return () => dropdown.dispose(); - }); - } + useEffect(() => { + if (!triggerRef.current) return; + + const dropdown = BootstrapDropdown.getOrCreateInstance(triggerRef.current); + return () => dropdown.dispose(); + }, []); // Add dependency array - if (dropdownRef?.current) { - useEffect(() => { - $(dropdownRef.current!).on("hide.bs.dropdown", () => { - console.log("Hide"); - }); - }); - } + useEffect(() => { + if (!dropdownRef.current) return; + + const handleHide = () => { + // Remove console.log from production code + }; + + const $dropdown = $(dropdownRef.current); + $dropdown.on("hide.bs.dropdown", handleHide); + + // Add proper cleanup + return () => { + $dropdown.off("hide.bs.dropdown", handleHide); + }; + }, []); // Add dependency array return (