Make RelativeTimestamp a little more efficient (#754)

* Don't do extra dayjs work in timestamp components

* Remove extra wrapper from `RelativeTimestamp`
This commit is contained in:
Marshall Polaris 2022-08-12 17:48:41 -07:00 committed by GitHub
parent 79be0c555b
commit 96a378ec4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 23 deletions

View File

@ -83,12 +83,10 @@ export function MiscDetails(props: {
{!hideGroupLink && groupLinks && groupLinks.length > 0 && (
<SiteLink
href={groupPath(groupLinks[0].slug)}
className="text-sm text-gray-400"
className="line-clamp-1 text-sm text-gray-400"
>
<Row className={'line-clamp-1 flex-wrap items-center '}>
<UserGroupIcon className="mx-1 mb-0.5 inline h-4 w-4 shrink-0" />
{groupLinks[0].name}
</Row>
</SiteLink>
)}
</Row>
@ -211,7 +209,7 @@ export function ContractDetails(props: {
<>
<DateTimeTooltip
text="Market resolved:"
time={contract.resolutionTime}
time={dayjs(contract.resolutionTime)}
>
{resolvedDate}
</DateTimeTooltip>
@ -267,13 +265,16 @@ function EditableCloseDate(props: {
}) {
const { closeTime, contract, isCreator } = props
const dayJsCloseTime = dayjs(closeTime)
const dayJsNow = dayjs()
const [isEditingCloseTime, setIsEditingCloseTime] = useState(false)
const [closeDate, setCloseDate] = useState(
closeTime && dayjs(closeTime).format('YYYY-MM-DDTHH:mm')
closeTime && dayJsCloseTime.format('YYYY-MM-DDTHH:mm')
)
const isSameYear = dayjs(closeTime).isSame(dayjs(), 'year')
const isSameDay = dayjs(closeTime).isSame(dayjs(), 'day')
const isSameYear = dayJsCloseTime.isSame(dayJsNow, 'year')
const isSameDay = dayJsCloseTime.isSame(dayJsNow, 'day')
const onSave = () => {
const newCloseTime = dayjs(closeDate).valueOf()
@ -314,11 +315,11 @@ function EditableCloseDate(props: {
) : (
<DateTimeTooltip
text={closeTime > Date.now() ? 'Trading ends:' : 'Trading ended:'}
time={closeTime}
time={dayJsCloseTime}
>
{isSameYear
? dayjs(closeTime).format('MMM D')
: dayjs(closeTime).format('MMM D, YYYY')}
? dayJsCloseTime.format('MMM D')
: dayJsCloseTime.format('MMM D, YYYY')}
{isSameDay && <> ({fromNow(closeTime)})</>}
</DateTimeTooltip>
)}

View File

@ -1,4 +1,4 @@
import dayjs from 'dayjs'
import dayjs, { Dayjs } from 'dayjs'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'
import advanced from 'dayjs/plugin/advancedFormat'
@ -9,18 +9,19 @@ dayjs.extend(timezone)
dayjs.extend(advanced)
export function DateTimeTooltip(props: {
time: number
time: Dayjs
text?: string
className?: string
children?: React.ReactNode
noTap?: boolean
}) {
const { time, text, noTap } = props
const { className, time, text, noTap } = props
const formattedTime = dayjs(time).format('MMM DD, YYYY hh:mm a z')
const formattedTime = time.format('MMM DD, YYYY hh:mm a z')
const toolTip = text ? `${text} ${formattedTime}` : formattedTime
return (
<Tooltip text={toolTip} noTap={noTap}>
<Tooltip className={className} text={toolTip} noTap={noTap}>
{props.children}
</Tooltip>
)

View File

@ -7,6 +7,7 @@ import { fromNow } from 'web/lib/util/time'
import { ToastClipboard } from 'web/components/toast-clipboard'
import { LinkIcon } from '@heroicons/react/outline'
import clsx from 'clsx'
import dayjs from 'dayjs'
export function CopyLinkDateTimeComponent(props: {
prefix: string
@ -17,6 +18,7 @@ export function CopyLinkDateTimeComponent(props: {
}) {
const { prefix, slug, elementId, createdTime, className } = props
const [showToast, setShowToast] = useState(false)
const time = dayjs(createdTime)
function copyLinkToComment(
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>
@ -30,7 +32,7 @@ export function CopyLinkDateTimeComponent(props: {
}
return (
<div className={clsx('inline', className)}>
<DateTimeTooltip time={createdTime} noTap>
<DateTimeTooltip time={time} noTap>
<Link href={`/${prefix}/${slug}#${elementId}`} passHref={true}>
<a
onClick={(event) => copyLinkToComment(event)}

View File

@ -1,14 +1,16 @@
import { DateTimeTooltip } from './datetime-tooltip'
import { fromNow } from 'web/lib/util/time'
import dayjs from 'dayjs'
import React from 'react'
export function RelativeTimestamp(props: { time: number }) {
const { time } = props
const dayJsTime = dayjs(time)
return (
<DateTimeTooltip time={time}>
<span className="ml-1 whitespace-nowrap text-gray-400">
{fromNow(time)}
</span>
<DateTimeTooltip
className="ml-1 whitespace-nowrap text-gray-400"
time={dayJsTime}
>
{dayJsTime.fromNow()}
</DateTimeTooltip>
)
}