Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions web/src/lib/__tests__/handleUiAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it, vi } from "vitest";
import { handleUiAction } from "../handleUiAction";

describe("handleUiAction", () => {
it("surfaces a rejected action without rethrowing it", async () => {
const setError = vi.fn();

await expect(
handleUiAction(() => Promise.reject(new Error("boom")), setError)
).resolves.toBeUndefined();

expect(setError).toHaveBeenCalledOnce();
expect(setError).toHaveBeenCalledWith("boom");
});

it("converts non-Error rejections to a readable message", async () => {
const setError = vi.fn();

await handleUiAction(() => Promise.reject("offline"), setError);

expect(setError).toHaveBeenCalledWith("offline");
});
});
10 changes: 10 additions & 0 deletions web/src/lib/handleUiAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export async function handleUiAction(
action: () => Promise<unknown>,
setError: (message: string) => void
): Promise<void> {
try {
await action();
} catch (error) {
setError(error instanceof Error ? error.message : String(error));
}
}
6 changes: 5 additions & 1 deletion web/src/pages/Events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
WebhookRegistrationData,
} from "../lib/api";
import { useTranslation } from "react-i18next";
import { handleUiAction } from "../lib/handleUiAction";

function fmtTime(value: string | number | null): string {
if (value === null) return "-";
Expand Down Expand Up @@ -419,7 +420,10 @@ export function Events() {
className="btn-ghost btn-sm"
onClick={() => {
setFilterType(type);
api.eventsList({ type, limit: 100 }).then((result) => setEvents(result.data.events));
void handleUiAction(async () => {
const result = await api.eventsList({ type, limit: 100 });
setEvents(result.data.events);
}, setError);
}}
>
{type}
Expand Down
19 changes: 13 additions & 6 deletions web/src/pages/Network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "../lib/api";
import { toast } from "../lib/toast-store";
import { useTranslation } from "react-i18next";
import { handleUiAction } from "../lib/handleUiAction";

const TRUST_LEVELS: NetworkTrustLevel[] = ["trusted", "verified", "untrusted"];
const AGENT_STATUSES: NetworkAgentStatus[] = ["available", "busy", "offline", "degraded"];
Expand Down Expand Up @@ -327,18 +328,24 @@ export function Network() {
};

const updateTrust = async (agent: NetworkAgentData, trustLevel: NetworkTrustLevel) => {
await api.updateNetworkAgentTrust(agent.id, { trustLevel });
await load();
await handleUiAction(async () => {
await api.updateNetworkAgentTrust(agent.id, { trustLevel });
await load();
}, setLastError);
};

const toggleBlocked = async (agent: NetworkAgentData) => {
await api.updateNetworkAgentTrust(agent.id, { blocked: !agent.blocked });
await load();
await handleUiAction(async () => {
await api.updateNetworkAgentTrust(agent.id, { blocked: !agent.blocked });
await load();
}, setLastError);
};

const removeAgent = async (agent: NetworkAgentData) => {
await api.removeNetworkAgent(agent.id);
await load();
await handleUiAction(async () => {
await api.removeNetworkAgent(agent.id);
await load();
}, setLastError);
};

const delegateTask = async () => {
Expand Down
Loading