批量转换脚本引入的语法错误已回退,保留稳定的 i18n 转换: - 翻译词典: 22 namespaces, 3600+ keys - 已接入: 145+ 文件 (Dashboard/Login/K8s 主页面/Monitor/System/Deploy) - 语言切换器在 Header 中可用 (🇨🇳/🇺🇸) - tsc 零错误, Vite 构建成功
139 lines
4.3 KiB
TypeScript
139 lines
4.3 KiB
TypeScript
import type { TFunction } from 'i18next';
|
||
import { Badge } from '@/components/ui/badge';
|
||
import { CheckCircle, XCircle, Loader2 } from 'lucide-react';
|
||
|
||
// 流水线阶段定义 // Logan.Li
|
||
export interface PipelineStage {
|
||
id: string;
|
||
name: string;
|
||
status: 'pending' | 'running' | 'success' | 'failed' | 'skipped';
|
||
duration: string;
|
||
logs: string[];
|
||
}
|
||
|
||
// 流水线模板(新建时快捷选择) // Logan.Li
|
||
export function getPipelineTemplates(t: TFunction) {
|
||
return [
|
||
{
|
||
id: 'template-1',
|
||
name: t('deploy.template.nodejs'),
|
||
description: t('deploy.template.nodejsDesc'),
|
||
stages: [
|
||
t('deploy.stage.checkout'),
|
||
t('deploy.template.dependencyInstall'),
|
||
t('deploy.releaseHelpers.pipelineStep.codeCheck'),
|
||
t('deploy.stage.buildDesc'),
|
||
t('deploy.stage.deploy'),
|
||
],
|
||
},
|
||
{
|
||
id: 'template-2',
|
||
name: t('deploy.template.go'),
|
||
description: t('deploy.template.goDesc'),
|
||
stages: [
|
||
t('deploy.stage.checkout'),
|
||
t('deploy.template.unitTest'),
|
||
t('deploy.stage.build'),
|
||
t('deploy.releaseHelpers.pipelineStep.buildImage'),
|
||
t('deploy.stage.deploy'),
|
||
],
|
||
},
|
||
{
|
||
id: 'template-3',
|
||
name: t('deploy.template.java'),
|
||
description: t('deploy.template.javaDesc'),
|
||
stages: [
|
||
t('deploy.stage.checkout'),
|
||
t('deploy.template.mavenBuild'),
|
||
t('deploy.template.unitTest'),
|
||
t('deploy.template.package'),
|
||
t('deploy.stage.deploy'),
|
||
],
|
||
},
|
||
{
|
||
id: 'template-4',
|
||
name: t('deploy.template.python'),
|
||
description: t('deploy.template.pythonDesc'),
|
||
stages: [
|
||
t('deploy.stage.checkout'),
|
||
t('deploy.template.dependencyInstall'),
|
||
t('deploy.releaseHelpers.pipelineStep.codeCheck'),
|
||
t('deploy.stage.test'),
|
||
t('deploy.stage.deploy'),
|
||
],
|
||
},
|
||
];
|
||
}
|
||
|
||
// 解析 stages 字段(兼容 JSON 数组 / 逗号分隔字符串) // Logan.Li
|
||
export function parseStages( stages: string): PipelineStage[] {
|
||
if (Array.isArray(stages)) return stages;
|
||
if (typeof stages === 'string') {
|
||
try {
|
||
const parsed = JSON.parse(stages);
|
||
if (Array.isArray(parsed)) return parsed;
|
||
} catch {
|
||
return stages.split(',').map((s, i) => ({ id: `stage-${i + 1}`, name: s.trim(), status: 'success' as const, duration: '-', logs: [] }));
|
||
}
|
||
}
|
||
return [];
|
||
}
|
||
|
||
// 由流水线名称生成 code(kebab-case) // Logan.Li
|
||
export function nameToCode(name: string): string {
|
||
return name
|
||
.toLowerCase()
|
||
.replace(/[^a-z0-9]/g, '-')
|
||
.replace(/-+/g, '-')
|
||
.replace(/^-|-$/g, '') || `pipeline-${Date.now()}`;
|
||
}
|
||
|
||
// 状态徽章(使用后端 last_status 字段) // Logan.Li
|
||
export function getStatusBadge(status: string, t: TFunction) {
|
||
switch (status) {
|
||
case 'success':
|
||
return (
|
||
<Badge variant="outline" className="border-emerald-500/30 dg-text-green bg-emerald-500/10">
|
||
<CheckCircle className="h-3 w-3 mr-1" />
|
||
{t('deploy.status.success')}
|
||
</Badge>
|
||
);
|
||
case 'failed':
|
||
return (
|
||
<Badge variant="outline" className="border-red-500/30 dg-text-red bg-red-500/10">
|
||
<XCircle className="h-3 w-3 mr-1" />
|
||
{t('deploy.status.failed')}
|
||
</Badge>
|
||
);
|
||
case 'running':
|
||
return (
|
||
<Badge variant="outline" className="border-blue-500/30 dg-text-blue bg-blue-500/10 animate-pulse">
|
||
<Loader2 className="h-3 w-3 mr-1 animate-spin" />
|
||
{t('deploy.status.running')}
|
||
</Badge>
|
||
);
|
||
default:
|
||
return (
|
||
<Badge variant="outline" className="border-[var(--dg-border)] text-[var(--dg-text-secondary)]">
|
||
{t('deploy.pipeline.neverRun')}
|
||
</Badge>
|
||
);
|
||
}
|
||
}
|
||
|
||
// 阶段状态图标 // Logan.Li
|
||
export function getStageStatusIcon(status: PipelineStage['status'], t: TFunction) {
|
||
switch (status) {
|
||
case 'success':
|
||
return <CheckCircle className="h-4 w-4 dg-text-green" />;
|
||
case 'failed':
|
||
return <XCircle className="h-4 w-4 dg-text-red" />;
|
||
case 'running':
|
||
return <Loader2 className="h-4 w-4 dg-text-blue animate-spin" />;
|
||
case 'skipped':
|
||
return <span className="text-[var(--dg-text-secondary)] text-xs">{t('deploy.stageStatus.skipped')}</span>;
|
||
default:
|
||
return <span className="text-[var(--dg-text-secondary)] opacity-60 text-xs">{t('deploy.status.pending')}</span>;
|
||
}
|
||
}
|