328 lines
10 KiB
TypeScript
328 lines
10 KiB
TypeScript
import { memo, useEffect, useState } from 'react'
|
|
import { useRouter } from 'next/router'
|
|
import { getEffectiveTier, User } from 'common/user'
|
|
import clsx from 'clsx'
|
|
import { track } from 'web/lib/service/analytics'
|
|
import { Row } from 'web/components/layout/row'
|
|
import { formatMoney } from 'common/util/format'
|
|
import { Modal } from 'web/components/layout/modal'
|
|
import { Col } from 'web/components/layout/col'
|
|
import { dailyStatsClass } from 'web/components/home/daily-stats'
|
|
import { InfoTooltip } from '../widgets/info-tooltip'
|
|
import {
|
|
BettingStreakModal,
|
|
hasCompletedStreakToday,
|
|
wasStreakFrozenRecently,
|
|
} from 'web/components/profile/betting-streak-modal'
|
|
import { Title } from 'web/components/widgets/title'
|
|
import { ProgressBar } from 'web/components/progress-bar'
|
|
import {
|
|
BETTING_STREAK_BONUS_AMOUNT,
|
|
BETTING_STREAK_BONUS_MAX,
|
|
} from 'common/economy'
|
|
import { QUEST_DETAILS } from 'common/quest'
|
|
import { getEffectiveBonusMultiplier } from 'common/supporter-config'
|
|
import { useQuestStatus } from 'web/hooks/use-quest-status'
|
|
import { LoadingIndicator } from 'web/components/widgets/loading-indicator'
|
|
import Link from 'next/link'
|
|
import { IconType } from 'react-icons'
|
|
import {
|
|
TbChartLine,
|
|
TbCompass,
|
|
TbFlame,
|
|
TbShare2,
|
|
TbSnowflake,
|
|
} from 'react-icons/tb'
|
|
import { linkClass } from '../widgets/site-link'
|
|
import { StreakProgressBar } from '../profile/streak-progress-bar'
|
|
import { ReducedBonusNotice } from 'web/components/upsell/reduced-bonus-notice'
|
|
|
|
const QUEST_STATS_CLICK_EVENT = 'click quest stats button'
|
|
|
|
export const QuestsOrStreak = memo(function DailyProfit(props: {
|
|
user: User | null | undefined
|
|
}) {
|
|
const { user } = props
|
|
const router = useRouter()
|
|
|
|
const [showQuestsModal, setShowQuestsModal] = useState(false)
|
|
|
|
useEffect(() => {
|
|
if (showQuestsModal) {
|
|
track(QUEST_STATS_CLICK_EVENT)
|
|
}
|
|
}, [showQuestsModal])
|
|
if (!user) return <></>
|
|
|
|
// Unverified users can still see and complete quests — bonuses are reduced
|
|
// (0.2x), not blocked. Verifying / subscribing scales up the rewards.
|
|
const handleQuestsClick = () => {
|
|
setShowQuestsModal(true)
|
|
}
|
|
|
|
// Preview mode: ?previewFrozen=true forces frozen state
|
|
const previewFrozen = router.query.previewFrozen === 'true'
|
|
|
|
const missingStreak = !hasCompletedStreakToday(user)
|
|
const wasFrozen = previewFrozen || wasStreakFrozenRecently(user)
|
|
|
|
// Determine visual state:
|
|
// - Normal (colored): streak completed today
|
|
// - Frozen: streak not completed but freeze was used (or previewFrozen)
|
|
// - Grayscale: streak not completed, no freeze used
|
|
const showFrozen = previewFrozen || (missingStreak && wasFrozen)
|
|
const getStyle = () => {
|
|
if (showFrozen) return ''
|
|
if (!missingStreak) return ''
|
|
return 'grayscale'
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
className={clsx('cursor-pointer', dailyStatsClass)}
|
|
onClick={handleQuestsClick}
|
|
>
|
|
<Col className={clsx(getStyle(), 'items-center')}>
|
|
<span className="flex items-center gap-1">
|
|
{showFrozen ? (
|
|
<TbSnowflake
|
|
role="img"
|
|
aria-label="Streak frozen"
|
|
className="h-4 w-4 text-cyan-500"
|
|
/>
|
|
) : (
|
|
<TbFlame
|
|
role="img"
|
|
aria-label="Streak"
|
|
className="h-4 w-4 text-orange-500"
|
|
/>
|
|
)}{' '}
|
|
{user?.currentBettingStreak ?? 0}
|
|
</span>
|
|
<span className="text-ink-600 text-xs">
|
|
{showFrozen ? 'Frozen' : 'Streak'}
|
|
</span>
|
|
</Col>
|
|
</button>
|
|
{showQuestsModal && (
|
|
<QuestsModal
|
|
open={showQuestsModal}
|
|
setOpen={setShowQuestsModal}
|
|
user={user}
|
|
previewFrozen={previewFrozen}
|
|
/>
|
|
)}
|
|
</>
|
|
)
|
|
})
|
|
|
|
export function QuestsModal(props: {
|
|
open: boolean
|
|
setOpen: (open: boolean) => void
|
|
user: User
|
|
previewFrozen?: boolean
|
|
}) {
|
|
const { open, setOpen, user, previewFrozen } = props
|
|
const questStatus = useQuestStatus(user)
|
|
const [showStreakModal, setShowStreakModal] = useState(false)
|
|
|
|
const { totalQuestsCompleted, totalQuests, questToCompletionStatus } =
|
|
questStatus ?? { questToCompletionStatus: null }
|
|
if (!questToCompletionStatus)
|
|
return (
|
|
<Modal open={open} setOpen={setOpen} size={'lg'}>
|
|
<LoadingIndicator />
|
|
</Modal>
|
|
)
|
|
const streakStatus = questToCompletionStatus['BETTING_STREAK']
|
|
const shareStatus = questToCompletionStatus['SHARES']
|
|
const createStatus = questToCompletionStatus['MARKETS_CREATED']
|
|
|
|
// Effective tier (verification + subscription) drives the bonus multiplier.
|
|
// Unverified: 0.2x, verified: 1x, subscribers higher.
|
|
const effectiveTier = getEffectiveTier(user)
|
|
const questMultiplier = getEffectiveBonusMultiplier(effectiveTier, 'quest')
|
|
const streakMultiplier = getEffectiveBonusMultiplier(effectiveTier, 'streak')
|
|
|
|
// Preview the streak bonus for the next-day streak (for the upsell notice).
|
|
const previewStreakAmount = Math.floor(
|
|
Math.min(
|
|
BETTING_STREAK_BONUS_AMOUNT * (user.currentBettingStreak || 1),
|
|
BETTING_STREAK_BONUS_MAX
|
|
) * streakMultiplier
|
|
)
|
|
|
|
return (
|
|
<Modal open={open} setOpen={setOpen} size={'lg'}>
|
|
<div className="bg-canvas-0 text-ink-1000 rounded-lg p-3">
|
|
<Col className={'mb-6 items-center justify-center gap-2'}>
|
|
<Title className={'!mb-1'}> Your quests</Title>
|
|
<span className="text-ink-700 flex items-center gap-1 text-sm">
|
|
<TbCompass
|
|
aria-hidden="true"
|
|
className="h-4 w-4"
|
|
/>
|
|
{`${totalQuestsCompleted}/${totalQuests}`} completed
|
|
</span>
|
|
<ProgressBar
|
|
value={totalQuestsCompleted / totalQuests}
|
|
max={1}
|
|
className={'mb-1 w-1/2'}
|
|
/>
|
|
{(effectiveTier === 'unverified' ||
|
|
effectiveTier === 'restricted') && (
|
|
<ReducedBonusNotice
|
|
tier={effectiveTier}
|
|
kind="quest"
|
|
earned={previewStreakAmount}
|
|
className="mx-auto max-w-md"
|
|
/>
|
|
)}
|
|
</Col>
|
|
<Col className={'mb-4 gap-6'}>
|
|
<Row className={'text-primary-700 '}>Daily</Row>
|
|
<QuestRow
|
|
icon={TbFlame}
|
|
iconClassName="text-orange-500"
|
|
title={
|
|
(user.currentBettingStreak ?? 0) > 0
|
|
? `Continue your ${user.currentBettingStreak}-day prediction streak`
|
|
: 'Make a prediction once per day'
|
|
}
|
|
complete={streakStatus.currentCount >= streakStatus.requiredCount}
|
|
status={`(${streakStatus.currentCount}/${streakStatus.requiredCount})`}
|
|
reward={previewStreakAmount}
|
|
onClick={() => setShowStreakModal(true)}
|
|
/>
|
|
{(user?.currentBettingStreak ?? 0) <= 5 && (
|
|
<Row className="-mt-2 w-full px-2 pl-12">
|
|
<StreakProgressBar
|
|
currentStreak={user?.currentBettingStreak ?? 0}
|
|
questMultiplier={streakMultiplier}
|
|
/>
|
|
</Row>
|
|
)}
|
|
<QuestRow
|
|
icon={TbShare2}
|
|
iconClassName="text-primary-500"
|
|
title={`Share ${shareStatus.requiredCount} market today`}
|
|
complete={shareStatus.currentCount >= shareStatus.requiredCount}
|
|
status={`(${shareStatus.currentCount}/${shareStatus.requiredCount})`}
|
|
reward={Math.floor(
|
|
QUEST_DETAILS.SHARES.rewardAmount * questMultiplier
|
|
)}
|
|
info={'Share a question with a friend!'}
|
|
/>
|
|
<Row className={'text-primary-700'}>Weekly</Row>
|
|
<QuestRow
|
|
icon={TbChartLine}
|
|
iconClassName="text-teal-500"
|
|
title={`Create a market this week`}
|
|
complete={createStatus.currentCount >= createStatus.requiredCount}
|
|
status={`(${createStatus.currentCount}/${createStatus.requiredCount})`}
|
|
reward={Math.floor(
|
|
QUEST_DETAILS.MARKETS_CREATED.rewardAmount * questMultiplier
|
|
)}
|
|
href={'/create'}
|
|
/>
|
|
{/* <QuestRow
|
|
emoji={'🙋️'}
|
|
title={`Refer a friend this week`}
|
|
complete={
|
|
referralsStatus.currentCount >= referralsStatus.requiredCount
|
|
}
|
|
status={`(${referralsStatus.currentCount}/${referralsStatus.requiredCount})`}
|
|
reward={QUEST_DETAILS.REFERRALS.rewardAmount}
|
|
info={
|
|
'Just click the share button on a question and your referral code will be added to the link'
|
|
}
|
|
href={'/referrals'}
|
|
/> */}
|
|
</Col>
|
|
</div>
|
|
<BettingStreakModal
|
|
isOpen={showStreakModal}
|
|
setOpen={setShowStreakModal}
|
|
currentUser={user}
|
|
previewFrozen={previewFrozen}
|
|
/>
|
|
</Modal>
|
|
)
|
|
}
|
|
|
|
const QuestRow = (props: {
|
|
icon: IconType
|
|
iconClassName?: string
|
|
title: string
|
|
complete: boolean
|
|
status: string
|
|
reward: number
|
|
info?: string
|
|
href?: string
|
|
onClick?: () => void
|
|
}) => {
|
|
const {
|
|
title,
|
|
complete,
|
|
status,
|
|
reward,
|
|
icon: Icon,
|
|
iconClassName,
|
|
info,
|
|
href,
|
|
onClick,
|
|
} = props
|
|
return (
|
|
<Row className={'justify-between'}>
|
|
<Col>
|
|
<Row className={'items-center gap-4 sm:gap-6'}>
|
|
<Icon
|
|
aria-hidden="true"
|
|
className={clsx(
|
|
'h-8 w-8 shrink-0 sm:h-9 sm:w-9',
|
|
complete ? iconClassName : 'text-ink-400'
|
|
)}
|
|
/>
|
|
<Col>
|
|
<span className={clsx('text-left sm:text-xl')}>
|
|
{href ? (
|
|
<Link className={linkClass} href={href}>
|
|
{title}
|
|
</Link>
|
|
) : onClick ? (
|
|
<button className="text-left" onClick={onClick}>
|
|
{title}
|
|
</button>
|
|
) : (
|
|
title
|
|
)}
|
|
{info && (
|
|
<InfoTooltip className={'!mb-1 ml-1 !h-4 !w-4'} text={info} />
|
|
)}
|
|
</span>
|
|
<span
|
|
className={clsx(
|
|
'text-ink-500 text-sm',
|
|
complete && 'text-primary-500'
|
|
)}
|
|
>
|
|
{complete ? 'Complete!' : status}
|
|
</span>
|
|
</Col>
|
|
</Row>
|
|
</Col>
|
|
<Col className={''}>
|
|
<span
|
|
className={clsx(
|
|
'text-lg sm:text-xl',
|
|
complete ? 'text-teal-600' : 'text-ink-500'
|
|
)}
|
|
>
|
|
+{formatMoney(reward)}
|
|
</span>
|
|
</Col>
|
|
</Row>
|
|
)
|
|
}
|