Files
vibemake-admin/src/components/ProjectTable.tsx
T
2026-11-03 15:50:13 +08:00

117 lines
4.6 KiB
TypeScript

import { Image, Table, Tag, Tooltip, Typography } from 'antd';
import type { TablePaginationConfig, TableProps } from 'antd';
import { Link } from 'react-router-dom';
import type { ManagedProjectView, TypeView } from '@/api/types';
import type { ProjectSort } from '@/api';
import StatusTag from './StatusTag';
import { formatTime, shortSha } from '@/utils/format';
import { publishStatusLabel, repoStatusLabel } from '@/utils/labels';
const gradients = [
'linear-gradient(135deg,#6366f1,#8b5cf6)',
'linear-gradient(135deg,#0ea5e9,#6366f1)',
'linear-gradient(135deg,#14b8a6,#0ea5e9)',
'linear-gradient(135deg,#f59e0b,#ef4444)',
'linear-gradient(135deg,#8b5cf6,#ec4899)',
'linear-gradient(135deg,#10b981,#14b8a6)',
];
export function coverGradient(seed: string) {
let h = 0;
for (const c of seed) h = (h * 31 + c.charCodeAt(0)) >>> 0;
return gradients[h % gradients.length];
}
export default function ProjectTable({
items,
loading,
pagination,
types = [],
hideOwner,
sort,
onSortChange,
}: {
items: ManagedProjectView[];
loading?: boolean;
pagination: TablePaginationConfig;
types?: TypeView[];
hideOwner?: boolean;
sort?: ProjectSort;
onSortChange?: (sort: ProjectSort) => void;
}) {
const typeName = (code: string) => types.find((t) => t.code === code)?.name ?? code;
const onChange: TableProps<ManagedProjectView>['onChange'] = (_, __, sorter, extra) => {
if (extra.action !== 'sort' || !onSortChange) return;
const current = Array.isArray(sorter) ? sorter[0] : sorter;
if (current?.columnKey === 'publishedAt') onSortChange('PUBLISHED_AT_DESC');
};
return (
<Table<ManagedProjectView>
rowKey={(r) => r.project.projectCode}
loading={loading}
dataSource={items}
pagination={pagination}
scroll={{ x: 1420 }}
onChange={onChange}
columns={[
{
title: '项目',
width: 300,
render: (_, { project: p }) => (
<div className="pt-cell">
{p.coverUrl ? (
<Image src={p.coverUrl} width={56} height={42} className="pt-cover" preview={false} />
) : (
<div className="pt-cover pt-cover-empty" style={{ background: coverGradient(p.projectCode) }}>
{p.name.slice(0, 1).toUpperCase()}
</div>
)}
<div className="pt-meta">
<Link to={`/projects/${p.projectCode}`} className="pt-name" title={p.name}>{p.name}</Link>
<Typography.Text type="secondary" className="mono pt-code">{p.projectCode}</Typography.Text>
</div>
</div>
),
},
{ title: '类型', width: 90, render: (_, r) => typeName(r.project.projectType) },
...(hideOwner
? []
: [{
title: '所有者',
width: 130,
render: (_: unknown, r: ManagedProjectView) =>
r.project.owner.userCode ? <Link to={`/users/${r.project.owner.userCode}`}>{r.project.owner.nickname}</Link> : r.project.owner.nickname,
}]),
{ title: '仓库', width: 110, render: (_, r) => <StatusTag map={repoStatusLabel} value={r.project.repoStatus} /> },
{
title: '发布',
width: 90,
render: (_, r) => (
<Tooltip title={r.moderationReason ? `下架原因:${r.moderationReason}` : undefined}>
<span><StatusTag map={publishStatusLabel} value={r.project.publishStatus} /></span>
</Tooltip>
),
},
{ title: '发布版本', width: 120, render: (_, r) => <span className="mono">{shortSha(r.project.publishedCommitSha)}</span> },
{
title: '是否有 STEP',
width: 110,
render: (_, r) => <Tag color={r.project.stepMainUrl ? 'green' : 'default'}>{r.project.stepMainUrl ? '有' : '无'}</Tag>,
},
{ title: '创建时间', key: 'createdAt', width: 180, render: (_, r) => <span className="nowrap">{formatTime(r.project.createdAt)}</span> },
{
title: '发布时间',
key: 'publishedAt',
sorter: !!onSortChange,
sortOrder: onSortChange && sort === 'PUBLISHED_AT_DESC' ? 'descend' : null,
// 仅支持发布时间倒序;不切换到升序或无序状态。
sortDirections: ['descend', 'descend'],
width: 180,
render: (_, r) => <span className="nowrap">{formatTime(r.project.publishedAt)}</span>,
},
{ title: '操作', fixed: 'right', width: 80, render: (_, r) => <Link to={`/projects/${r.project.projectCode}`}>管理</Link> },
]}
/>
);
}