mirror of https://github.com/jetkvm/kvm.git
use notifications component and handle jsonrpc errors
This commit is contained in:
parent
2ba8e1981b
commit
452e7827c3
|
@ -750,7 +750,7 @@ export const useMacrosStore = create<MacrosState>((set, get) => ({
|
||||||
const { sendFn } = get();
|
const { sendFn } = get();
|
||||||
if (!sendFn) {
|
if (!sendFn) {
|
||||||
console.warn("JSON-RPC send function not available.");
|
console.warn("JSON-RPC send function not available.");
|
||||||
return;
|
throw new Error("JSON-RPC send function not available");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (macros.length > MAX_TOTAL_MACROS) {
|
if (macros.length > MAX_TOTAL_MACROS) {
|
||||||
|
@ -781,22 +781,25 @@ export const useMacrosStore = create<MacrosState>((set, get) => ({
|
||||||
sortOrder: macro.sortOrder !== undefined ? macro.sortOrder : index
|
sortOrder: macro.sortOrder !== undefined ? macro.sortOrder : index
|
||||||
}));
|
}));
|
||||||
|
|
||||||
set({ macros: macrosWithSortOrder });
|
const response = await new Promise<JsonRpcResponse>((resolve) => {
|
||||||
|
|
||||||
await new Promise<void>((resolve, reject) => {
|
|
||||||
sendFn("setKeyboardMacros", { params: { macros: macrosWithSortOrder } }, (response) => {
|
sendFn("setKeyboardMacros", { params: { macros: macrosWithSortOrder } }, (response) => {
|
||||||
if (response.error) {
|
resolve(response);
|
||||||
console.error("Error saving macros:", response.error);
|
|
||||||
reject(new Error(response.error.message));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (response.error) {
|
||||||
|
console.error("Error saving macros:", response.error);
|
||||||
|
const errorMessage = typeof response.error.data === 'string'
|
||||||
|
? response.error.data
|
||||||
|
: response.error.message || "Failed to save macros";
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only update the store if the request was successful
|
||||||
|
set({ macros: macrosWithSortOrder });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to save macros:", error);
|
console.error("Failed to save macros:", error);
|
||||||
get().loadMacros();
|
throw error;
|
||||||
} finally {
|
} finally {
|
||||||
set({ loading: false });
|
set({ loading: false });
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { SettingsPageHeader } from "../components/SettingsPageheader";
|
||||||
import { Button } from "../components/Button";
|
import { Button } from "../components/Button";
|
||||||
import { keys, modifiers } from "../keyboardMappings";
|
import { keys, modifiers } from "../keyboardMappings";
|
||||||
import { useJsonRpc } from "../hooks/useJsonRpc";
|
import { useJsonRpc } from "../hooks/useJsonRpc";
|
||||||
|
import notifications from "../notifications";
|
||||||
|
|
||||||
const DEFAULT_DELAY = 50;
|
const DEFAULT_DELAY = 50;
|
||||||
|
|
||||||
|
@ -660,10 +661,13 @@ export default function SettingsMacrosRoute() {
|
||||||
await saveMacros(normalizeSortOrders([...macros, macro]));
|
await saveMacros(normalizeSortOrders([...macros, macro]));
|
||||||
resetNewMacro();
|
resetNewMacro();
|
||||||
setShowAddMacro(false);
|
setShowAddMacro(false);
|
||||||
|
notifications.success(`Macro "${macro.name}" created successfully`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
|
notifications.error(`Failed to create macro: ${error.message}`);
|
||||||
showTemporaryError(error.message);
|
showTemporaryError(error.message);
|
||||||
} else {
|
} else {
|
||||||
|
notifications.error("Failed to create macro");
|
||||||
showTemporaryError("Failed to save macro");
|
showTemporaryError("Failed to save macro");
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -703,7 +707,19 @@ export default function SettingsMacrosRoute() {
|
||||||
const draggedItem = macroCopy.splice(dragItem.current, 1)[0];
|
const draggedItem = macroCopy.splice(dragItem.current, 1)[0];
|
||||||
macroCopy.splice(dragOverItem.current, 0, draggedItem);
|
macroCopy.splice(dragOverItem.current, 0, draggedItem);
|
||||||
const updatedMacros = normalizeSortOrders(macroCopy);
|
const updatedMacros = normalizeSortOrders(macroCopy);
|
||||||
await saveMacros(updatedMacros);
|
|
||||||
|
try {
|
||||||
|
await saveMacros(updatedMacros);
|
||||||
|
notifications.success("Macro order updated successfully");
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
notifications.error(`Failed to reorder macros: ${error.message}`);
|
||||||
|
showTemporaryError(error.message);
|
||||||
|
} else {
|
||||||
|
notifications.error("Failed to reorder macros");
|
||||||
|
showTemporaryError("Failed to save reordered macros");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const allItems = document.querySelectorAll('[data-macro-item]');
|
const allItems = document.querySelectorAll('[data-macro-item]');
|
||||||
allItems.forEach(el => {
|
allItems.forEach(el => {
|
||||||
|
@ -738,10 +754,13 @@ export default function SettingsMacrosRoute() {
|
||||||
await saveMacros(normalizeSortOrders(newMacros));
|
await saveMacros(normalizeSortOrders(newMacros));
|
||||||
setEditingMacro(null);
|
setEditingMacro(null);
|
||||||
clearErrors();
|
clearErrors();
|
||||||
|
notifications.success(`Macro "${editingMacro.name}" updated successfully`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
|
notifications.error(`Failed to update macro: ${error.message}`);
|
||||||
showTemporaryError(error.message);
|
showTemporaryError(error.message);
|
||||||
} else {
|
} else {
|
||||||
|
notifications.error("Failed to update macro");
|
||||||
showTemporaryError("Failed to update macro");
|
showTemporaryError("Failed to update macro");
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -765,6 +784,9 @@ export default function SettingsMacrosRoute() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDeleteMacro = async (id: string) => {
|
const handleDeleteMacro = async (id: string) => {
|
||||||
|
const macroToBeDeleted = macros.find(m => m.id === id);
|
||||||
|
if (!macroToBeDeleted) return;
|
||||||
|
|
||||||
setIsDeleting(true);
|
setIsDeleting(true);
|
||||||
try {
|
try {
|
||||||
const updatedMacros = normalizeSortOrders(macros.filter(macro => macro.id !== id));
|
const updatedMacros = normalizeSortOrders(macros.filter(macro => macro.id !== id));
|
||||||
|
@ -772,10 +794,14 @@ export default function SettingsMacrosRoute() {
|
||||||
if (editingMacro?.id === id) {
|
if (editingMacro?.id === id) {
|
||||||
setEditingMacro(null);
|
setEditingMacro(null);
|
||||||
}
|
}
|
||||||
|
setMacroToDelete(null);
|
||||||
|
notifications.success(`Macro "${macroToBeDeleted.name}" deleted successfully`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
|
notifications.error(`Failed to delete macro: ${error.message}`);
|
||||||
showTemporaryError(error.message);
|
showTemporaryError(error.message);
|
||||||
} else {
|
} else {
|
||||||
|
notifications.error("Failed to delete macro");
|
||||||
showTemporaryError("Failed to delete macro");
|
showTemporaryError("Failed to delete macro");
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -783,7 +809,7 @@ export default function SettingsMacrosRoute() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDuplicateMacro = (macro: KeySequence) => {
|
const handleDuplicateMacro = async (macro: KeySequence) => {
|
||||||
if (isMaxMacrosReached) {
|
if (isMaxMacrosReached) {
|
||||||
showTemporaryError(`Maximum of ${MAX_TOTAL_MACROS} macros allowed`);
|
showTemporaryError(`Maximum of ${MAX_TOTAL_MACROS} macros allowed`);
|
||||||
return;
|
return;
|
||||||
|
@ -804,11 +830,14 @@ export default function SettingsMacrosRoute() {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
saveMacros(normalizeSortOrders([...macros, newMacroCopy]));
|
await saveMacros(normalizeSortOrders([...macros, newMacroCopy]));
|
||||||
|
notifications.success(`Macro "${newMacroCopy.name}" duplicated successfully`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
|
notifications.error(`Failed to duplicate macro: ${error.message}`);
|
||||||
showTemporaryError(error.message);
|
showTemporaryError(error.message);
|
||||||
} else {
|
} else {
|
||||||
|
notifications.error("Failed to duplicate macro");
|
||||||
showTemporaryError("Failed to duplicate macro");
|
showTemporaryError("Failed to duplicate macro");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -834,7 +863,18 @@ export default function SettingsMacrosRoute() {
|
||||||
macros,
|
macros,
|
||||||
async (newMacros) => {
|
async (newMacros) => {
|
||||||
const updatedMacros = normalizeSortOrders(newMacros);
|
const updatedMacros = normalizeSortOrders(newMacros);
|
||||||
await saveMacros(updatedMacros);
|
try {
|
||||||
|
await saveMacros(updatedMacros);
|
||||||
|
notifications.success("Macro order updated successfully");
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
notifications.error(`Failed to reorder macros: ${error.message}`);
|
||||||
|
showTemporaryError(error.message);
|
||||||
|
} else {
|
||||||
|
notifications.error("Failed to reorder macros");
|
||||||
|
showTemporaryError("Failed to save reordered macros");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue