feat: 优化项目目录结构
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
'use client'
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog"
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { AdminApi } from "@/lib/api";
|
||||
import { BlogPermission } from "@/lib/types/Blog.Permission.enum";
|
||||
import { useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { BlogPermissionCheckBoxs } from "./BlogPermissionCheckBoxs";
|
||||
|
||||
interface AddBlogProps {
|
||||
children: React.ReactNode;
|
||||
onRefresh: () => void;
|
||||
}
|
||||
|
||||
export default function AddBlog({ children, onRefresh }: AddBlogProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [blog, setBlog] = useState({
|
||||
title: "",
|
||||
description: "",
|
||||
contentUrl: "",
|
||||
permissions: [] as BlogPermission[],
|
||||
password: "",
|
||||
});
|
||||
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
const res = await AdminApi.web.blog.create({
|
||||
...blog,
|
||||
});
|
||||
if (res) {
|
||||
setOpen(false);
|
||||
onRefresh();
|
||||
toast.success("添加成功");
|
||||
setBlog({
|
||||
title: '',
|
||||
description: '',
|
||||
contentUrl: '',
|
||||
permissions: [],
|
||||
password: '',
|
||||
})
|
||||
} else {
|
||||
throw new Error();
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error((error as Error).message || "添加失败");
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
{children}
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-[425px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>添加博客</DialogTitle>
|
||||
<DialogDescription>
|
||||
保存前请确认博客信息填写正确、权限配置合理
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="grid gap-4 py-4">
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="title" className="text-right">
|
||||
标题
|
||||
</Label>
|
||||
<Input
|
||||
id="title"
|
||||
className="col-span-3"
|
||||
value={blog.title}
|
||||
onChange={(e) => setBlog({ ...blog, title: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="description" className="text-right">
|
||||
描述
|
||||
</Label>
|
||||
<Input
|
||||
id="description"
|
||||
className="col-span-3"
|
||||
value={blog.description}
|
||||
onChange={(e) => setBlog({ ...blog, description: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="contentUrl" className="text-right">
|
||||
文章URL
|
||||
</Label>
|
||||
<Input
|
||||
id="contentUrl"
|
||||
className="col-span-3"
|
||||
value={blog.contentUrl}
|
||||
onChange={(e) => setBlog({ ...blog, contentUrl: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label className="text-right">
|
||||
文章权限
|
||||
</Label>
|
||||
<div className="col-span-3">
|
||||
{
|
||||
<BlogPermissionCheckBoxs
|
||||
permissions={blog.permissions}
|
||||
onCheckedChange={(p, n) => setBlog({
|
||||
...blog,
|
||||
permissions: n ?
|
||||
[...blog.permissions, p] :
|
||||
[...blog.permissions].filter(p => p !== p),
|
||||
})}
|
||||
/>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
{
|
||||
blog.permissions.includes(BlogPermission.ByPassword) &&
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="password" className="text-right">
|
||||
密码
|
||||
</Label>
|
||||
<Input
|
||||
id="password"
|
||||
className="col-span-3"
|
||||
value={blog.password}
|
||||
onChange={(e) => setBlog({ ...blog, password: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button type="button" variant='secondary' onClick={() => setOpen(false)}>取消</Button>
|
||||
<Button type="button" onClick={handleSubmit}>保存</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
"use client"
|
||||
|
||||
import React, { useState } from "react"
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
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 { AdminApi } from "@/lib/api"
|
||||
import useSWR from "swr"
|
||||
import { ApiError } from "next/dist/server/api-utils"
|
||||
import { BlogPermissionCheckBoxs } from "./BlogPermissionCheckBoxs"
|
||||
import { BlogPermission } from "@/lib/types/Blog.Permission.enum"
|
||||
import { SetPasswordDialog } from "./SetPasswordDialog"
|
||||
|
||||
interface BlogEditProps {
|
||||
id: string;
|
||||
children?: React.ReactNode;
|
||||
onRefresh: () => void;
|
||||
}
|
||||
|
||||
export default function BlogEdit({ id, children, onRefresh }: BlogEditProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const { data: blog, mutate } = useSWR(
|
||||
open ? `/api/admin/web/blog/${id}` : null,
|
||||
() => AdminApi.web.blog.get(id),
|
||||
{
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false,
|
||||
revalidateIfStale: false,
|
||||
dedupingInterval: 5000,
|
||||
}
|
||||
)
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!blog) return;
|
||||
try {
|
||||
await AdminApi.web.blog.update(id, {
|
||||
title: blog.title,
|
||||
description: blog.description,
|
||||
contentUrl: blog.contentUrl,
|
||||
permissions: blog.permissions,
|
||||
});
|
||||
toast.success("更新成功")
|
||||
setOpen(false);
|
||||
onRefresh();
|
||||
} catch (error) {
|
||||
toast.error((error as ApiError).message || "更新失败")
|
||||
}
|
||||
}
|
||||
|
||||
const handleDelete = async () => {
|
||||
try {
|
||||
await AdminApi.web.blog.remove(id);
|
||||
toast.success("删除成功")
|
||||
setOpen(false);
|
||||
onRefresh();
|
||||
} catch (error) {
|
||||
toast.error((error as ApiError).message || "删除失败")
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
{children}
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-[425px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>编辑博客</DialogTitle>
|
||||
<DialogDescription>
|
||||
保存前请确认博客信息填写正确、权限配置合理
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
{
|
||||
blog && (
|
||||
<>
|
||||
<div className="grid gap-4 py-4">
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="title" className="text-right">
|
||||
标题
|
||||
</Label>
|
||||
<Input
|
||||
id="title"
|
||||
className="col-span-3"
|
||||
value={blog.title}
|
||||
onChange={(e) => mutate({ ...blog, title: e.target.value }, false)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="description" className="text-right">
|
||||
描述
|
||||
</Label>
|
||||
<Input
|
||||
id="description"
|
||||
className="col-span-3"
|
||||
value={blog.description}
|
||||
onChange={(e) => mutate({ ...blog, description: e.target.value }, false)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="contentUrl" className="text-right">
|
||||
文章URL
|
||||
</Label>
|
||||
<Input
|
||||
id="contentUrl"
|
||||
className="col-span-3"
|
||||
value={blog.contentUrl}
|
||||
onChange={(e) => mutate({ ...blog, contentUrl: e.target.value }, false)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="permissions" className="text-right">
|
||||
文章权限
|
||||
</Label>
|
||||
<div className="col-span-3">
|
||||
<BlogPermissionCheckBoxs
|
||||
permissions={blog.permissions}
|
||||
onCheckedChange={(permission, newState) => {
|
||||
mutate({
|
||||
...blog,
|
||||
permissions: newState ?
|
||||
[...blog.permissions, permission] :
|
||||
blog.permissions.filter(p => p !== permission)
|
||||
}, false)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{
|
||||
blog.permissions.includes(BlogPermission.ByPassword) &&
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="permissions" className="text-right">
|
||||
文章保护密码
|
||||
</Label>
|
||||
<SetPasswordDialog id={id}>
|
||||
<Button variant='outline'>修改</Button>
|
||||
</SetPasswordDialog>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<div className="w-full flex justify-between">
|
||||
<div>
|
||||
<Button variant='destructive' onClick={handleDelete}>删除</Button>
|
||||
</div>
|
||||
<div>
|
||||
<Button type="button" variant='secondary' onClick={() => setOpen(false)}>取消</Button>
|
||||
<Button type="button" onClick={handleSubmit} className="ml-5">保存</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</>
|
||||
)
|
||||
}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { BlogPermission } from "@/lib/types/Blog.Permission.enum";
|
||||
|
||||
const blogPermissions = [
|
||||
{
|
||||
permission: BlogPermission.Public,
|
||||
localText: '公开可读',
|
||||
},
|
||||
{
|
||||
permission: BlogPermission.ByPassword,
|
||||
localText: '受密码保护',
|
||||
},
|
||||
{
|
||||
permission: BlogPermission.List,
|
||||
localText: '显示在列表中',
|
||||
},
|
||||
{
|
||||
permission: BlogPermission.AllowComments,
|
||||
localText: '允许评论',
|
||||
}
|
||||
] as const;
|
||||
|
||||
interface BlogPermissionCheckBoxsProps {
|
||||
permissions: BlogPermission[];
|
||||
onCheckedChange: (permission: BlogPermission, newState: boolean) => void;
|
||||
}
|
||||
|
||||
export function BlogPermissionCheckBoxs({ permissions, onCheckedChange }: BlogPermissionCheckBoxsProps) {
|
||||
return (
|
||||
<div className="flex gap-3 gap-x-8 flex-wrap">
|
||||
{
|
||||
blogPermissions.map((v, i) => (
|
||||
<div key={`blog-permission-option-${i}`} className="flex gap-2">
|
||||
<Checkbox
|
||||
id={`blog-permission-option-checkbox-${i}`}
|
||||
checked={permissions.includes(v.permission)}
|
||||
onCheckedChange={newChecked => onCheckedChange(v.permission, !!newChecked)} />
|
||||
<Label htmlFor={`blog-permission-option-checkbox-${i}`} className="whitespace-nowrap">{v.localText}</Label>
|
||||
</div>
|
||||
))
|
||||
}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCaption,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/components/ui/table"
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
|
||||
import { Blog } from "@/lib/types/blog"
|
||||
import BlogEdit from "./BlogEdit"
|
||||
import { Button } from "@/components/ui/button"
|
||||
|
||||
interface BlogTableProps {
|
||||
blogs: Blog[],
|
||||
error?: string,
|
||||
onRefresh: () => void,
|
||||
}
|
||||
|
||||
export default function BlogTable({ blogs, error, onRefresh }: BlogTableProps) {
|
||||
return (
|
||||
<Table className="w-full overflow-x-auto">
|
||||
{
|
||||
error && (
|
||||
<TableCaption>{error}</TableCaption>
|
||||
)
|
||||
}
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead className="w-[100px]">Id</TableHead>
|
||||
<TableHead>标题</TableHead>
|
||||
<TableHead>描述</TableHead>
|
||||
<TableHead>文章URL</TableHead>
|
||||
<TableHead className="text-right">操作</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{blogs.map((blog) => (
|
||||
<TableRow key={blog.id}>
|
||||
<TableCell className="font-medium">
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<div className="max-w-[100px] overflow-hidden text-ellipsis">{blog.id}</div>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>{blog.id}</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
</TableCell>
|
||||
<TableCell className="whitespace-normal break-all">{blog.title}</TableCell>
|
||||
<TableCell className="whitespace-normal break-all">{blog.description}</TableCell>
|
||||
<TableCell className="whitespace-normal break-all">{blog.contentUrl}</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<BlogEdit id={blog.id} onRefresh={() => onRefresh()}>
|
||||
<Button variant={'outline'} size={'sm'}>编辑</Button>
|
||||
</BlogEdit>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
'use client';
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { AdminApi } from "@/lib/api";
|
||||
import { base62 } from "@/lib/utils";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
interface SetPasswordDialogProps {
|
||||
id: string;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function SetPasswordDialog({ id, children }: SetPasswordDialogProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [password, setPassword] = useState('');
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!password) {
|
||||
return toast.error('请输入密码');
|
||||
}
|
||||
|
||||
await AdminApi.web.blog.setPassword(id, password).then(() => {
|
||||
toast.success('修改成功');
|
||||
setOpen(false);
|
||||
}).catch(e => {
|
||||
toast.error(`${e.message || e || '请求失败'}`)
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setPassword('');
|
||||
}
|
||||
}, [open])
|
||||
|
||||
const handleCopyShareURL = () => {
|
||||
if (!password) {
|
||||
return toast.warning('请先填写新密码');
|
||||
}
|
||||
const url = `${window.location.origin}/blog/${base62.encode(Buffer.from(id.replace(/-/g, ''), 'hex'))}?p=${password}`;
|
||||
navigator.clipboard.writeText(url);
|
||||
toast.success('分享链接复制成功,请点击保存按钮以提交新密码');
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={v => setOpen(v)}>
|
||||
<form>
|
||||
<DialogTrigger asChild>
|
||||
{children}
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-[425px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>修改密码</DialogTitle>
|
||||
<DialogDescription>
|
||||
通过密码访问受保护的文章,需开启“受密码保护”权限。注意复制URL需要填写完新密码后再点击
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="grid gap-4">
|
||||
<div className="grid gap-3">
|
||||
<Label htmlFor="new-password">新密码</Label>
|
||||
<Input
|
||||
id="new-password"
|
||||
name="password"
|
||||
value={password}
|
||||
onChange={e => setPassword(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<div className="w-full flex justify-between">
|
||||
<div>
|
||||
<Button variant='secondary' onClick={handleCopyShareURL}>复制URl</Button>
|
||||
</div>
|
||||
<div className="flex gap-5">
|
||||
<DialogClose asChild>
|
||||
<Button variant="outline">取消</Button>
|
||||
</DialogClose>
|
||||
<Button type="submit" onClick={handleSubmit}>保存</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</form>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
21
apps/frontend/app/console/(with-menu)/web/blog/page.tsx
Normal file
21
apps/frontend/app/console/(with-menu)/web/blog/page.tsx
Normal file
@@ -0,0 +1,21 @@
|
||||
"use client"
|
||||
|
||||
import { useBlogList } from "@/hooks/admin/web/blog/use-blog-list"
|
||||
import BlogTable from "./components/BlogTable"
|
||||
import AddBlog from "./components/AddBlog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
export default function Page() {
|
||||
const { blogs, refresh } = useBlogList();
|
||||
|
||||
return (
|
||||
<>
|
||||
<div>
|
||||
<AddBlog onRefresh={refresh}>
|
||||
<Button>添加博客</Button>
|
||||
</AddBlog>
|
||||
</div>
|
||||
<BlogTable blogs={blogs || []} onRefresh={refresh} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
"use client"
|
||||
|
||||
import React, { useState } from "react"
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
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 { AdminApi } from "@/lib/api"
|
||||
import { toast } from "sonner"
|
||||
import { ApiError } from "next/dist/server/api-utils"
|
||||
import { ResourceBadge } from "@/components/resource"
|
||||
import AddResourceTag from "./AddResourceTag"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import { Plus } from "lucide-react"
|
||||
|
||||
|
||||
interface AddResourceProps {
|
||||
children: React.ReactNode;
|
||||
refresh: () => void;
|
||||
}
|
||||
|
||||
export default function AddResource({ children, refresh }: AddResourceProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
title: "",
|
||||
description: "",
|
||||
imageUrl: "",
|
||||
link: "",
|
||||
tags: [] as { type: string, name: string }[],
|
||||
});
|
||||
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
await AdminApi.web.resource.create({
|
||||
...formData,
|
||||
});
|
||||
toast.success("添加成功");
|
||||
setOpen(false);
|
||||
refresh();
|
||||
} catch (error) {
|
||||
toast.error((error as ApiError).message || "添加失败");
|
||||
}
|
||||
}
|
||||
|
||||
const handleOpenChange = (open: boolean) => {
|
||||
setOpen(open);
|
||||
if (open) {
|
||||
setFormData({
|
||||
title: "",
|
||||
description: "",
|
||||
imageUrl: "",
|
||||
link: "",
|
||||
tags: [] as { type: string, name: string }[],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleOpenChange}>
|
||||
<DialogTrigger asChild>
|
||||
{children}
|
||||
</DialogTrigger>
|
||||
<DialogContent className="w-300">
|
||||
<DialogHeader>
|
||||
<DialogTitle>添加资源</DialogTitle>
|
||||
<DialogDescription>
|
||||
添加一个新的资源到资源列表中
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="grid gap-4 py-4">
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-title" className="text-right">
|
||||
标题
|
||||
</Label>
|
||||
<Input
|
||||
id="admin-web-resource-add-title"
|
||||
value={formData.title}
|
||||
onChange={(e) => setFormData({ ...formData, title: e.target.value })}
|
||||
className="col-span-3"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-description" className="text-right">
|
||||
描述
|
||||
</Label>
|
||||
<Textarea
|
||||
id="admin-web-resource-add-description"
|
||||
value={formData.description}
|
||||
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
||||
className="col-span-3 max-h-15"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-imageUrl" className="text-right">
|
||||
图标URL
|
||||
</Label>
|
||||
<Input
|
||||
id="admin-web-resource-add-imageUrl"
|
||||
value={formData.imageUrl}
|
||||
onChange={(e) => setFormData({ ...formData, imageUrl: e.target.value })}
|
||||
className="col-span-3"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-link" className="text-right">
|
||||
链接
|
||||
</Label>
|
||||
<Input
|
||||
id="admin-web-resource-add-link"
|
||||
value={formData.link}
|
||||
onChange={(e) => setFormData({ ...formData, link: e.target.value })}
|
||||
className="col-span-3"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-tags" className="text-right">
|
||||
标签
|
||||
</Label>
|
||||
<div className="flex items-center gap-2">
|
||||
{
|
||||
formData.tags.map((tag, index) => (
|
||||
<ResourceBadge tag={tag} key={index} editMode={true} onClose={name => setFormData({ ...formData, tags: formData.tags.filter(tag => tag.name !== name) })} />
|
||||
))
|
||||
}
|
||||
|
||||
<AddResourceTag onTagAdd={(tag) => {
|
||||
if (!tag.name) return;
|
||||
if (formData.tags.find(t => t.name === tag.name)) {
|
||||
toast.error("标签已存在");
|
||||
return;
|
||||
}
|
||||
setFormData({
|
||||
...formData,
|
||||
tags: [...formData.tags, tag],
|
||||
})
|
||||
}}>
|
||||
<Button size='sm' variant='outline'>
|
||||
<Plus />
|
||||
</Button>
|
||||
</AddResourceTag>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button type="submit" onClick={handleSubmit} disabled={loading}>保存</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
"use client";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import { useState } from "react";
|
||||
|
||||
interface AddResourceTagProps {
|
||||
children: React.ReactNode;
|
||||
onTagAdd: (tag: { name: string; type: string }) => void;
|
||||
}
|
||||
|
||||
export default function AddResourceTag({ onTagAdd, children }: AddResourceTagProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [tag, setTag] = useState({ name: '', type: 'default' });
|
||||
|
||||
function handleSetOpen(open: boolean) {
|
||||
setOpen(open);
|
||||
if (open) {
|
||||
setTag({ name: '', type: 'default' });
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Popover open={open} onOpenChange={handleSetOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
{children}
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-80">
|
||||
<div className="grid gap-4">
|
||||
<div className="space-y-2">
|
||||
<h4 className="font-medium leading-none">添加标签</h4>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<div className="grid grid-cols-3 items-center gap-4">
|
||||
<Label htmlFor="add-tag-name">内容</Label>
|
||||
<Input
|
||||
id="add-tag-name"
|
||||
className="col-span-2 h-8"
|
||||
value={tag.name}
|
||||
onChange={(e) => setTag({ ...tag, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-3 items-center gap-4">
|
||||
<Label htmlFor="add-tag-type">类型</Label>
|
||||
<Select onValueChange={v => setTag({ ...tag, type: v })} defaultValue="default">
|
||||
<SelectTrigger className="w-[185px]">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="default">默认</SelectItem>
|
||||
<SelectItem value="os">OS</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<Button onClick={() => {
|
||||
onTagAdd({
|
||||
...tag,
|
||||
});
|
||||
setOpen(false);
|
||||
}}>添加</Button>
|
||||
</div>
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
"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, isLoading, mutate } = useSWR<Resource>(
|
||||
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 (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
{children}
|
||||
</DialogTrigger>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>编辑资源</DialogTitle>
|
||||
</DialogHeader>
|
||||
{
|
||||
isLoading && (
|
||||
[...Array(5)].map((_, index) => (
|
||||
<Skeleton className="w-full h-14 rounded" key={index} />
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
{resource && (
|
||||
<>
|
||||
<div className="grid gap-4 py-4">
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-title" className="text-right">
|
||||
标题
|
||||
</Label>
|
||||
<Input
|
||||
id="admin-web-resource-add-title"
|
||||
value={resource.title}
|
||||
onChange={(e) => mutate({ ...resource, title: e.target.value }, false)}
|
||||
className="col-span-3"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-description" className="text-right">
|
||||
描述
|
||||
</Label>
|
||||
<Textarea
|
||||
id="admin-web-resource-add-description"
|
||||
value={resource.description}
|
||||
onChange={(e) => mutate({ ...resource, description: e.target.value }, false)}
|
||||
className="col-span-3 max-h-15"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-imageUrl" className="text-right">
|
||||
图标URL
|
||||
</Label>
|
||||
<Input
|
||||
id="admin-web-resource-add-imageUrl"
|
||||
value={resource.imageUrl}
|
||||
onChange={(e) => mutate({ ...resource, imageUrl: e.target.value }, false)}
|
||||
className="col-span-3"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-link" className="text-right">
|
||||
链接
|
||||
</Label>
|
||||
<Input
|
||||
id="admin-web-resource-add-link"
|
||||
value={resource.link}
|
||||
onChange={(e) => mutate({ ...resource, link: e.target.value }, false)}
|
||||
className="col-span-3"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="admin-web-resource-add-tags" className="text-right">
|
||||
标签
|
||||
</Label>
|
||||
<div className="col-span-3 w-full flex flex-wrap items-center gap-2 gap-y-1">
|
||||
{
|
||||
resource.tags.map((tag, index) => (
|
||||
<ResourceBadge tag={tag} key={index} editMode={true} onClose={name => mutate({ ...resource, tags: resource.tags.filter(tag => tag.name !== name) }, false)} />
|
||||
))
|
||||
}
|
||||
|
||||
<AddResourceTag onTagAdd={(tag) => {
|
||||
if (!tag.name) return;
|
||||
if (resource.tags.find(t => t.name === tag.name)) {
|
||||
toast.error("标签已存在");
|
||||
return;
|
||||
}
|
||||
mutate({
|
||||
...resource,
|
||||
tags: [...resource.tags, tag],
|
||||
}, false)
|
||||
}}>
|
||||
<Button size='sm' variant='outline'>
|
||||
<Plus />
|
||||
</Button>
|
||||
</AddResourceTag>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<div className="w-full flex justify-between">
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button variant={'destructive'}>删除</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>是否要删除该资源?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
该操作不可逆,删除后将无法恢复该资源
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>取消</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={() => handleRemove(id)}>删除</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
<div >
|
||||
<Button variant={'secondary'} onClick={() => setOpen(false)}>取消</Button>
|
||||
<Button type="button" onClick={handleSubmit} className="ml-3">保存</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
"use client"
|
||||
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCaption,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/components/ui/table"
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { Resource } from "@/lib/types/resource";
|
||||
import { ResourceBadge } from "@/components/resource";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import ResourceEdit from "./ResourceEdit";
|
||||
|
||||
interface ResourceTableProps {
|
||||
resources: Resource[];
|
||||
errorMessage?: string;
|
||||
onRefresh: () => void;
|
||||
}
|
||||
|
||||
export default function ResourceTable({ resources, errorMessage, onRefresh }: ResourceTableProps) {
|
||||
return (
|
||||
<>
|
||||
<Table>
|
||||
{errorMessage && <TableCaption>{errorMessage}</TableCaption>}
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead className="w-[100px]">Id</TableHead>
|
||||
<TableHead>标题</TableHead>
|
||||
<TableHead>描述</TableHead>
|
||||
<TableHead>图标URL</TableHead>
|
||||
<TableHead>链接</TableHead>
|
||||
<TableHead>标签</TableHead>
|
||||
<TableHead className="text-right w-10">操作</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{resources.length > 0 ? resources.map((resource) => (
|
||||
<TableRow key={resource.id}>
|
||||
<TableCell className="font-medium">
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<div className="max-w-[100px] overflow-hidden text-ellipsis">{resource.id}</div>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>{resource.id}</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
</TableCell>
|
||||
<TableCell className="whitespace-normal break-all">{resource.title}</TableCell>
|
||||
<TableCell className="whitespace-normal break-all">{resource.description}</TableCell>
|
||||
<TableCell className="whitespace-normal break-all">{resource.imageUrl}</TableCell>
|
||||
<TableCell className="whitespace-normal break-all">{resource.link}</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{resource.tags.map((tag, index) => (
|
||||
<ResourceBadge tag={tag} key={index} />
|
||||
))}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<ResourceEdit id={resource.id} onRefresh={() => onRefresh()}>
|
||||
<Button variant={'outline'} size={'sm'}>编辑</Button>
|
||||
</ResourceEdit>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)) : (
|
||||
<TableRow>
|
||||
<TableCell colSpan={7} className="text-center">
|
||||
<div className="my-5 text-zinc-500">暂无数据</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</>
|
||||
)
|
||||
}
|
||||
24
apps/frontend/app/console/(with-menu)/web/resource/page.tsx
Normal file
24
apps/frontend/app/console/(with-menu)/web/resource/page.tsx
Normal file
@@ -0,0 +1,24 @@
|
||||
"use client";
|
||||
|
||||
import { useResourceList } from "@/hooks/admin/web/resource/use-resource-list"
|
||||
import ResourceTable from "./components/ResourceTable"
|
||||
import { Button } from "@/components/ui/button";
|
||||
import AddResource from "./components/AddResource";
|
||||
|
||||
export default function Page() {
|
||||
const { resources, refresh } = useResourceList();
|
||||
|
||||
return (
|
||||
<>
|
||||
<div>
|
||||
<AddResource refresh={refresh}>
|
||||
<Button>添加资源</Button>
|
||||
</AddResource>
|
||||
</div>
|
||||
<ResourceTable
|
||||
resources={resources || []}
|
||||
onRefresh={refresh}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user