acc9c84e2e
* Configure functions module to allow absolute imports * Convert common imports in functions to be absolute * Convert common imports in web to be absolute * Convert lib imports in web to be absolute * Convert hooks imports in web to be absolute * Convert components imports in web to be absolute
46 lines
1.0 KiB
TypeScript
46 lines
1.0 KiB
TypeScript
import _ from 'lodash'
|
|
import { useEffect, RefObject, useState } from 'react'
|
|
import { Contract } from 'common/contract'
|
|
import { trackView } from 'web/lib/firebase/tracking'
|
|
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>,
|
|
contract: Contract
|
|
) => {
|
|
const isVisible = useIsVisible(ref)
|
|
|
|
useEffect(() => {
|
|
if (isVisible) {
|
|
const newSeenContracts = {
|
|
...getSeenContracts(),
|
|
[contract.id]: Date.now(),
|
|
}
|
|
localStorage.setItem(key, JSON.stringify(newSeenContracts))
|
|
|
|
trackView(contract.id)
|
|
}
|
|
}, [isVisible, contract])
|
|
}
|
|
|
|
const key = 'feed-seen-contracts'
|
|
|
|
const getSeenContracts = () => {
|
|
return _.mapValues(
|
|
JSON.parse(localStorage.getItem(key) ?? '{}'),
|
|
(time) => +time
|
|
)
|
|
}
|