279 lines
9.1 KiB
TypeScript
279 lines
9.1 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
import { useParams, useRouter } from 'next/navigation';
|
|
import Link from 'next/link';
|
|
|
|
interface Version {
|
|
id: string;
|
|
version: string;
|
|
releaseNotes: string | null;
|
|
minRequiredVersion: string | null;
|
|
isSecurityUpdate: boolean;
|
|
isCriticalUpdate: boolean;
|
|
downloadSize: string | null;
|
|
releaseDate: string;
|
|
}
|
|
|
|
export default function ProjectVersionsPage() {
|
|
const router = useRouter();
|
|
const params = useParams();
|
|
const projectCode = params.projectCode as string;
|
|
const [versions, setVersions] = useState<Version[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [showModal, setShowModal] = useState(false);
|
|
|
|
const loadVersions = async () => {
|
|
try {
|
|
const res = await fetch(`/api/admin/${projectCode}/versions`);
|
|
|
|
if (res.status === 401) {
|
|
router.push('/admin/login');
|
|
return;
|
|
}
|
|
|
|
if (res.status === 403 || res.status === 404) {
|
|
router.push('/admin/select-project');
|
|
return;
|
|
}
|
|
|
|
const data = await res.json();
|
|
setVersions(data.versions || []);
|
|
} catch (error) {
|
|
console.error('获取版本列表失败:', error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
void loadVersions();
|
|
}, [projectCode]);
|
|
|
|
return (
|
|
<div className="min-h-screen bg-gray-100">
|
|
<nav className="bg-white shadow">
|
|
<div className="mx-auto flex max-w-7xl items-center justify-between px-4 py-4">
|
|
<div className="flex items-center gap-4">
|
|
<Link href={`/admin/${projectCode}`} className="text-gray-600 hover:text-gray-900">
|
|
← 返回
|
|
</Link>
|
|
<h1 className="text-xl font-bold">版本管理</h1>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<main className="mx-auto max-w-7xl px-4 py-8">
|
|
<div className="rounded-lg bg-white shadow">
|
|
<div className="flex items-center justify-between border-b p-6">
|
|
<h2 className="font-medium">版本列表</h2>
|
|
<button
|
|
onClick={() => setShowModal(true)}
|
|
className="rounded-lg bg-blue-600 px-4 py-2 text-white hover:bg-blue-700"
|
|
>
|
|
发布新版本
|
|
</button>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="p-8 text-center text-gray-500">加载中...</div>
|
|
) : (
|
|
<table className="w-full">
|
|
<thead className="bg-gray-50">
|
|
<tr>
|
|
<th className="px-6 py-3 text-left text-xs font-medium uppercase text-gray-500">
|
|
版本号
|
|
</th>
|
|
<th className="px-6 py-3 text-left text-xs font-medium uppercase text-gray-500">
|
|
发布日期
|
|
</th>
|
|
<th className="px-6 py-3 text-left text-xs font-medium uppercase text-gray-500">
|
|
最低要求版本
|
|
</th>
|
|
<th className="px-6 py-3 text-left text-xs font-medium uppercase text-gray-500">
|
|
标签
|
|
</th>
|
|
<th className="px-6 py-3 text-left text-xs font-medium uppercase text-gray-500">
|
|
更新说明
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-200">
|
|
{versions.map((item) => (
|
|
<tr key={item.id}>
|
|
<td className="px-6 py-4 font-mono font-medium">{item.version}</td>
|
|
<td className="px-6 py-4 text-gray-600">
|
|
{new Date(item.releaseDate).toLocaleDateString()}
|
|
</td>
|
|
<td className="px-6 py-4 text-gray-600">{item.minRequiredVersion || '-'}</td>
|
|
<td className="px-6 py-4">
|
|
{item.isSecurityUpdate && (
|
|
<span className="mr-1 rounded bg-red-100 px-2 py-1 text-xs text-red-700">
|
|
安全更新
|
|
</span>
|
|
)}
|
|
{item.isCriticalUpdate && (
|
|
<span className="rounded bg-yellow-100 px-2 py-1 text-xs text-yellow-700">
|
|
重要更新
|
|
</span>
|
|
)}
|
|
</td>
|
|
<td className="max-w-xs truncate px-6 py-4 text-gray-600">
|
|
{item.releaseNotes || '-'}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
{!versions.length && (
|
|
<tr>
|
|
<td colSpan={5} className="px-6 py-8 text-center text-gray-500">
|
|
暂无版本数据
|
|
</td>
|
|
</tr>
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
</div>
|
|
</main>
|
|
|
|
{showModal && (
|
|
<VersionModal
|
|
projectCode={projectCode}
|
|
onClose={() => setShowModal(false)}
|
|
onSuccess={() => {
|
|
setShowModal(false);
|
|
void loadVersions();
|
|
}}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function VersionModal({
|
|
projectCode,
|
|
onClose,
|
|
onSuccess,
|
|
}: {
|
|
projectCode: string;
|
|
onClose: () => void;
|
|
onSuccess: () => void;
|
|
}) {
|
|
const [form, setForm] = useState({
|
|
version: '',
|
|
releaseNotes: '',
|
|
minRequiredVersion: '',
|
|
isSecurityUpdate: false,
|
|
isCriticalUpdate: false,
|
|
});
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setLoading(true);
|
|
setError('');
|
|
|
|
try {
|
|
const res = await fetch(`/api/admin/${projectCode}/versions`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(form),
|
|
});
|
|
const data = await res.json();
|
|
if (data.success) {
|
|
onSuccess();
|
|
} else {
|
|
setError(data.error || '发布失败');
|
|
}
|
|
} catch {
|
|
setError('网络错误');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
|
|
<div className="w-full max-w-md rounded-lg bg-white shadow-xl">
|
|
<div className="border-b p-6">
|
|
<h2 className="text-lg font-medium">发布新版本</h2>
|
|
</div>
|
|
<form onSubmit={handleSubmit} className="space-y-4 p-6">
|
|
<div>
|
|
<label className="mb-1 block text-sm font-medium text-gray-700">版本号 *</label>
|
|
<input
|
|
type="text"
|
|
value={form.version}
|
|
onChange={(e) => setForm({ ...form, version: e.target.value })}
|
|
className="w-full rounded-lg border px-3 py-2"
|
|
placeholder="1.0.0"
|
|
required
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="mb-1 block text-sm font-medium text-gray-700">更新说明</label>
|
|
<textarea
|
|
value={form.releaseNotes}
|
|
onChange={(e) => setForm({ ...form, releaseNotes: e.target.value })}
|
|
className="w-full rounded-lg border px-3 py-2"
|
|
rows={3}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="mb-1 block text-sm font-medium text-gray-700">
|
|
最低要求版本
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={form.minRequiredVersion}
|
|
onChange={(e) => setForm({ ...form, minRequiredVersion: e.target.value })}
|
|
className="w-full rounded-lg border px-3 py-2"
|
|
placeholder="留空表示不强制更新"
|
|
/>
|
|
</div>
|
|
<div className="flex gap-4">
|
|
<label className="flex items-center gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={form.isSecurityUpdate}
|
|
onChange={(e) =>
|
|
setForm({ ...form, isSecurityUpdate: e.target.checked })
|
|
}
|
|
/>
|
|
<span className="text-sm">安全更新</span>
|
|
</label>
|
|
<label className="flex items-center gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={form.isCriticalUpdate}
|
|
onChange={(e) =>
|
|
setForm({ ...form, isCriticalUpdate: e.target.checked })
|
|
}
|
|
/>
|
|
<span className="text-sm">重要更新</span>
|
|
</label>
|
|
</div>
|
|
{error && <div className="rounded-lg bg-red-50 p-3 text-sm text-red-600">{error}</div>}
|
|
<div className="flex gap-3 pt-4">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="flex-1 rounded-lg border py-2 hover:bg-gray-50"
|
|
>
|
|
取消
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="flex-1 rounded-lg bg-blue-600 py-2 text-white hover:bg-blue-700 disabled:opacity-50"
|
|
>
|
|
{loading ? '发布中...' : '发布'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|