|
| 1 | +import React, { useCallback, useEffect, useMemo, useRef } from 'react'; |
| 2 | +import type { ReactElement } from 'react'; |
| 3 | +import classNames from 'classnames'; |
| 4 | +import { |
| 5 | + Typography, |
| 6 | + TypographyColor, |
| 7 | + TypographyTag, |
| 8 | + TypographyType, |
| 9 | +} from '../typography/Typography'; |
| 10 | +import { Button, ButtonSize, ButtonVariant } from '../buttons/Button'; |
| 11 | +import ProgressCircle from '../ProgressCircle'; |
| 12 | +import CloseButton from '../CloseButton'; |
| 13 | +import { useAuthContext } from '../../contexts/AuthContext'; |
| 14 | +import { useLogContext } from '../../contexts/LogContext'; |
| 15 | +import { useActions } from '../../hooks'; |
| 16 | +import { ActionType } from '../../graphql/actions'; |
| 17 | +import type { ProfileCompletion } from '../../lib/user'; |
| 18 | +import { webappUrl } from '../../lib/constants'; |
| 19 | +import { LogEvent, TargetType } from '../../lib/log'; |
| 20 | +import { |
| 21 | + profileCompletionCardBorder, |
| 22 | + profileCompletionCardBg, |
| 23 | + profileCompletionButtonBg, |
| 24 | +} from '../../styles/custom'; |
| 25 | + |
| 26 | +type CompletionItem = { |
| 27 | + label: string; |
| 28 | + completed: boolean; |
| 29 | + redirectPath: string; |
| 30 | + cta: string; |
| 31 | + benefit: string; |
| 32 | +}; |
| 33 | + |
| 34 | +type ProfileCompletionCardProps = { |
| 35 | + className?: Partial<{ |
| 36 | + container: string; |
| 37 | + card: string; |
| 38 | + }>; |
| 39 | +}; |
| 40 | + |
| 41 | +const getCompletionItems = ( |
| 42 | + completion: ProfileCompletion, |
| 43 | +): CompletionItem[] => { |
| 44 | + return [ |
| 45 | + { |
| 46 | + label: 'Profile image', |
| 47 | + completed: completion.hasProfileImage, |
| 48 | + redirectPath: `${webappUrl}settings/profile`, |
| 49 | + cta: 'Add profile image', |
| 50 | + benefit: |
| 51 | + 'Stand out in comments and discussions. Profiles with photos get more engagement.', |
| 52 | + }, |
| 53 | + { |
| 54 | + label: 'Headline', |
| 55 | + completed: completion.hasHeadline, |
| 56 | + redirectPath: `${webappUrl}settings/profile?field=bio`, |
| 57 | + cta: 'Write your headline', |
| 58 | + benefit: |
| 59 | + 'Tell the community who you are. A good headline helps others connect with you.', |
| 60 | + }, |
| 61 | + { |
| 62 | + label: 'Experience level', |
| 63 | + completed: completion.hasExperienceLevel, |
| 64 | + redirectPath: `${webappUrl}settings/profile?field=experienceLevel`, |
| 65 | + cta: 'Set experience level', |
| 66 | + benefit: |
| 67 | + 'Get personalized content recommendations based on where you are in your career.', |
| 68 | + }, |
| 69 | + { |
| 70 | + label: 'Work experience', |
| 71 | + completed: completion.hasWork, |
| 72 | + redirectPath: `${webappUrl}settings/profile/experience/work`, |
| 73 | + cta: 'Add work experience', |
| 74 | + benefit: |
| 75 | + 'Showcase your background and unlock opportunities from companies looking for talent like you.', |
| 76 | + }, |
| 77 | + { |
| 78 | + label: 'Education', |
| 79 | + completed: completion.hasEducation, |
| 80 | + redirectPath: `${webappUrl}settings/profile/experience/education`, |
| 81 | + cta: 'Add education', |
| 82 | + benefit: |
| 83 | + 'Complete your story. Education helps others understand your journey.', |
| 84 | + }, |
| 85 | + ]; |
| 86 | +}; |
| 87 | + |
| 88 | +export const ProfileCompletionCard = ({ |
| 89 | + className, |
| 90 | +}: ProfileCompletionCardProps): ReactElement | null => { |
| 91 | + const { user } = useAuthContext(); |
| 92 | + const { logEvent } = useLogContext(); |
| 93 | + const { checkHasCompleted, completeAction, isActionsFetched } = useActions(); |
| 94 | + const profileCompletion = user?.profileCompletion; |
| 95 | + const isImpressionTracked = useRef(false); |
| 96 | + |
| 97 | + const items = useMemo( |
| 98 | + () => (profileCompletion ? getCompletionItems(profileCompletion) : []), |
| 99 | + [profileCompletion], |
| 100 | + ); |
| 101 | + |
| 102 | + const incompleteItems = useMemo( |
| 103 | + () => items.filter((item) => !item.completed), |
| 104 | + [items], |
| 105 | + ); |
| 106 | + |
| 107 | + const firstIncompleteItem = incompleteItems[0]; |
| 108 | + const progress = profileCompletion?.percentage ?? 0; |
| 109 | + const isCompleted = progress === 100; |
| 110 | + const isDismissed = |
| 111 | + isActionsFetched && checkHasCompleted(ActionType.ProfileCompletionCard); |
| 112 | + |
| 113 | + const shouldShow = |
| 114 | + profileCompletion && !isCompleted && firstIncompleteItem && !isDismissed; |
| 115 | + |
| 116 | + useEffect(() => { |
| 117 | + if (!shouldShow || isImpressionTracked.current) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + logEvent({ |
| 122 | + event_name: LogEvent.Impression, |
| 123 | + target_type: TargetType.ProfileCompletionCard, |
| 124 | + }); |
| 125 | + isImpressionTracked.current = true; |
| 126 | + }, [shouldShow, logEvent]); |
| 127 | + |
| 128 | + const handleCtaClick = useCallback(() => { |
| 129 | + logEvent({ |
| 130 | + event_name: LogEvent.Click, |
| 131 | + target_type: TargetType.ProfileCompletionCard, |
| 132 | + target_id: 'cta', |
| 133 | + }); |
| 134 | + }, [logEvent]); |
| 135 | + |
| 136 | + const handleDismiss = useCallback(() => { |
| 137 | + logEvent({ |
| 138 | + event_name: LogEvent.Click, |
| 139 | + target_type: TargetType.ProfileCompletionCard, |
| 140 | + target_id: 'dismiss', |
| 141 | + }); |
| 142 | + completeAction(ActionType.ProfileCompletionCard); |
| 143 | + }, [logEvent, completeAction]); |
| 144 | + |
| 145 | + if (!shouldShow) { |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + return ( |
| 150 | + <div |
| 151 | + className={classNames('flex flex-1 p-2 laptop:p-0', className?.container)} |
| 152 | + > |
| 153 | + <div |
| 154 | + style={{ |
| 155 | + border: profileCompletionCardBorder, |
| 156 | + background: profileCompletionCardBg, |
| 157 | + }} |
| 158 | + className={classNames( |
| 159 | + 'relative flex flex-1 flex-col gap-4 rounded-16 px-6 py-4', |
| 160 | + 'backdrop-blur-3xl', |
| 161 | + className?.card, |
| 162 | + )} |
| 163 | + > |
| 164 | + <CloseButton |
| 165 | + className="absolute right-2 top-2" |
| 166 | + size={ButtonSize.XSmall} |
| 167 | + onClick={handleDismiss} |
| 168 | + /> |
| 169 | + <ProgressCircle progress={progress} size={48} showPercentage /> |
| 170 | + <Typography |
| 171 | + type={TypographyType.Title2} |
| 172 | + color={TypographyColor.Primary} |
| 173 | + bold |
| 174 | + > |
| 175 | + Complete your profile |
| 176 | + </Typography> |
| 177 | + <div className="flex flex-col gap-2"> |
| 178 | + <Typography |
| 179 | + type={TypographyType.Callout} |
| 180 | + color={TypographyColor.Tertiary} |
| 181 | + > |
| 182 | + Finish setting up your profile to get the most out of daily.dev: |
| 183 | + </Typography> |
| 184 | + <ul className="flex list-inside list-disc flex-col gap-1"> |
| 185 | + {incompleteItems.map((item) => ( |
| 186 | + <li key={item.label}> |
| 187 | + <Typography |
| 188 | + tag={TypographyTag.Span} |
| 189 | + type={TypographyType.Callout} |
| 190 | + color={TypographyColor.Secondary} |
| 191 | + > |
| 192 | + {item.label} |
| 193 | + </Typography> |
| 194 | + </li> |
| 195 | + ))} |
| 196 | + </ul> |
| 197 | + </div> |
| 198 | + <Button |
| 199 | + style={{ |
| 200 | + background: profileCompletionButtonBg, |
| 201 | + }} |
| 202 | + className="mt-auto w-full" |
| 203 | + tag="a" |
| 204 | + href={firstIncompleteItem.redirectPath} |
| 205 | + type="button" |
| 206 | + variant={ButtonVariant.Primary} |
| 207 | + size={ButtonSize.Small} |
| 208 | + onClick={handleCtaClick} |
| 209 | + > |
| 210 | + {firstIncompleteItem.cta} |
| 211 | + </Button> |
| 212 | + </div> |
| 213 | + </div> |
| 214 | + ); |
| 215 | +}; |
| 216 | + |
| 217 | +export default ProfileCompletionCard; |
0 commit comments