Skip to content

Commit

Permalink
actually, we should update this continually
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff committed Nov 18, 2024
1 parent 13b8ef5 commit 533d59d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
28 changes: 10 additions & 18 deletions app/src/pages/ODD/ProtocolDashboard/ProtocolCard.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react'
import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { Trans, useTranslation } from 'react-i18next'
import { useQueryClient } from 'react-query'
import { formatDistance } from 'date-fns'
import last from 'lodash/last'
import { css } from 'styled-components'

Expand Down Expand Up @@ -34,6 +33,7 @@ import { SmallButton } from '/app/atoms/buttons'
import { OddModal } from '/app/molecules/OddModal'
import { LongPressModal } from './LongPressModal'
import { formatTimeWithUtcLabel } from '/app/resources/runs'
import { useUpdatedLastRunTime } from '/app/pages/ODD/ProtocolDashboard/hooks'

import type { UseLongPressResult } from '@opentrons/components'
import type { ProtocolResource } from '@opentrons/shared-data'
Expand All @@ -60,16 +60,17 @@ export function ProtocolCard(props: ProtocolCardProps): JSX.Element {
setIsRequiredCSV,
} = props
const navigate = useNavigate()
const [showIcon, setShowIcon] = React.useState<boolean>(false)
const [showIcon, setShowIcon] = useState<boolean>(false)
const [
showFailedAnalysisModal,
setShowFailedAnalysisModal,
] = React.useState<boolean>(false)
] = useState<boolean>(false)
const { t, i18n } = useTranslation(['protocol_info', 'branded'])
const protocolName = protocol.metadata.protocolName ?? protocol.files[0].name
const longpress = useLongPress()
const queryClient = useQueryClient()
const host = useHost()
const updatedLastRun = useUpdatedLastRunTime(lastRun)

const { id: protocolId, analysisSummaries } = protocol
const {
Expand Down Expand Up @@ -121,7 +122,7 @@ export function ProtocolCard(props: ProtocolCardProps): JSX.Element {
}
}

React.useEffect(() => {
useEffect(() => {
if (longpress.isLongPressed) {
longPress(true)
setTargetProtocolId(protocol.id)
Expand Down Expand Up @@ -195,13 +196,8 @@ export function ProtocolCard(props: ProtocolCardProps): JSX.Element {
if (isFailedAnalysis) protocolCardBackgroundColor = COLORS.red35
if (isRequiredCSV) protocolCardBackgroundColor = COLORS.yellow35

const textWrap = (lastRun?: string): string => {
if (lastRun != null) {
lastRun = formatDistance(new Date(lastRun), new Date(), {
addSuffix: true,
}).replace('about ', '')
}
return lastRun === 'less than a minute ago' ? 'normal' : 'nowrap'
const textWrap = (updatedLastRun: string): string => {
return updatedLastRun === 'less than a minute ago' ? 'normal' : 'nowrap'
}

return (
Expand Down Expand Up @@ -257,13 +253,9 @@ export function ProtocolCard(props: ProtocolCardProps): JSX.Element {
<LegacyStyledText
as="p"
color={COLORS.grey60}
whiteSpace={textWrap(lastRun)}
whiteSpace={textWrap(updatedLastRun)}
>
{lastRun != null
? formatDistance(new Date(lastRun), new Date(), {
addSuffix: true,
}).replace('about ', '')
: t('no_history')}
{updatedLastRun}
</LegacyStyledText>
</Flex>
<Flex width="12.5rem" whiteSpace={NO_WRAP}>
Expand Down
38 changes: 38 additions & 0 deletions app/src/pages/ODD/ProtocolDashboard/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useEffect, useState } from 'react'
import { formatDistance } from 'date-fns'
import { useTranslation } from 'react-i18next'

import type { TFunction } from 'i18next'

const UPDATE_TIME_INTERVAL_MS = 60000

// Given the last run timestamp, update the time since the last run on an interval.
export function useUpdatedLastRunTime(lastRun: string | undefined): string {
const { t } = useTranslation(['protocol_info'])

const [updatedLastRun, setUpdatedLastRun] = useState(() =>
computeLastRunFromNow(lastRun, t as TFunction)
)
useEffect(() => {
const timer = setInterval(() => {
setUpdatedLastRun(computeLastRunFromNow(lastRun, t as TFunction))
}, UPDATE_TIME_INTERVAL_MS)

return () => {
clearInterval(timer)
}
}, [lastRun, t])

return updatedLastRun
}

function computeLastRunFromNow(
lastRun: string | undefined,
t: TFunction
): string {
return lastRun != null
? formatDistance(new Date(lastRun), new Date(), {
addSuffix: true,
}).replace('about ', '')
: t('no_history')
}
3 changes: 2 additions & 1 deletion app/src/pages/ODD/ProtocolDashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ export function ProtocolDashboard(): JSX.Element {
</Flex>
<Flex flexDirection={DIRECTION_COLUMN}>
{sortedProtocols.map(protocol => {
const lastRun = runs.data?.data.find(
// Run data is ordered based on timestamp. We want the last time a matching run was ran.
const lastRun = runs.data?.data.findLast(
run => run.protocolId === protocol.id
)?.completedAt

Expand Down

0 comments on commit 533d59d

Please sign in to comment.