From de432f06c3365b7c34b8888fa92a63f0560d747a Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Fri, 29 Apr 2022 14:32:51 -0600 Subject: [PATCH] WIP - got comments on the user page --- web/components/feed/feed-items.tsx | 12 +---- web/components/relative-timestamp.tsx | 14 +++++ web/components/user-page.tsx | 76 ++++++++++++++++++++++++++- web/lib/firebase/comments.ts | 8 +++ 4 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 web/components/relative-timestamp.tsx diff --git a/web/components/feed/feed-items.tsx b/web/components/feed/feed-items.tsx index 7518c998..89ddbcaf 100644 --- a/web/components/feed/feed-items.tsx +++ b/web/components/feed/feed-items.tsx @@ -50,6 +50,7 @@ import { trackClick } from '../../lib/firebase/tracking' import { firebaseLogin } from '../../lib/firebase/users' import { DAY_MS } from '../../../common/util/time' import NewContractBadge from '../new-contract-badge' +import { RelativeTimestamp } from '../relative-timestamp' export function FeedItems(props: { contract: Contract @@ -180,17 +181,6 @@ export function FeedComment(props: { ) } -function RelativeTimestamp(props: { time: number }) { - const { time } = props - return ( - - - {fromNow(time)} - - - ) -} - export function CommentInput(props: { contract: Contract }) { // see if we can comment input on any bet: const { contract } = props diff --git a/web/components/relative-timestamp.tsx b/web/components/relative-timestamp.tsx new file mode 100644 index 00000000..cd5ca713 --- /dev/null +++ b/web/components/relative-timestamp.tsx @@ -0,0 +1,14 @@ +import { DateTimeTooltip } from './datetime-tooltip' +import { fromNow } from '../lib/util/time' +import React from 'react' + +export function RelativeTimestamp(props: { time: number }) { + const { time } = props + return ( + + + {fromNow(time)} + + + ) +} diff --git a/web/components/user-page.tsx b/web/components/user-page.tsx index b73cffc4..3e23a625 100644 --- a/web/components/user-page.tsx +++ b/web/components/user-page.tsx @@ -12,6 +12,13 @@ import { Row } from './layout/row' import { LinkIcon } from '@heroicons/react/solid' import { genHash } from '../../common/util/random' import { PencilIcon } from '@heroicons/react/outline' +import { Tabs } from './layout/tabs' +import React, { useEffect, useState } from 'react' +import { Comment } from '../../common/comment' +import { getUsersComments } from '../lib/firebase/comments' +import { Bet } from '../../common/bet' +import { formatMoney } from '../../common/util/format' +import { RelativeTimestamp } from './relative-timestamp' export function UserLink(props: { name: string @@ -33,7 +40,20 @@ export function UserPage(props: { user: User; currentUser?: User }) { const { user, currentUser } = props const isCurrentUser = user.id === currentUser?.id const bannerUrl = user.bannerUrl ?? defaultBannerUrl(user.id) + const [comments, setComments] = useState([] as Comment[]) + useEffect(() => { + if (user) { + getUsersComments(user.id).then(setComments) + } + }, [user]) + + const items = comments + .sort((a, b) => b.createdTime - a.createdTime) + .map((comment) => ({ + comment, + bet: undefined, + })) return ( - - + , + }, + { + title: 'Comments', + content: ( + <> + {items.map((item, activityItemIdx) => ( +
+
+ +
+
+ ))} + + ), + }, + ]} + />
) @@ -157,3 +197,35 @@ export function defaultBannerUrl(userId: string) { ] return defaultBanner[genHash(userId)() % defaultBanner.length] } + +function ProfileComment(props: { comment: Comment; bet: Bet | undefined }) { + const { comment, bet } = props + let money: string | undefined + let outcome: string | undefined + let bought: string | undefined + if (bet) { + outcome = bet.outcome + bought = bet.amount >= 0 ? 'bought' : 'sold' + money = formatMoney(Math.abs(bet.amount)) + } + const { text, userUsername, userName, userAvatarUrl, createdTime } = comment + + return ( + <> + +
+
+

+ {' '} + +

+
+ {text} +
+ + ) +} diff --git a/web/lib/firebase/comments.ts b/web/lib/firebase/comments.ts index 65c86621..7ef44ccb 100644 --- a/web/lib/firebase/comments.ts +++ b/web/lib/firebase/comments.ts @@ -121,3 +121,11 @@ export async function getDailyComments( return commentsByDay } + +// TODO: add firebase index for comments - userid +export async function getUsersComments(userId: string) { + const getUsersCommentsQuery = (userId: string) => + query(collectionGroup(db, 'comments'), where('userId', '==', userId)) + const comments = await getValues(getUsersCommentsQuery(userId)) + return comments +}