-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathAchievementCompletionModal.tsx
More file actions
400 lines (370 loc) · 14.4 KB
/
AchievementCompletionModal.tsx
File metadata and controls
400 lines (370 loc) · 14.4 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import type { KeyboardEvent, MouseEvent, ReactElement } from 'react';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { Button, ButtonVariant } from '../buttons/Button';
import { ClickableText } from '../buttons/ClickableText';
import { LazyImage } from '../LazyImage';
import { Loader } from '../Loader';
import { useAuthContext } from '../../contexts/AuthContext';
import { useLogContext } from '../../contexts/LogContext';
import { useActions } from '../../hooks/useActions';
import { useProfileAchievements } from '../../hooks/profile/useProfileAchievements';
import { useTrackedAchievement } from '../../hooks/profile/useTrackedAchievement';
import { ActionType } from '../../graphql/actions';
import { LogEvent, TargetType } from '../../lib/log';
import type { LazyModalCommonProps, ModalProps } from './common/Modal';
import { Modal } from './common/Modal';
import { ModalClose } from './common/ModalClose';
import {
Typography,
TypographyColor,
TypographyTag,
TypographyType,
} from '../typography/Typography';
import { Checkbox } from '../fields/Checkbox';
import { getTargetCount } from '../../graphql/user/achievements';
import { formatAchievementReward } from '../../lib/achievements';
import { useAchievementRewardDisplay } from '../../hooks/useAchievementRewardDisplay';
import { sortLockedAchievements } from './achievement/sortAchievements';
const SPARKLE_DURATION_MS = 4500;
const sparkles = Array.from({ length: 60 }, (_, i) => ({
id: `s${i}`,
left: `${5 + ((i * 17 + 7) % 90)}%`,
drift: ((i * 13) % 25) - 12,
fall: 320 + ((i * 19) % 120),
delay: (i * 37) % 1200,
size: 3 + ((i * 7) % 5),
}));
interface AchievementCompletionModalProps
extends Omit<ModalProps, 'onRequestClose'> {
achievementId: string;
onAfterClose?: () => void;
onRequestClose?: LazyModalCommonProps['onRequestClose'];
}
export const AchievementCompletionModal = ({
achievementId,
onRequestClose,
...props
}: AchievementCompletionModalProps): ReactElement => {
const { user } = useAuthContext();
const { logEvent } = useLogContext();
const { completeAction, checkHasCompleted } = useActions();
const { achievements, isPending } = useProfileAchievements(user);
const { trackedAchievement, trackAchievement } = useTrackedAchievement(
user?.id,
);
const { showAchievementXp } = useAchievementRewardDisplay();
const isOptedOut = checkHasCompleted(ActionType.DisableAchievementCompletion);
const handleOptOut = () => {
if (!isOptedOut) {
logEvent({
event_name: LogEvent.DismissAchievementCompletion,
});
completeAction(ActionType.DisableAchievementCompletion);
}
};
const [phase, setPhase] = useState<'celebrate' | 'pickNext'>('celebrate');
const [isTracking, setIsTracking] = useState(false);
const [showSparkles, setShowSparkles] = useState(true);
const [sparklesFalling, setSparklesFalling] = useState(false);
const loggedImpression = useRef(false);
const handleClose = (event?: MouseEvent | KeyboardEvent) =>
onRequestClose?.(event);
const unlockedAchievement = useMemo(
() => achievements?.find((item) => item.achievement.id === achievementId),
[achievementId, achievements],
);
const lockedAchievements = useMemo(() => {
return achievements
? sortLockedAchievements(achievements, showAchievementXp)
: [];
}, [achievements, showAchievementXp]);
useEffect(() => {
if (!showSparkles) {
return undefined;
}
const fallTimer = setTimeout(() => setSparklesFalling(true), 10);
const hideTimer = setTimeout(() => {
setShowSparkles(false);
setSparklesFalling(false);
}, SPARKLE_DURATION_MS);
return () => {
clearTimeout(fallTimer);
clearTimeout(hideTimer);
};
}, [showSparkles]);
useEffect(() => {
if (phase !== 'celebrate' || !achievementId || loggedImpression.current) {
return;
}
logEvent({
event_name: LogEvent.Impression,
target_type: TargetType.AchievementCompletion,
target_id: achievementId,
});
loggedImpression.current = true;
}, [achievementId, logEvent, phase]);
const handleTrack = async (
event: MouseEvent,
selectedAchievementId: string,
): Promise<void> => {
setIsTracking(true);
try {
await trackAchievement(selectedAchievementId);
logEvent({
event_name: LogEvent.Click,
target_type: TargetType.AchievementCompletion,
target_id: selectedAchievementId,
});
handleClose(event);
} finally {
setIsTracking(false);
}
};
return (
<Modal
{...props}
kind={Modal.Kind.FlexibleCenter}
size={Modal.Size.Small}
onRequestClose={handleClose}
isDrawerOnMobile
>
<ModalClose className="top-2" onClick={handleClose} />
<Modal.Body className="relative flex flex-col gap-4 overflow-hidden">
{phase === 'celebrate' && showSparkles && (
<div className="pointer-events-none absolute inset-0 overflow-hidden">
{sparkles.map((sparkle) => (
<span
key={sparkle.id}
className="absolute top-0 block rounded-full bg-accent-cheese-default transition-all ease-out"
style={{
left: sparkle.left,
width: sparkle.size,
height: sparkle.size,
opacity: sparklesFalling ? 0 : 1,
transform: sparklesFalling
? `translateY(${sparkle.fall}px) translateX(${sparkle.drift}px)`
: 'translateY(-8px) translateX(0px)',
transitionDuration: '2500ms',
transitionDelay: `${sparkle.delay}ms`,
boxShadow: `0 0 ${sparkle.size + 2}px ${
sparkle.size / 2
}px rgba(255, 207, 80, 0.4)`,
}}
/>
))}
</div>
)}
{phase === 'celebrate' && (
<>
{isPending && (
<div className="flex flex-col items-center gap-3 py-10 text-center">
<Loader className="size-8" />
<Typography type={TypographyType.Callout} bold>
Loading achievement...
</Typography>
</div>
)}
{!isPending && unlockedAchievement && (
<>
<div className="relative mx-auto mt-2">
<div className="absolute inset-2 rounded-full bg-accent-cheese-subtler blur-xl" />
<LazyImage
imgSrc={unlockedAchievement.achievement.image}
imgAlt={unlockedAchievement.achievement.name}
className="relative size-24 rounded-16 object-cover"
fallbackSrc="https://daily.dev/default-achievement.png"
/>
</div>
<div className="flex flex-col items-center gap-2 text-center">
<Typography
tag={TypographyTag.H2}
type={TypographyType.Title3}
bold
>
Achievement Unlocked!
</Typography>
<Typography type={TypographyType.Title4} bold>
{unlockedAchievement.achievement.name}
</Typography>
<Typography
type={TypographyType.Callout}
color={TypographyColor.Tertiary}
>
{unlockedAchievement.achievement.description}
</Typography>
<div className="text-text-invert rounded-14 bg-accent-cabbage-default px-3 py-1 font-bold typo-subhead">
{formatAchievementReward(unlockedAchievement.achievement, {
showAchievementXp,
signed: true,
})}
</div>
</div>
<Button
variant={ButtonVariant.Primary}
onClick={() => setPhase('pickNext')}
>
Choose next goal
</Button>
<Checkbox
name="disable_achievement_completion"
className="mt-2"
checked={isOptedOut}
onToggleCallback={handleOptOut}
>
Never show this again
</Checkbox>
</>
)}
{!isPending && !unlockedAchievement && (
<div className="flex flex-col items-center gap-4 py-4 text-center">
<Typography type={TypographyType.Title3} bold>
Achievement Unlocked!
</Typography>
<Typography
type={TypographyType.Callout}
color={TypographyColor.Tertiary}
>
Let's pick your next goal.
</Typography>
<Button
variant={ButtonVariant.Primary}
onClick={() => setPhase('pickNext')}
>
Choose next goal
</Button>
<Checkbox
name="disable_achievement_completion_fallback"
className="mt-2"
checked={isOptedOut}
onToggleCallback={handleOptOut}
>
Never show this again
</Checkbox>
</div>
)}
</>
)}
{phase === 'pickNext' && (
<>
<div className="flex flex-col gap-1 text-center">
<Typography
tag={TypographyTag.H2}
type={TypographyType.Title3}
bold
>
What's your next goal?
</Typography>
<Typography
type={TypographyType.Callout}
color={TypographyColor.Tertiary}
>
Pick an achievement to focus on.
</Typography>
</div>
{lockedAchievements.length === 0 && (
<Typography
type={TypographyType.Callout}
color={TypographyColor.Tertiary}
>
You unlocked every achievement.
</Typography>
)}
{lockedAchievements.length > 0 && (
<div className="flex max-h-[22rem] flex-col gap-2 overflow-y-auto">
{lockedAchievements.map((userAchievement) => {
const target = getTargetCount(userAchievement.achievement);
const progressPercentage = Math.min(
(userAchievement.progress / target) * 100,
100,
);
const isTracked =
userAchievement.achievement.id ===
trackedAchievement?.achievement.id;
return (
<div
key={userAchievement.achievement.id}
className="rounded-12 border border-border-subtlest-tertiary bg-surface-float p-3"
>
<div className="flex items-start gap-3">
<LazyImage
imgSrc={userAchievement.achievement.image}
imgAlt={userAchievement.achievement.name}
className="size-10 rounded-10 object-cover"
fallbackSrc="https://daily.dev/default-achievement.png"
/>
<div className="min-w-0 flex-1">
<Typography
type={TypographyType.Callout}
bold
className="truncate"
>
{userAchievement.achievement.name}
</Typography>
<Typography
type={TypographyType.Footnote}
color={TypographyColor.Tertiary}
className="line-clamp-2"
>
{userAchievement.achievement.description}
</Typography>
</div>
<Button
variant={
isTracked
? ButtonVariant.Secondary
: ButtonVariant.Primary
}
disabled={isTracking || isTracked}
onClick={(event: React.MouseEvent) =>
handleTrack(event, userAchievement.achievement.id)
}
>
{isTracked ? 'Tracking' : 'Track'}
</Button>
</div>
<div className="mt-2 flex items-center justify-between">
<Typography
type={TypographyType.Footnote}
color={TypographyColor.Tertiary}
>
{userAchievement.progress}/{target}
</Typography>
<Typography
type={TypographyType.Footnote}
color={TypographyColor.Tertiary}
>
{formatAchievementReward(
userAchievement.achievement,
{
showAchievementXp,
short: !showAchievementXp,
},
)}
</Typography>
</div>
<div className="rounded-sm mt-1 h-1.5 w-full overflow-hidden bg-accent-pepper-subtler">
<div
className="rounded-sm h-full bg-accent-cabbage-default transition-all"
style={{ width: `${progressPercentage}%` }}
/>
</div>
</div>
);
})}
</div>
)}
<div className="text-center">
<ClickableText
className="text-text-tertiary typo-callout"
onClick={handleClose}
>
Maybe later
</ClickableText>
</div>
</>
)}
</Modal.Body>
</Modal>
);
};
export default AchievementCompletionModal;