* add TimeElapsed util component, integrate into PostThreadItem * integrate into posts * use consistent naming * use mobx and single interval for TimeElapsed
This commit is contained in:
parent
4515559b1a
commit
0ae52e91ce
4 changed files with 51 additions and 12 deletions
|
@ -224,6 +224,7 @@ export class ShellUiModel {
|
||||||
activeLightbox: ProfileImageLightbox | ImagesLightbox | null = null
|
activeLightbox: ProfileImageLightbox | ImagesLightbox | null = null
|
||||||
isComposerActive = false
|
isComposerActive = false
|
||||||
composerOpts: ComposerOpts | undefined
|
composerOpts: ComposerOpts | undefined
|
||||||
|
tickEveryMinute = Date.now()
|
||||||
|
|
||||||
constructor(public rootStore: RootStoreModel) {
|
constructor(public rootStore: RootStoreModel) {
|
||||||
makeAutoObservable(this, {
|
makeAutoObservable(this, {
|
||||||
|
@ -231,6 +232,8 @@ export class ShellUiModel {
|
||||||
rootStore: false,
|
rootStore: false,
|
||||||
hydrate: false,
|
hydrate: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
this.setupClock()
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize(): unknown {
|
serialize(): unknown {
|
||||||
|
@ -341,4 +344,10 @@ export class ShellUiModel {
|
||||||
this.isComposerActive = false
|
this.isComposerActive = false
|
||||||
this.composerOpts = undefined
|
this.composerOpts = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setupClock() {
|
||||||
|
setInterval(() => {
|
||||||
|
this.tickEveryMinute = Date.now()
|
||||||
|
}, 60_000)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ import {PostDropdownBtn} from '../util/forms/DropdownButton'
|
||||||
import * as Toast from '../util/Toast'
|
import * as Toast from '../util/Toast'
|
||||||
import {PreviewableUserAvatar} from '../util/UserAvatar'
|
import {PreviewableUserAvatar} from '../util/UserAvatar'
|
||||||
import {s} from 'lib/styles'
|
import {s} from 'lib/styles'
|
||||||
import {ago, niceDate} from 'lib/strings/time'
|
import {niceDate} from 'lib/strings/time'
|
||||||
import {sanitizeDisplayName} from 'lib/strings/display-names'
|
import {sanitizeDisplayName} from 'lib/strings/display-names'
|
||||||
import {pluralize} from 'lib/strings/helpers'
|
import {pluralize} from 'lib/strings/helpers'
|
||||||
import {getTranslatorLink, isPostInLanguage} from '../../../locale/helpers'
|
import {getTranslatorLink, isPostInLanguage} from '../../../locale/helpers'
|
||||||
|
@ -30,6 +30,7 @@ import {PostSandboxWarning} from '../util/PostSandboxWarning'
|
||||||
import {ErrorMessage} from '../util/error/ErrorMessage'
|
import {ErrorMessage} from '../util/error/ErrorMessage'
|
||||||
import {usePalette} from 'lib/hooks/usePalette'
|
import {usePalette} from 'lib/hooks/usePalette'
|
||||||
import {formatCount} from '../util/numeric/format'
|
import {formatCount} from '../util/numeric/format'
|
||||||
|
import {TimeElapsed} from 'view/com/util/TimeElapsed'
|
||||||
|
|
||||||
const PARENT_REPLY_LINE_LENGTH = 8
|
const PARENT_REPLY_LINE_LENGTH = 8
|
||||||
|
|
||||||
|
@ -189,7 +190,10 @@ export const PostThreadItem = observer(function PostThreadItem({
|
||||||
</Text>
|
</Text>
|
||||||
</Link>
|
</Link>
|
||||||
<Text type="md" style={[styles.metaItem, pal.textLight]}>
|
<Text type="md" style={[styles.metaItem, pal.textLight]}>
|
||||||
· {ago(item.post.indexedAt)}
|
·
|
||||||
|
<TimeElapsed timestamp={item.post.indexedAt}>
|
||||||
|
{({timeElapsed}) => <>{timeElapsed}</>}
|
||||||
|
</TimeElapsed>
|
||||||
</Text>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
<View style={s.flex1} />
|
<View style={s.flex1} />
|
||||||
|
|
|
@ -2,12 +2,13 @@ import React from 'react'
|
||||||
import {StyleSheet, View} from 'react-native'
|
import {StyleSheet, View} from 'react-native'
|
||||||
import {Text} from './text/Text'
|
import {Text} from './text/Text'
|
||||||
import {DesktopWebTextLink} from './Link'
|
import {DesktopWebTextLink} from './Link'
|
||||||
import {ago, niceDate} from 'lib/strings/time'
|
import {niceDate} from 'lib/strings/time'
|
||||||
import {usePalette} from 'lib/hooks/usePalette'
|
import {usePalette} from 'lib/hooks/usePalette'
|
||||||
import {UserAvatar} from './UserAvatar'
|
import {UserAvatar} from './UserAvatar'
|
||||||
import {observer} from 'mobx-react-lite'
|
import {observer} from 'mobx-react-lite'
|
||||||
import {sanitizeDisplayName} from 'lib/strings/display-names'
|
import {sanitizeDisplayName} from 'lib/strings/display-names'
|
||||||
import {isAndroid} from 'platform/detection'
|
import {isAndroid} from 'platform/detection'
|
||||||
|
import {TimeElapsed} from './TimeElapsed'
|
||||||
|
|
||||||
interface PostMetaOpts {
|
interface PostMetaOpts {
|
||||||
authorAvatar?: string
|
authorAvatar?: string
|
||||||
|
@ -64,15 +65,19 @@ export const PostMeta = observer(function (opts: PostMetaOpts) {
|
||||||
·
|
·
|
||||||
</Text>
|
</Text>
|
||||||
)}
|
)}
|
||||||
<DesktopWebTextLink
|
<TimeElapsed timestamp={opts.timestamp}>
|
||||||
type="md"
|
{({timeElapsed}) => (
|
||||||
style={pal.textLight}
|
<DesktopWebTextLink
|
||||||
lineHeight={1.2}
|
type="md"
|
||||||
text={ago(opts.timestamp)}
|
style={pal.textLight}
|
||||||
accessibilityLabel={niceDate(opts.timestamp)}
|
lineHeight={1.2}
|
||||||
accessibilityHint=""
|
text={timeElapsed}
|
||||||
href={opts.postHref}
|
accessibilityLabel={niceDate(opts.timestamp)}
|
||||||
/>
|
accessibilityHint=""
|
||||||
|
href={opts.postHref}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</TimeElapsed>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
21
src/view/com/util/TimeElapsed.tsx
Normal file
21
src/view/com/util/TimeElapsed.tsx
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import React from 'react'
|
||||||
|
import {observer} from 'mobx-react-lite'
|
||||||
|
import {ago} from 'lib/strings/time'
|
||||||
|
import {useStores} from 'state/index'
|
||||||
|
|
||||||
|
export const TimeElapsed = observer(function TimeElapsed({
|
||||||
|
timestamp,
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
timestamp: string
|
||||||
|
children: ({timeElapsed}: {timeElapsed: string}) => JSX.Element
|
||||||
|
}) {
|
||||||
|
const stores = useStores()
|
||||||
|
const [timeElapsed, setTimeAgo] = React.useState(ago(timestamp))
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
setTimeAgo(ago(timestamp))
|
||||||
|
}, [timestamp, setTimeAgo, stores.shell.tickEveryMinute])
|
||||||
|
|
||||||
|
return children({timeElapsed})
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue