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