mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-04-18 16:30:32 +08:00
refactor(notice): extract resolveNoticeMessage helper
This commit is contained in:
parent
2417d064e1
commit
63cd4905f9
@ -17,6 +17,8 @@ import {
|
|||||||
import type { TranslationKey } from "@/types/generated/i18n-keys";
|
import type { TranslationKey } from "@/types/generated/i18n-keys";
|
||||||
|
|
||||||
type NoticePosition = NonNullable<IVergeConfig["notice_position"]>;
|
type NoticePosition = NonNullable<IVergeConfig["notice_position"]>;
|
||||||
|
type NoticeItem = ReturnType<typeof getSnapshotNotices>[number];
|
||||||
|
type TranslationFn = ReturnType<typeof useTranslation>["t"];
|
||||||
|
|
||||||
const VALID_POSITIONS: NoticePosition[] = [
|
const VALID_POSITIONS: NoticePosition[] = [
|
||||||
"top-left",
|
"top-left",
|
||||||
@ -40,6 +42,49 @@ const getAnchorOrigin = (position: NoticePosition): SnackbarOrigin => {
|
|||||||
return { vertical, horizontal };
|
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<string, unknown>;
|
||||||
|
const { prefixKey, prefixParams, prefix, message, ...restParams } = params;
|
||||||
|
|
||||||
|
const prefixKeyParams =
|
||||||
|
prefixParams && typeof prefixParams === "object"
|
||||||
|
? (prefixParams as Record<string, unknown>)
|
||||||
|
: 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 {
|
interface NoticeManagerProps {
|
||||||
position?: NoticePosition | null;
|
position?: NoticePosition | null;
|
||||||
}
|
}
|
||||||
@ -104,61 +149,7 @@ export const NoticeManager: React.FC<NoticeManagerProps> = ({ position }) => {
|
|||||||
</IconButton>
|
</IconButton>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{notice.i18n
|
{resolveNoticeMessage(notice, t)}
|
||||||
? (() => {
|
|
||||||
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<string, unknown>)
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const resolvedPrefix =
|
|
||||||
typeof prefixKey === "string"
|
|
||||||
? t(prefixKey as TranslationKey, {
|
|
||||||
defaultValue: prefixKey,
|
|
||||||
...(prefixKeyParams ?? {}),
|
|
||||||
...restParams,
|
|
||||||
})
|
|
||||||
: typeof prefix === "string"
|
|
||||||
? prefix
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const finalParams: Record<string, unknown> = {
|
|
||||||
...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}
|
|
||||||
</Alert>
|
</Alert>
|
||||||
</Snackbar>
|
</Snackbar>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user