-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: Add DismissibleUpsell component for dismissible messages #7842
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,160 @@ | ||
| import { memo, ReactNode, useEffect, useState, useRef } from "react" | ||
| import styled from "styled-components" | ||
|
|
||
| import { vscode } from "@src/utils/vscode" | ||
| import { useAppTranslation } from "@src/i18n/TranslationContext" | ||
|
|
||
| interface DismissibleUpsellProps { | ||
| /** Required unique identifier for this upsell */ | ||
| id: string | ||
| /** Optional CSS class name for styling */ | ||
| className?: string | ||
| /** Content to display inside the upsell */ | ||
| children: ReactNode | ||
| /** Visual variant of the upsell */ | ||
| variant?: "banner" | "default" | ||
| /** Optional callback when upsell is dismissed */ | ||
| onDismiss?: () => void | ||
| } | ||
|
|
||
| const UpsellContainer = styled.div<{ $variant: "banner" | "default" }>` | ||
| position: relative; | ||
| padding: 12px 40px 12px 16px; | ||
| border-radius: 6px; | ||
| margin-bottom: 8px; | ||
| display: flex; | ||
| align-items: center; | ||
|
|
||
| ${(props) => | ||
| props.$variant === "banner" | ||
| ? ` | ||
| background-color: var(--vscode-button-background); | ||
| color: var(--vscode-button-foreground); | ||
| ` | ||
| : ` | ||
| background-color: var(--vscode-notifications-background); | ||
| color: var(--vscode-notifications-foreground); | ||
| border: 1px solid var(--vscode-notifications-border); | ||
| `} | ||
| ` | ||
|
|
||
| const DismissButton = styled.button<{ $variant: "banner" | "default" }>` | ||
| position: absolute; | ||
| top: 50%; | ||
| right: 12px; | ||
| transform: translateY(-50%); | ||
| background: none; | ||
| border: none; | ||
| cursor: pointer; | ||
| padding: 4px; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| border-radius: 4px; | ||
| transition: background-color 0.2s; | ||
|
|
||
| ${(props) => | ||
| props.$variant === "banner" | ||
| ? ` | ||
| color: var(--vscode-button-foreground); | ||
|
|
||
| &:hover { | ||
| background-color: rgba(255, 255, 255, 0.1); | ||
| } | ||
| ` | ||
| : ` | ||
| color: var(--vscode-notifications-foreground); | ||
|
|
||
| &:hover { | ||
| background-color: var(--vscode-toolbar-hoverBackground); | ||
| } | ||
| `} | ||
|
|
||
| &:focus { | ||
| outline: 1px solid var(--vscode-focusBorder); | ||
| outline-offset: 1px; | ||
| } | ||
| ` | ||
|
|
||
| const DismissIcon = () => ( | ||
| <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"> | ||
| <path | ||
| fillRule="evenodd" | ||
| clipRule="evenodd" | ||
| d="M8 8.707l3.646 3.647.708-.707L8.707 8l3.647-3.646-.707-.708L8 7.293 4.354 3.646l-.707.708L7.293 8l-3.647 3.646.708.707L8 8.707z" | ||
| fill="currentColor" | ||
| /> | ||
| </svg> | ||
| ) | ||
|
|
||
| const DismissibleUpsell = memo(({ id, className, children, variant = "banner", onDismiss }: DismissibleUpsellProps) => { | ||
| const { t } = useAppTranslation() | ||
| const [isVisible, setIsVisible] = useState(true) | ||
| const isMountedRef = useRef(true) | ||
|
|
||
| useEffect(() => { | ||
| // Track mounted state | ||
| isMountedRef.current = true | ||
|
|
||
| // Request the current list of dismissed upsells from the extension | ||
| vscode.postMessage({ type: "getDismissedUpsells" }) | ||
|
|
||
| // Listen for the response | ||
| const handleMessage = (event: MessageEvent) => { | ||
| // Only update state if component is still mounted | ||
| if (!isMountedRef.current) return | ||
|
|
||
| const message = event.data | ||
| // Add null/undefined check for message | ||
| if (message && message.type === "dismissedUpsells" && Array.isArray(message.list)) { | ||
| // Check if this upsell has been dismissed | ||
| if (message.list.includes(id)) { | ||
| setIsVisible(false) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| window.addEventListener("message", handleMessage) | ||
| return () => { | ||
| isMountedRef.current = false | ||
| window.removeEventListener("message", handleMessage) | ||
| } | ||
| }, [id]) | ||
|
|
||
| const handleDismiss = async () => { | ||
| // First notify the extension to persist the dismissal | ||
| // This ensures the message is sent even if the component unmounts quickly | ||
| vscode.postMessage({ | ||
| type: "dismissUpsell", | ||
| upsellId: id, | ||
| }) | ||
|
|
||
| // Then hide the upsell | ||
| setIsVisible(false) | ||
|
|
||
| // Call the optional callback | ||
| onDismiss?.() | ||
| } | ||
|
Contributor
Author
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. Race condition: If the component unmounts immediately after clicking dismiss but before the message is sent, the dismissal won't be persisted. Consider ensuring the message is sent before hiding the component. |
||
|
|
||
| // Don't render if not visible | ||
| if (!isVisible) { | ||
| return null | ||
| } | ||
|
|
||
| return ( | ||
| <UpsellContainer $variant={variant} className={className}> | ||
| {children} | ||
| <DismissButton | ||
| $variant={variant} | ||
| onClick={handleDismiss} | ||
| aria-label={t("common:dismiss")} | ||
| title={t("common:dismissAndDontShowAgain")}> | ||
| <DismissIcon /> | ||
| </DismissButton> | ||
| </UpsellContainer> | ||
| ) | ||
| }) | ||
|
|
||
| DismissibleUpsell.displayName = "DismissibleUpsell" | ||
|
|
||
| export default DismissibleUpsell | ||
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.
Missing error handling. What happens if updateGlobalState fails? Consider wrapping in try-catch and logging any errors.
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.
It's OK to fail silently in this case