Skip to content

Commit

Permalink
fix: hydration error
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Apr 23, 2024
1 parent 185b5d5 commit 0132723
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/components/modules/post/PostMetaBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { FloatPopover } from '~/components/ui/float-popover'
import { useModalStack } from '~/components/ui/modal'
import { NumberSmoothTransition } from '~/components/ui/number-transition/NumberSmoothTransition'
import { RelativeTime } from '~/components/ui/relative-time'
import { useIsClient } from '~/hooks/common/use-is-client'
import { clsxm } from '~/lib/helper'
import { routeBuilder, Routes } from '~/lib/route-builder'

Expand All @@ -24,6 +25,7 @@ export const PostMetaBar: Component<{
}> = ({ className, meta, children }) => {
const { present } = useModalStack()
const router = useRouter()
const isClient = useIsClient()
return (
<div
className={clsxm(
Expand All @@ -40,7 +42,7 @@ export const PostMetaBar: Component<{
</span>
</>
)}
{!!meta.modified && (
{!!meta.modified && isClient ? (
<FloatPopover
mobileAsSheet
wrapperClassName="text-xs"
Expand All @@ -50,6 +52,8 @@ export const PostMetaBar: Component<{
>
编辑于 <RelativeTime date={meta.modified} />
</FloatPopover>
) : (
<span className="text-xs">(已编辑)</span>
)}
</div>

Expand Down
30 changes: 15 additions & 15 deletions src/components/ui/relative-time/RelativeTime.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
'use client'

import { useEffect, useState } from 'react'
import { Fragment, useEffect, useState } from 'react'
import dayjs from 'dayjs'
import type { FC } from 'react'

import { parseDate, relativeTimeFromNow } from '~/lib/datetime'

const formatTime = (date: string | Date, relativeBeforeDay?: number) => {
if (
relativeBeforeDay &&
Math.abs(dayjs(date).diff(new Date(), 'd')) > relativeBeforeDay
) {
return parseDate(date, 'YYYY 年 M 月 D 日 dddd')
}
return relativeTimeFromNow(date)
}
export const RelativeTime: FC<{
date: string | Date
displayAbsoluteTimeAfterDay?: number
}> = (props) => {
const { displayAbsoluteTimeAfterDay = 29 } = props
const [relative, setRelative] = useState<string>(
relativeTimeFromNow(props.date),
formatTime(props.date, displayAbsoluteTimeAfterDay),
)

const { displayAbsoluteTimeAfterDay = 29 } = props

useEffect(() => {
setRelative(relativeTimeFromNow(props.date))
setRelative(formatTime(props.date, displayAbsoluteTimeAfterDay))
let timer: any = setInterval(() => {
setRelative(relativeTimeFromNow(props.date))
setRelative(formatTime(props.date, displayAbsoluteTimeAfterDay))
}, 1000)

if (
Math.abs(dayjs(props.date).diff(new Date(), 'd')) >
displayAbsoluteTimeAfterDay
) {
timer = clearInterval(timer)
// @ts-expect-error
setRelative(parseDate(props.date, 'YY 年 M 月 D 日'))
}
return () => {
timer = clearInterval(timer)
}
}, [props.date, displayAbsoluteTimeAfterDay])

return <>{relative}</>
return <Fragment>{relative}</Fragment>
}

0 comments on commit 0132723

Please sign in to comment.