Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions web/src/components/admin/crud-panels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,23 @@ export function CategoryPanel({
};

const save = async () => {
const nextName = name.trim();
if (!nextName) {
toast.error("分类名称不能为空");
return;
}
// 同名分类前端拦一道(后端无唯一约束):trim + 不区分大小写,编辑时排除自身
const duplicated = categories.some(
(c) =>
c.id !== editing?.id &&
c.name.trim().toLowerCase() === nextName.toLowerCase(),
);
if (duplicated) {
toast.error(`分类「${nextName}」已存在,请换一个名称`);
return;
}
const previousIcon = editing?.icon ?? null;
const payload = { name, icon: icon || null, visibility };
const payload = { name: nextName, icon: icon || null, visibility };
if (editing) {
await adminUpdateCategory(editing.id, payload);
toast.success("分类已更新");
Expand Down Expand Up @@ -134,6 +149,8 @@ export function CategoryPanel({
onReorder={reorder}
onEdit={openEdit}
onRemove={remove}
confirmRemove
removeDescription="删除分类后无法恢复,该分类下的链接也会一并删除。"
/>

<Dialog
Expand Down Expand Up @@ -181,6 +198,9 @@ export function CategoryPanel({
);
}

/** 「所有」筛选项的哨兵值:Radix Select 不允许 SelectItem 用空串(空串代表未选) */
const ALL_CATEGORIES = "__all__";

/** 链接 CRUD + 分类内排序(后台 links 为扁平列表,按 categoryId 过滤) */
export function LinkPanel({
categories,
Expand All @@ -191,11 +211,12 @@ export function LinkPanel({
links: AdminLink[];
onChanged: () => void;
}) {
const [categoryId, setCategoryId] = React.useState(categories[0]?.id ?? "");
const category = categories.find((c) => c.id === categoryId) ?? categories[0];
const categoryLinks = links
.filter((l) => l.categoryId === category?.id)
.sort((a, b) => a.sortOrder - b.sortOrder);
const [categoryId, setCategoryId] = React.useState<string>(ALL_CATEGORIES);
const category = categories.find((c) => c.id === categoryId);
// 「所有」:不过滤分类,直接按全局 sortOrder 展示全部链接
const categoryLinks = (
category ? links.filter((l) => l.categoryId === category.id) : [...links]
).sort((a, b) => a.sortOrder - b.sortOrder);
const [editingId, setEditingId] = React.useState<string | null>(null);
const [creating, setCreating] = React.useState(false);
const [form, setForm] = React.useState({
Expand All @@ -208,6 +229,10 @@ export function LinkPanel({
const draft = useAssetDraftCleanup();

const openCreate = () => {
if (!category) {
toast.error("请先选择一个分类");
return;
}
setForm({
title: "",
url: "",
Expand Down Expand Up @@ -240,7 +265,6 @@ export function LinkPanel({
};

const save = async () => {
if (!category) return;
const previousIcon = links.find((l) => l.id === editingId)?.iconUrl ?? null;
const payload = {
title: form.title,
Expand All @@ -253,6 +277,7 @@ export function LinkPanel({
await adminUpdateLink(editingId, payload);
toast.success("链接已更新");
} else {
if (!category) return;
await adminCreateLink({ ...payload, categoryId: category.id });
toast.success("链接已创建");
}
Expand Down Expand Up @@ -292,25 +317,27 @@ export function LinkPanel({
<div className="flex items-center justify-between gap-2">
<h2 className="font-heading text-lg font-semibold">链接</h2>
<div className="flex items-center gap-2">
<Select value={categoryId} onValueChange={setCategoryId}>
<Select
value={category ? categoryId : ALL_CATEGORIES}
onValueChange={setCategoryId}
>
<SelectTrigger className="w-40" aria-label="选择分类">
<SelectValue placeholder="选择分类" />
</SelectTrigger>
<SelectContent>
<SelectItem value={ALL_CATEGORIES}>所有</SelectItem>
{categories.map((c) => (
<SelectItem key={c.id} value={c.id}>
{c.name}
</SelectItem>
))}
</SelectContent>
</Select>
<Button onClick={openCreate} disabled={!category}>
新建链接
</Button>
<Button onClick={openCreate}>新建链接</Button>
</div>
</div>

{category ? (
{categories.length > 0 ? (
<SortableList
entries={categoryLinks.map((l) => ({
id: l.id,
Expand All @@ -334,6 +361,8 @@ export function LinkPanel({
onReorder={reorder}
onEdit={openEdit}
onRemove={remove}
confirmRemove
removeDescription="删除链接后无法恢复,请确认。"
/>
) : (
<p className="text-sm text-muted-foreground">
Expand Down
2 changes: 2 additions & 0 deletions web/src/components/admin/settings-panels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export function SearchEnginePanel({
onReorder={reorder}
onEdit={openEdit}
onRemove={remove}
confirmRemove
removeDescription="删除后无法恢复,请确认。"
/>

{!engines.some((e) => e.isDefault) && engines.length > 0 && (
Expand Down
80 changes: 71 additions & 9 deletions web/src/components/admin/sortable-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@ import {
verticalListSortingStrategy,
} from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import { GripVertical, Trash2 } from "lucide-react";
import { GripVertical, Pencil, Trash2 } from "lucide-react";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";

/**
* 可拖拽排序列表(dnd-kit 原生 sortable)。
* 每行自带拖拽把手 + 「上移 / 下移」按钮(WCAG 2.2 单指/键盘替代,MASTER.md §6)。
* 行内容只做展示,编辑 / 删除走行尾图标按钮(避免「点行即编辑」的误触)。
*/
export interface SortableEntry {
id: string;
Expand Down Expand Up @@ -73,16 +84,12 @@ function SortableRow({
<GripVertical className="size-4" aria-hidden="true" />
</button>

<button
type="button"
onClick={onEdit}
className="flex min-w-0 flex-1 items-center gap-2 text-left"
>
<div className="flex min-w-0 flex-1 items-center gap-2">
<span className="truncate font-medium">{entry.label}</span>
{entry.meta}
</button>
</div>

<div className="flex items-center gap-1">
<div className="flex shrink-0 items-center gap-1">
<Button
variant="ghost"
size="icon"
Expand All @@ -103,6 +110,17 @@ function SortableRow({
>
↓
</Button>
{onEdit && (
<Button
variant="ghost"
size="icon"
className="size-8"
aria-label={`编辑 ${entry.label}`}
onClick={onEdit}
>
<Pencil className="size-4" aria-hidden="true" />
</Button>
)}
{onRemove && (
<Button
variant="ghost"
Expand All @@ -124,12 +142,21 @@ export function SortableList({
onReorder,
onEdit,
onRemove,
confirmRemove = false,
removeDescription = "删除后无法恢复,请确认。",
}: {
entries: SortableEntry[];
onReorder: (ids: string[]) => void;
onEdit?: (id: string) => void;
onRemove?: (id: string) => void;
/** 删除前弹 AlertDialog 二次确认(破坏性删除,如分类会连带删掉其链接) */
confirmRemove?: boolean;
/** 二次确认弹窗的补充说明 */
removeDescription?: string;
}) {
const [confirmTarget, setConfirmTarget] =
React.useState<SortableEntry | null>(null);
const [confirmOpen, setConfirmOpen] = React.useState(false);
const sensors = useSensors(
useSensor(PointerSensor, { activationConstraint: { distance: 6 } }),
useSensor(KeyboardSensor, {
Expand All @@ -153,6 +180,15 @@ export function SortableList({
onReorder(arrayMove(ids, index, next));
};

const requestRemove = (entry: SortableEntry) => {
if (confirmRemove) {
setConfirmTarget(entry);
setConfirmOpen(true);
return;
}
onRemove?.(entry.id);
};

return (
<DndContext
sensors={sensors}
Expand All @@ -169,11 +205,37 @@ export function SortableList({
canMoveUp={index > 0}
canMoveDown={index < entries.length - 1}
onEdit={onEdit ? () => onEdit(entry.id) : undefined}
onRemove={onRemove ? () => onRemove(entry.id) : undefined}
onRemove={onRemove ? () => requestRemove(entry) : undefined}
/>
))}
</div>
</SortableContext>

{confirmRemove && (
<AlertDialog open={confirmOpen} onOpenChange={setConfirmOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
删除「{confirmTarget?.label}」?
</AlertDialogTitle>
<AlertDialogDescription>
{removeDescription}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>取消</AlertDialogCancel>
<AlertDialogAction
className="bg-destructive/10 text-destructive hover:bg-destructive/20"
onClick={() => {
if (confirmTarget) onRemove?.(confirmTarget.id);
}}
>
删除
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)}
</DndContext>
);
}
Loading
Loading