fix(views/table): bulk actions sometimes not working

This commit is contained in:
Elian Doran 2025-07-19 12:44:55 +03:00
parent e2c8443778
commit 5d619131ec
No known key found for this signature in database
3 changed files with 20 additions and 15 deletions

View file

@ -1,6 +1,4 @@
import { t } from "i18next";
import attributes from "../../../services/attributes";
import froca from "../../../services/froca";
import server from "../../../services/server";
import toast from "../../../services/toast";
import ws from "../../../services/ws";
@ -36,16 +34,10 @@ export async function deleteColumn(parentNoteId: string, type: "label" | "relati
}
async function executeBulkAction(parentNoteId: string, action: {}) {
const bulkActionNote = await froca.getNote("_bulkAction");
if (!bulkActionNote) {
console.warn("Bulk action note not found");
return;
}
attributes.setLabel("_bulkAction", "action", JSON.stringify(action));
await server.post("bulk-action/execute", {
noteIds: [ parentNoteId ],
includeDescendants: true
includeDescendants: true,
actions: [ action ]
});
await ws.waitForMaxKnownEntityChangeId();

View file

@ -3,7 +3,7 @@ import becca from "../../becca/becca.js";
import bulkActionService from "../../services/bulk_actions.js";
function execute(req: Request) {
const { noteIds, includeDescendants } = req.body;
const { noteIds, includeDescendants, actions } = req.body;
if (!Array.isArray(noteIds)) {
throw new Error("noteIds must be an array");
}
@ -12,7 +12,16 @@ function execute(req: Request) {
const bulkActionNote = becca.getNoteOrThrow("_bulkAction");
bulkActionService.executeActionsFromNote(bulkActionNote, affectedNoteIds);
if (actions && actions.length > 0) {
for (const action of actions) {
if (!action.name) {
throw new Error("Action must have a name");
}
}
bulkActionService.executeActions(actions, affectedNoteIds);
} else {
bulkActionService.executeActionsFromNote(bulkActionNote, affectedNoteIds);
}
}
function getAffectedNoteCount(req: Request) {

View file

@ -50,13 +50,15 @@ type ActionHandlers = {
}
};
type BulkActionData<T extends keyof ActionHandlers> = ActionHandlers[T] & { name: T };
type ActionHandler<T> = (action: T, note: BNote) => void;
type ActionHandlerMap = {
[K in keyof ActionHandlers]: ActionHandler<ActionHandlers[K] & { name: K }>;
[K in keyof ActionHandlers]: ActionHandler<BulkActionData<K>>;
};
export type BulkAction = { name: keyof ActionHandlers } & ActionHandlers[keyof ActionHandlers];
export type BulkAction = BulkActionData<keyof ActionHandlers>;
const ACTION_HANDLERS: ActionHandlerMap = {
addLabel: (action, note) => {
@ -207,7 +209,9 @@ function executeActions(actions: BulkAction[], noteIds: string[] | Set<string>)
try {
log.info(`Applying action handler to note ${resultNote.noteId}: ${JSON.stringify(action)}`);
ACTION_HANDLERS[action.name](action, resultNote);
const handler = ACTION_HANDLERS[action.name];
//@ts-ignore
handler(action as BulkAction, resultNote);
} catch (e: any) {
log.error(`ExecuteScript search action failed with ${e.message}`);
}