-
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
Merged
+343
−25
Merged
feat: gift user modal #4091
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
4637cf2
feat: gift user modal
sshanzel b6701f1
Merge branch 'main' into MI-751
sshanzel 707572f
feat: search user with mention
sshanzel 3f53473
tmp: show modal
sshanzel 7797626
fix: on change handler
sshanzel c9c4e2d
fix: debounce
sshanzel 6649903
fix: keyboard navigation
sshanzel 4c43d6c
fix: arrow and on focus
sshanzel f1c525b
feat: on hover, set selected
sshanzel 84091c1
fix: ref
sshanzel bbc0c94
fix: index
sshanzel 92d8ee2
fix: overlay append
sshanzel f577767
fix: interactivity
sshanzel 663a58c
fix: on close
sshanzel f6c79ff
fix: selected ui
sshanzel 84ab16b
fix: gap
sshanzel 3924acb
fix: alignment
sshanzel a3c0455
fix: roundness
sshanzel 7a3fd26
fix: TODO
sshanzel e24a2fb
fix: checking disabled
sshanzel 720a6f1
Revert "tmp: show modal"
sshanzel 3726a1b
fix: missing context provider
sshanzel 72a8eab
Merge branch 'main' into MI-751
sshanzel dee522c
fix: modal pricing
sshanzel f25e989
fix: missing preselected user
sshanzel 5b435c1
Merge branch 'main' into MI-751
sshanzel 058f1de
Merge branch 'main' into MI-751
sshanzel 4318c3b
fix: extra label
sshanzel e055a80
refactor: variable name
sshanzel 066ad9f
fix: loop condition
sshanzel d9e0f03
refactor: usage of enums
sshanzel eaa4ae2
chore: cleanup
sshanzel 837a213
Merge branch 'main' into MI-751
sshanzel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
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'; | ||
import { ArrowKey, KeyboardCommand } from '../../lib/element'; | ||
|
||
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 = [ArrowKey.Up, ArrowKey.Down, KeyboardCommand.Enter]; | ||
if (!movement.includes(e.key as (typeof movement)[number])) { | ||
return; | ||
} | ||
|
||
e.preventDefault(); | ||
|
||
if (e.key === ArrowKey.Down) { | ||
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 === ArrowKey.Up) { | ||
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 | ||
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} /> | ||
</PaymentContextProvider> | ||
); | ||
} | ||
|
||
export default GiftPlusModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
|
40 changes: 40 additions & 0 deletions
40
packages/shared/src/components/plus/PlusPlanExtraLabel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is this prop needed?
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.
Ah, should be not since it is always going to be true. Let me clean it up.