-
Notifications
You must be signed in to change notification settings - Fork 1
chore: fire pixel on app clicks #78
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
Open
thesmithdao
wants to merge
2
commits into
shapeshift:develop
Choose a base branch
from
thesmithdao:addrsbl-pixel-v2
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some comments aren't visible on the classic Files Changed page.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,162 @@ | ||
'use client'; | ||
'use client' | ||
|
||
import Link from 'next/link'; | ||
import {usePathname} from 'next/navigation'; | ||
import {forwardRef} from 'react'; | ||
import Link from 'next/link' | ||
import {usePathname} from 'next/navigation' | ||
import {forwardRef} from 'react' | ||
|
||
import {DEFAULT_LANGUAGE, getLanguageFromPath} from '@/app/[lang]/_utils/i18nconfig'; | ||
import {DEFAULT_LANGUAGE, getLanguageFromPath} from '@/app/[lang]/_utils/i18nconfig' | ||
|
||
import type {LinkProps} from 'next/link'; | ||
import type {AnchorHTMLAttributes, ReactNode} from 'react'; | ||
import type {UrlObject} from 'url'; | ||
import type {LinkProps} from 'next/link' | ||
import type {AnchorHTMLAttributes, ReactNode} from 'react' | ||
import type {UrlObject} from 'url' | ||
|
||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
declare const __adrsbl: {run: (event: string, conversion: boolean) => void} | undefined | ||
|
||
type TLocalizedLinkProps = LinkProps & | ||
AnchorHTMLAttributes<HTMLAnchorElement> & { | ||
children: ReactNode; | ||
}; | ||
children: ReactNode | ||
} | ||
|
||
/** | ||
* A localized version of Next.js Link that automatically prepends the current language | ||
* to internal links when 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. This line is still removed? |
||
*/ | ||
export const LocalizedLink = forwardRef<HTMLAnchorElement, TLocalizedLinkProps>(({href, children, ...props}, ref) => { | ||
const pathname = usePathname(); | ||
const currentLanguage = getLanguageFromPath(pathname) || DEFAULT_LANGUAGE; | ||
|
||
// Convert href to string for processing | ||
const hrefString = | ||
typeof href === 'string' | ||
? href | ||
: typeof href === 'object' && | ||
href !== null && | ||
'pathname' in href && | ||
typeof (href as UrlObject).pathname === 'string' | ||
? (href as UrlObject).pathname | ||
: ''; | ||
|
||
// Don't modify external links, anchors, or already localized paths | ||
if ( | ||
hrefString?.startsWith('http') || | ||
hrefString?.startsWith('#') || | ||
hrefString?.startsWith('mailto:') || | ||
hrefString?.startsWith('tel:') || | ||
!hrefString?.startsWith('/') | ||
) { | ||
export const LocalizedLink = forwardRef<HTMLAnchorElement, TLocalizedLinkProps>( | ||
({href, children, onClick, ...props}, ref) => { | ||
const pathname = usePathname() | ||
const currentLanguage = getLanguageFromPath(pathname) || DEFAULT_LANGUAGE | ||
|
||
// Preserve UrlObject hrefs (pathname + query + hash) when provided | ||
const hrefObj = typeof href === 'object' && href !== null ? (href as UrlObject) : undefined | ||
|
||
// Convert href to string for pathname-based processing when needed | ||
const hrefString = typeof href === 'string' ? href : (hrefObj?.pathname ?? '') | ||
|
||
// Helper to build a full absolute URL string from a UrlObject when it has host/protocol | ||
const fullUrlFromObj = (obj: UrlObject) => { | ||
if (!obj) { | ||
return '' | ||
} | ||
const protocol = (obj.protocol as string) ?? '' | ||
const host = (obj.host as string) ?? (obj.hostname as string) ?? '' | ||
const pathname = (obj.pathname as string) ?? '' | ||
const hash = (obj.hash as string) ?? '' | ||
let query = '' | ||
// Support both string-form and object-form queries. | ||
if (obj.query) { | ||
if (typeof obj.query === 'string') { | ||
// Use provided string query, ensure it starts with '?' | ||
const raw = obj.query as string | ||
query = raw.startsWith('?') ? raw : `?${raw}` | ||
} else if (typeof obj.query === 'object') { | ||
// obj.query can be Record<string, string | string[]>. | ||
const params = new URLSearchParams() | ||
for (const [k, v] of Object.entries(obj.query as Record<string, string | string[]>)) { | ||
if (Array.isArray(v)) { | ||
for (const item of v) { | ||
params.append(k, String(item)) | ||
} | ||
} else if (v !== undefined && v !== null) { | ||
params.append(k, String(v)) | ||
} | ||
} | ||
const qs = params.toString() | ||
query = qs ? `?${qs}` : '' | ||
} | ||
} | ||
if (host) { | ||
return `${protocol || 'https:'}//${host}${pathname}${query}${hash}` | ||
} | ||
return `${pathname}${query}${hash}` | ||
} | ||
|
||
// External app.shapeshift.com link detection (works for string hrefs and UrlObject with host) | ||
const isAppLink = (() => { | ||
if (typeof href === 'string') { | ||
return /^https?:\/\/app\.shapeshift\.com(\/|$)/i.test(href) | ||
} | ||
if (hrefObj) { | ||
const full = fullUrlFromObj(hrefObj) | ||
return /^https?:\/\/app\.shapeshift\.com(\/|$)/i.test(full) | ||
} | ||
return false | ||
})() | ||
|
||
// Compose click handler for external app links | ||
const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => { | ||
if (isAppLink) { | ||
try { | ||
if (__adrsbl?.run) { | ||
__adrsbl.run('app_click', true) | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
e.preventDefault() | ||
const absolute = hrefObj | ||
? fullUrlFromObj(hrefObj) | ||
: hrefString || (typeof href === 'string' ? href : '') | ||
try { | ||
window.open(absolute, '_blank', 'noopener,noreferrer') | ||
} catch { | ||
if (absolute) { | ||
window.location.assign(absolute) | ||
} | ||
} | ||
} | ||
if (onClick) { | ||
onClick(e) | ||
} | ||
} | ||
|
||
// Don't modify external links, anchors, or already localized paths | ||
const isExternalObject = !!hrefObj && !!(hrefObj.host || hrefObj.hostname || hrefObj.protocol) | ||
if ( | ||
typeof href === 'string' | ||
? hrefString.startsWith('http') || | ||
hrefString.startsWith('#') || | ||
hrefString.startsWith('mailto:') || | ||
hrefString.startsWith('tel:') || | ||
!hrefString.startsWith('/') | ||
: // href is an object | ||
isExternalObject | ||
) { | ||
return ( | ||
<Link | ||
href={href} | ||
ref={ref} | ||
{...props} | ||
onClick={isAppLink ? handleClick : onClick}> | ||
{children} | ||
</Link> | ||
) | ||
} | ||
|
||
// Check if the href already has a language prefix | ||
const hasLanguagePrefix = hrefString.match(/^\/([a-z]{2})(\/|$)/) | ||
|
||
// Build the localized href. Preserve query/hash by returning a UrlObject when the original | ||
// href was a UrlObject, otherwise return a string. | ||
let localizedPathname = hrefString | ||
if (!hasLanguagePrefix && currentLanguage !== DEFAULT_LANGUAGE) { | ||
localizedPathname = `/${currentLanguage}${hrefString}` | ||
} | ||
|
||
const localizedHref = hrefObj | ||
? // copy the original UrlObject but replace/normalize pathname | ||
({...hrefObj, pathname: localizedPathname} as UrlObject) | ||
: localizedPathname | ||
|
||
return ( | ||
<Link | ||
href={href} | ||
href={localizedHref} | ||
ref={ref} | ||
{...props}> | ||
{...props} | ||
onClick={onClick}> | ||
{children} | ||
</Link> | ||
); | ||
} | ||
|
||
// Check if the href already has a language prefix | ||
const hasLanguagePrefix = hrefString.match(/^\/[a-z]{2}(\/|$)/); | ||
|
||
// Build the localized href | ||
let localizedHref = hrefString; | ||
if (!hasLanguagePrefix && currentLanguage !== DEFAULT_LANGUAGE) { | ||
localizedHref = `/${currentLanguage}${hrefString}`; | ||
) | ||
} | ||
) | ||
|
||
return ( | ||
<Link | ||
href={localizedHref} | ||
ref={ref} | ||
{...props}> | ||
{children} | ||
</Link> | ||
); | ||
}); | ||
|
||
LocalizedLink.displayName = 'LocalizedLink'; | ||
LocalizedLink.displayName = 'LocalizedLink' |
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
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.