feat: 优化项目目录结构

This commit is contained in:
2025-12-12 17:25:26 +08:00
parent ae627d0496
commit b89f83291e
235 changed files with 0 additions and 0 deletions

View File

@@ -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>
)
}

View File

@@ -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>
)
}

View File

@@ -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>
)
}

View File

@@ -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>
</>
)
}

View 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}
/>
</>
)
}