/
new-user-goals.tsx
177 lines (166 loc) · 4.65 KB
/
new-user-goals.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { sumBy, uniqBy } from 'lodash'
import clsx from 'clsx'
import { GoGoal } from 'react-icons/go'
import Link from 'next/link'
import { useLeagueInfo } from 'web/hooks/use-leagues'
import { formatMoney } from 'common/util/format'
import { ProgressBar } from 'web/components/progress-bar'
import { useRemainingNewUserSignupBonuses } from 'web/hooks/use-request-new-user-signup-bonus'
import { useBets } from 'web/hooks/use-bets-supabase'
import {
DAYS_TO_USE_FREE_QUESTIONS,
User,
freeQuestionRemaining,
} from 'common/user'
import { Col } from '../layout/col'
import { Row } from '../layout/row'
import { useSubscription } from 'web/lib/supabase/realtime/use-subscription'
import { Bet } from 'common/bet'
import { ReactNode } from 'react'
import { InfoTooltip } from '../widgets/info-tooltip'
import { CreateQuestionButton } from '../buttons/create-question-button'
import { simpleFromNow } from 'web/lib/util/shortenedFromNow'
import { DAY_MS } from 'common/util/time'
import { Button } from '../buttons/button'
export const NewUserGoals = (props: { user: User }) => {
const { user } = props
const remainingViewBonuses = useRemainingNewUserSignupBonuses()
const bets = useBets({ userId: user.id, limit: 100, order: 'desc' })
const { rows } = useSubscription('contract_bets', {
k: 'user_id',
v: user.id,
})
const liveBets = rows?.map((r) => r.data as Bet)
const combinedBets = uniqBy(
[...(bets ?? []), ...(liveBets ?? [])],
(b) => b.id
)
const amountBet = sumBy(combinedBets, (b) => (b.amount > 0 ? b.amount : 0))
const leagueInfo = useLeagueInfo(user.id)
if (remainingViewBonuses === undefined) {
return null
}
if (remainingViewBonuses > 0) {
return (
<ProgressDisplay
className="w-full"
label="Goal 1"
value={10 - remainingViewBonuses}
goal={10}
description="questions visited"
/>
)
}
if (bets === undefined) {
return null
}
const goalAmountBet = 250
if (amountBet < goalAmountBet) {
return (
<ProgressDisplay
className="w-full"
label="Goal 2"
value={amountBet}
goal={goalAmountBet}
format={formatMoney}
description="bet"
/>
)
}
if (leagueInfo === undefined) {
return null
}
const manaEarned = leagueInfo?.mana_earned ?? 0
const manaEarnedGoal = 100
if (manaEarned < manaEarnedGoal) {
return (
<ProgressDisplay
className="w-full"
label="Goal 3"
value={manaEarned}
goal={manaEarnedGoal}
format={formatMoney}
description={
<>
mana earned{' '}
<InfoTooltip text="hi" tooltipParams={{ placement: 'bottom' }} />
</>
}
>
<Link href="/browse/for-you?s=newest" className="w-full">
<Button className="mt-2 w-full" size="xs">
Find profit in new markets
</Button>
</Link>
</ProgressDisplay>
)
}
const remaining = freeQuestionRemaining(
user?.freeQuestionsCreated,
user?.createdTime
)
const questionsCreated = user.freeQuestionsCreated ?? 0
const questionsCreatedGoal = 3
if (questionsCreated < questionsCreatedGoal) {
return (
<ProgressDisplay
className="w-full"
label="Goal 4"
value={questionsCreated}
goal={questionsCreatedGoal}
description={<>questions created</>}
>
<Row className="mt-2 gap-x-1">
🎉 You've got {remaining} free questions!
<span>
Expires in{' '}
{simpleFromNow(
user.createdTime + DAY_MS * DAYS_TO_USE_FREE_QUESTIONS
)}
</span>
</Row>
<CreateQuestionButton color="indigo" size="xs" />
</ProgressDisplay>
)
}
return null
}
const ProgressDisplay = (props: {
label: string
value: number
goal: number
description: ReactNode
format?: (value: number) => string
children?: ReactNode
className?: string
}) => {
const {
label,
value,
goal,
description,
format = (x: number) => x.toString(),
children,
className,
} = props
return (
<Col
className={clsx('gap-2 rounded-md bg-indigo-100 px-4 py-3', className)}
>
<Row className="items-center justify-between gap-4">
<div className="flex-shrink-0">
<span className="font-semibold">
{format(value)} of {format(goal)}
</span>{' '}
{description}
</div>
<Row className="items-center gap-2">
<GoGoal className="text-indigo-800" />
{label}
</Row>
</Row>
<ProgressBar className="mb-1" value={value / goal} max={1} />
{children}
</Col>
)
}