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

View File

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

View File

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

View File

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