about summary refs log tree commit diff
path: root/src/view/com/util/TimeElapsed.tsx
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2024-06-18 10:55:02 -0500
committerGitHub <noreply@github.com>2024-06-18 10:55:02 -0500
commit443beda74190b5af3083625116e9a9fdd4aa0fe0 (patch)
treeda09e78eccc720239155bd65ba0ddd53bd4cd22f /src/view/com/util/TimeElapsed.tsx
parent08cfb0958907408933982ece4a16d59625b423b0 (diff)
downloadvoidsky-443beda74190b5af3083625116e9a9fdd4aa0fe0.tar.zst
Add `useGetTimeAgo` and utils (#4556)
* Create a testable version of ago() and re-enable the disabled test (#4364)

* Enable the test of ago()

* Use test cases

This puts the input and the expected values next to each other.

* Create dateDiff function

This is a copy of ago(), but with the ability to specify the second date instead of using Date.now().

* Let ago() use dateDiff()

* Move constants close to usage

* Test dateDiff instead of ago

This makes it possible to test the dates without being forced to rely on what the current date is.

The commented out tests do not yet pass. This is fixed in later commits.

* Update dateDiff and enable the remaining tests

* Split up tests, use date-fns as helpers

* Remove old test

* Add long format

* Add hook

* Migrate to hooks

* Delete old code

* Or equal to

* Update comment

---------

Co-authored-by: Jan Aagaard <jan@aagaard.net>
Diffstat (limited to 'src/view/com/util/TimeElapsed.tsx')
-rw-r--r--src/view/com/util/TimeElapsed.tsx12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/view/com/util/TimeElapsed.tsx b/src/view/com/util/TimeElapsed.tsx
index a5d3a5372..d939b3163 100644
--- a/src/view/com/util/TimeElapsed.tsx
+++ b/src/view/com/util/TimeElapsed.tsx
@@ -1,26 +1,26 @@
 import React from 'react'
 
+import {useGetTimeAgo} from '#/lib/hooks/useTimeAgo'
 import {useTickEveryMinute} from '#/state/shell'
-import {ago} from 'lib/strings/time'
 
 export function TimeElapsed({
   timestamp,
   children,
-  timeToString = ago,
+  timeToString,
 }: {
   timestamp: string
   children: ({timeElapsed}: {timeElapsed: string}) => JSX.Element
   timeToString?: (timeElapsed: string) => string
 }) {
+  const ago = useGetTimeAgo()
+  const format = timeToString ?? ago
   const tick = useTickEveryMinute()
-  const [timeElapsed, setTimeAgo] = React.useState(() =>
-    timeToString(timestamp),
-  )
+  const [timeElapsed, setTimeAgo] = React.useState(() => format(timestamp))
 
   const [prevTick, setPrevTick] = React.useState(tick)
   if (prevTick !== tick) {
     setPrevTick(tick)
-    setTimeAgo(timeToString(timestamp))
+    setTimeAgo(format(timestamp))
   }
 
   return children({timeElapsed})