-
Notifications
You must be signed in to change notification settings - Fork 65
feat: request Ads on page load #711
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3b6a101
feat: sponsored products request on PLP
rodorgas 10d9ede
refactor: timeout to wait ads before rendering
rodorgas f361622
fix: remove debugging text
rodorgas 0e56842
fix: remove debugging text
rodorgas 61b6753
add changelog
MatheusLealv aa62fa0
rmv unnecessary operator
MatheusLealv 0dceb26
update ads-js version from beta to stable
MatheusLealv 6d5848f
readd removeTreePath utility for cleaner props
MatheusLealv 571cb54
fix lint
MatheusLealv aa8e8ef
Merge branch 'master' into feat/use-ads-sdk
MatheusLealv 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,106 @@ | ||
| import { useEffect, useMemo, useState } from 'react' | ||
| import type { UseAdsReturn } from '@vtex/ads-react' | ||
|
|
||
| interface UseMergeResultsProps { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| props: any | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| sponsoredSearchResult: UseAdsReturn<any> | ||
| } | ||
|
|
||
| const useMergeResults = ({ | ||
| props, | ||
| sponsoredSearchResult, | ||
| }: UseMergeResultsProps) => { | ||
| const [holdOrganic, setHoldOrganic] = useState(false) | ||
|
|
||
| const isFirstPage = Boolean( | ||
| (props as unknown as { from?: number })?.from === 0 || | ||
| (props as unknown as { page?: number })?.page === 1 | ||
| ) | ||
|
|
||
| const isAdsLoading = sponsoredSearchResult?.isLoading ?? false | ||
|
|
||
| useEffect(() => { | ||
| if (!isFirstPage) { | ||
| setHoldOrganic(false) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if (!isAdsLoading) { | ||
| setHoldOrganic(false) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| setHoldOrganic(true) | ||
|
|
||
| const ADS_WAIT_MS = 2000 | ||
| const timeoutId = setTimeout(() => { | ||
| setHoldOrganic(false) | ||
| }, ADS_WAIT_MS) | ||
|
|
||
| return () => { | ||
| clearTimeout(timeoutId) | ||
| } | ||
| }, [isAdsLoading, isFirstPage]) | ||
|
|
||
| const mergedProps = useMemo(() => { | ||
| const newProps = { ...props } | ||
|
|
||
| // While ads are loading (and within wait window), hide organic products to avoid flicker | ||
| if (isFirstPage && holdOrganic && newProps.searchQuery) { | ||
| newProps.searchQuery = { | ||
| ...newProps.searchQuery, | ||
| loading: true, | ||
| data: { | ||
| ...newProps.searchQuery.data, | ||
| products: [], | ||
| productSearch: { | ||
| ...newProps.searchQuery.data.productSearch, | ||
| products: [], | ||
| }, | ||
| }, | ||
| products: [], | ||
| } | ||
| } else if ( | ||
| isFirstPage && | ||
| sponsoredSearchResult?.ads?.length && | ||
| newProps.searchQuery | ||
| ) { | ||
| const sponsoredProducts = sponsoredSearchResult.ads.map( | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| (ad: any) => ({ | ||
| ...(ad.product ?? ad), | ||
| advertisement: ad.advertisement, | ||
| cacheId: `ad-${ad.product?.cacheId ?? ''}`, | ||
| }) | ||
| ) | ||
|
|
||
| const mergedProducts = [ | ||
| ...sponsoredProducts, | ||
| ...(newProps.searchQuery.products || []), | ||
| ] | ||
|
|
||
| newProps.searchQuery = { | ||
| ...newProps.searchQuery, | ||
| data: { | ||
| ...newProps.searchQuery.data, | ||
| products: mergedProducts, | ||
| productSearch: { | ||
| ...newProps.searchQuery.data.productSearch, | ||
| products: mergedProducts, | ||
| }, | ||
| }, | ||
| products: mergedProducts, | ||
| } | ||
| } | ||
|
|
||
| return newProps | ||
| }, [props, sponsoredSearchResult, holdOrganic, isFirstPage]) | ||
|
|
||
| return mergedProps | ||
| } | ||
|
|
||
| export default useMergeResults | ||
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 |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| interface Window extends Window { | ||
| __hostname__: string | undefined | ||
| } | ||
|
|
||
| declare module '../utils/removeTreePath' { | ||
| export function removeTreePath<T = unknown>(props: T): T | ||
| } |
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,46 @@ | ||
| // Use types from @vtex/ads-react where available | ||
| import type { useAds } from '@vtex/ads-react' | ||
|
|
||
| type SponsoredResult = ReturnType<typeof useAds> | ||
|
|
||
| export const mergeWithSponsoredProducts = ( | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| productSearchResult: any, | ||
| sponsoredSearchResult: SponsoredResult, | ||
| from?: number | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| ): unknown => { | ||
| const isFirstPage = from === 0 | ||
|
|
||
| if (!isFirstPage) return productSearchResult | ||
|
|
||
| const originalProducts = | ||
| productSearchResult?.data?.productSearch?.products ?? [] | ||
|
|
||
| const sponsoredProducts = sponsoredSearchResult.ads | ||
| .map(p => p.product ?? p) | ||
| .filter(Boolean) as any[] | ||
|
|
||
| if (!sponsoredProducts?.length) return productSearchResult | ||
|
|
||
| const sponsoredIds = new Set( | ||
| sponsoredProducts.map(p => p.id).filter(Boolean) as string[] | ||
| ) | ||
|
|
||
| const filteredOriginalProducts = originalProducts.filter( | ||
| (p: { id: string }) => !sponsoredIds.has(p.id) | ||
| ) | ||
|
|
||
| const combinedProducts = [...sponsoredProducts, ...filteredOriginalProducts] | ||
|
|
||
| return { | ||
| ...productSearchResult, | ||
| data: { | ||
| ...(productSearchResult?.data ?? {}), | ||
| productSearch: { | ||
| ...(productSearchResult?.data?.productSearch ?? {}), | ||
| products: combinedProducts, | ||
| }, | ||
| }, | ||
| } | ||
| } |
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 @@ | ||
| export function removeTreePath<T = unknown>(props: T): T |
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.
Uh oh!
There was an error while loading. Please reload this page.