2022-03-17 07:29:08 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
import { useEffect, RefObject, useState } from 'react'
|
|
|
|
import { Contract } from '../../common/contract'
|
2022-04-19 05:16:08 +00:00
|
|
|
import { trackView } from '../lib/firebase/tracking'
|
2022-03-17 07:29:08 +00:00
|
|
|
import { useIsVisible } from './use-is-visible'
|
|
|
|
|
|
|
|
export const useSeenContracts = () => {
|
|
|
|
const [seenContracts, setSeenContracts] = useState<{
|
|
|
|
[contractId: string]: number
|
|
|
|
}>({})
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setSeenContracts(getSeenContracts())
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return seenContracts
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useSaveSeenContract = (
|
|
|
|
ref: RefObject<Element>,
|
2022-04-19 05:16:08 +00:00
|
|
|
contract: Contract
|
2022-03-17 07:29:08 +00:00
|
|
|
) => {
|
|
|
|
const isVisible = useIsVisible(ref)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isVisible) {
|
|
|
|
const newSeenContracts = {
|
|
|
|
...getSeenContracts(),
|
|
|
|
[contract.id]: Date.now(),
|
|
|
|
}
|
|
|
|
localStorage.setItem(key, JSON.stringify(newSeenContracts))
|
2022-04-05 03:28:38 +00:00
|
|
|
|
2022-04-19 05:16:08 +00:00
|
|
|
trackView(contract.id)
|
2022-03-17 07:29:08 +00:00
|
|
|
}
|
2022-04-19 05:16:08 +00:00
|
|
|
}, [isVisible, contract])
|
2022-03-17 07:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const key = 'feed-seen-contracts'
|
|
|
|
|
|
|
|
const getSeenContracts = () => {
|
|
|
|
return _.mapValues(
|
|
|
|
JSON.parse(localStorage.getItem(key) ?? '{}'),
|
|
|
|
(time) => +time
|
|
|
|
)
|
|
|
|
}
|