-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathClickbaitShield.tsx
More file actions
139 lines (134 loc) · 4.22 KB
/
ClickbaitShield.tsx
File metadata and controls
139 lines (134 loc) · 4.22 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
import type { ReactElement } from 'react';
import React from 'react';
import { useRouter } from 'next/router';
import { Button, ButtonSize } from '../../buttons/Button';
import { ShieldCheckIcon, ShieldIcon, ShieldWarningIcon } from '../../icons';
import {
usePlusSubscription,
useViewSize,
ViewSize,
useClickbaitTries,
useConditionalFeature,
} from '../../../hooks';
import { useLazyModal } from '../../../hooks/useLazyModal';
import { LazyModal } from '../../modals/common/types';
import type { Post } from '../../../graphql/posts';
import { useSmartTitle } from '../../../hooks/post/useSmartTitle';
import { FeedSettingsMenu } from '../../feeds/FeedSettings/types';
import { useAuthContext } from '../../../contexts/AuthContext';
import { webappUrl } from '../../../lib/constants';
import { Tooltip } from '../../tooltip/Tooltip';
import { featureFeedClickbaitShieldWarning } from '../../../lib/featureManagement';
export const ClickbaitShield = ({
post,
}: {
post: Post;
}): ReactElement | null => {
const { openModal } = useLazyModal();
const { isPlus } = usePlusSubscription();
const { fetchSmartTitle, fetchedSmartTitle, shieldActive } =
useSmartTitle(post);
const isMobile = useViewSize(ViewSize.MobileL);
const router = useRouter();
const { user } = useAuthContext();
const { hasUsedFreeTrial, triesLeft } = useClickbaitTries();
const { value: showWarningShield } = useConditionalFeature({
feature: featureFeedClickbaitShieldWarning,
shouldEvaluate: !isPlus,
});
if (!isPlus) {
if (!fetchedSmartTitle && !showWarningShield) {
return null;
}
return (
<Tooltip
className="max-w-70 text-center !typo-subhead"
content={
fetchedSmartTitle ? (
<>
{hasUsedFreeTrial &&
'Want to automatically optimize titles across your feed? Upgrade to Plus'}
</>
) : (
<>
{hasUsedFreeTrial
? `Potential issues detected in this title. To get clearer, more informative titles, enable Clickbait Shield`
: `This title could be clearer and more informative. Try out Clickbait Shield for free (${triesLeft} uses left this month).`}
</>
)
}
>
<Button
className="relative mr-2 text-accent-cheese-default"
size={ButtonSize.XSmall}
icon={
fetchedSmartTitle ? (
<ShieldCheckIcon className="text-status-success" />
) : (
<ShieldWarningIcon
className={
hasUsedFreeTrial
? 'text-accent-ketchup-default'
: 'text-accent-cheese-default'
}
/>
)
}
iconSecondaryOnHover
onClick={async () => {
if (hasUsedFreeTrial) {
if (isMobile) {
openModal({
type: LazyModal.ClickbaitShield,
});
} else {
if (!user) {
throw new Error(
'ClickbaitShield requires an authenticated user to edit feed settings',
);
}
router.push(
`${webappUrl}feeds/${user.id}/edit?dview=${FeedSettingsMenu.AI}`,
);
}
} else if (isMobile) {
openModal({
type: LazyModal.ClickbaitShield,
props: {
hasUsedFreeTrial,
fetchSmartTitle,
},
});
} else {
await fetchSmartTitle();
}
}}
/>
</Tooltip>
);
}
return (
<Tooltip
className="max-w-70 text-center !typo-subhead"
content={
shieldActive
? 'Click to see the original title'
: 'Click to see the optimized title'
}
>
<Button
className="relative mr-2"
size={ButtonSize.XSmall}
icon={
shieldActive ? (
<ShieldCheckIcon className="text-status-success" />
) : (
<ShieldIcon />
)
}
iconSecondaryOnHover
onClick={fetchSmartTitle}
/>
</Tooltip>
);
};