refactor(bulk_action): add basic type safety for client

This commit is contained in:
Elian Doran
2025-07-19 12:54:16 +03:00
parent 0d3de92890
commit 409638151c
4 changed files with 51 additions and 49 deletions

View File

@@ -16,6 +16,7 @@ import RenameNoteBulkAction from "../widgets/bulk_actions/note/rename_note.js";
import { t } from "./i18n.js";
import type FNote from "../entities/fnote.js";
import toast from "./toast.js";
import { BulkAction } from "@triliumnext/commons";
const ACTION_GROUPS = [
{
@@ -90,7 +91,7 @@ function parseActions(note: FNote) {
.filter((action) => !!action);
}
export async function executeBulkActions(parentNoteId: string, actions: {}[]) {
export async function executeBulkActions(parentNoteId: string, actions: BulkAction[]) {
await server.post("bulk-action/execute", {
noteIds: [ parentNoteId ],
includeDescendants: true,

View File

@@ -5,52 +5,7 @@ import branchService from "./branches.js";
import { randomString } from "./utils.js";
import eraseService from "./erase.js";
import type BNote from "../becca/entities/bnote.js";
type ActionHandlers = {
addLabel: {
labelName: string;
labelValue?: string;
},
addRelation: {
relationName: string;
targetNoteId: string;
},
deleteNote: {},
deleteRevisions: {},
deleteLabel: {
labelName: string;
},
deleteRelation: {
relationName: string;
},
renameNote: {
newTitle: string;
},
renameLabel: {
oldLabelName: string;
newLabelName: string;
},
renameRelation: {
oldRelationName: string;
newRelationName: string;
},
updateLabelValue: {
labelName: string;
labelValue: string;
},
updateRelationTarget: {
relationName: string;
targetNoteId: string;
},
moveNote: {
targetParentNoteId: string;
},
executeScript: {
script: string;
}
};
type BulkActionData<T extends keyof ActionHandlers> = ActionHandlers[T] & { name: T };
import { ActionHandlers, BulkAction, BulkActionData } from "@triliumnext/commons";
type ActionHandler<T> = (action: T, note: BNote) => void;
@@ -58,8 +13,6 @@ type ActionHandlerMap = {
[K in keyof ActionHandlers]: ActionHandler<BulkActionData<K>>;
};
export type BulkAction = BulkActionData<keyof ActionHandlers>;
const ACTION_HANDLERS: ActionHandlerMap = {
addLabel: (action, note) => {
note.addLabel(action.labelName, action.labelValue);

View File

@@ -5,3 +5,4 @@ export * from "./lib/hidden_subtree.js";
export * from "./lib/rows.js";
export * from "./lib/test-utils.js";
export * from "./lib/mime_type.js";
export * from "./lib/bulk_actions.js";

View File

@@ -0,0 +1,47 @@
export type ActionHandlers = {
addLabel: {
labelName: string;
labelValue?: string;
},
addRelation: {
relationName: string;
targetNoteId: string;
},
deleteNote: {},
deleteRevisions: {},
deleteLabel: {
labelName: string;
},
deleteRelation: {
relationName: string;
},
renameNote: {
newTitle: string;
},
renameLabel: {
oldLabelName: string;
newLabelName: string;
},
renameRelation: {
oldRelationName: string;
newRelationName: string;
},
updateLabelValue: {
labelName: string;
labelValue: string;
},
updateRelationTarget: {
relationName: string;
targetNoteId: string;
},
moveNote: {
targetParentNoteId: string;
},
executeScript: {
script: string;
}
};
export type BulkActionData<T extends keyof ActionHandlers> = ActionHandlers[T] & { name: T };
export type BulkAction = BulkActionData<keyof ActionHandlers>;