manifold/web/lib/firebase/comments.ts
Austin Chen 07ce27f20b
Show activity feed on each market & allow comments on your bets (#18)
* Copy feed template from TailwindUI

* Show all bets in a feed-like manner

* Tweak design of individual trades

* Allow traders to comment on their bets

* Code cleanups

* Incorporate contract description into the feed

* Support description editing from contract feed

* Group together bets placed within 24h

* Fix build error

* Add a feed item for market resolution

* Add a feed item for markets that have closed

* Comment on a separate subcollection
2022-01-03 23:21:14 -08:00

61 lines
1.5 KiB
TypeScript

import { doc, collection, onSnapshot, setDoc } from 'firebase/firestore'
import { db } from './init'
import { User } from './users'
// Currently, comments are created after the bet, not atomically with the bet.
// They're uniquely identified by the pair contractId/betId.
export type Comment = {
contractId: string
betId: string
text: string
createdTime: number
// Denormalized, for rendering comments
userName?: string
userUsername?: string
userAvatarUrl?: string
}
export async function createComment(
contractId: string,
betId: string,
text: string,
commenter: User
) {
const ref = doc(getCommentsCollection(contractId), betId)
return await setDoc(ref, {
contractId,
betId,
text,
createdTime: Date.now(),
userName: commenter.name,
userUsername: commenter.username,
userAvatarUrl: commenter.avatarUrl,
})
}
function getCommentsCollection(contractId: string) {
return collection(db, 'contracts', contractId, 'comments')
}
export function listenForComments(
contractId: string,
setComments: (comments: Comment[]) => void
) {
return onSnapshot(getCommentsCollection(contractId), (snap) => {
const comments = snap.docs.map((doc) => doc.data() as Comment)
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
setComments(comments)
})
}
// Return a map of betId -> comment
export function mapCommentsByBetId(comments: Comment[]) {
const map: Record<string, Comment> = {}
for (const comment of comments) {
map[comment.betId] = comment
}
return map
}