2022-09-13 23:00:34 +00:00
|
|
|
import React, { memo, ReactNode, useEffect, useState } from 'react'
|
2022-09-12 16:34:56 +00:00
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import clsx from 'clsx'
|
2022-09-14 23:17:32 +00:00
|
|
|
import { PrivateUser } from 'common/user'
|
2022-09-12 16:34:56 +00:00
|
|
|
import { updatePrivateUser } from 'web/lib/firebase/users'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import {
|
|
|
|
CashIcon,
|
|
|
|
ChatIcon,
|
|
|
|
ChevronDownIcon,
|
|
|
|
ChevronUpIcon,
|
|
|
|
CurrencyDollarIcon,
|
|
|
|
InboxInIcon,
|
|
|
|
InformationCircleIcon,
|
|
|
|
LightBulbIcon,
|
|
|
|
TrendingUpIcon,
|
|
|
|
UserIcon,
|
|
|
|
UsersIcon,
|
|
|
|
} from '@heroicons/react/outline'
|
|
|
|
import { WatchMarketModal } from 'web/components/contract/watch-market-modal'
|
|
|
|
import toast from 'react-hot-toast'
|
|
|
|
import { SwitchSetting } from 'web/components/switch-setting'
|
2022-09-13 23:00:34 +00:00
|
|
|
import { uniq } from 'lodash'
|
|
|
|
import {
|
|
|
|
storageStore,
|
|
|
|
usePersistentState,
|
|
|
|
} from 'web/hooks/use-persistent-state'
|
|
|
|
import { safeLocalStorage } from 'web/lib/util/local'
|
2022-09-14 23:17:32 +00:00
|
|
|
import { NOTIFICATION_DESCRIPTIONS } from 'common/notification'
|
|
|
|
import {
|
|
|
|
notification_destination_types,
|
|
|
|
notification_preference,
|
|
|
|
} from 'common/user-notification-preferences'
|
2022-09-12 16:34:56 +00:00
|
|
|
|
|
|
|
export function NotificationSettings(props: {
|
|
|
|
navigateToSection: string | undefined
|
2022-09-13 23:00:34 +00:00
|
|
|
privateUser: PrivateUser
|
2022-09-12 16:34:56 +00:00
|
|
|
}) {
|
2022-09-13 23:00:34 +00:00
|
|
|
const { navigateToSection, privateUser } = props
|
2022-09-12 16:34:56 +00:00
|
|
|
const [showWatchModal, setShowWatchModal] = useState(false)
|
|
|
|
|
2022-09-14 23:17:32 +00:00
|
|
|
const emailsEnabled: Array<notification_preference> = [
|
2022-09-12 16:34:56 +00:00
|
|
|
'all_comments_on_watched_markets',
|
|
|
|
'all_replies_to_my_comments_on_watched_markets',
|
|
|
|
'all_comments_on_contracts_with_shares_in_on_watched_markets',
|
|
|
|
|
|
|
|
'all_answers_on_watched_markets',
|
|
|
|
'all_replies_to_my_answers_on_watched_markets',
|
|
|
|
'all_answers_on_contracts_with_shares_in_on_watched_markets',
|
|
|
|
|
|
|
|
'your_contract_closed',
|
|
|
|
'all_comments_on_my_markets',
|
|
|
|
'all_answers_on_my_markets',
|
|
|
|
|
|
|
|
'resolutions_on_watched_markets_with_shares_in',
|
|
|
|
'resolutions_on_watched_markets',
|
|
|
|
|
|
|
|
'trending_markets',
|
|
|
|
'onboarding_flow',
|
|
|
|
'thank_you_for_purchases',
|
|
|
|
|
2022-09-13 15:54:51 +00:00
|
|
|
'tagged_user', // missing tagged on contract description email
|
|
|
|
'contract_from_followed_user',
|
2022-09-13 22:12:53 +00:00
|
|
|
'unique_bettors_on_your_contract',
|
2022-09-12 16:34:56 +00:00
|
|
|
// TODO: add these
|
2022-09-13 22:12:53 +00:00
|
|
|
// one-click unsubscribe only unsubscribes them from that type only, (well highlighted), then a link to manage the rest of their notifications
|
|
|
|
// 'profit_loss_updates', - changes in markets you have shares in
|
|
|
|
// biggest winner, here are the rest of your markets
|
|
|
|
|
2022-09-12 16:34:56 +00:00
|
|
|
// 'referral_bonuses',
|
2022-09-12 20:17:39 +00:00
|
|
|
// 'on_new_follow',
|
2022-09-12 16:34:56 +00:00
|
|
|
// 'tips_on_your_markets',
|
|
|
|
// 'tips_on_your_comments',
|
|
|
|
// maybe the following?
|
|
|
|
// 'probability_updates_on_watched_markets',
|
|
|
|
// 'limit_order_fills',
|
|
|
|
]
|
2022-09-14 23:17:32 +00:00
|
|
|
const browserDisabled: Array<notification_preference> = [
|
2022-09-12 16:34:56 +00:00
|
|
|
'trending_markets',
|
|
|
|
'profit_loss_updates',
|
|
|
|
'onboarding_flow',
|
|
|
|
'thank_you_for_purchases',
|
|
|
|
]
|
|
|
|
|
2022-09-13 23:18:16 +00:00
|
|
|
type SectionData = {
|
2022-09-12 16:34:56 +00:00
|
|
|
label: string
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypes: Partial<notification_preference>[]
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:18:16 +00:00
|
|
|
const comments: SectionData = {
|
2022-09-12 16:34:56 +00:00
|
|
|
label: 'New Comments',
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypes: [
|
|
|
|
'all_comments_on_watched_markets',
|
|
|
|
'all_comments_on_contracts_with_shares_in_on_watched_markets',
|
2022-09-13 15:54:51 +00:00
|
|
|
// TODO: combine these two
|
2022-09-14 23:17:32 +00:00
|
|
|
'all_replies_to_my_comments_on_watched_markets',
|
|
|
|
'all_replies_to_my_answers_on_watched_markets',
|
|
|
|
],
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:18:16 +00:00
|
|
|
const answers: SectionData = {
|
2022-09-12 16:34:56 +00:00
|
|
|
label: 'New Answers',
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypes: [
|
|
|
|
'all_answers_on_watched_markets',
|
|
|
|
'all_answers_on_contracts_with_shares_in_on_watched_markets',
|
|
|
|
],
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
2022-09-13 23:18:16 +00:00
|
|
|
const updates: SectionData = {
|
2022-09-12 16:34:56 +00:00
|
|
|
label: 'Updates & Resolutions',
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypes: [
|
|
|
|
'market_updates_on_watched_markets',
|
|
|
|
'market_updates_on_watched_markets_with_shares_in',
|
|
|
|
'resolutions_on_watched_markets',
|
|
|
|
'resolutions_on_watched_markets_with_shares_in',
|
|
|
|
],
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
2022-09-13 23:18:16 +00:00
|
|
|
const yourMarkets: SectionData = {
|
2022-09-12 16:34:56 +00:00
|
|
|
label: 'Markets You Created',
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypes: [
|
|
|
|
'your_contract_closed',
|
|
|
|
'all_comments_on_my_markets',
|
|
|
|
'all_answers_on_my_markets',
|
|
|
|
'subsidized_your_market',
|
|
|
|
'tips_on_your_markets',
|
|
|
|
],
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
2022-09-13 23:18:16 +00:00
|
|
|
const bonuses: SectionData = {
|
2022-09-12 16:34:56 +00:00
|
|
|
label: 'Bonuses',
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypes: [
|
|
|
|
'betting_streaks',
|
|
|
|
'referral_bonuses',
|
|
|
|
'unique_bettors_on_your_contract',
|
|
|
|
],
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
2022-09-13 23:18:16 +00:00
|
|
|
const otherBalances: SectionData = {
|
2022-09-12 16:34:56 +00:00
|
|
|
label: 'Other',
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypes: [
|
|
|
|
'loan_income',
|
|
|
|
'limit_order_fills',
|
|
|
|
'tips_on_your_comments',
|
|
|
|
],
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
2022-09-13 23:18:16 +00:00
|
|
|
const userInteractions: SectionData = {
|
2022-09-12 16:34:56 +00:00
|
|
|
label: 'Users',
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypes: [
|
|
|
|
'tagged_user',
|
|
|
|
'on_new_follow',
|
|
|
|
'contract_from_followed_user',
|
|
|
|
],
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
2022-09-13 23:18:16 +00:00
|
|
|
const generalOther: SectionData = {
|
2022-09-12 16:34:56 +00:00
|
|
|
label: 'Other',
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypes: [
|
|
|
|
'trending_markets',
|
|
|
|
'thank_you_for_purchases',
|
|
|
|
'onboarding_flow',
|
|
|
|
],
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:00:34 +00:00
|
|
|
function NotificationSettingLine(props: {
|
|
|
|
description: string
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypeKey: notification_preference
|
2022-09-13 23:00:34 +00:00
|
|
|
destinations: notification_destination_types[]
|
|
|
|
}) {
|
|
|
|
const { description, subscriptionTypeKey, destinations } = props
|
|
|
|
const previousInAppValue = destinations.includes('browser')
|
|
|
|
const previousEmailValue = destinations.includes('email')
|
2022-09-12 16:34:56 +00:00
|
|
|
const [inAppEnabled, setInAppEnabled] = useState(previousInAppValue)
|
|
|
|
const [emailEnabled, setEmailEnabled] = useState(previousEmailValue)
|
|
|
|
const loading = 'Changing Notifications Settings'
|
|
|
|
const success = 'Changed Notification Settings!'
|
2022-09-13 23:00:34 +00:00
|
|
|
const highlight = navigateToSection === subscriptionTypeKey
|
2022-09-12 16:34:56 +00:00
|
|
|
|
2022-09-13 23:00:34 +00:00
|
|
|
const changeSetting = (setting: 'browser' | 'email', newValue: boolean) => {
|
|
|
|
toast
|
|
|
|
.promise(
|
2022-09-12 16:34:56 +00:00
|
|
|
updatePrivateUser(privateUser.id, {
|
2022-09-14 16:26:08 +00:00
|
|
|
notificationPreferences: {
|
|
|
|
...privateUser.notificationPreferences,
|
2022-09-13 23:00:34 +00:00
|
|
|
[subscriptionTypeKey]: destinations.includes(setting)
|
|
|
|
? destinations.filter((d) => d !== setting)
|
|
|
|
: uniq([...destinations, setting]),
|
2022-09-12 16:34:56 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
success,
|
|
|
|
loading,
|
|
|
|
error: 'Error changing notification settings. Try again?',
|
|
|
|
}
|
|
|
|
)
|
2022-09-13 23:00:34 +00:00
|
|
|
.then(() => {
|
|
|
|
if (setting === 'browser') {
|
|
|
|
setInAppEnabled(newValue)
|
|
|
|
} else {
|
|
|
|
setEmailEnabled(newValue)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-09-12 16:34:56 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Row
|
|
|
|
className={clsx(
|
|
|
|
'my-1 gap-1 text-gray-300',
|
|
|
|
highlight ? 'rounded-md bg-indigo-100 p-1' : ''
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Col className="ml-3 gap-2 text-sm">
|
|
|
|
<Row className="gap-2 font-medium text-gray-700">
|
|
|
|
<span>{description}</span>
|
|
|
|
</Row>
|
|
|
|
<Row className={'gap-4'}>
|
2022-09-13 23:00:34 +00:00
|
|
|
{!browserDisabled.includes(subscriptionTypeKey) && (
|
2022-09-12 16:34:56 +00:00
|
|
|
<SwitchSetting
|
|
|
|
checked={inAppEnabled}
|
2022-09-13 23:00:34 +00:00
|
|
|
onChange={(newVal) => changeSetting('browser', newVal)}
|
2022-09-12 20:48:42 +00:00
|
|
|
label={'Web'}
|
2022-09-12 16:34:56 +00:00
|
|
|
/>
|
|
|
|
)}
|
2022-09-13 23:00:34 +00:00
|
|
|
{emailsEnabled.includes(subscriptionTypeKey) && (
|
2022-09-12 16:34:56 +00:00
|
|
|
<SwitchSetting
|
|
|
|
checked={emailEnabled}
|
2022-09-13 23:00:34 +00:00
|
|
|
onChange={(newVal) => changeSetting('email', newVal)}
|
2022-09-12 20:48:16 +00:00
|
|
|
label={'Email'}
|
2022-09-12 16:34:56 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-14 23:17:32 +00:00
|
|
|
const getUsersSavedPreference = (key: notification_preference) => {
|
2022-09-14 16:26:08 +00:00
|
|
|
return privateUser.notificationPreferences[key] ?? []
|
2022-09-12 16:34:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:00:34 +00:00
|
|
|
const Section = memo(function Section(props: {
|
|
|
|
icon: ReactNode
|
2022-09-13 23:18:16 +00:00
|
|
|
data: SectionData
|
2022-09-13 23:00:34 +00:00
|
|
|
}) {
|
|
|
|
const { icon, data } = props
|
2022-09-14 23:17:32 +00:00
|
|
|
const { label, subscriptionTypes } = data
|
2022-09-12 16:34:56 +00:00
|
|
|
const expand =
|
|
|
|
navigateToSection &&
|
2022-09-14 23:25:17 +00:00
|
|
|
subscriptionTypes.includes(navigateToSection as notification_preference)
|
2022-09-13 23:00:34 +00:00
|
|
|
|
|
|
|
// Not sure how to prevent re-render (and collapse of an open section)
|
|
|
|
// due to a private user settings change. Just going to persist expanded state here
|
|
|
|
const [expanded, setExpanded] = usePersistentState(expand ?? false, {
|
2022-09-14 23:25:17 +00:00
|
|
|
key: 'NotificationsSettingsSection-' + subscriptionTypes.join('-'),
|
2022-09-13 23:00:34 +00:00
|
|
|
store: storageStore(safeLocalStorage()),
|
|
|
|
})
|
2022-09-12 16:34:56 +00:00
|
|
|
|
|
|
|
// Not working as the default value for expanded, so using a useEffect
|
|
|
|
useEffect(() => {
|
|
|
|
if (expand) setExpanded(true)
|
2022-09-13 23:18:16 +00:00
|
|
|
}, [expand, setExpanded])
|
2022-09-12 16:34:56 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Col className={clsx('ml-2 gap-2')}>
|
|
|
|
<Row
|
|
|
|
className={'mt-1 cursor-pointer items-center gap-2 text-gray-600'}
|
|
|
|
onClick={() => setExpanded(!expanded)}
|
|
|
|
>
|
|
|
|
{icon}
|
|
|
|
<span>{label}</span>
|
|
|
|
|
|
|
|
{expanded ? (
|
|
|
|
<ChevronUpIcon className="h-5 w-5 text-xs text-gray-500">
|
|
|
|
Hide
|
|
|
|
</ChevronUpIcon>
|
|
|
|
) : (
|
|
|
|
<ChevronDownIcon className="h-5 w-5 text-xs text-gray-500">
|
|
|
|
Show
|
|
|
|
</ChevronDownIcon>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
<Col className={clsx(expanded ? 'block' : 'hidden', 'gap-2 p-2')}>
|
2022-09-14 23:17:32 +00:00
|
|
|
{subscriptionTypes.map((subType) => (
|
2022-09-13 23:00:34 +00:00
|
|
|
<NotificationSettingLine
|
2022-09-14 23:17:32 +00:00
|
|
|
subscriptionTypeKey={subType as notification_preference}
|
2022-09-13 23:00:34 +00:00
|
|
|
destinations={getUsersSavedPreference(
|
2022-09-14 23:17:32 +00:00
|
|
|
subType as notification_preference
|
2022-09-13 23:00:34 +00:00
|
|
|
)}
|
2022-09-14 23:17:32 +00:00
|
|
|
description={NOTIFICATION_DESCRIPTIONS[subType].simple}
|
2022-09-13 23:00:34 +00:00
|
|
|
/>
|
|
|
|
))}
|
2022-09-12 16:34:56 +00:00
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
)
|
2022-09-13 23:00:34 +00:00
|
|
|
})
|
2022-09-12 16:34:56 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={'p-2'}>
|
|
|
|
<Col className={'gap-6'}>
|
|
|
|
<Row className={'gap-2 text-xl text-gray-700'}>
|
|
|
|
<span>Notifications for Watched Markets</span>
|
|
|
|
<InformationCircleIcon
|
|
|
|
className="-mb-1 h-5 w-5 cursor-pointer text-gray-500"
|
|
|
|
onClick={() => setShowWatchModal(true)}
|
|
|
|
/>
|
|
|
|
</Row>
|
2022-09-13 23:00:34 +00:00
|
|
|
<Section icon={<ChatIcon className={'h-6 w-6'} />} data={comments} />
|
|
|
|
<Section
|
|
|
|
icon={<TrendingUpIcon className={'h-6 w-6'} />}
|
|
|
|
data={updates}
|
|
|
|
/>
|
|
|
|
<Section
|
|
|
|
icon={<LightBulbIcon className={'h-6 w-6'} />}
|
|
|
|
data={answers}
|
|
|
|
/>
|
|
|
|
<Section icon={<UserIcon className={'h-6 w-6'} />} data={yourMarkets} />
|
2022-09-12 16:34:56 +00:00
|
|
|
<Row className={'gap-2 text-xl text-gray-700'}>
|
|
|
|
<span>Balance Changes</span>
|
|
|
|
</Row>
|
2022-09-13 23:00:34 +00:00
|
|
|
<Section
|
|
|
|
icon={<CurrencyDollarIcon className={'h-6 w-6'} />}
|
|
|
|
data={bonuses}
|
|
|
|
/>
|
|
|
|
<Section
|
|
|
|
icon={<CashIcon className={'h-6 w-6'} />}
|
|
|
|
data={otherBalances}
|
|
|
|
/>
|
2022-09-12 16:34:56 +00:00
|
|
|
<Row className={'gap-2 text-xl text-gray-700'}>
|
|
|
|
<span>General</span>
|
|
|
|
</Row>
|
2022-09-13 23:00:34 +00:00
|
|
|
<Section
|
|
|
|
icon={<UsersIcon className={'h-6 w-6'} />}
|
|
|
|
data={userInteractions}
|
|
|
|
/>
|
|
|
|
<Section
|
|
|
|
icon={<InboxInIcon className={'h-6 w-6'} />}
|
|
|
|
data={generalOther}
|
|
|
|
/>
|
2022-09-12 16:34:56 +00:00
|
|
|
<WatchMarketModal open={showWatchModal} setOpen={setShowWatchModal} />
|
|
|
|
</Col>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|