Skip to content

Commit

Permalink
Merge pull request tangly1024#1354 from tangly1024/fix/random-post-jump
Browse files Browse the repository at this point in the history
fix 随便逛逛两次点击报错
  • Loading branch information
tangly1024 committed Jul 27, 2023
2 parents a3f7daf + 75f2cc0 commit a434453
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 5 additions & 1 deletion components/CustomContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRouter } from 'next/router'
import { useEffect, useState, useRef } from 'react'
import { useGlobal } from '@/lib/global'
import { saveDarkModeToCookies, THEMES } from '@/themes/theme'
import BLOG from '@/blog.config'

/**
* 自定义右键菜单
Expand All @@ -17,10 +18,13 @@ export default function CustomContextMenu(props) {

const { latestPosts } = props
const router = useRouter()
/**
* 随机跳转文章
*/
function handleJumpToRandomPost() {
const randomIndex = Math.floor(Math.random() * latestPosts.length)
const randomPost = latestPosts[randomIndex]
router.push(randomPost.slug)
router.push(`${BLOG.SUB_PATH}/${randomPost?.slug}`)
}

useEffect(() => {
Expand Down
6 changes: 4 additions & 2 deletions themes/heo/components/Hero.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ function BannerGroup(props) {
function Banner(props) {
const router = useRouter()
const { latestPosts } = props
// 跳转到任意文章
/**
* 随机跳转文章
*/
function handleClickBanner() {
const randomIndex = Math.floor(Math.random() * latestPosts.length)
const randomPost = latestPosts[randomIndex]
router.push(randomPost.slug)
router.push(`${BLOG.SUB_PATH}/${randomPost?.slug}`)
}

return <div id='banners' onClick={handleClickBanner} className="hidden xl:flex xl:flex-col group h-full bg-white dark:bg-[#1e1e1e] rounded-xl border dark:border-gray-700 mb-3 relative overflow-hidden">
Expand Down
11 changes: 9 additions & 2 deletions themes/heo/components/RandomPostButton.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import BLOG from '@/blog.config'
import { useGlobal } from '@/lib/global'
import { useRouter } from 'next/router'

/**
Expand All @@ -6,13 +8,18 @@ import { useRouter } from 'next/router'
export default function RandomPostButton(props) {
const { latestPosts } = props
const router = useRouter()
const { locale } = useGlobal()
/**
* 随机跳转文章
*/
function handleClick() {
const randomIndex = Math.floor(Math.random() * latestPosts.length)
const randomPost = latestPosts[randomIndex]
router.push(randomPost.slug)
router.push(`${BLOG.SUB_PATH}/${randomPost?.slug}`)
}

return (
<div title={'随机前往一篇文章'} className='cursor-pointer hover:bg-black hover:bg-opacity-10 rounded-full w-10 h-10 flex justify-center items-center duration-200 transition-all' onClick={handleClick}>
<div title={locale.MENU.WALK_AROUND} className='cursor-pointer hover:bg-black hover:bg-opacity-10 rounded-full w-10 h-10 flex justify-center items-center duration-200 transition-all' onClick={handleClick}>
<i className="fa-solid fa-podcast"></i>
</div>
)
Expand Down

0 comments on commit a434453

Please sign in to comment.