mirror of
https://github.com/zadam/trilium.git
synced 2025-10-21 23:59:02 +02:00
refactor(react): move effects outside conditional
This commit is contained in:
parent
e659266d62
commit
a6e56be55a
@ -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", {
|
|
||||||
noteIds: selectedOrActiveNoteIds,
|
|
||||||
includeDescendants
|
|
||||||
}).then(({ affectedNoteCount }) => setAffectedNoteCount(affectedNoteCount));
|
|
||||||
}, [ selectedOrActiveNoteIds, includeDescendants ]);
|
|
||||||
|
|
||||||
function refreshExistingActions() {
|
|
||||||
setExistingActions(bulk_action.parseActions(bulkActionNote!));
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => refreshExistingActions(), []);
|
server.post<BulkActionAffectedNotes>("bulk-action/affected-notes", {
|
||||||
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
|
noteIds: selectedOrActiveNoteIds,
|
||||||
if (loadResults.getAttributeRows().find((row) =>
|
includeDescendants
|
||||||
row.type === "label" && row.name === "action" && row.noteId === "_bulkAction")) {
|
}).then(({ affectedNoteCount }) => setAffectedNoteCount(affectedNoteCount));
|
||||||
refreshExistingActions();
|
}, [ selectedOrActiveNoteIds, includeDescendants, bulkActionNote ]);
|
||||||
}
|
|
||||||
}, shown);
|
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 (
|
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(() => {
|
if (!triggerRef.current) return;
|
||||||
const dropdown = BootstrapDropdown.getOrCreateInstance(triggerRef.current!);
|
|
||||||
return () => dropdown.dispose();
|
const dropdown = BootstrapDropdown.getOrCreateInstance(triggerRef.current);
|
||||||
});
|
return () => dropdown.dispose();
|
||||||
}
|
}, []); // Add dependency array
|
||||||
|
|
||||||
if (dropdownRef?.current) {
|
useEffect(() => {
|
||||||
useEffect(() => {
|
if (!dropdownRef.current) return;
|
||||||
$(dropdownRef.current!).on("hide.bs.dropdown", () => {
|
|
||||||
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" }}>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user