-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathReadingStreakButton.tsx
More file actions
161 lines (150 loc) · 4.85 KB
/
ReadingStreakButton.tsx
File metadata and controls
161 lines (150 loc) · 4.85 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
import type { ReactElement } from 'react';
import React, { useCallback, useState } from 'react';
import classnames from 'classnames';
import { ReadingStreakPopup } from './popup/ReadingStreakPopup';
import type { ButtonIconPosition } from '../buttons/Button';
import { Button, ButtonSize, ButtonVariant } from '../buttons/Button';
import { ReadingStreakIcon, WarningIcon } from '../icons';
import { SimpleTooltip } from '../tooltips';
import type { UserStreak } from '../../graphql/users';
import { useViewSize, ViewSize } from '../../hooks';
import { isTesting } from '../../lib/constants';
import { useLogContext } from '../../contexts/LogContext';
import { LogEvent } from '../../lib/log';
import { RootPortal } from '../tooltips/Portal';
import { Drawer } from '../drawers';
import ConditionalWrapper from '../ConditionalWrapper';
import type { TooltipPosition } from '../tooltips/BaseTooltipContainer';
import { useAuthContext } from '../../contexts/AuthContext';
import { isSameDayInTimezone } from '../../lib/timezones';
import { IconWrapper } from '../Icon';
import { useStreakTimezoneOk } from '../../hooks/streaks/useStreakTimezoneOk';
interface ReadingStreakButtonProps {
streak: UserStreak;
isLoading: boolean;
compact?: boolean;
iconPosition?: ButtonIconPosition;
className?: string;
}
interface CustomStreaksTooltipProps {
streak: UserStreak;
children?: ReactElement;
shouldShowStreaks?: boolean;
setShouldShowStreaks?: (value: boolean) => void;
placement: TooltipPosition;
}
function CustomStreaksTooltip({
streak,
children,
shouldShowStreaks,
setShouldShowStreaks,
placement,
}: CustomStreaksTooltipProps): ReactElement {
return (
<SimpleTooltip
interactive
showArrow={false}
placement={placement}
visible={shouldShowStreaks}
forceLoad={!isTesting}
container={{
paddingClassName: 'p-0',
bgClassName: 'bg-accent-pepper-subtlest',
textClassName: 'text-text-primary typo-callout',
className: 'border border-border-subtlest-tertiary rounded-16',
}}
content={<ReadingStreakPopup streak={streak} />}
onClickOutside={() => setShouldShowStreaks!(false)}
>
{children}
</SimpleTooltip>
);
}
export function ReadingStreakButton({
streak,
isLoading,
compact,
iconPosition,
className,
}: ReadingStreakButtonProps): ReactElement | null {
const { logEvent } = useLogContext();
const { user } = useAuthContext();
const isLaptop = useViewSize(ViewSize.Laptop);
const isMobile = useViewSize(ViewSize.MobileL);
const [shouldShowStreaks, setShouldShowStreaks] = useState(false);
const hasReadToday =
streak?.lastViewAt &&
isSameDayInTimezone(new Date(streak.lastViewAt), new Date(), user!.timezone);
const isTimezoneOk = useStreakTimezoneOk();
const handleToggle = useCallback(() => {
setShouldShowStreaks((state) => !state);
if (!shouldShowStreaks) {
logEvent({
event_name: LogEvent.OpenStreaks,
});
}
}, [shouldShowStreaks, logEvent]);
const Tooltip = shouldShowStreaks ? CustomStreaksTooltip : SimpleTooltip;
if (isLoading) {
return <div className="h-8 w-14 rounded-12 bg-surface-float" />;
}
if (!streak) {
return null;
}
return (
<>
<ConditionalWrapper
condition={!isMobile}
wrapper={(children: ReactElement) => (
<Tooltip
content="Current streak"
streak={streak}
shouldShowStreaks={shouldShowStreaks}
setShouldShowStreaks={setShouldShowStreaks}
placement={!isMobile && !isLaptop ? 'bottom-start' : 'bottom-end'}
>
{children}
</Tooltip>
)}
>
<Button
id="reading-streak-header-button"
type="button"
iconPosition={iconPosition}
icon={
<IconWrapper wrapperClassName="relative flex items-center gap-2">
<ReadingStreakIcon secondary={hasReadToday} />
{!isTimezoneOk && (
<WarningIcon className="!mr-0 text-raw-cheese-40" secondary />
)}
</IconWrapper>
}
variant={
isLaptop || isMobile ? ButtonVariant.Tertiary : ButtonVariant.Float
}
onClick={handleToggle}
className={classnames(
'gap-1',
compact && 'text-accent-bacon-default',
className,
)}
size={!compact && !isMobile ? ButtonSize.Medium : ButtonSize.Small}
>
{streak?.current}
{!compact && ' reading days'}
</Button>
</ConditionalWrapper>
{isMobile && (
<RootPortal>
<Drawer
displayCloseButton
isOpen={shouldShowStreaks}
onClose={handleToggle}
>
<ReadingStreakPopup streak={streak} fullWidth />
</Drawer>
</RootPortal>
)}
</>
);
}