Log views of contracts in feed

This commit is contained in:
James Grugett 2022-04-04 22:28:38 -05:00
parent f577437a8b
commit f749eb1df8
6 changed files with 35 additions and 4 deletions

4
common/view.ts Normal file
View File

@ -0,0 +1,4 @@
export type View = {
contractId: string
timestamp: number
}

View File

@ -22,6 +22,10 @@ service cloud.firestore {
allow read: if resource.data.id == request.auth.uid || isAdmin();
}
match /private-users/{userId}/views/{viewId} {
allow create: if userId == request.auth.uid;
}
match /contracts/{contractId} {
allow read;
allow update: if request.resource.data.diff(resource.data).affectedKeys()

View File

@ -47,6 +47,7 @@ export function ContractActivity(props: {
return (
<FeedItems
contract={contract}
user={user}
items={items}
betRowClassName={betRowClassName}
/>

View File

@ -52,14 +52,15 @@ import { Modal } from '../layout/modal'
export function FeedItems(props: {
contract: Contract
user: User | null | undefined
items: ActivityItem[]
betRowClassName?: string
}) {
const { contract, items, betRowClassName } = props
const { contract, user, items, betRowClassName } = props
const { outcomeType } = contract
const ref = useRef<HTMLDivElement | null>(null)
useSaveSeenContract(ref, contract)
useSaveSeenContract(ref, contract, user)
return (
<div className="flow-root pr-2 md:pr-0" ref={ref}>

View File

@ -1,6 +1,8 @@
import _ from 'lodash'
import { useEffect, RefObject, useState } from 'react'
import { Contract } from '../../common/contract'
import { User } from '../../common/user'
import { logView } from '../lib/firebase/views'
import { useIsVisible } from './use-is-visible'
export const useSeenContracts = () => {
@ -17,7 +19,8 @@ export const useSeenContracts = () => {
export const useSaveSeenContract = (
ref: RefObject<Element>,
contract: Contract
contract: Contract,
user: User | null | undefined
) => {
const isVisible = useIsVisible(ref)
@ -28,8 +31,10 @@ export const useSaveSeenContract = (
[contract.id]: Date.now(),
}
localStorage.setItem(key, JSON.stringify(newSeenContracts))
if (user) logView(user.id, contract.id)
}
}, [isVisible, contract])
}, [isVisible, contract, user])
}
const key = 'feed-seen-contracts'

16
web/lib/firebase/views.ts Normal file
View File

@ -0,0 +1,16 @@
import { doc, collection, setDoc } from 'firebase/firestore'
import _ from 'lodash'
import { db } from './init'
import { View } from '../../../common/view'
export async function logView(userId: string, contractId: string) {
const ref = doc(collection(db, 'private-users', userId, 'views'))
const view: View = {
contractId,
timestamp: Date.now(),
}
return await setDoc(ref, view)
}