mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 04:16:17 +01:00 
			
		
		
		
	refactor(react): move effects outside conditional
This commit is contained in:
		@@ -1,4 +1,4 @@
 | 
				
			|||||||
import { useEffect, useState } from "preact/hooks";
 | 
					import { useEffect, useState, useCallback } from "preact/hooks";
 | 
				
			||||||
import { t } from "../../services/i18n";
 | 
					import { t } from "../../services/i18n";
 | 
				
			||||||
import Modal from "../react/Modal";
 | 
					import Modal from "../react/Modal";
 | 
				
			||||||
import ReactBasicWidget from "../react/ReactBasicWidget";
 | 
					import ReactBasicWidget from "../react/ReactBasicWidget";
 | 
				
			||||||
@@ -28,26 +28,30 @@ function BulkActionComponent() {
 | 
				
			|||||||
        setShown(true);
 | 
					        setShown(true);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (selectedOrActiveNoteIds && bulkActionNote) {
 | 
					 | 
				
			||||||
    useEffect(() => {
 | 
					    useEffect(() => {
 | 
				
			||||||
 | 
					        if (!selectedOrActiveNoteIds || !bulkActionNote) return;
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        server.post<BulkActionAffectedNotes>("bulk-action/affected-notes", {
 | 
					        server.post<BulkActionAffectedNotes>("bulk-action/affected-notes", {
 | 
				
			||||||
            noteIds: selectedOrActiveNoteIds,
 | 
					            noteIds: selectedOrActiveNoteIds,
 | 
				
			||||||
            includeDescendants
 | 
					            includeDescendants
 | 
				
			||||||
        }).then(({ affectedNoteCount }) => setAffectedNoteCount(affectedNoteCount));
 | 
					        }).then(({ affectedNoteCount }) => setAffectedNoteCount(affectedNoteCount));
 | 
				
			||||||
        }, [ selectedOrActiveNoteIds, includeDescendants ]);
 | 
					    }, [ selectedOrActiveNoteIds, includeDescendants, bulkActionNote ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        function refreshExistingActions() {
 | 
					    const refreshExistingActions = useCallback(() => {
 | 
				
			||||||
            setExistingActions(bulk_action.parseActions(bulkActionNote!));
 | 
					        if (!bulkActionNote) return;
 | 
				
			||||||
        }
 | 
					        setExistingActions(bulk_action.parseActions(bulkActionNote));
 | 
				
			||||||
 | 
					    }, [bulkActionNote]);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    useEffect(() => {
 | 
				
			||||||
 | 
					        refreshExistingActions();
 | 
				
			||||||
 | 
					    }, [refreshExistingActions]);
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        useEffect(() => refreshExistingActions(), []);
 | 
					 | 
				
			||||||
    useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
 | 
					    useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
 | 
				
			||||||
        if (loadResults.getAttributeRows().find((row) =>
 | 
					        if (loadResults.getAttributeRows().find((row) =>
 | 
				
			||||||
            row.type === "label" && row.name === "action" && row.noteId === "_bulkAction")) {
 | 
					            row.type === "label" && row.name === "action" && row.noteId === "_bulkAction")) {
 | 
				
			||||||
                refreshExistingActions();
 | 
					                refreshExistingActions();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }, shown);
 | 
					    }, shown);
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return (
 | 
					    return (
 | 
				
			||||||
        <Modal
 | 
					        <Modal
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,9 +1,8 @@
 | 
				
			|||||||
import { useRef, useState } from "preact/hooks";
 | 
					import { useRef, useState, useEffect } from "preact/hooks";
 | 
				
			||||||
import { t } from "../../services/i18n.js";
 | 
					import { t } from "../../services/i18n.js";
 | 
				
			||||||
import FormCheckbox from "../react/FormCheckbox.js";
 | 
					import FormCheckbox from "../react/FormCheckbox.js";
 | 
				
			||||||
import Modal from "../react/Modal.js";
 | 
					import Modal from "../react/Modal.js";
 | 
				
			||||||
import ReactBasicWidget from "../react/ReactBasicWidget.js";
 | 
					import ReactBasicWidget from "../react/ReactBasicWidget.js";
 | 
				
			||||||
import { useEffect } from "preact/hooks";
 | 
					 | 
				
			||||||
import type { DeleteNotesPreview } from "@triliumnext/commons";
 | 
					import type { DeleteNotesPreview } from "@triliumnext/commons";
 | 
				
			||||||
import server from "../../services/server.js";
 | 
					import server from "../../services/server.js";
 | 
				
			||||||
import froca from "../../services/froca.js";
 | 
					import froca from "../../services/froca.js";
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,20 +12,28 @@ export default function Dropdown({ className, isStatic, children }: DropdownProp
 | 
				
			|||||||
    const dropdownRef = useRef<HTMLDivElement | null>(null);
 | 
					    const dropdownRef = useRef<HTMLDivElement | null>(null);
 | 
				
			||||||
    const triggerRef = useRef<HTMLButtonElement | null>(null);
 | 
					    const triggerRef = useRef<HTMLButtonElement | null>(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (triggerRef?.current) {
 | 
					 | 
				
			||||||
    useEffect(() => {
 | 
					    useEffect(() => {
 | 
				
			||||||
            const dropdown = BootstrapDropdown.getOrCreateInstance(triggerRef.current!);
 | 
					        if (!triggerRef.current) return;
 | 
				
			||||||
            return () => dropdown.dispose();
 | 
					        
 | 
				
			||||||
        });
 | 
					        const dropdown = BootstrapDropdown.getOrCreateInstance(triggerRef.current);
 | 
				
			||||||
    }
 | 
					        return () => dropdown.dispose();
 | 
				
			||||||
 | 
					    }, []); // Add dependency array
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (dropdownRef?.current) {
 | 
					 | 
				
			||||||
    useEffect(() => {
 | 
					    useEffect(() => {
 | 
				
			||||||
            $(dropdownRef.current!).on("hide.bs.dropdown", () => {
 | 
					        if (!dropdownRef.current) return;
 | 
				
			||||||
                console.log("Hide");
 | 
					        
 | 
				
			||||||
            });
 | 
					        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 (
 | 
					    return (
 | 
				
			||||||
        <div ref={dropdownRef} class="dropdown" style={{ display: "flex" }}>
 | 
					        <div ref={dropdownRef} class="dropdown" style={{ display: "flex" }}>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user