86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import type { PlatformErrorDialogPayload } from './PlatformErrorDialog';
|
|
import type { PlatformTaskCompletionDialogPayload } from './PlatformTaskCompletionDialog';
|
|
|
|
export type PlatformErrorDialogState = PlatformErrorDialogPayload & {
|
|
key: string;
|
|
};
|
|
|
|
export type PlatformTaskFailureDialogState = PlatformErrorDialogState & {
|
|
failedAtMs: number;
|
|
};
|
|
|
|
export type PlatformTaskCompletionDialogState =
|
|
PlatformTaskCompletionDialogPayload & {
|
|
key: string;
|
|
completedAtMs: number | null;
|
|
};
|
|
|
|
export type PlatformDialogCandidate = {
|
|
key: string;
|
|
source: string;
|
|
message: string | null | undefined;
|
|
};
|
|
|
|
export const PLATFORM_TASK_COMPLETION_MESSAGE =
|
|
'生成任务已完成,可以继续查看草稿。';
|
|
|
|
/** 收口平台弹窗候选的纯状态规则,壳层只负责副作用清理。 */
|
|
export function normalizePlatformDialogMessage(
|
|
message: string | null | undefined,
|
|
) {
|
|
const normalized = message?.trim();
|
|
return normalized ? normalized : null;
|
|
}
|
|
|
|
export function formatPlatformDialogSource(label: string, id?: string | null) {
|
|
const normalizedId = id?.trim();
|
|
return normalizedId ? `${label} ${normalizedId}` : label;
|
|
}
|
|
|
|
export function isBackgroundGenerationStillRunningMessage(message: string) {
|
|
return /仍在后台处理|后台仍在处理|仍在生成|后台生成/u.test(message);
|
|
}
|
|
|
|
export function resolvePlatformErrorDialog(
|
|
candidates: readonly PlatformDialogCandidate[],
|
|
): PlatformErrorDialogState | null {
|
|
for (const candidate of candidates) {
|
|
const message = normalizePlatformDialogMessage(candidate.message);
|
|
if (message) {
|
|
return {
|
|
key: candidate.key,
|
|
source: candidate.source,
|
|
message,
|
|
};
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function buildPlatformErrorDialogDismissKey(
|
|
error: PlatformErrorDialogState | null,
|
|
) {
|
|
return error ? `${error.key}:${error.source}:${error.message}` : null;
|
|
}
|
|
|
|
export function buildPlatformTaskCompletionDialogDismissKey(
|
|
completion: PlatformTaskCompletionDialogState | null,
|
|
) {
|
|
return completion
|
|
? `${completion.key}:${completion.source}:${completion.message}:${completion.completedAtMs ?? 0}`
|
|
: null;
|
|
}
|
|
|
|
export function resolveActivePlatformDialog<TDialog>(
|
|
currentDialog: TDialog | null,
|
|
dismissedDialogKey: string | null,
|
|
buildDismissKey: (dialog: TDialog | null) => string | null,
|
|
): TDialog | null {
|
|
const currentDialogDismissKey = buildDismissKey(currentDialog);
|
|
return currentDialogDismissKey &&
|
|
currentDialogDismissKey === dismissedDialogKey
|
|
? null
|
|
: currentDialog;
|
|
}
|