From 63cd4905f947e6fc9ddbe1d8488872cf86e59ff5 Mon Sep 17 00:00:00 2001 From: Slinetrac Date: Fri, 26 Dec 2025 16:42:31 +0800 Subject: [PATCH] refactor(notice): extract resolveNoticeMessage helper --- src/components/layout/notice-manager.tsx | 101 +++++++++++------------ 1 file changed, 46 insertions(+), 55 deletions(-) diff --git a/src/components/layout/notice-manager.tsx b/src/components/layout/notice-manager.tsx index d573268d0..4c07671f4 100644 --- a/src/components/layout/notice-manager.tsx +++ b/src/components/layout/notice-manager.tsx @@ -17,6 +17,8 @@ import { import type { TranslationKey } from "@/types/generated/i18n-keys"; type NoticePosition = NonNullable; +type NoticeItem = ReturnType[number]; +type TranslationFn = ReturnType["t"]; const VALID_POSITIONS: NoticePosition[] = [ "top-left", @@ -40,6 +42,49 @@ const getAnchorOrigin = (position: NoticePosition): SnackbarOrigin => { return { vertical, horizontal }; }; +const resolveNoticeMessage = ( + notice: NoticeItem, + t: TranslationFn, +): React.ReactNode => { + const i18n = notice.i18n; + if (!i18n) return notice.message; + + const params = (i18n.params ?? {}) as Record; + const { prefixKey, prefixParams, prefix, message, ...restParams } = params; + + const prefixKeyParams = + prefixParams && typeof prefixParams === "object" + ? (prefixParams as Record) + : undefined; + + const resolvedPrefix = + typeof prefixKey === "string" + ? t(prefixKey as TranslationKey, { + defaultValue: prefixKey, + ...(prefixKeyParams ?? {}), + ...restParams, + }) + : typeof prefix === "string" + ? prefix + : undefined; + + const messageStr = typeof message === "string" ? message : undefined; + + const defaultValue = + messageStr === undefined + ? undefined + : resolvedPrefix + ? `${resolvedPrefix} ${messageStr}` + : messageStr; + + return t(i18n.key as TranslationKey, { + defaultValue, + ...restParams, + ...(resolvedPrefix !== undefined ? { prefix: resolvedPrefix } : {}), + ...(messageStr !== undefined ? { message: messageStr } : {}), + }); +}; + interface NoticeManagerProps { position?: NoticePosition | null; } @@ -104,61 +149,7 @@ export const NoticeManager: React.FC = ({ position }) => { } > - {notice.i18n - ? (() => { - const params = (notice.i18n.params ?? {}) as Record< - string, - unknown - >; - const { - prefixKey, - prefixParams, - prefix, - message, - ...restParams - } = params; - - const prefixKeyParams = - prefixParams && - typeof prefixParams === "object" && - prefixParams !== null - ? (prefixParams as Record) - : undefined; - - const resolvedPrefix = - typeof prefixKey === "string" - ? t(prefixKey as TranslationKey, { - defaultValue: prefixKey, - ...(prefixKeyParams ?? {}), - ...restParams, - }) - : typeof prefix === "string" - ? prefix - : undefined; - - const finalParams: Record = { - ...restParams, - }; - if (resolvedPrefix !== undefined) { - finalParams.prefix = resolvedPrefix; - } - if (typeof message === "string") { - finalParams.message = message; - } - - const defaultValue = - resolvedPrefix && typeof message === "string" - ? `${resolvedPrefix} ${message}` - : typeof message === "string" - ? message - : undefined; - - return t(notice.i18n.key as TranslationKey, { - defaultValue, - ...finalParams, - }); - })() - : notice.message} + {resolveNoticeMessage(notice, t)} ))}