fix: replace this.forceUpdate() with useState rerender in MacroViewer

Same root cause as the command list fix: MacroViewer is a Preact
functional component, so this.forceUpdate() throws (this is undefined),
leaving the macro list stale after add/edit/delete. Use a useState
counter to trigger re-renders.
This commit is contained in:
Carles Ballester 2026-06-25 15:39:17 +02:00
parent ecc9c06fd9
commit 0f352bc56c
No known key found for this signature in database

View file

@ -1,5 +1,6 @@
import { Platform } from "obsidian";
import { Fragment, h } from "preact";
import { useState } from "preact/hooks";
import t from "src/l10n";
import CommanderPlugin from "src/main";
import { Macro } from "src/types";
@ -16,6 +17,8 @@ export default function MacroViewer({
plugin,
macros,
}: MacroBuilderProps): h.JSX.Element {
const [, rerender] = useState(0);
const forceUpdate = (): void => rerender((n) => n + 1);
const handleBuilder = (macro: Macro, idx?: number): void => {
const onClose = (updatedMacro: Macro): void => {
macros.splice(
@ -25,7 +28,7 @@ export default function MacroViewer({
);
plugin.saveSettings();
this.forceUpdate();
forceUpdate();
updateMacroCommands(plugin);
modal.close();
};
@ -36,7 +39,7 @@ export default function MacroViewer({
const handleDelete = (idx: number): void => {
macros.splice(idx, 1);
plugin.saveSettings();
this.forceUpdate();
forceUpdate();
updateMacroCommands(plugin);
};