-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathSentimentPopupModal.tsx
More file actions
188 lines (174 loc) · 6.78 KB
/
SentimentPopupModal.tsx
File metadata and controls
188 lines (174 loc) · 6.78 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
import type { ReactElement } from 'react';
import { useState, useCallback, useMemo, useEffect } from 'react';
import { useMutation } from '@tanstack/react-query';
import classNames from 'classnames';
import type { ModalProps } from './common/Modal';
import { Modal } from './common/Modal';
import { ModalSize } from './common/types';
import { submitFeedSentiment } from '../../graphql/feedSentiment';
import type { FeedSentiment } from '../../graphql/feedSentiment';
import { useToastNotification } from '../../hooks/useToastNotification';
import { useLogContext } from '../../contexts/LogContext';
import { LogEvent, TargetType } from '../../lib/log';
const emojis = [
{ emoji: '😊', sentiment: 'good' as const, label: 'Happy' },
{ emoji: '😐', sentiment: 'neutral' as const, label: 'Neutral' },
{ emoji: '😞', sentiment: 'bad' as const, label: 'Unhappy' },
];
// Fisher-Yates shuffle
const shuffleArray = <T,>(array: T[]): T[] => {
const shuffled = [...array];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
};
const SentimentPopupModal = ({
onRequestClose,
...props
}: ModalProps): ReactElement => {
const { displayToast } = useToastNotification();
const { logEvent } = useLogContext();
const [selectedSentiment, setSelectedSentiment] =
useState<FeedSentiment | null>(null);
const [isSelecting, setIsSelecting] = useState(false);
const [showSuccess, setShowSuccess] = useState(false);
const shuffledEmojis = useMemo(() => shuffleArray(emojis), []);
useEffect(() => {
logEvent({
event_name: LogEvent.OpenFeedSentiment,
target_type: TargetType.FeedSentiment,
});
}, [logEvent]);
const { mutate: submitMutation, isPending } = useMutation({
mutationFn: (sentiment: FeedSentiment) => submitFeedSentiment(sentiment),
onSuccess: () => {
setShowSuccess(true);
setTimeout(() => {
onRequestClose?.(null);
}, 1500);
},
onError: () => {
displayToast('Failed to submit feedback. Please try again.');
setIsSelecting(false);
},
});
const handleEmojiClick = useCallback(
(sentiment: FeedSentiment) => {
if (isPending || isSelecting) return;
setSelectedSentiment(sentiment);
setIsSelecting(true);
logEvent({
event_name: LogEvent.SubmitFeedSentiment,
target_type: TargetType.FeedSentiment,
extra: JSON.stringify({ sentiment }),
});
submitMutation(sentiment);
},
[isPending, isSelecting, logEvent, submitMutation],
);
return (
<Modal
{...props}
onRequestClose={onRequestClose}
size={ModalSize.Small}
shouldCloseOnOverlayClick={!isPending}
className={{
overlay:
'bg-overlay-quaternary-onion backdrop-blur-[8px] animate-fade-in',
modal: classNames(
'relative overflow-hidden',
'border-2 border-border-subtlest-tertiary',
'shadow-2 animate-slide-up',
'before:absolute before:-top-0.5 before:left-1/2 before:-translate-x-1/2',
'before:w-3/5 before:h-0.5',
'before:bg-gradient-to-r before:from-transparent before:via-accent-avocado-default before:via-40% before:via-accent-cheese-default before:via-60% before:to-transparent',
'before:opacity-60 before:blur-[1px]',
),
}}
>
<div className="relative px-14 py-12">
{/* Question */}
<h2
className={classNames(
'mb-9 text-center font-bold typo-title2',
'bg-gradient-to-br from-text-primary to-text-secondary',
'bg-clip-text text-transparent',
'animate-fade-slide-in',
)}
>
How happy with the feed are you?
</h2>
{/* Emoji options */}
<div
className={classNames(
'flex items-center justify-center gap-5',
isSelecting && 'selecting',
)}
role="radiogroup"
aria-label="Feed sentiment options"
>
{shuffledEmojis.map((item, index) => (
<button
key={item.sentiment}
type="button"
role="radio"
aria-label={item.label}
aria-checked={selectedSentiment === item.sentiment}
disabled={isPending || isSelecting}
onClick={() => handleEmojiClick(item.sentiment)}
className={classNames(
'relative flex h-24 w-24 items-center justify-center',
'rounded-full border-2 border-border-subtlest-tertiary',
'bg-surface-float text-5xl',
'transition-all duration-150',
'hover:scale-110 hover:-rotate-3 hover:border-border-subtlest-secondary',
'active:scale-95 active:rotate-0',
'disabled:pointer-events-none',
'animate-pop-in',
index === 0 && 'animation-delay-300',
index === 1 && 'animation-delay-380',
index === 2 && 'animation-delay-460',
'before:absolute before:-inset-1 before:rounded-full before:opacity-0',
'before:transition-opacity before:duration-300',
'hover:before:opacity-30',
item.sentiment === 'good' &&
'before:bg-[radial-gradient(circle,var(--theme-accent-avocado-default)_0%,transparent_70%)]',
item.sentiment === 'neutral' &&
'before:bg-[radial-gradient(circle,var(--theme-accent-cheese-default)_0%,transparent_70%)]',
item.sentiment === 'bad' &&
'before:bg-[radial-gradient(circle,var(--theme-accent-bacon-default)_0%,transparent_70%)]',
// Selection state
selectedSentiment === item.sentiment &&
isSelecting &&
'animate-emoji-select before:!opacity-80 before:animate-glow-pulse',
// Hide non-selected when selecting
isSelecting &&
selectedSentiment !== item.sentiment &&
'animate-emoji-disappear',
)}
>
<span className="relative z-10">{item.emoji}</span>
</button>
))}
</div>
{/* Success message */}
{showSuccess && (
<div
className={classNames(
'absolute inset-0 flex flex-col items-center justify-center',
'animate-fade-in',
)}
>
<div className="mb-4 text-6xl animate-success-bounce">✨</div>
<div className="font-bold text-accent-avocado-default typo-title3 animate-success-fade">
Thanks for your feedback!
</div>
</div>
)}
</div>
</Modal>
);
};
export default SentimentPopupModal;