"use client" import React, { useState } from "react" import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Button } from "@/components/ui/button" import { toast } from "sonner" import { ResourceBadge } from "@/components/resource" import AddResourceTag from "./AddResourceTag" import { Textarea } from "@/components/ui/textarea" import { Plus } from "lucide-react" import { Resource } from "@/lib/types/resource" import { AdminApi } from "@/lib/api" import useSWR from "swr" import { ApiError } from "next/dist/server/api-utils" import { Skeleton } from "@/components/ui/skeleton" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog" interface ResourceEditProps { children: React.ReactNode id: string; onRefresh: () => void; } export default function ResourceEdit({ children, id, onRefresh }: ResourceEditProps) { const [open, setOpen] = useState(false); const { data: resource, error, isLoading, mutate } = useSWR( open ? [`/api/admin/web/resource/${id}`] : null, () => AdminApi.web.resource.get(id), { revalidateOnFocus: false, revalidateOnReconnect: false, revalidateIfStale: false, dedupingInterval: 5000, } ) const handleSubmit = async () => { if (!resource) return; try { await AdminApi.web.resource.update(id, { title: resource.title, description: resource.description, imageUrl: resource.imageUrl, link: resource.link, tags: resource.tags, }); toast.success("资源更新成功"); onRefresh(); setOpen(false); } catch (error) { toast.error((error as ApiError).message || "资源更新失败"); } } const handleRemove = async (id: string) => { try { await AdminApi.web.resource.remove(id); toast.success("资源删除成功"); onRefresh(); setOpen(false); } catch (error) { toast.error((error as ApiError).message || "资源删除失败"); } } return ( {children} 编辑资源 { isLoading && ( [...Array(5)].map((_, index) => ( )) ) } {resource && ( <>
mutate({ ...resource, title: e.target.value }, false)} className="col-span-3" />