refactor: 调整复制博客URL到通用函数
This commit is contained in:
@@ -17,6 +17,7 @@ import { useState } from "react";
|
|||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { BlogPermissionCheckBoxs } from "./BlogPermissionCheckBoxs";
|
import { BlogPermissionCheckBoxs } from "./BlogPermissionCheckBoxs";
|
||||||
import { AdminAPI } from "@/lib/api/client";
|
import { AdminAPI } from "@/lib/api/client";
|
||||||
|
import { handleCopyShareURL } from "./utils";
|
||||||
|
|
||||||
interface AddBlogProps {
|
interface AddBlogProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
@@ -34,30 +35,6 @@ export default function AddBlog({ children, onRefresh }: AddBlogProps) {
|
|||||||
password: "",
|
password: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleCopyShareURL = () => {
|
|
||||||
const slug = blog.slug.trim();
|
|
||||||
if (slug.length === 0) {
|
|
||||||
return toast.warning('请先填写Slug')
|
|
||||||
}
|
|
||||||
|
|
||||||
let url = `${window.location.origin}/blog/${slug}`;
|
|
||||||
|
|
||||||
const password = blog.password.trim();
|
|
||||||
if (blog.permissions.includes(BlogPermission.ByPassword)) {
|
|
||||||
if (password.length === 0) {
|
|
||||||
return toast.warning('开启了密码保护,但没有填写有效的密码,无法生成有效URL')
|
|
||||||
} else {
|
|
||||||
url += `?p=${blog.password.trim()}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
navigator.clipboard.writeText(url).then(() => {
|
|
||||||
toast.success('复制成功');
|
|
||||||
}, () => {
|
|
||||||
toast.error('复制失败,请手动复制');
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await AdminAPI.createBlog({
|
const res = await AdminAPI.createBlog({
|
||||||
@@ -175,7 +152,11 @@ export default function AddBlog({ children, onRefresh }: AddBlogProps) {
|
|||||||
</div>
|
</div>
|
||||||
<DialogFooter >
|
<DialogFooter >
|
||||||
<div className="flex justify-between w-full">
|
<div className="flex justify-between w-full">
|
||||||
<Button type="button" variant='outline' onClick={handleCopyShareURL}>复制分享链接</Button>
|
<Button type="button" variant='outline' onClick={() => handleCopyShareURL({
|
||||||
|
slug: blog.slug,
|
||||||
|
password: blog.password,
|
||||||
|
permissions: blog.permissions,
|
||||||
|
})}>复制分享链接</Button>
|
||||||
<div>
|
<div>
|
||||||
<Button type="button" variant='secondary' onClick={() => setOpen(false)}>取消</Button>
|
<Button type="button" variant='secondary' onClick={() => setOpen(false)}>取消</Button>
|
||||||
<Button type="button" onClick={handleSubmit}>保存</Button>
|
<Button type="button" onClick={handleSubmit}>保存</Button>
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { BlogPermission } from "@/lib/types/Blog.Permission.enum";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
export function handleCopyShareURL(data: {
|
||||||
|
slug: string;
|
||||||
|
password: string;
|
||||||
|
permissions: BlogPermission[];
|
||||||
|
}) {
|
||||||
|
const slug = data.slug.trim();
|
||||||
|
const password = data.password.trim();
|
||||||
|
const permissions = data.permissions;
|
||||||
|
|
||||||
|
if (slug.length === 0) {
|
||||||
|
return toast.warning('请先填写Slug')
|
||||||
|
}
|
||||||
|
|
||||||
|
let url = `${window.location.origin}/blog/${slug}`;
|
||||||
|
|
||||||
|
if (permissions.includes(BlogPermission.ByPassword)) {
|
||||||
|
if (password.length === 0) {
|
||||||
|
return toast.warning('开启了密码保护,但无法获取有效的密码,无法生成有效URL')
|
||||||
|
} else {
|
||||||
|
url += `?p=${password}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
navigator.clipboard.writeText(url).then(() => {
|
||||||
|
toast.success('复制成功');
|
||||||
|
}, () => {
|
||||||
|
toast.error('复制失败,请手动复制');
|
||||||
|
});
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user