fix: replace nested ternaries and remove type casts per review

- useNotificationForm: replace nested ternary with if/else if/else block
- TelegramProvider: type error as unknown and access properties safely
  via instanceof checks instead of casting to HTTPError
This commit is contained in:
LeC-D committed 2026-03-25 14:03:49 -04:00
1 parent d1f1d31ea2
commit fe242b1977
2 files changed
+33 -26

No files matched your search

+24 -21
View File
@@ -8,27 +8,30 @@ interface UseNotificationFormOptions {
export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => { export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => {
return useMemo(() => { return useMemo(() => {
const defaults = let defaults;
data?.type === "matrix"
? { if (data?.type === "matrix") {
type: "matrix" as const, defaults = {
notificationName: data.notificationName || "", type: "matrix" as const,
homeserverUrl: data.homeserverUrl || "", notificationName: data.notificationName || "",
roomId: data.roomId || "", homeserverUrl: data.homeserverUrl || "",
accessToken: data.accessToken || "", roomId: data.roomId || "",
} accessToken: data.accessToken || "",
: data?.type === "telegram" };
? { } else if (data?.type === "telegram") {
type: "telegram" as const, defaults = {
notificationName: data.notificationName || "", type: "telegram" as const,
address: data.address || "", notificationName: data.notificationName || "",
accessToken: data.accessToken || "", address: data.address || "",
} accessToken: data.accessToken || "",
: { };
type: (data?.type || "email") as Exclude<Notification["type"], "matrix" | "telegram">, } else {
notificationName: data?.notificationName || "", defaults = {
address: data?.address || "", type: (data?.type || "email") as Exclude<Notification["type"], "matrix" | "telegram">,
}; notificationName: data?.notificationName || "",
address: data?.address || "",
};
}
return { schema: notificationSchema, defaults }; return { schema: notificationSchema, defaults };
}, [data]); }, [data]);
@@ -3,7 +3,7 @@ import type { Notification } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js"; import { INotificationProvider } from "@/service/index.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import got, { HTTPError } from "got"; import got from "got";
import { ILogger } from "@/utils/logger.js"; import { ILogger } from "@/utils/logger.js";
export class TelegramProvider implements INotificationProvider { export class TelegramProvider implements INotificationProvider {
@@ -28,12 +28,14 @@ export class TelegramProvider implements INotificationProvider {
}); });
return true; return true;
} catch (error) { } catch (error) {
const err = error as HTTPError; const errMsg = error instanceof Error ? error.message : "unknown error";
const errStack = error instanceof Error ? error.stack : undefined;
this.logger.warn({ this.logger.warn({
message: "Telegram test alert failed", message: "Telegram test alert failed",
service: SERVICE_NAME, service: SERVICE_NAME,
method: "sendTestAlert", method: "sendTestAlert",
stack: err?.stack, stack: errStack,
error: errMsg,
}); });
return false; return false;
} }
@@ -61,12 +63,14 @@ export class TelegramProvider implements INotificationProvider {
}); });
return true; return true;
} catch (error) { } catch (error) {
const err = error as HTTPError; const errMsg = error instanceof Error ? error.message : "unknown error";
const errStack = error instanceof Error ? error.stack : undefined;
this.logger.warn({ this.logger.warn({
message: "Telegram alert failed", message: "Telegram alert failed",
service: SERVICE_NAME, service: SERVICE_NAME,
method: "sendMessage", method: "sendMessage",
stack: err?.stack, stack: errStack,
error: errMsg,
}); });
return false; return false;
} }