-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: gift user modal #4091
feat: gift user modal #4091
Changes from 30 commits
4637cf2
b6701f1
707572f
3f53473
7797626
c9c4e2d
6649903
4c43d6c
f1c525b
84091c1
bbc0c94
92d8ee2
f577767
663a58c
f6c79ff
84ab16b
3924acb
a3c0455
7a3fd26
e24a2fb
720a6f1
3726a1b
72a8eab
dee522c
f25e989
5b435c1
058f1de
4318c3b
e055a80
066ad9f
d9e0f03
eaa4ae2
837a213
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
import type { ReactElement } from 'react'; | ||
import React, { useState } from 'react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import type { ModalProps } from '../modals/common/Modal'; | ||
import { Modal } from '../modals/common/Modal'; | ||
import { | ||
Typography, | ||
TypographyColor, | ||
TypographyType, | ||
} from '../typography/Typography'; | ||
import CloseButton from '../CloseButton'; | ||
import { ButtonSize, ButtonVariant } from '../buttons/common'; | ||
import { Button } from '../buttons/Button'; | ||
import { PlusTitle } from './PlusTitle'; | ||
import { | ||
PaymentContextProvider, | ||
usePaymentContext, | ||
} from '../../contexts/PaymentContext'; | ||
import { plusUrl } from '../../lib/constants'; | ||
import { TextField } from '../fields/TextField'; | ||
import { UserIcon } from '../icons'; | ||
import { gqlClient } from '../../graphql/common'; | ||
import { RECOMMEND_MENTIONS_QUERY } from '../../graphql/comments'; | ||
import { RecommendedMention } from '../RecommendedMention'; | ||
import { BaseTooltip } from '../tooltips/BaseTooltip'; | ||
import type { UserShortProfile } from '../../lib/user'; | ||
import useDebounceFn from '../../hooks/useDebounceFn'; | ||
import { ProfileImageSize, ProfilePicture } from '../ProfilePicture'; | ||
import { PlusLabelColor, PlusPlanExtraLabel } from './PlusPlanExtraLabel'; | ||
|
||
interface SelectedUserProps { | ||
user: UserShortProfile; | ||
onClose: () => void; | ||
} | ||
|
||
const SelectedUser = ({ user, onClose }: SelectedUserProps) => { | ||
const { username, name } = user; | ||
|
||
return ( | ||
<div className="flex w-full max-w-full flex-row items-center gap-2 rounded-10 bg-surface-float p-2"> | ||
<ProfilePicture user={user} size={ProfileImageSize.Medium} /> | ||
<span className="flex w-full flex-1 flex-row items-center gap-2 truncate"> | ||
<Typography bold type={TypographyType.Callout}> | ||
{name} | ||
</Typography> | ||
<Typography | ||
type={TypographyType.Footnote} | ||
color={TypographyColor.Secondary} | ||
> | ||
{username} | ||
</Typography> | ||
</span> | ||
<CloseButton type="button" onClick={onClose} size={ButtonSize.XSmall} /> | ||
</div> | ||
); | ||
}; | ||
|
||
interface GiftPlusModalProps extends ModalProps { | ||
preselected?: UserShortProfile; | ||
} | ||
|
||
export function GiftPlusModalComponent({ | ||
preselected, | ||
...props | ||
}: GiftPlusModalProps): ReactElement { | ||
const [overlay, setOverlay] = useState<HTMLElement>(); | ||
const { onRequestClose } = props; | ||
const { oneTimePayment } = usePaymentContext(); | ||
const [selected, setSelected] = useState(preselected); | ||
const [index, setIndex] = useState(0); | ||
const [query, setQuery] = useState(''); | ||
const [onSearch] = useDebounceFn(setQuery, 500); | ||
const { data: users } = useQuery<UserShortProfile[]>({ | ||
queryKey: ['search', 'users', query], | ||
queryFn: async () => { | ||
const result = await gqlClient.request(RECOMMEND_MENTIONS_QUERY, { | ||
query, | ||
}); | ||
|
||
return result.recommendedMentions; | ||
}, | ||
enabled: !!query?.length, | ||
}); | ||
const isVisible = !!users?.length && !!query?.length; | ||
const onKeyDown = (e: React.KeyboardEvent) => { | ||
const movement = ['ArrowUp', 'ArrowDown', 'Enter']; | ||
if (!movement.includes(e.key)) { | ||
return; | ||
} | ||
|
||
e.preventDefault(); | ||
|
||
if (e.key === 'ArrowDown') { | ||
setIndex((prev) => { | ||
let next = prev + 1; | ||
let counter = 0; | ||
while (users[next % users.length]?.isPlus) { | ||
next += 1; | ||
counter += 1; | ||
|
||
if (counter > users.length) { | ||
return -1; | ||
} | ||
} | ||
return next % users.length; | ||
}); | ||
} else if (e.key === 'ArrowUp') { | ||
setIndex((prev) => { | ||
let next = prev - 1; | ||
let counter = 0; | ||
while (users[(next + users.length) % users.length]?.isPlus) { | ||
next -= 1; | ||
counter += 1; | ||
|
||
if (counter > users.length) { | ||
return -1; | ||
} | ||
} | ||
return (next + users.length) % users.length; | ||
}); | ||
} else { | ||
setSelected(users[index]); | ||
} | ||
}; | ||
|
||
const onSelect = (user: UserShortProfile) => { | ||
setSelected(user); | ||
setIndex(0); | ||
setQuery(''); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
{...props} | ||
isOpen | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this prop needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, should be not since it is always going to be true. Let me clean it up. |
||
kind={Modal.Kind.FixedCenter} | ||
size={Modal.Size.Small} | ||
overlayRef={setOverlay} | ||
> | ||
<Modal.Body className="gap-4"> | ||
<div className="flex flex-row justify-between"> | ||
<PlusTitle type={TypographyType.Callout} bold /> | ||
<CloseButton | ||
type="button" | ||
size={ButtonSize.Small} | ||
onClick={onRequestClose} | ||
/> | ||
</div> | ||
<Typography bold type={TypographyType.Title1}> | ||
Gift daily.dev Plus 🎁 | ||
</Typography> | ||
{selected ? ( | ||
<SelectedUser user={selected} onClose={() => setSelected(null)} /> | ||
) : ( | ||
<div className="flex flex-col"> | ||
<BaseTooltip | ||
appendTo={overlay} | ||
onClickOutside={() => setQuery('')} | ||
visible={isVisible} | ||
showArrow={false} | ||
interactive | ||
content={ | ||
<RecommendedMention | ||
users={users} | ||
selected={index} | ||
onClick={onSelect} | ||
onHover={setIndex} | ||
checkIsDisabled={(user) => user.isPlus} | ||
/> | ||
} | ||
container={{ | ||
className: 'shadow', | ||
paddingClassName: 'p-0', | ||
roundedClassName: 'rounded-16', | ||
bgClassName: 'bg-accent-pepper-subtlest', | ||
}} | ||
> | ||
<TextField | ||
leftIcon={<UserIcon />} | ||
inputId="search_user" | ||
fieldType="tertiary" | ||
autoComplete="off" | ||
label="Select a recipient by name or handle" | ||
onKeyDown={onKeyDown} | ||
onChange={(e) => onSearch(e.currentTarget.value.trim())} | ||
onFocus={(e) => setQuery(e.currentTarget.value.trim())} | ||
/> | ||
</BaseTooltip> | ||
</div> | ||
)} | ||
<div className="flex w-full flex-row items-center gap-2 rounded-10 bg-surface-float p-2"> | ||
<Typography bold type={TypographyType.Callout}> | ||
One-year plan | ||
</Typography> | ||
<PlusPlanExtraLabel | ||
color={PlusLabelColor.Success} | ||
label={oneTimePayment.extraLabel} | ||
typographyProps={{ color: TypographyColor.StatusSuccess }} | ||
/> | ||
<Typography type={TypographyType.Body} className="ml-auto mr-1"> | ||
<strong className="mr-1">{oneTimePayment?.price}</strong> | ||
{oneTimePayment?.currencyCode} | ||
</Typography> | ||
</div> | ||
<Typography type={TypographyType.Callout}> | ||
Gift one year of daily.dev Plus for {oneTimePayment?.price}. Once the | ||
payment is processed, they’ll be notified of your gift. This is a | ||
one-time purchase, not a recurring subscription. | ||
</Typography> | ||
<Button | ||
tag="a" | ||
variant={ButtonVariant.Primary} | ||
href={`${plusUrl}?giftToUserId=${selected?.id}`} | ||
disabled={!selected} | ||
> | ||
Gift & Pay {oneTimePayment?.price} | ||
</Button> | ||
</Modal.Body> | ||
</Modal> | ||
); | ||
} | ||
|
||
export function GiftPlusModal(props: ModalProps): ReactElement { | ||
return ( | ||
<PaymentContextProvider> | ||
<GiftPlusModalComponent {...props} />{' '} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The space probably not necessary here ? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. I think it was linting that did this. I'll remove. Thank you! |
||
</PaymentContextProvider> | ||
); | ||
} | ||
|
||
export default GiftPlusModal; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import { IconSize } from '../Icon'; | |
import { useFeature } from '../GrowthBookProvider'; | ||
import { feature } from '../../lib/featureManagement'; | ||
import { PlusPriceType } from '../../lib/featureValues'; | ||
import { PlusLabelColor, PlusPlanExtraLabel } from './PlusPlanExtraLabel'; | ||
|
||
export enum OnboardingPlans { | ||
Free = 'Free', | ||
|
@@ -122,15 +123,12 @@ const PlusCard = ({ | |
{heading.label} | ||
</Typography> | ||
{hasDiscount && discountPlan?.extraLabel && ( | ||
<Typography | ||
tag={TypographyTag.Span} | ||
type={TypographyType.Caption1} | ||
color={TypographyColor.StatusHelp} | ||
className="ml-3 rounded-10 bg-action-help-float px-2 py-1" | ||
bold | ||
> | ||
{discountPlan.extraLabel} | ||
</Typography> | ||
<PlusPlanExtraLabel | ||
color={PlusLabelColor.Help} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess ideally it would come from paddle but it's ok for now. |
||
label={discountPlan.extraLabel} | ||
className="ml-3" | ||
typographyProps={{ color: TypographyColor.StatusHelp }} | ||
/> | ||
)} | ||
</div> | ||
<div className="flex items-baseline gap-0.5"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { ReactElement } from 'react'; | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import type { AllowedTags, TypographyProps } from '../typography/Typography'; | ||
import { | ||
Typography, | ||
TypographyTag, | ||
TypographyType, | ||
} from '../typography/Typography'; | ||
|
||
interface PlusPlanExtraLabelProps { | ||
label: string; | ||
color: PlusLabelColor; | ||
className?: string; | ||
typographyProps?: Omit<TypographyProps<AllowedTags>, 'className'>; | ||
} | ||
|
||
export enum PlusLabelColor { | ||
Success = 'bg-action-upvote-float', | ||
Help = 'bg-action-help-float', | ||
} | ||
|
||
export function PlusPlanExtraLabel({ | ||
label, | ||
color, | ||
className, | ||
typographyProps = {}, | ||
}: PlusPlanExtraLabelProps): ReactElement { | ||
return ( | ||
<Typography | ||
tag={TypographyTag.Span} | ||
type={TypographyType.Caption1} | ||
{...typographyProps} | ||
className={classNames('rounded-10 px-2 py-1', color, className)} | ||
bold | ||
> | ||
{label} | ||
</Typography> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use
KeyboardCommand
andArrowKey
enums wdyt?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right. Forgot they existed. Let me update it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sshanzel ping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally forgot about this 🤦
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ilasw should be available now 🚀