import { App, Button, Dropdown, Table } from "antd";
import type { ButtonProps, MenuProps, TableProps } from "antd";
import { saveAs } from "file-saver";
import { CheckSquare2, ChevronDown, ChevronLeft, ChevronRight, Download, ListFilter, RotateCcw, SearchX, X } from "lucide-react";
import { useEffect, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
import { Select } from "@/components/ui/base/select";
export const configuredSecretText = "已配置 · 留空不改";
export type AdminStatusTone = "neutral" | "success" | "warning" | "error" | "info";
export function AdminStatusBadge({ label, tone = "neutral", title }: { label: string; tone?: AdminStatusTone; title?: string; variant?: string }) {
return (
{label}
);
}
export function AdminStatTile({ label, value, detail, trend }: { label: string; value: string | number; detail?: string; trend?: { value: string; tone?: AdminStatusTone } }) {
return (
{label}
{value}
{trend || detail ? (
{trend ?
: null}
{trend && detail ?
· : null}
{detail ?
{detail} : null}
) : null}
);
}
export function ListToolbar({
children,
filters,
filtersAlwaysVisible = false,
activeFilters,
trailing,
active,
onReset,
className,
}: {
children: ReactNode;
filters?: ReactNode;
filtersAlwaysVisible?: boolean;
activeFilters?: ReactNode;
trailing?: ReactNode;
active?: boolean;
onReset?: () => void;
className?: string;
}) {
const [filtersOpen, setFiltersOpen] = useState(false);
useEffect(() => {
if (active) setFiltersOpen(true);
}, [active]);
return (
{children}
{filters ? (
<>
{!filtersAlwaysVisible ? (
} onClick={() => setFiltersOpen((open) => !open)}>
筛选{active ?
: null}
) : null}
{filters}
>
) : null}
{activeFilters ?
{activeFilters}
: null}
{active && onReset ? (
} onClick={onReset}>
重置
) : null}
{trailing}
);
}
function pageItems(current: number, pages: number): Array {
if (pages <= 7) return Array.from({ length: pages }, (_, index) => index + 1);
if (current <= 4) return [1, 2, 3, 4, 5, "…", pages];
if (current >= pages - 3) return [1, "…", pages - 4, pages - 3, pages - 2, pages - 1, pages];
return [1, "…", current - 1, current, current + 1, "…", pages];
}
export function PaginationBar({
current,
pageSize,
total,
onChange,
pageSizeOptions = [20, 50, 100],
alwaysShow = false,
itemLabel = "条",
}: {
current: number;
pageSize: number;
total: number;
onChange: (page: number, pageSize: number) => void;
pageSizeOptions?: number[];
alwaysShow?: boolean;
itemLabel?: string;
}) {
if (!alwaysShow && total <= pageSize && current === 1) return null;
const pages = Math.max(1, Math.ceil(total / pageSize));
const start = total === 0 ? 0 : (current - 1) * pageSize + 1;
const end = total === 0 ? 0 : Math.min(total, current * pageSize);
const items = pageItems(current, pages);
return (
{total === 0 ? `共 0 ${itemLabel}` : `${start}-${end} / 共 ${total} ${itemLabel}`}
);
}
export function AdminDataTable({
toolbar,
toolbarActive,
toolbarFilters,
toolbarFiltersAlwaysVisible = true,
toolbarActiveFilters,
onReset,
trailing,
batchActions,
footer,
table,
empty,
skeletonColumns = 6,
skeletonRows = 8,
className,
}: {
toolbar?: ReactNode;
toolbarActive?: boolean;
toolbarFilters?: ReactNode;
toolbarFiltersAlwaysVisible?: boolean;
toolbarActiveFilters?: ReactNode;
onReset?: () => void;
trailing?: ReactNode;
batchActions?: ReactNode;
footer?: ReactNode;
table: TableProps;
empty?: ReactNode;
skeletonColumns?: number;
skeletonRows?: number;
className?: string;
}) {
const dataSource = table.dataSource as readonly RecordType[] | undefined;
const showSkeleton = Boolean(table.loading) && !dataSource?.length;
return (
{toolbar ? (
{toolbar}
) : null}
{batchActions}
{footer ?
{footer}
: null}
);
}
function isStatusConfig(value: ReactNode | { label: string; color?: string }): value is { label: string; color?: string } {
if (!value || typeof value !== "object") return false;
return typeof (value as { label?: unknown }).label === "string";
}
export function AdminExportButton({
exportFile,
fileName,
label = "导出",
successMessage,
errorMessage = "导出失败",
size,
...buttonProps
}: Omit & {
exportFile: () => Blob | Promise;
fileName: string | (() => string);
label?: string;
successMessage?: string;
errorMessage?: string;
}) {
const { message } = App.useApp();
const [exporting, setExporting] = useState(false);
const runExport = async () => {
setExporting(true);
try {
const blob = await exportFile();
saveAs(blob, typeof fileName === "function" ? fileName() : fileName);
if (successMessage) message.success(successMessage);
} catch (error) {
message.error(error instanceof Error ? error.message : errorMessage);
} finally {
setExporting(false);
}
};
return (
} loading={exporting} onClick={() => void runExport()}>
{label}
);
}
export function AdminTableEmpty({ filtered = false, title, description, action }: { filtered?: boolean; title?: string; description?: string; action?: ReactNode }) {
return (
{title || (filtered ? "没有符合筛选条件的数据" : "暂无数据")}
{description ?
{description}
: null}
{action}
);
}
export function AdminEmpty({ title, description, action }: { title?: string; description?: string; action?: ReactNode; size?: string; icon?: unknown }) {
return ;
}
export function AdminFilterChip({ label, onRemove }: { label: ReactNode; onRemove: () => void }) {
return (
);
}
export function AdminTableSkeleton({ rows = 8, columns = 6 }: { rows?: number; columns?: number }) {
return (
{Array.from({ length: columns }).map((_, index) => (
))}
{Array.from({ length: Math.max(8, rows) }).map((_, rowIndex) => (
{Array.from({ length: columns }).map((_, columnIndex) => (
))}
))}
);
}
export function AdminBatchBar({ count, onClear, children }: { count: number; onClear: () => void; children: ReactNode }) {
if (count <= 0) return null;
return (
已选择 {count} 项
{children}
} onClick={onClear}>
取消选择
);
}
export type AdminRowAction = {
key: string;
label: ReactNode;
icon?: ReactNode;
danger?: boolean;
disabled?: boolean;
onClick: () => void | Promise;
confirm?: {
title: string;
description: string;
okText: string;
};
};
export function AdminRowActions({ primary, actions, visibleActionCount }: { primary?: { label: ReactNode; icon?: ReactNode; onClick: () => void | Promise; disabled?: boolean }; actions: AdminRowAction[]; visibleActionCount?: number }) {
const { modal } = App.useApp();
const resolvedVisibleActionCount = visibleActionCount ?? (actions.length <= 1 ? actions.length : 1);
const visibleActions = actions.slice(0, Math.max(0, resolvedVisibleActionCount));
const menuActions = actions.slice(Math.max(0, resolvedVisibleActionCount));
const items: MenuProps["items"] = menuActions.map((action) => ({
key: action.key,
label: action.label,
icon: action.icon,
danger: action.danger,
disabled: action.disabled,
}));
const runAction = (action: AdminRowAction) => {
if (!action.confirm) {
void action.onClick();
return;
}
modal.confirm({
title: action.confirm.title,
content: action.confirm.description,
okText: action.confirm.okText,
cancelText: "取消",
okButtonProps: { danger: action.danger },
onOk: action.onClick,
});
};
const renderActionButton = (action: AdminRowAction) => (
);
return (
{primary ? (
) : null}
{visibleActions.map(renderActionButton)}
{menuActions.length ? (
{
const action = actions.find((item) => item.key === key);
if (action) runAction(action);
},
}}
>
) : null}
);
}
export function SettingsSectionCard({
icon,
title,
description,
status,
children,
footer,
className,
contentClassName,
layout = "split",
}: {
icon?: ReactNode;
title: string;
description?: string;
status?: { label: string; color?: string } | ReactNode;
children: ReactNode;
footer?: ReactNode;
className?: string;
contentClassName?: string;
layout?: "split" | "stacked";
}) {
const isStacked = layout === "stacked";
return (
{icon ?
{icon} : null}
{title}
{description ?
{description}
: null}
{status ? (
{isStatusConfig(status) ? (
) : (
status
)}
) : null}
{children}
{footer ?
{footer}
: null}
);
}