2022-06-01 13:11:25 +00:00
|
|
|
import { Tabs } from 'web/components/layout/tabs'
|
2022-07-07 20:41:50 +00:00
|
|
|
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
2022-06-01 13:11:25 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-07-06 19:30:51 +00:00
|
|
|
import { Notification, notification_source_types } from 'common/notification'
|
2022-06-23 17:55:41 +00:00
|
|
|
import { Avatar, EmptyAvatar } from 'web/components/avatar'
|
2022-06-01 13:11:25 +00:00
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { Title } from 'web/components/title'
|
|
|
|
import { doc, updateDoc } from 'firebase/firestore'
|
|
|
|
import { db } from 'web/lib/firebase/init'
|
|
|
|
import { UserLink } from 'web/components/user-page'
|
2022-06-06 17:36:59 +00:00
|
|
|
import { notification_subscribe_types, PrivateUser } from 'common/user'
|
2022-06-01 18:26:41 +00:00
|
|
|
import { Contract } from 'common/contract'
|
2022-06-06 17:36:59 +00:00
|
|
|
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
|
|
|
|
import { listenForPrivateUser, updatePrivateUser } from 'web/lib/firebase/users'
|
|
|
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
|
|
|
import clsx from 'clsx'
|
|
|
|
import { RelativeTimestamp } from 'web/components/relative-timestamp'
|
|
|
|
import { Linkify } from 'web/components/linkify'
|
|
|
|
import {
|
2022-06-09 16:30:26 +00:00
|
|
|
BinaryOutcomeLabel,
|
|
|
|
CancelLabel,
|
|
|
|
MultiLabel,
|
|
|
|
ProbPercentLabel,
|
2022-06-06 17:36:59 +00:00
|
|
|
} from 'web/components/outcome-label'
|
2022-06-06 22:15:36 +00:00
|
|
|
import {
|
|
|
|
NotificationGroup,
|
|
|
|
usePreferredGroupedNotifications,
|
|
|
|
} from 'web/hooks/use-notifications'
|
2022-07-05 17:29:26 +00:00
|
|
|
import { CheckIcon, TrendingUpIcon, XIcon } from '@heroicons/react/outline'
|
2022-06-06 17:36:59 +00:00
|
|
|
import toast from 'react-hot-toast'
|
2022-06-13 04:42:41 +00:00
|
|
|
import { formatMoney } from 'common/util/format'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { groupPath } from 'web/lib/firebase/groups'
|
2022-07-05 17:29:26 +00:00
|
|
|
import { UNIQUE_BETTOR_BONUS_AMOUNT } from 'common/numeric-constants'
|
2022-07-06 19:30:51 +00:00
|
|
|
import { groupBy, sum, uniq } from 'lodash'
|
2022-07-07 20:41:50 +00:00
|
|
|
import Custom404 from 'web/pages/404'
|
2022-07-07 21:06:29 +00:00
|
|
|
import { track } from '@amplitude/analytics-browser'
|
2022-07-05 17:29:26 +00:00
|
|
|
|
|
|
|
export const NOTIFICATIONS_PER_PAGE = 30
|
2022-07-06 19:30:51 +00:00
|
|
|
const MULTIPLE_USERS_KEY = 'multipleUsers'
|
2022-06-06 17:36:59 +00:00
|
|
|
|
2022-06-01 13:11:25 +00:00
|
|
|
export default function Notifications() {
|
|
|
|
const user = useUser()
|
2022-07-07 20:41:50 +00:00
|
|
|
const privateUser = usePrivateUser(user?.id)
|
|
|
|
|
|
|
|
if (!user) return <Custom404 />
|
|
|
|
return (
|
|
|
|
<Page>
|
|
|
|
<div className={'p-2 sm:p-4'}>
|
|
|
|
<Title text={'Notifications'} className={'hidden md:block'} />
|
|
|
|
<div>
|
|
|
|
<Tabs
|
|
|
|
labelClassName={'pb-2 pt-1 '}
|
|
|
|
className={'mb-0 sm:mb-2'}
|
|
|
|
defaultIndex={0}
|
|
|
|
tabs={[
|
|
|
|
{
|
|
|
|
title: 'Notifications',
|
|
|
|
content: privateUser ? (
|
|
|
|
<NotificationsList privateUser={privateUser} />
|
|
|
|
) : (
|
|
|
|
<LoadingIndicator />
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Settings',
|
|
|
|
content: (
|
|
|
|
<div className={''}>
|
|
|
|
<NotificationSettings />
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function NotificationsList(props: { privateUser: PrivateUser }) {
|
|
|
|
const { privateUser } = props
|
2022-07-05 17:29:26 +00:00
|
|
|
const [page, setPage] = useState(1)
|
2022-07-07 20:41:50 +00:00
|
|
|
const allGroupedNotifications = usePreferredGroupedNotifications(privateUser)
|
|
|
|
const [paginatedGroupedNotifications, setPaginatedGroupedNotifications] =
|
|
|
|
useState<NotificationGroup[] | undefined>(undefined)
|
2022-07-05 17:29:26 +00:00
|
|
|
|
2022-06-01 13:11:25 +00:00
|
|
|
useEffect(() => {
|
2022-07-07 20:41:50 +00:00
|
|
|
if (!allGroupedNotifications) return
|
2022-07-05 17:29:26 +00:00
|
|
|
const start = (page - 1) * NOTIFICATIONS_PER_PAGE
|
|
|
|
const end = start + NOTIFICATIONS_PER_PAGE
|
2022-07-07 20:41:50 +00:00
|
|
|
const maxNotificationsToShow = allGroupedNotifications.slice(start, end)
|
|
|
|
const remainingNotification = allGroupedNotifications.slice(end)
|
2022-07-05 17:29:26 +00:00
|
|
|
for (const notification of remainingNotification) {
|
|
|
|
if (notification.isSeen) break
|
|
|
|
else setNotificationsAsSeen(notification.notifications)
|
|
|
|
}
|
2022-07-07 20:41:50 +00:00
|
|
|
setPaginatedGroupedNotifications(maxNotificationsToShow)
|
|
|
|
}, [allGroupedNotifications, page])
|
2022-06-06 17:36:59 +00:00
|
|
|
|
2022-07-07 20:41:50 +00:00
|
|
|
if (!paginatedGroupedNotifications || !allGroupedNotifications)
|
2022-06-06 17:36:59 +00:00
|
|
|
return <LoadingIndicator />
|
2022-06-01 13:11:25 +00:00
|
|
|
|
|
|
|
return (
|
2022-07-07 20:41:50 +00:00
|
|
|
<div className={'min-h-[100vh]'}>
|
|
|
|
{paginatedGroupedNotifications.length === 0 && (
|
|
|
|
<div className={'mt-2'}>
|
|
|
|
You don't have any notifications. Try changing your settings to see
|
|
|
|
more.
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{paginatedGroupedNotifications.map((notification) =>
|
|
|
|
notification.type === 'income' ? (
|
|
|
|
<IncomeNotificationGroupItem
|
|
|
|
notificationGroup={notification}
|
|
|
|
key={notification.groupedById + notification.timePeriod}
|
|
|
|
/>
|
|
|
|
) : notification.notifications.length === 1 ? (
|
|
|
|
<NotificationItem
|
|
|
|
notification={notification.notifications[0]}
|
|
|
|
key={notification.notifications[0].id}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<NotificationGroupItem
|
|
|
|
notificationGroup={notification}
|
|
|
|
key={notification.groupedById + notification.timePeriod}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
{paginatedGroupedNotifications.length > 0 &&
|
|
|
|
allGroupedNotifications.length > NOTIFICATIONS_PER_PAGE && (
|
|
|
|
<nav
|
|
|
|
className="flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6"
|
|
|
|
aria-label="Pagination"
|
|
|
|
>
|
|
|
|
<div className="hidden sm:block">
|
|
|
|
<p className="text-sm text-gray-700">
|
|
|
|
Showing{' '}
|
|
|
|
<span className="font-medium">
|
|
|
|
{page === 1 ? page : (page - 1) * NOTIFICATIONS_PER_PAGE}
|
|
|
|
</span>{' '}
|
|
|
|
to{' '}
|
|
|
|
<span className="font-medium">
|
|
|
|
{page * NOTIFICATIONS_PER_PAGE}
|
|
|
|
</span>{' '}
|
|
|
|
of{' '}
|
|
|
|
<span className="font-medium">
|
|
|
|
{allGroupedNotifications.length}
|
|
|
|
</span>{' '}
|
|
|
|
results
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-1 justify-between sm:justify-end">
|
|
|
|
<a
|
|
|
|
href="#"
|
|
|
|
className="relative inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
|
|
|
|
onClick={() => page > 1 && setPage(page - 1)}
|
|
|
|
>
|
|
|
|
Previous
|
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
href="#"
|
|
|
|
className="relative ml-3 inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
|
|
|
|
onClick={() =>
|
|
|
|
page <
|
|
|
|
allGroupedNotifications?.length / NOTIFICATIONS_PER_PAGE &&
|
|
|
|
setPage(page + 1)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Next
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
)}
|
|
|
|
</div>
|
2022-06-01 13:11:25 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-05 17:29:26 +00:00
|
|
|
function IncomeNotificationGroupItem(props: {
|
|
|
|
notificationGroup: NotificationGroup
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const { notificationGroup, className } = props
|
|
|
|
const { notifications } = notificationGroup
|
|
|
|
const numSummaryLines = 3
|
|
|
|
const [expanded, setExpanded] = useState(false)
|
2022-07-06 19:30:51 +00:00
|
|
|
const [highlighted, setHighlighted] = useState(
|
|
|
|
notifications.some((n) => !n.isSeen)
|
|
|
|
)
|
|
|
|
|
2022-07-05 17:29:26 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setNotificationsAsSeen(notifications)
|
|
|
|
}, [notifications])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (expanded) setHighlighted(false)
|
|
|
|
}, [expanded])
|
|
|
|
|
2022-07-06 19:30:51 +00:00
|
|
|
const totalIncome = sum(
|
|
|
|
notifications.map((notification) =>
|
|
|
|
notification.sourceText ? parseInt(notification.sourceText) : 0
|
|
|
|
)
|
2022-07-05 17:29:26 +00:00
|
|
|
)
|
2022-07-06 19:30:51 +00:00
|
|
|
// Loop through the contracts and combine the notification items into one
|
|
|
|
function combineNotificationsByAddingNumericSourceTexts(
|
2022-07-05 17:29:26 +00:00
|
|
|
notifications: Notification[]
|
|
|
|
) {
|
|
|
|
const newNotifications = []
|
2022-07-06 19:30:51 +00:00
|
|
|
const groupedNotificationsBySourceType = groupBy(
|
2022-07-05 17:29:26 +00:00
|
|
|
notifications,
|
2022-07-06 19:30:51 +00:00
|
|
|
(n) => n.sourceType
|
2022-07-05 17:29:26 +00:00
|
|
|
)
|
2022-07-06 19:30:51 +00:00
|
|
|
for (const sourceType in groupedNotificationsBySourceType) {
|
|
|
|
const groupedNotificationsByContractId = groupBy(
|
|
|
|
groupedNotificationsBySourceType[sourceType],
|
|
|
|
(notification) => {
|
|
|
|
return notification.sourceContractId
|
|
|
|
}
|
2022-07-05 17:29:26 +00:00
|
|
|
)
|
2022-07-06 19:30:51 +00:00
|
|
|
for (const contractId in groupedNotificationsByContractId) {
|
|
|
|
const notificationsForContractId =
|
|
|
|
groupedNotificationsByContractId[contractId]
|
|
|
|
if (notificationsForContractId.length === 1) {
|
|
|
|
newNotifications.push(notificationsForContractId[0])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let sum = 0
|
|
|
|
notificationsForContractId.forEach(
|
|
|
|
(notification) =>
|
|
|
|
notification.sourceText &&
|
|
|
|
(sum = parseInt(notification.sourceText) + sum)
|
|
|
|
)
|
|
|
|
const uniqueUsers = uniq(
|
|
|
|
notificationsForContractId.map((notification) => {
|
|
|
|
return notification.sourceUserUsername
|
|
|
|
})
|
|
|
|
)
|
2022-07-05 17:29:26 +00:00
|
|
|
|
2022-07-06 19:30:51 +00:00
|
|
|
const newNotification = {
|
|
|
|
...notificationsForContractId[0],
|
|
|
|
sourceText: sum.toString(),
|
|
|
|
sourceUserUsername:
|
|
|
|
uniqueUsers.length > 1
|
|
|
|
? MULTIPLE_USERS_KEY
|
|
|
|
: notificationsForContractId[0].sourceType,
|
|
|
|
}
|
|
|
|
newNotifications.push(newNotification)
|
|
|
|
}
|
2022-07-05 17:29:26 +00:00
|
|
|
}
|
|
|
|
return newNotifications
|
|
|
|
}
|
|
|
|
|
|
|
|
const combinedNotifs =
|
2022-07-06 19:30:51 +00:00
|
|
|
combineNotificationsByAddingNumericSourceTexts(notifications)
|
2022-07-05 17:29:26 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
'relative cursor-pointer bg-white px-2 pt-6 text-sm',
|
|
|
|
className,
|
|
|
|
!expanded ? 'hover:bg-gray-100' : '',
|
|
|
|
highlighted && !expanded ? 'bg-indigo-200 hover:bg-indigo-100' : ''
|
|
|
|
)}
|
|
|
|
onClick={() => setExpanded(!expanded)}
|
|
|
|
>
|
|
|
|
{expanded && (
|
|
|
|
<span
|
|
|
|
className="absolute top-14 left-6 -ml-px h-[calc(100%-5rem)] w-0.5 bg-gray-200"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Row className={'items-center text-gray-500 sm:justify-start'}>
|
|
|
|
<TrendingUpIcon className={'text-primary h-7 w-7'} />
|
2022-07-07 20:41:50 +00:00
|
|
|
<div
|
|
|
|
className={'flex w-full flex-row flex-wrap pl-1 sm:pl-0'}
|
|
|
|
onClick={() => setExpanded(!expanded)}
|
|
|
|
>
|
|
|
|
<div className={'flex w-full flex-row justify-between'}>
|
|
|
|
<div>
|
2022-07-05 17:29:26 +00:00
|
|
|
{'Daily Income Summary: '}
|
2022-07-06 13:45:47 +00:00
|
|
|
<span className={'text-primary'}>
|
|
|
|
{'+' + formatMoney(totalIncome)}
|
|
|
|
</span>
|
2022-07-07 20:41:50 +00:00
|
|
|
</div>
|
|
|
|
<div className={'inline-block'}>
|
|
|
|
<RelativeTimestamp time={notifications[0].createdTime} />
|
|
|
|
</div>
|
2022-07-05 17:29:26 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
<div>
|
|
|
|
<div className={clsx('mt-1 md:text-base', expanded ? 'pl-4' : '')}>
|
|
|
|
{' '}
|
2022-07-06 19:30:51 +00:00
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
'mt-1 ml-1 gap-1 whitespace-pre-line',
|
|
|
|
!expanded ? 'line-clamp-4' : ''
|
|
|
|
)}
|
|
|
|
>
|
2022-07-05 17:29:26 +00:00
|
|
|
{!expanded ? (
|
|
|
|
<>
|
|
|
|
{combinedNotifs
|
|
|
|
.slice(0, numSummaryLines)
|
2022-07-06 19:30:51 +00:00
|
|
|
.map((notification) => (
|
|
|
|
<IncomeNotificationItem
|
|
|
|
notification={notification}
|
|
|
|
justSummary={true}
|
|
|
|
key={notification.id}
|
|
|
|
/>
|
|
|
|
))}
|
2022-07-05 17:29:26 +00:00
|
|
|
<div className={'text-sm text-gray-500 hover:underline '}>
|
|
|
|
{combinedNotifs.length - numSummaryLines > 0
|
|
|
|
? 'And ' +
|
|
|
|
(combinedNotifs.length - numSummaryLines) +
|
|
|
|
' more...'
|
|
|
|
: ''}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{combinedNotifs.map((notification) => (
|
2022-07-06 19:30:51 +00:00
|
|
|
<IncomeNotificationItem
|
2022-07-05 17:29:26 +00:00
|
|
|
notification={notification}
|
|
|
|
key={notification.id}
|
|
|
|
justSummary={false}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={'mt-6 border-b border-gray-300'} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:30:51 +00:00
|
|
|
function IncomeNotificationItem(props: {
|
|
|
|
notification: Notification
|
|
|
|
justSummary?: boolean
|
|
|
|
}) {
|
|
|
|
const { notification, justSummary } = props
|
2022-07-07 20:41:50 +00:00
|
|
|
const { sourceType, sourceUserName, sourceUserUsername } = notification
|
2022-07-06 19:30:51 +00:00
|
|
|
const [highlighted] = useState(!notification.isSeen)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setNotificationsAsSeen([notification])
|
|
|
|
}, [notification])
|
|
|
|
|
|
|
|
function getReasonForShowingIncomeNotification(simple: boolean) {
|
|
|
|
const { sourceText } = notification
|
|
|
|
let reasonText = ''
|
|
|
|
if (sourceType === 'bonus' && sourceText) {
|
|
|
|
reasonText = !simple
|
2022-07-06 23:24:53 +00:00
|
|
|
? `Bonus for ${
|
2022-07-06 19:30:51 +00:00
|
|
|
parseInt(sourceText) / UNIQUE_BETTOR_BONUS_AMOUNT
|
|
|
|
} unique bettors`
|
2022-07-06 23:24:53 +00:00
|
|
|
: 'bonus on'
|
2022-07-06 19:30:51 +00:00
|
|
|
} else if (sourceType === 'tip') {
|
|
|
|
reasonText = !simple ? `tipped you` : `in tips on`
|
|
|
|
}
|
2022-07-07 20:41:50 +00:00
|
|
|
return reasonText
|
2022-07-06 19:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (justSummary) {
|
|
|
|
return (
|
|
|
|
<Row className={'items-center text-sm text-gray-500 sm:justify-start'}>
|
|
|
|
<div className={'line-clamp-1 flex-1 overflow-hidden sm:flex'}>
|
|
|
|
<div className={'flex pl-1 sm:pl-0'}>
|
|
|
|
<div className={'inline-flex overflow-hidden text-ellipsis pl-1'}>
|
|
|
|
<div className={'mr-1 text-black'}>
|
|
|
|
<NotificationTextLabel
|
|
|
|
contract={null}
|
|
|
|
defaultText={notification.sourceText ?? ''}
|
|
|
|
className={'line-clamp-1'}
|
|
|
|
notification={notification}
|
|
|
|
justSummary={true}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<span className={'flex truncate'}>
|
|
|
|
{getReasonForShowingIncomeNotification(true)}
|
2022-07-07 21:06:29 +00:00
|
|
|
<QuestionLink notification={notification} ignoreClick={true} />
|
2022-07-06 19:30:51 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
'bg-white px-2 pt-6 text-sm sm:px-4',
|
|
|
|
highlighted && 'bg-indigo-200 hover:bg-indigo-100'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<a href={getSourceUrl(notification)}>
|
|
|
|
<Row className={'items-center text-gray-500 sm:justify-start'}>
|
2022-07-07 20:41:50 +00:00
|
|
|
<div className={'line-clamp-2 flex max-w-xl shrink '}>
|
|
|
|
<div className={'inline'}>
|
|
|
|
<span className={'mr-1'}>
|
|
|
|
<NotificationTextLabel
|
|
|
|
contract={null}
|
|
|
|
defaultText={notification.sourceText ?? ''}
|
|
|
|
notification={notification}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<span>
|
|
|
|
{sourceType != 'bonus' &&
|
|
|
|
(sourceUserUsername === MULTIPLE_USERS_KEY ? (
|
|
|
|
<span className={'mr-1 truncate'}>Multiple users</span>
|
|
|
|
) : (
|
|
|
|
<UserLink
|
|
|
|
name={sourceUserName || ''}
|
|
|
|
username={sourceUserUsername || ''}
|
|
|
|
className={'mr-1 flex-shrink-0'}
|
|
|
|
justFirstName={true}
|
2022-07-06 19:30:51 +00:00
|
|
|
/>
|
2022-07-07 20:41:50 +00:00
|
|
|
))}
|
|
|
|
{getReasonForShowingIncomeNotification(false)} {' on'}
|
2022-07-07 21:06:29 +00:00
|
|
|
<QuestionLink notification={notification} />
|
2022-07-06 19:30:51 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
<div className={'mt-4 border-b border-gray-300'} />
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:36:59 +00:00
|
|
|
function NotificationGroupItem(props: {
|
|
|
|
notificationGroup: NotificationGroup
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const { notificationGroup, className } = props
|
2022-07-01 13:47:19 +00:00
|
|
|
const { notifications } = notificationGroup
|
2022-07-06 19:30:51 +00:00
|
|
|
const { sourceContractTitle } = notifications[0]
|
2022-06-06 17:36:59 +00:00
|
|
|
const numSummaryLines = 3
|
2022-06-09 17:39:23 +00:00
|
|
|
|
2022-06-06 17:36:59 +00:00
|
|
|
const [expanded, setExpanded] = useState(false)
|
2022-07-06 19:30:51 +00:00
|
|
|
const [highlighted, setHighlighted] = useState(
|
|
|
|
notifications.some((n) => !n.isSeen)
|
|
|
|
)
|
2022-06-06 17:36:59 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setNotificationsAsSeen(notifications)
|
2022-06-09 17:39:23 +00:00
|
|
|
}, [notifications])
|
2022-06-06 17:36:59 +00:00
|
|
|
|
2022-07-05 17:29:26 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (expanded) setHighlighted(false)
|
|
|
|
}, [expanded])
|
|
|
|
|
2022-06-06 17:36:59 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
'relative cursor-pointer bg-white px-2 pt-6 text-sm',
|
|
|
|
className,
|
2022-07-05 17:29:26 +00:00
|
|
|
!expanded ? 'hover:bg-gray-100' : '',
|
|
|
|
highlighted && !expanded ? 'bg-indigo-200 hover:bg-indigo-100' : ''
|
2022-06-06 17:36:59 +00:00
|
|
|
)}
|
|
|
|
onClick={() => setExpanded(!expanded)}
|
|
|
|
>
|
|
|
|
{expanded && (
|
|
|
|
<span
|
|
|
|
className="absolute top-14 left-6 -ml-px h-[calc(100%-5rem)] w-0.5 bg-gray-200"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Row className={'items-center text-gray-500 sm:justify-start'}>
|
2022-06-23 17:55:41 +00:00
|
|
|
<EmptyAvatar multi />
|
2022-07-07 20:41:50 +00:00
|
|
|
<div
|
|
|
|
className={'line-clamp-2 flex w-full flex-row flex-wrap pl-1 sm:pl-0'}
|
|
|
|
>
|
|
|
|
{sourceContractTitle ? (
|
|
|
|
<div className={'flex w-full flex-row justify-between'}>
|
|
|
|
<div className={'ml-2'}>
|
|
|
|
Activity on
|
2022-07-07 21:06:29 +00:00
|
|
|
<QuestionLink notification={notifications[0]} />
|
2022-07-07 20:41:50 +00:00
|
|
|
</div>
|
|
|
|
<div className={'hidden sm:inline-block'}>
|
|
|
|
<RelativeTimestamp time={notifications[0].createdTime} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<span>
|
|
|
|
Other activity
|
|
|
|
<RelativeTimestamp time={notifications[0].createdTime} />
|
|
|
|
</span>
|
|
|
|
)}
|
2022-06-06 17:36:59 +00:00
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
<div>
|
|
|
|
<div className={clsx('mt-1 md:text-base', expanded ? 'pl-4' : '')}>
|
|
|
|
{' '}
|
2022-07-06 19:30:51 +00:00
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
'mt-1 ml-1 gap-1 whitespace-pre-line',
|
|
|
|
!expanded ? 'line-clamp-4' : ''
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{' '}
|
2022-06-06 17:36:59 +00:00
|
|
|
{!expanded ? (
|
|
|
|
<>
|
2022-06-06 22:15:36 +00:00
|
|
|
{notifications.slice(0, numSummaryLines).map((notification) => {
|
|
|
|
return (
|
|
|
|
<NotificationItem
|
|
|
|
notification={notification}
|
|
|
|
justSummary={true}
|
|
|
|
key={notification.id}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})}
|
2022-06-06 17:36:59 +00:00
|
|
|
<div className={'text-sm text-gray-500 hover:underline '}>
|
|
|
|
{notifications.length - numSummaryLines > 0
|
|
|
|
? 'And ' +
|
|
|
|
(notifications.length - numSummaryLines) +
|
|
|
|
' more...'
|
|
|
|
: ''}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2022-06-06 22:15:36 +00:00
|
|
|
{notifications.map((notification) => (
|
2022-06-06 17:36:59 +00:00
|
|
|
<NotificationItem
|
|
|
|
notification={notification}
|
|
|
|
key={notification.id}
|
|
|
|
justSummary={false}
|
2022-07-07 20:41:50 +00:00
|
|
|
isChildOfGroup={true}
|
2022-06-06 17:36:59 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={'mt-6 border-b border-gray-300'} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-06 21:06:41 +00:00
|
|
|
function NotificationItem(props: {
|
|
|
|
notification: Notification
|
|
|
|
justSummary?: boolean
|
2022-07-07 20:41:50 +00:00
|
|
|
isChildOfGroup?: boolean
|
2022-07-06 21:06:41 +00:00
|
|
|
}) {
|
2022-07-07 20:41:50 +00:00
|
|
|
const { notification, justSummary, isChildOfGroup } = props
|
2022-07-06 21:06:41 +00:00
|
|
|
const {
|
|
|
|
sourceType,
|
|
|
|
sourceUserName,
|
|
|
|
sourceUserAvatarUrl,
|
|
|
|
sourceUpdateType,
|
|
|
|
reasonText,
|
|
|
|
reason,
|
|
|
|
sourceUserUsername,
|
|
|
|
sourceText,
|
|
|
|
} = notification
|
2022-06-06 17:36:59 +00:00
|
|
|
|
2022-07-06 21:06:41 +00:00
|
|
|
const [defaultNotificationText, setDefaultNotificationText] =
|
|
|
|
useState<string>('')
|
2022-06-06 17:36:59 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-07-06 21:06:41 +00:00
|
|
|
if (sourceText) {
|
|
|
|
setDefaultNotificationText(sourceText)
|
|
|
|
} else if (reasonText) {
|
|
|
|
// Handle arbitrary notifications with reason text here.
|
|
|
|
setDefaultNotificationText(reasonText)
|
2022-06-06 17:36:59 +00:00
|
|
|
}
|
2022-07-06 21:06:41 +00:00
|
|
|
}, [reasonText, sourceText])
|
2022-06-06 17:36:59 +00:00
|
|
|
|
2022-07-06 21:06:41 +00:00
|
|
|
const [highlighted] = useState(!notification.isSeen)
|
2022-06-06 17:36:59 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-07-06 21:06:41 +00:00
|
|
|
setNotificationsAsSeen([notification])
|
|
|
|
}, [notification])
|
2022-06-06 17:36:59 +00:00
|
|
|
|
2022-07-06 21:06:41 +00:00
|
|
|
if (justSummary) {
|
2022-06-06 17:36:59 +00:00
|
|
|
return (
|
2022-07-06 21:06:41 +00:00
|
|
|
<Row className={'items-center text-sm text-gray-500 sm:justify-start'}>
|
|
|
|
<div className={'line-clamp-1 flex-1 overflow-hidden sm:flex'}>
|
|
|
|
<div className={'flex pl-1 sm:pl-0'}>
|
|
|
|
<UserLink
|
|
|
|
name={sourceUserName || ''}
|
|
|
|
username={sourceUserUsername || ''}
|
|
|
|
className={'mr-0 flex-shrink-0'}
|
|
|
|
justFirstName={true}
|
|
|
|
/>
|
|
|
|
<div className={'inline-flex overflow-hidden text-ellipsis pl-1'}>
|
|
|
|
<span className={'flex-shrink-0'}>
|
|
|
|
{sourceType &&
|
|
|
|
reason &&
|
|
|
|
getReasonForShowingNotification(notification, true, true)}
|
|
|
|
</span>
|
|
|
|
<div className={'ml-1 text-black'}>
|
|
|
|
<NotificationTextLabel
|
|
|
|
contract={null}
|
|
|
|
defaultText={defaultNotificationText}
|
|
|
|
className={'line-clamp-1'}
|
|
|
|
notification={notification}
|
|
|
|
justSummary={true}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-06 17:36:59 +00:00
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-07-06 21:06:41 +00:00
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
'bg-white px-2 pt-6 text-sm sm:px-4',
|
|
|
|
highlighted && 'bg-indigo-200 hover:bg-indigo-100'
|
|
|
|
)}
|
|
|
|
>
|
2022-07-07 21:06:29 +00:00
|
|
|
<a
|
|
|
|
href={getSourceUrl(notification)}
|
|
|
|
onClick={() =>
|
|
|
|
track('Notification Clicked', {
|
|
|
|
type: 'notification item',
|
|
|
|
sourceType,
|
|
|
|
sourceUserName,
|
|
|
|
sourceUserAvatarUrl,
|
|
|
|
sourceUpdateType,
|
|
|
|
reasonText,
|
|
|
|
reason,
|
|
|
|
sourceUserUsername,
|
|
|
|
sourceText,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
2022-07-06 21:06:41 +00:00
|
|
|
<Row className={'items-center text-gray-500 sm:justify-start'}>
|
|
|
|
<Avatar
|
|
|
|
avatarUrl={sourceUserAvatarUrl}
|
|
|
|
size={'sm'}
|
|
|
|
className={'mr-2'}
|
|
|
|
username={sourceUserName}
|
2022-06-08 17:24:07 +00:00
|
|
|
/>
|
2022-07-07 20:41:50 +00:00
|
|
|
<div className={'flex w-full flex-row pl-1 sm:pl-0'}>
|
2022-07-06 21:06:41 +00:00
|
|
|
<div
|
|
|
|
className={
|
2022-07-07 20:41:50 +00:00
|
|
|
'line-clamp-2 sm:line-clamp-none flex w-full flex-row justify-between'
|
2022-07-06 21:06:41 +00:00
|
|
|
}
|
|
|
|
>
|
2022-07-07 20:41:50 +00:00
|
|
|
<div>
|
|
|
|
{sourceUpdateType != 'closed' && (
|
|
|
|
<UserLink
|
|
|
|
name={sourceUserName || ''}
|
|
|
|
username={sourceUserUsername || ''}
|
|
|
|
className={'mr-1 flex-shrink-0'}
|
|
|
|
justFirstName={true}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{getReasonForShowingNotification(
|
|
|
|
notification,
|
|
|
|
false,
|
|
|
|
isChildOfGroup
|
|
|
|
)}
|
|
|
|
{isChildOfGroup ? (
|
|
|
|
<RelativeTimestamp time={notification.createdTime} />
|
|
|
|
) : (
|
2022-07-07 21:06:29 +00:00
|
|
|
<QuestionLink notification={notification} />
|
2022-07-07 20:41:50 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2022-07-06 21:06:41 +00:00
|
|
|
</div>
|
2022-07-07 20:41:50 +00:00
|
|
|
{!isChildOfGroup && (
|
|
|
|
<div className={'hidden sm:inline-block'}>
|
|
|
|
<RelativeTimestamp time={notification.createdTime} />
|
|
|
|
</div>
|
|
|
|
)}
|
2022-07-06 21:06:41 +00:00
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
<div className={'mt-1 ml-1 md:text-base'}>
|
|
|
|
<NotificationTextLabel
|
|
|
|
contract={null}
|
|
|
|
defaultText={defaultNotificationText}
|
|
|
|
notification={notification}
|
2022-06-08 17:24:07 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2022-07-06 21:06:41 +00:00
|
|
|
|
|
|
|
<div className={'mt-6 border-b border-gray-300'} />
|
|
|
|
</a>
|
2022-06-06 17:36:59 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-06 21:06:41 +00:00
|
|
|
export const setNotificationsAsSeen = (notifications: Notification[]) => {
|
|
|
|
notifications.forEach((notification) => {
|
|
|
|
if (!notification.isSeen)
|
|
|
|
updateDoc(
|
|
|
|
doc(db, `users/${notification.userId}/notifications/`, notification.id),
|
|
|
|
{
|
|
|
|
isSeen: true,
|
|
|
|
viewTime: new Date(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return notifications
|
|
|
|
}
|
|
|
|
|
2022-07-07 21:06:29 +00:00
|
|
|
function QuestionLink(props: {
|
2022-07-07 20:41:50 +00:00
|
|
|
notification: Notification
|
2022-07-07 21:06:29 +00:00
|
|
|
ignoreClick?: boolean
|
2022-07-07 20:41:50 +00:00
|
|
|
}) {
|
2022-07-07 21:06:29 +00:00
|
|
|
const { notification, ignoreClick } = props
|
2022-07-06 13:45:47 +00:00
|
|
|
const {
|
|
|
|
sourceType,
|
|
|
|
sourceContractTitle,
|
|
|
|
sourceContractCreatorUsername,
|
|
|
|
sourceContractSlug,
|
|
|
|
sourceSlug,
|
|
|
|
sourceTitle,
|
|
|
|
} = notification
|
2022-07-07 21:06:29 +00:00
|
|
|
|
|
|
|
if (ignoreClick)
|
2022-07-07 20:41:50 +00:00
|
|
|
return (
|
|
|
|
<span className={'ml-1 font-bold '}>
|
|
|
|
{sourceContractTitle || sourceTitle}
|
|
|
|
</span>
|
|
|
|
)
|
2022-07-06 13:45:47 +00:00
|
|
|
return (
|
|
|
|
<a
|
2022-07-07 20:41:50 +00:00
|
|
|
className={
|
|
|
|
'ml-1 font-bold hover:underline hover:decoration-indigo-400 hover:decoration-2 '
|
|
|
|
}
|
2022-07-06 13:45:47 +00:00
|
|
|
href={
|
|
|
|
sourceContractCreatorUsername
|
|
|
|
? `/${sourceContractCreatorUsername}/${sourceContractSlug}`
|
|
|
|
: sourceType === 'group' && sourceSlug
|
|
|
|
? `${groupPath(sourceSlug)}`
|
|
|
|
: ''
|
|
|
|
}
|
2022-07-07 21:06:29 +00:00
|
|
|
onClick={() =>
|
|
|
|
track('Notification Clicked', {
|
|
|
|
type: 'question title',
|
|
|
|
sourceType,
|
|
|
|
sourceContractTitle,
|
|
|
|
sourceContractCreatorUsername,
|
|
|
|
sourceContractSlug,
|
|
|
|
sourceSlug,
|
|
|
|
sourceTitle,
|
|
|
|
})
|
|
|
|
}
|
2022-07-06 13:45:47 +00:00
|
|
|
>
|
|
|
|
{sourceContractTitle || sourceTitle}
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:30:51 +00:00
|
|
|
function getSourceUrl(notification: Notification) {
|
|
|
|
const {
|
|
|
|
sourceType,
|
|
|
|
sourceId,
|
|
|
|
sourceUserUsername,
|
|
|
|
sourceContractCreatorUsername,
|
|
|
|
sourceContractSlug,
|
|
|
|
sourceSlug,
|
|
|
|
} = notification
|
|
|
|
if (sourceType === 'follow') return `/${sourceUserUsername}`
|
|
|
|
if (sourceType === 'group' && sourceSlug) return `${groupPath(sourceSlug)}`
|
|
|
|
if (
|
|
|
|
sourceContractCreatorUsername &&
|
|
|
|
sourceContractSlug &&
|
|
|
|
sourceType === 'user'
|
|
|
|
)
|
|
|
|
return `/${sourceContractCreatorUsername}/${sourceContractSlug}`
|
|
|
|
if (sourceType === 'tip')
|
|
|
|
return `/${sourceContractCreatorUsername}/${sourceContractSlug}#${sourceSlug}`
|
|
|
|
if (sourceContractCreatorUsername && sourceContractSlug)
|
|
|
|
return `/${sourceContractCreatorUsername}/${sourceContractSlug}#${getSourceIdForLinkComponent(
|
|
|
|
sourceId ?? '',
|
|
|
|
sourceType
|
|
|
|
)}`
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSourceIdForLinkComponent(
|
|
|
|
sourceId: string,
|
|
|
|
sourceType?: notification_source_types
|
|
|
|
) {
|
|
|
|
switch (sourceType) {
|
|
|
|
case 'answer':
|
|
|
|
return `answer-${sourceId}`
|
|
|
|
case 'comment':
|
|
|
|
return sourceId
|
|
|
|
case 'contract':
|
|
|
|
return ''
|
|
|
|
default:
|
|
|
|
return sourceId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:36:59 +00:00
|
|
|
function NotificationTextLabel(props: {
|
2022-06-06 22:15:36 +00:00
|
|
|
defaultText: string
|
2022-06-08 14:43:24 +00:00
|
|
|
contract?: Contract | null
|
|
|
|
notification: Notification
|
2022-06-06 17:36:59 +00:00
|
|
|
className?: string
|
2022-06-09 17:39:23 +00:00
|
|
|
justSummary?: boolean
|
2022-06-06 17:36:59 +00:00
|
|
|
}) {
|
2022-06-09 17:39:23 +00:00
|
|
|
const { contract, className, defaultText, notification, justSummary } = props
|
2022-06-08 14:43:24 +00:00
|
|
|
const { sourceUpdateType, sourceType, sourceText, sourceContractTitle } =
|
|
|
|
notification
|
2022-06-10 22:48:28 +00:00
|
|
|
if (sourceType === 'contract') {
|
|
|
|
if (justSummary)
|
|
|
|
return <span>{contract?.question || sourceContractTitle}</span>
|
|
|
|
if (!sourceText) return <div />
|
|
|
|
// Resolved contracts
|
2022-07-05 17:29:26 +00:00
|
|
|
if (sourceType === 'contract' && sourceUpdateType === 'resolved') {
|
2022-06-10 22:48:28 +00:00
|
|
|
{
|
|
|
|
if (sourceText === 'YES' || sourceText == 'NO') {
|
|
|
|
return <BinaryOutcomeLabel outcome={sourceText as any} />
|
|
|
|
}
|
|
|
|
if (sourceText.includes('%'))
|
|
|
|
return (
|
|
|
|
<ProbPercentLabel prob={parseFloat(sourceText.replace('%', ''))} />
|
|
|
|
)
|
|
|
|
if (sourceText === 'CANCEL') return <CancelLabel />
|
|
|
|
if (sourceText === 'MKT' || sourceText === 'PROB') return <MultiLabel />
|
2022-06-09 16:30:26 +00:00
|
|
|
}
|
2022-06-10 22:48:28 +00:00
|
|
|
}
|
|
|
|
// Close date will be a number - it looks better without it
|
|
|
|
if (sourceUpdateType === 'closed') {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
// Updated contracts
|
|
|
|
// Description will be in default text
|
|
|
|
if (parseInt(sourceText) > 0) {
|
2022-06-06 17:36:59 +00:00
|
|
|
return (
|
2022-06-10 22:48:28 +00:00
|
|
|
<span>
|
|
|
|
Updated close time: {new Date(parseInt(sourceText)).toLocaleString()}
|
|
|
|
</span>
|
2022-06-06 17:36:59 +00:00
|
|
|
)
|
2022-06-10 22:48:28 +00:00
|
|
|
}
|
2022-07-01 13:47:19 +00:00
|
|
|
} else if (sourceType === 'user' && sourceText) {
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
As a thank you, we sent you{' '}
|
|
|
|
<span className="text-primary">
|
|
|
|
{formatMoney(parseInt(sourceText))}
|
|
|
|
</span>
|
|
|
|
!
|
|
|
|
</span>
|
|
|
|
)
|
2022-06-10 22:48:28 +00:00
|
|
|
} else if (sourceType === 'liquidity' && sourceText) {
|
2022-06-06 17:36:59 +00:00
|
|
|
return (
|
2022-06-10 22:48:28 +00:00
|
|
|
<span className="text-blue-400">{formatMoney(parseInt(sourceText))}</span>
|
2022-06-06 17:36:59 +00:00
|
|
|
)
|
2022-07-06 19:30:51 +00:00
|
|
|
} else if ((sourceType === 'bonus' || sourceType === 'tip') && sourceText) {
|
2022-07-05 17:29:26 +00:00
|
|
|
return (
|
2022-07-06 19:30:51 +00:00
|
|
|
<span className="text-primary">
|
|
|
|
{'+' + formatMoney(parseInt(sourceText))}
|
|
|
|
</span>
|
2022-07-05 17:29:26 +00:00
|
|
|
)
|
2022-06-06 17:36:59 +00:00
|
|
|
}
|
2022-06-10 22:48:28 +00:00
|
|
|
// return default text
|
|
|
|
return (
|
|
|
|
<div className={className ? className : 'line-clamp-4 whitespace-pre-line'}>
|
|
|
|
<Linkify text={defaultText} />
|
|
|
|
</div>
|
|
|
|
)
|
2022-06-06 17:36:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 22:15:36 +00:00
|
|
|
function getReasonForShowingNotification(
|
2022-07-05 17:29:26 +00:00
|
|
|
notification: Notification,
|
2022-07-06 19:30:51 +00:00
|
|
|
simple?: boolean,
|
|
|
|
replaceOn?: boolean
|
2022-06-01 18:26:41 +00:00
|
|
|
) {
|
2022-07-06 19:30:51 +00:00
|
|
|
const { sourceType, sourceUpdateType, reason, sourceSlug } = notification
|
2022-06-06 22:15:36 +00:00
|
|
|
let reasonText: string
|
2022-07-05 17:29:26 +00:00
|
|
|
switch (sourceType) {
|
2022-06-01 18:26:41 +00:00
|
|
|
case 'comment':
|
2022-06-06 17:36:59 +00:00
|
|
|
if (reason === 'reply_to_users_answer')
|
2022-07-06 19:30:51 +00:00
|
|
|
reasonText = !simple ? 'replied to you on' : 'replied'
|
2022-06-06 17:36:59 +00:00
|
|
|
else if (reason === 'tagged_user')
|
2022-07-06 19:30:51 +00:00
|
|
|
reasonText = !simple ? 'tagged you on' : 'tagged you'
|
2022-06-06 17:36:59 +00:00
|
|
|
else if (reason === 'reply_to_users_comment')
|
2022-07-06 19:30:51 +00:00
|
|
|
reasonText = !simple ? 'replied to you on' : 'replied'
|
2022-06-06 17:36:59 +00:00
|
|
|
else reasonText = `commented on`
|
|
|
|
break
|
2022-06-01 18:26:41 +00:00
|
|
|
case 'contract':
|
2022-07-06 13:45:47 +00:00
|
|
|
if (reason === 'you_follow_user') reasonText = 'asked'
|
2022-07-05 17:29:26 +00:00
|
|
|
else if (sourceUpdateType === 'resolved') reasonText = `resolved`
|
2022-06-10 22:48:28 +00:00
|
|
|
else if (sourceUpdateType === 'closed')
|
2022-07-05 17:29:26 +00:00
|
|
|
reasonText = `Please resolve your question`
|
2022-06-06 17:36:59 +00:00
|
|
|
else reasonText = `updated`
|
|
|
|
break
|
2022-06-01 18:26:41 +00:00
|
|
|
case 'answer':
|
2022-06-06 17:36:59 +00:00
|
|
|
if (reason === 'on_users_contract') reasonText = `answered your question `
|
|
|
|
else reasonText = `answered`
|
|
|
|
break
|
|
|
|
case 'follow':
|
|
|
|
reasonText = 'followed you'
|
|
|
|
break
|
2022-06-10 22:48:28 +00:00
|
|
|
case 'liquidity':
|
|
|
|
reasonText = 'added liquidity to your question'
|
|
|
|
break
|
2022-06-22 16:35:50 +00:00
|
|
|
case 'group':
|
|
|
|
reasonText = 'added you to the group'
|
|
|
|
break
|
2022-07-01 13:47:19 +00:00
|
|
|
case 'user':
|
|
|
|
if (sourceSlug && reason === 'user_joined_to_bet_on_your_market')
|
|
|
|
reasonText = 'joined to bet on your market'
|
|
|
|
else if (sourceSlug) reasonText = 'joined because you shared'
|
|
|
|
else reasonText = 'joined because of you'
|
|
|
|
break
|
2022-06-01 18:26:41 +00:00
|
|
|
default:
|
2022-06-06 17:36:59 +00:00
|
|
|
reasonText = ''
|
2022-06-01 18:26:41 +00:00
|
|
|
}
|
2022-07-07 20:41:50 +00:00
|
|
|
return replaceOn ? reasonText.replace(' on', '') : reasonText
|
2022-06-01 18:26:41 +00:00
|
|
|
}
|
2022-07-06 21:06:41 +00:00
|
|
|
|
|
|
|
// TODO: where should we put referral bonus notifications?
|
|
|
|
function NotificationSettings() {
|
|
|
|
const user = useUser()
|
|
|
|
const [notificationSettings, setNotificationSettings] =
|
|
|
|
useState<notification_subscribe_types>('all')
|
|
|
|
const [emailNotificationSettings, setEmailNotificationSettings] =
|
|
|
|
useState<notification_subscribe_types>('all')
|
|
|
|
const [privateUser, setPrivateUser] = useState<PrivateUser | null>(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user) listenForPrivateUser(user.id, setPrivateUser)
|
|
|
|
}, [user])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!privateUser) return
|
|
|
|
if (privateUser.notificationPreferences) {
|
|
|
|
setNotificationSettings(privateUser.notificationPreferences)
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
privateUser.unsubscribedFromResolutionEmails &&
|
|
|
|
privateUser.unsubscribedFromCommentEmails &&
|
|
|
|
privateUser.unsubscribedFromAnswerEmails
|
|
|
|
) {
|
|
|
|
setEmailNotificationSettings('none')
|
|
|
|
} else if (
|
|
|
|
!privateUser.unsubscribedFromResolutionEmails &&
|
|
|
|
!privateUser.unsubscribedFromCommentEmails &&
|
|
|
|
!privateUser.unsubscribedFromAnswerEmails
|
|
|
|
) {
|
|
|
|
setEmailNotificationSettings('all')
|
|
|
|
} else {
|
|
|
|
setEmailNotificationSettings('less')
|
|
|
|
}
|
|
|
|
}, [privateUser])
|
|
|
|
|
|
|
|
const loading = 'Changing Notifications Settings'
|
|
|
|
const success = 'Notification Settings Changed!'
|
|
|
|
function changeEmailNotifications(newValue: notification_subscribe_types) {
|
|
|
|
if (!privateUser) return
|
|
|
|
if (newValue === 'all') {
|
|
|
|
toast.promise(
|
|
|
|
updatePrivateUser(privateUser.id, {
|
|
|
|
unsubscribedFromResolutionEmails: false,
|
|
|
|
unsubscribedFromCommentEmails: false,
|
|
|
|
unsubscribedFromAnswerEmails: false,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
loading,
|
|
|
|
success,
|
|
|
|
error: (err) => `${err.message}`,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else if (newValue === 'less') {
|
|
|
|
toast.promise(
|
|
|
|
updatePrivateUser(privateUser.id, {
|
|
|
|
unsubscribedFromResolutionEmails: false,
|
|
|
|
unsubscribedFromCommentEmails: true,
|
|
|
|
unsubscribedFromAnswerEmails: true,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
loading,
|
|
|
|
success,
|
|
|
|
error: (err) => `${err.message}`,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else if (newValue === 'none') {
|
|
|
|
toast.promise(
|
|
|
|
updatePrivateUser(privateUser.id, {
|
|
|
|
unsubscribedFromResolutionEmails: true,
|
|
|
|
unsubscribedFromCommentEmails: true,
|
|
|
|
unsubscribedFromAnswerEmails: true,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
loading,
|
|
|
|
success,
|
|
|
|
error: (err) => `${err.message}`,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function changeInAppNotificationSettings(
|
|
|
|
newValue: notification_subscribe_types
|
|
|
|
) {
|
|
|
|
if (!privateUser) return
|
2022-07-07 21:06:29 +00:00
|
|
|
track('In-App Notification Preferences Changed', {
|
|
|
|
newPreference: newValue,
|
|
|
|
oldPreference: privateUser.notificationPreferences,
|
|
|
|
})
|
2022-07-06 21:06:41 +00:00
|
|
|
toast.promise(
|
|
|
|
updatePrivateUser(privateUser.id, {
|
|
|
|
notificationPreferences: newValue,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
loading,
|
|
|
|
success,
|
|
|
|
error: (err) => `${err.message}`,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (privateUser && privateUser.notificationPreferences)
|
|
|
|
setNotificationSettings(privateUser.notificationPreferences)
|
|
|
|
else setNotificationSettings('all')
|
|
|
|
}, [privateUser])
|
|
|
|
|
|
|
|
if (!privateUser) {
|
|
|
|
return <LoadingIndicator spinnerClassName={'border-gray-500 h-4 w-4'} />
|
|
|
|
}
|
|
|
|
|
|
|
|
function NotificationSettingLine(props: {
|
|
|
|
label: string
|
|
|
|
highlight: boolean
|
|
|
|
}) {
|
|
|
|
const { label, highlight } = props
|
|
|
|
return (
|
|
|
|
<Row className={clsx('my-1 text-gray-300', highlight && '!text-black')}>
|
|
|
|
{highlight ? <CheckIcon height={20} /> : <XIcon height={20} />}
|
|
|
|
{label}
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={'p-2'}>
|
|
|
|
<div>In App Notifications</div>
|
|
|
|
<ChoicesToggleGroup
|
|
|
|
currentChoice={notificationSettings}
|
|
|
|
choicesMap={{ All: 'all', Less: 'less', None: 'none' }}
|
|
|
|
setChoice={(choice) =>
|
|
|
|
changeInAppNotificationSettings(
|
|
|
|
choice as notification_subscribe_types
|
|
|
|
)
|
|
|
|
}
|
|
|
|
className={'col-span-4 p-2'}
|
|
|
|
toggleClassName={'w-24'}
|
|
|
|
/>
|
|
|
|
<div className={'mt-4 text-sm'}>
|
|
|
|
<div>
|
|
|
|
<div className={''}>
|
|
|
|
You will receive notifications for:
|
|
|
|
<NotificationSettingLine
|
|
|
|
label={"Resolution of questions you've interacted with"}
|
|
|
|
highlight={notificationSettings !== 'none'}
|
|
|
|
/>
|
|
|
|
<NotificationSettingLine
|
|
|
|
highlight={notificationSettings !== 'none'}
|
|
|
|
label={'Activity on your own questions, comments, & answers'}
|
|
|
|
/>
|
|
|
|
<NotificationSettingLine
|
|
|
|
highlight={notificationSettings !== 'none'}
|
|
|
|
label={"Activity on questions you're betting on"}
|
|
|
|
/>
|
|
|
|
<NotificationSettingLine
|
|
|
|
highlight={notificationSettings !== 'none'}
|
|
|
|
label={"Income & referral bonuses you've received"}
|
|
|
|
/>
|
|
|
|
<NotificationSettingLine
|
|
|
|
label={"Activity on questions you've ever bet or commented on"}
|
|
|
|
highlight={notificationSettings === 'all'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className={'mt-4'}>Email Notifications</div>
|
|
|
|
<ChoicesToggleGroup
|
|
|
|
currentChoice={emailNotificationSettings}
|
|
|
|
choicesMap={{ All: 'all', Less: 'less', None: 'none' }}
|
|
|
|
setChoice={(choice) =>
|
|
|
|
changeEmailNotifications(choice as notification_subscribe_types)
|
|
|
|
}
|
|
|
|
className={'col-span-4 p-2'}
|
|
|
|
toggleClassName={'w-24'}
|
|
|
|
/>
|
|
|
|
<div className={'mt-4 text-sm'}>
|
|
|
|
<div>
|
|
|
|
You will receive emails for:
|
|
|
|
<NotificationSettingLine
|
|
|
|
label={"Resolution of questions you're betting on"}
|
|
|
|
highlight={emailNotificationSettings !== 'none'}
|
|
|
|
/>
|
|
|
|
<NotificationSettingLine
|
|
|
|
label={'Closure of your questions'}
|
|
|
|
highlight={emailNotificationSettings !== 'none'}
|
|
|
|
/>
|
|
|
|
<NotificationSettingLine
|
|
|
|
label={'Activity on your questions'}
|
|
|
|
highlight={emailNotificationSettings === 'all'}
|
|
|
|
/>
|
|
|
|
<NotificationSettingLine
|
|
|
|
label={"Activity on questions you've answered or commented on"}
|
|
|
|
highlight={emailNotificationSettings === 'all'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|