short timestamp on mobile fixed and improved

Massimo Melina committed Jan 13, 2022 at 11:32 UTC af4bac5840a217e5359440ca5ddb15cf7626469c
1 file changed +14 -10
frontend/src/BrowseFiles.ts
+14 -10
@@ -1,6 +1,6 @@
1 import { Link, useLocation } from 'react-router-dom'
2 -import { createContext, createElement as h, Fragment, useContext, useEffect, useMemo } from 'react'
3 -import { formatBytes, hError, hIcon, Html, useForceUpdate, hfsEvent } from './misc'
2 +import { createContext, createElement as h, Fragment, useContext, useEffect, useMemo, useState } from 'react'
3 +import { formatBytes, hError, hIcon, Html, hfsEvent } from './misc'
4 import { Spinner } from './components'
5 import { Head } from './Head'
6 import { state, useSnapState } from './state'
@@ -43,15 +43,19 @@ function FilesList() {
43 }
44
45 function useMidnight() {
46 - const midnight = new Date()
47 - midnight.setHours(0,0,0,0)
48 - const [forceUpdate] = useForceUpdate()
49 - useEffect(()=>{
50 - const nextMidnight = new Date(midnight) // as an optimization we calculate this only once per list
51 - nextMidnight.setDate( 1 + nextMidnight.getDate() )
52 - setTimeout(forceUpdate, +nextMidnight - +midnight)
53 - },[])
46 + const [midnight, setMidnight] = useState(calcMidnight)
47 + useEffect(() => {
48 + setTimeout(()=> setMidnight(calcMidnight()), 10 * 60_000) // refresh every 10 minutes
49 + }, [])
50 return midnight
51 +
52 + function calcMidnight() {
53 + const recent = new Date()
54 + recent.setHours(recent.getHours() - 6)
55 + const midnight = new Date()
56 + midnight.setHours(0,0,0,0)
57 + return recent < midnight ? recent : midnight
58 + }
59 }
60
61 function isMobile() {