"use client"; import { useState, useEffect, useCallback } from "react"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { portalLinksApi, type PortalLinkRead, type PortalLinkCreate, } from "@/lib/api/client"; import { Globe, Plus, Pencil, Trash2, Loader2, ExternalLink } from "lucide-react"; import { toast } from "sonner"; export default function SettingsPortalLinksPage() { const [links, setLinks] = useState([]); const [loading, setLoading] = useState(true); const [dialogOpen, setDialogOpen] = useState(false); const [editingId, setEditingId] = useState(null); const [saving, setSaving] = useState(false); const [form, setForm] = useState({ name: "", url: "" }); const loadLinks = useCallback(async () => { try { const list = await portalLinksApi.list(); setLinks(list); } catch { toast.error("加载快捷门户列表失败"); } finally { setLoading(false); } }, []); useEffect(() => { loadLinks(); }, [loadLinks]); const openAdd = () => { setEditingId(null); setForm({ name: "", url: "" }); setDialogOpen(true); }; const openEdit = (item: PortalLinkRead) => { setEditingId(item.id); setForm({ name: item.name, url: item.url }); setDialogOpen(true); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!form.name.trim() || !form.url.trim()) { toast.error("请填写名称和链接"); return; } setSaving(true); try { if (editingId) { await portalLinksApi.update(editingId, { name: form.name.trim(), url: form.url.trim(), }); toast.success("已更新"); } else { const payload: PortalLinkCreate = { name: form.name.trim(), url: form.url.trim(), }; await portalLinksApi.create(payload); toast.success("已添加"); } setDialogOpen(false); await loadLinks(); if (typeof window !== "undefined") { window.dispatchEvent(new CustomEvent("portal-links-changed")); } } catch (e) { toast.error(e instanceof Error ? e.message : "保存失败"); } finally { setSaving(false); } }; const handleDelete = async (id: string) => { if (!confirm("确定删除该快捷门户入口?")) return; try { await portalLinksApi.delete(id); toast.success("已删除"); await loadLinks(); if (typeof window !== "undefined") { window.dispatchEvent(new CustomEvent("portal-links-changed")); } } catch (e) { toast.error(e instanceof Error ? e.message : "删除失败"); } }; return (
← 设置
快捷门户

添加税务、公积金等门户链接,侧栏可快速打开。

{loading ? (

加载中…

) : links.length === 0 ? (

暂无快捷门户入口。添加后将显示在左侧边栏「快捷门户」区域。

) : ( 名称 链接 操作 {links.map((item) => ( {item.name} {item.url}
))}
)}
{editingId ? "编辑快捷门户入口" : "添加快捷门户入口"}
setForm((f) => ({ ...f, name: e.target.value }))} placeholder="如:电子税务局、公积金" />
setForm((f) => ({ ...f, url: e.target.value }))} placeholder="https://..." />
); }