mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 11:06:23 +00:00
✨ feat: 重构评论功能,支持删除和点赞,更新国际化文本,优化组件结构
This commit is contained in:
25
web/src/hooks/use-double-confirm.tsx
Normal file
25
web/src/hooks/use-double-confirm.tsx
Normal file
@ -0,0 +1,25 @@
|
||||
import { useRef, useState } from "react";
|
||||
|
||||
export function useDoubleConfirm(timeout = 2000) {
|
||||
const [confirming, setConfirming] = useState(false);
|
||||
const timer = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
const onClick = (callback: () => void) => {
|
||||
if (confirming) {
|
||||
setConfirming(false);
|
||||
if (timer.current) clearTimeout(timer.current);
|
||||
callback();
|
||||
} else {
|
||||
setConfirming(true);
|
||||
timer.current = setTimeout(() => setConfirming(false), timeout);
|
||||
}
|
||||
};
|
||||
|
||||
// 可选:失焦时自动取消
|
||||
const onBlur = () => {
|
||||
setConfirming(false);
|
||||
if (timer.current) clearTimeout(timer.current);
|
||||
};
|
||||
|
||||
return { confirming, onClick, onBlur };
|
||||
}
|
27
web/src/hooks/use-route.ts
Normal file
27
web/src/hooks/use-route.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { useRouter, usePathname } from "next/navigation"
|
||||
|
||||
/**
|
||||
* 用于跳转到登录页并自动带上 redirect_back 参数
|
||||
* 用法:const toLogin = useToLogin(); <Button onClick={toLogin}>去登录</Button>
|
||||
*/
|
||||
export function useToLogin() {
|
||||
const router = useRouter()
|
||||
const pathname = usePathname()
|
||||
return () => {
|
||||
router.push(`/login?redirect_back=${encodeURIComponent(pathname)}`)
|
||||
}
|
||||
}
|
||||
|
||||
export function clickToUserprofile(username: string) {
|
||||
const router = useRouter()
|
||||
return () => {
|
||||
router.push(`/user/${username}`)
|
||||
}
|
||||
}
|
||||
|
||||
export function clickToPost(postId: number) {
|
||||
const router = useRouter()
|
||||
return () => {
|
||||
router.push(`/p/${postId}`)
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
import { useRouter, usePathname } from "next/navigation"
|
||||
|
||||
/**
|
||||
* 用于跳转到登录页并自动带上 redirect_back 参数
|
||||
* 用法:const toLogin = useToLogin(); <Button onClick={toLogin}>去登录</Button>
|
||||
*/
|
||||
export function useToLogin() {
|
||||
const router = useRouter()
|
||||
const pathname = usePathname()
|
||||
return () => {
|
||||
router.push(`/login?redirect_back=${encodeURIComponent(pathname)}`)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user