Skip to content

Build your personal knowledge base with Trilium Notes

custom task states: use localized for the validator

Adorian Dorana8e09603dcc5
diff --git a/apps/client/src/services/task_states.ts b/apps/client/src/services/task_states.ts
index 3c156f791e..1d2fa7b42d 100644
--- a/apps/client/src/services/task_states.ts
+++ b/apps/client/src/services/task_states.ts
@@ -12,6 +12,7 @@ import {
import appContext from "../components/app_context.js";
import froca from "./froca.js";
+import { t } from "./i18n.js";
import { showError } from "./toast.js";
let validationReported = false;
@@ -23,7 +24,8 @@ function reportValidationErrors(errors: TaskStateValidationError[]) {
}
validationReported = true;
for (const error of errors) {
- showError(error.message);
+ const reason = t(`text-editor.validation-errors.${error.reason}`);
+ showError(t("text-editor.task-state-dropped", {title: error.title, id: error.id, reason}));
}
}
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index a61dd6636a..0e24ccf937 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2660,6 +2660,16 @@
"text-editor": {
"checkbox-tooltip": "Right click for more task states. Press Ctrl+Shift+Enter to cycle between states.",
"edit-states-tooltip": "Edit task states",
- "unknown-task-state": "Undefined custom task state"
+ "unknown-task-state": "Undefined custom task state",
+ "task-state-dropped": "Dropped custom task state definition \"{{title}}\" ({{id}}) due to: {{reason}}",
+ "validation-errors": {
+ "undefined-name": "undefined stateName",
+ "invalid-name": "invalid stateName",
+ "duplicate-name": "duplicate stateName",
+ "missing-title": "missing title",
+ "missing-icon": "missing icon",
+ "invalid-symbol": "the markdown symbol should be one character, excepting \"[\", \" \", \"X\", and \"]\"",
+ "duplicate-symbol": "duplicate markdown symbol"
+ }
}
}
diff --git a/packages/commons/src/lib/task_states.spec.ts b/packages/commons/src/lib/task_states.spec.ts
index f4c1a58838..a45b033600 100644
--- a/packages/commons/src/lib/task_states.spec.ts
+++ b/packages/commons/src/lib/task_states.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { DONE_TASK_STATE, NONE_TASK_STATE, type TaskStateDef, validateTaskStates } from "./task_states.js";
+import { DONE_TASK_STATE, NONE_TASK_STATE, type TaskStateDef, type TaskStateValidationReason, validateTaskStates } from "./task_states.js";
function customState(overrides: Partial<TaskStateDef>): TaskStateDef {
return {
@@ -15,8 +15,6 @@ function customState(overrides: Partial<TaskStateDef>): TaskStateDef {
};
}
-const SYMBOL_REASON = `the markdown symbol should be one character, excepting "[", " ", "X", and "]"`;
-
describe("validateTaskStates", () => {
it("keeps anchors and valid custom states", () => {
const states = [NONE_TASK_STATE, customState({ id: "a", name: "doing" }), DONE_TASK_STATE];
@@ -27,18 +25,18 @@ describe("validateTaskStates", () => {
});
it("drops invalid custom states with the matching reason", () => {
- const scenarios: Array<[Partial<TaskStateDef>, string]> = [
- [{ name: "" }, "undefined stateName"],
- [{ name: "in progress" }, "invalid stateName"],
- [{ name: "<b>" }, "invalid stateName"],
- [{ name: "done" }, "duplicate stateName"],
- [{ name: "none" }, "duplicate stateName"],
- [{ title: "" }, "missing title"],
- [{ icon: "" }, "missing icon"],
- [{ markdownSymbol: "//" }, SYMBOL_REASON],
- [{ markdownSymbol: " " }, SYMBOL_REASON],
- [{ markdownSymbol: "[" }, SYMBOL_REASON],
- [{ markdownSymbol: "X" }, SYMBOL_REASON]
+ const scenarios: Array<[Partial<TaskStateDef>, TaskStateValidationReason]> = [
+ [{ name: "" }, "undefined-name"],
+ [{ name: "in progress" }, "invalid-name"],
+ [{ name: "<b>" }, "invalid-name"],
+ [{ name: "done" }, "duplicate-name"],
+ [{ name: "none" }, "duplicate-name"],
+ [{ title: "" }, "missing-title"],
+ [{ icon: "" }, "missing-icon"],
+ [{ markdownSymbol: "//" }, "invalid-symbol"],
+ [{ markdownSymbol: " " }, "invalid-symbol"],
+ [{ markdownSymbol: "[" }, "invalid-symbol"],
+ [{ markdownSymbol: "X" }, "invalid-symbol"]
];
for (const [overrides, reason] of scenarios) {
@@ -57,12 +55,12 @@ describe("validateTaskStates", () => {
const { valid, errors } = validateTaskStates([first, dupName, dupSymbol]);
expect(valid).toEqual([first]);
- expect(errors.map((e) => e.reason)).toEqual(["duplicate stateName", "duplicate markdown symbol"]);
+ expect(errors.map((e) => e.reason)).toEqual(["duplicate-name", "duplicate-symbol"]);
});
- it("builds the drop message", () => {
+ it("reports the dropped state's id and title", () => {
const { errors } = validateTaskStates([customState({ id: "abc", title: "My State", name: "" })]);
- expect(errors[0].message).toBe(`Dropped custom task state definition "My State" (abc) due to: undefined stateName`);
+ expect(errors[0]).toEqual({ id: "abc", title: "My State", reason: "undefined-name" });
});
it("allows a custom state with no markdown symbol", () => {
diff --git a/packages/commons/src/lib/task_states.ts b/packages/commons/src/lib/task_states.ts
index cda4d6b5b9..ce6ec7af52 100644
--- a/packages/commons/src/lib/task_states.ts
+++ b/packages/commons/src/lib/task_states.ts
@@ -74,16 +74,24 @@ export const DEFAULT_TASK_STATES: TaskStateDef[] = [
DEFAULT_CUSTOM_TASK_STATES[2]
];
+/** Stable identifier of why a custom task state failed validation. */
+export type TaskStateValidationReason =
+ | "undefined-name"
+ | "invalid-name"
+ | "duplicate-name"
+ | "missing-title"
+ | "missing-icon"
+ | "invalid-symbol"
+ | "duplicate-symbol";
+
/** A custom task state that failed validation and was dropped. */
export interface TaskStateValidationError {
/** The dropped state note's id. */
id: string;
/** The dropped state's title (falls back to its name). */
title: string;
- /** Short machine-ish reason. */
- reason: string;
- /** Ready-to-display message. */
- message: string;
+ /** Stable reason key — the caller localizes it for display. */
+ reason: TaskStateValidationReason;
}
export interface TaskStateValidationResult {
@@ -102,28 +110,28 @@ function checkCustomState(
state: TaskStateDef,
seenNames: Set<string>,
seenSymbols: Set<string>
-): string | null {
+): TaskStateValidationReason | null {
if (!state.name) {
- return "undefined stateName";
+ return "undefined-name";
}
if (!VALID_STATE_NAME_PATTERN.test(state.name)) {
- return "invalid stateName";
+ return "invalid-name";
}
if (seenNames.has(state.name)) {
- return "duplicate stateName";
+ return "duplicate-name";
}
if (!state.title) {
- return "missing title";
+ return "missing-title";
}
if (!state.icon) {
- return "missing icon";
+ return "missing-icon";
}
if (state.markdownSymbol) {
if (state.markdownSymbol.length !== 1 || FORBIDDEN_MARKDOWN_SYMBOLS.has(state.markdownSymbol)) {
- return `the markdown symbol should be one character, excepting "[", " ", "X", and "]"`;
+ return "invalid-symbol";
}
if (seenSymbols.has(state.markdownSymbol)) {
- return "duplicate markdown symbol";
+ return "duplicate-symbol";
}
}
return null;
@@ -148,13 +156,10 @@ export function validateTaskStates(states: TaskStateDef[]): TaskStateValidationR
const reason = checkCustomState(state, seenNames, seenSymbols);
if (reason) {
- const title = state.title || state.name || "";
- const id = state.id ?? "";
errors.push({
- id,
- title,
- reason,
- message: `Dropped custom task state definition "${title}" (${id}) due to: ${reason}`
+ id: state.id ?? "",
+ title: state.title || state.name || "",
+ reason
});
continue;
}