Skip to content

Commit

Permalink
fix: Navbar DownloadButton behavior and track event (#230)
Browse files Browse the repository at this point in the history
* fix: Navbar DownloadButton behavior and track event

* feat: update workflow  pull-request to use ubuntu-latest

* feat: add the option to hide download button
  • Loading branch information
braianj authored Feb 13, 2025
1 parent e863dbd commit 82a828c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

jobs:
release:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion src/components/DownloadButton/DownloadButton.styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const DownloadButtonStyled = styled(Button)((props) => {
const { theme } = props
return {
"&.MuiButton-sizeMedium.MuiButton-containedPrimary": {
height: "48px",
height: "46px",
fontSize: "14px",
fontWeight: 600,
borderRadius: "6px",
Expand Down
53 changes: 26 additions & 27 deletions src/components/UserMenu/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const UserMenu = React.memo((props: UserMenuProps) => {
isDisconnecting,
manaBalances,
i18n = i18nUserMenu,
hideDownloadButton,
onClickSignIn,
onClickBalance,
onClickOpen,
Expand Down Expand Up @@ -119,9 +120,7 @@ const UserMenu = React.memo((props: UserMenuProps) => {

setTimeout(
() => {
onClickDownload
? onClickDownload(event)
: window.open(config.get("DOWNLOAD_URL"), "_blank", "noopener")
window.open(config.get("DOWNLOAD_URL"), "_blank", "noopener")
},
onClickUserMenuItem ? 300 : 0
)
Expand All @@ -135,20 +134,11 @@ const UserMenu = React.memo((props: UserMenuProps) => {

if (!userAgentData || !defaultDownloadOption) return

onClickUserMenuItem &&
onClickUserMenuItem(event, {
type: UserMenuEventId.DOWNLOAD,
track_uuid: trackingId || undefined,
url: config.get("DOWNLOAD_URL"),
onClickDownload &&
onClickDownload(event, {
href: config.get("DOWNLOAD_URL"),
})

setTimeout(
() => {
triggerFileDownload(defaultDownloadOption.link)
},
onClickUserMenuItem ? 300 : 0
)

const redirectUrl = updateUrlWithLastValue(
config.get("DOWNLOAD_SUCCESS_URL"),
"os",
Expand All @@ -158,9 +148,15 @@ const UserMenu = React.memo((props: UserMenuProps) => {
const finalUrl = addQueryParamsToUrlString(redirectUrl, {
arch: userAgentData.cpu.architecture,
})
setTimeout(() => {
window.location.href = finalUrl
}, 3000)

setTimeout(
() => {
triggerFileDownload(defaultDownloadOption.link).then(() => {
window.location.href = finalUrl
})
},
onClickDownload ? 300 : 0
)
},
[defaultDownloadOption, userAgentData]
)
Expand Down Expand Up @@ -242,7 +238,8 @@ const UserMenu = React.memo((props: UserMenuProps) => {
{(isLoadingUserAgentData || !defaultDownloadOption) &&
userAgentData &&
!userAgentData.mobile &&
!userAgentData.tablet && (
!userAgentData.tablet &&
!hideDownloadButton && (
<DownloadButton
href={config.get("DOWNLOAD_URL")}
onClick={handleClickDownload}
Expand All @@ -251,14 +248,16 @@ const UserMenu = React.memo((props: UserMenuProps) => {
/>
)}

{!isLoadingUserAgentData && defaultDownloadOption && (
<DownloadButton
href={defaultDownloadOption.link!}
onClick={onClickDownloadOsHandler}
endIcon={defaultDownloadOption.icon}
label={i18n.download}
/>
)}
{!isLoadingUserAgentData &&
defaultDownloadOption &&
!hideDownloadButton && (
<DownloadButton
href={defaultDownloadOption.link!}
onClick={onClickDownloadOsHandler}
endIcon={defaultDownloadOption.icon}
label={i18n.download}
/>
)}
</>
)}
</UserMenuContainer>
Expand Down
6 changes: 5 additions & 1 deletion src/components/UserMenu/UserMenu.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ type UserMenuProps = Omit<
isDisconnecting?: boolean
isActivity?: boolean
i18n?: UserMenuI18N
hideDownloadButton?: boolean
onClickSignIn?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void
onClickOpen?: (
event: React.MouseEvent<HTMLElement, MouseEvent>,
trackingId: string
) => void
onClickDownload?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void
onClickDownload?: (
event: React.MouseEvent<HTMLElement, MouseEvent>,
options: { href: string }
) => void
}

enum UserMenuEventId {
Expand Down
17 changes: 10 additions & 7 deletions src/modules/file.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const triggerFileDownload = (link: string): void => {
const a = document.createElement("a")
a.href = link
a.setAttribute("download", "")
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
const triggerFileDownload = (link: string): Promise<void> => {
return new Promise((resolve) => {
const a = document.createElement("a")
a.href = link
a.setAttribute("download", "")
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
setTimeout(resolve, 3000)
})
}

export { triggerFileDownload }

0 comments on commit 82a828c

Please sign in to comment.