-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pwa install on desktop (#3889)
* feat: init pwa install on desktop * fix: enum value * refactor: add implementation in hook * feat: add browser icons * feat: add browser event in enum * feat: add extension step in onboarding * feat: add extension image * feat: laptop check * feat: add skip button * feat: add skip button and experiment check * fix: revert default step * fix: add explicit type in return * refactor: naming variable * fix: enable button * feat: check as completed extension step if installed * refactor: force not load step if not right browser * fix: removed typo * fix: removed typo in exp value * refactor: extract feature outside, f_auto in src; * fix: add type in Typography element * fix: add label change based on browser * feat: add step on skip extension * feat: revert changes on useSquadChecklist * feat: add logic for this step * feat: add style and icon * feat: add navigation on resolve, clean logs; * feat: add double icon/image and better naming variables * feat: add event log * fix: wrong experiment name * fix: lint console.log * fix: lint consistent return * feat: add install pwa for firefox users * feat: enable default for test on preview * feat: enable default for test on preview * feat: disable default for test on preview * feat: add getCurrentBrowserName function and refactored pwa/extension step * feat: add barrel index for PWA icons and custom text for safari * fix: safari step doesn't require skipping extension * fix: dupe variable name in image * fix: lint import issues * feat: add onClickNext type to common file * refactor: rename BrowserName default to singular * feat: evaluate user only on right condition * fix: revert default value * fix: use common type on onClickNext * refactor: rename interface * feat: enable by default step * feat: help test on preview * feat: help test on preview * fix: revert debug variables/log * fix: step logic --------- Co-authored-by: Lee Hansel Solevilla <[email protected]>
- Loading branch information
Showing
21 changed files
with
333 additions
and
41 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { ReactElement } from 'react'; | ||
import React from 'react'; | ||
import type { IconProps } from '../../../Icon'; | ||
import Icon from '../../../Icon'; | ||
import OutlinedIcon from './pwa.svg'; | ||
|
||
export const PWAChromeIcon = (props: IconProps): ReactElement => ( | ||
<Icon {...props} IconPrimary={OutlinedIcon} IconSecondary={OutlinedIcon} /> | ||
); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,9 @@ | ||
import type { ReactElement } from 'react'; | ||
import React from 'react'; | ||
import type { IconProps } from '../../../Icon'; | ||
import Icon from '../../../Icon'; | ||
import OutlinedIcon from './pwa.svg'; | ||
|
||
export const PWAEdgeIcon = (props: IconProps): ReactElement => ( | ||
<Icon {...props} IconPrimary={OutlinedIcon} IconSecondary={OutlinedIcon} /> | ||
); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,9 @@ | ||
import type { ReactElement } from 'react'; | ||
import React from 'react'; | ||
import type { IconProps } from '../../../Icon'; | ||
import Icon from '../../../Icon'; | ||
import OutlinedIcon from './pwa.svg'; | ||
|
||
export const PWASafariIcon = (props: IconProps): ReactElement => ( | ||
<Icon {...props} IconPrimary={OutlinedIcon} IconSecondary={OutlinedIcon} /> | ||
); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,3 @@ | ||
export { PWAChromeIcon } from './Chrome'; | ||
export { PWAEdgeIcon } from './Edge'; | ||
export { PWASafariIcon } from './Safari'; |
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
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
98 changes: 98 additions & 0 deletions
98
packages/shared/src/components/onboarding/PWA/OnboardingInstallDesktop.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,98 @@ | ||
import React from 'react'; | ||
import type { FC, ReactElement } from 'react'; | ||
import classNames from 'classnames'; | ||
import { | ||
Typography, | ||
TypographyColor, | ||
TypographyTag, | ||
TypographyType, | ||
} from '../../typography/Typography'; | ||
import { Button, ButtonVariant } from '../../buttons/Button'; | ||
import { useInstallPWA } from './useInstallPWA'; | ||
import { | ||
cloudinaryPWADesktopChrome, | ||
cloudinaryPWADesktopEdge, | ||
cloudinaryPWADesktopSafari, | ||
} from '../../../lib/image'; | ||
import { BrowserName } from '../../../lib/func'; | ||
import { PWAChromeIcon, PWAEdgeIcon, PWASafariIcon } from '../../icons'; | ||
import type { IconProps } from '../../Icon'; | ||
import { useLogContext } from '../../../contexts/LogContext'; | ||
import { LogEvent } from '../../../lib/log'; | ||
|
||
const icons: Partial<Record<BrowserName, FC<IconProps>>> = { | ||
[BrowserName.Chrome]: PWAChromeIcon, | ||
[BrowserName.Edge]: PWAEdgeIcon, | ||
[BrowserName.Safari]: PWASafariIcon, | ||
}; | ||
const images: Partial<Record<BrowserName, string>> = { | ||
[BrowserName.Chrome]: cloudinaryPWADesktopChrome, | ||
[BrowserName.Edge]: cloudinaryPWADesktopEdge, | ||
[BrowserName.Safari]: cloudinaryPWADesktopSafari, | ||
}; | ||
|
||
export const OnboardingInstallDesktop = ({ | ||
onClickNext, | ||
}: { | ||
onClickNext: () => void; | ||
}): ReactElement => { | ||
const { logEvent } = useLogContext(); | ||
const { promptToInstall, browserName } = useInstallPWA(); | ||
const isSafari = browserName === BrowserName.Safari; | ||
const PWAIcon = icons[browserName] ?? icons[BrowserName.Chrome]; | ||
const imageSrc = images[browserName] ?? images[BrowserName.Chrome]; | ||
|
||
return ( | ||
<div className="flex flex-1 flex-col laptop:justify-between"> | ||
<div className="mb-14 flex flex-col items-center gap-6 justify-self-start text-center"> | ||
<Typography | ||
bold | ||
tag={TypographyTag.H1} | ||
type={TypographyType.LargeTitle} | ||
> | ||
More daily.dev on your desktop? | ||
<br /> | ||
Yes, please! 👀 | ||
</Typography> | ||
<Button | ||
icon={<PWAIcon aria-hidden />} | ||
onClick={async () => { | ||
logEvent({ | ||
event_name: LogEvent.InstallPWA, | ||
}); | ||
await promptToInstall?.(); | ||
onClickNext?.(); | ||
}} | ||
variant={ButtonVariant.Primary} | ||
> | ||
{isSafari ? 'Add to Dock' : 'Install on desktop'} | ||
</Button> | ||
<Typography | ||
bold | ||
color={TypographyColor.Tertiary} | ||
tag={TypographyTag.P} | ||
type={TypographyType.Subhead} | ||
> | ||
Manual: Press the | ||
<span className="mx-2 inline-grid size-7 place-content-center rounded-1/2 bg-accent-salt-bolder align-middle text-text-primary"> | ||
<PWAIcon | ||
className={classNames('inline-block', isSafari && '-mt-0.5')} | ||
aria-hidden | ||
/> | ||
</span> | ||
icon next to the browser search bar and choose{' '} | ||
{isSafari ? `"Add to Dock"` : `"Install"`} to level up! | ||
</Typography> | ||
</div> | ||
<figure className="pointer-events-none mx-auto"> | ||
<img | ||
alt="Install daily.dev on desktop" | ||
className="w-full" | ||
loading="lazy" | ||
role="presentation" | ||
src={imageSrc} | ||
/> | ||
</figure> | ||
</div> | ||
); | ||
}; |
42 changes: 42 additions & 0 deletions
42
packages/shared/src/components/onboarding/PWA/useInstallPWA.ts
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,42 @@ | ||
import type { BrowserName } from '../../../lib/func'; | ||
import { getCurrentBrowserName, isPWA } from '../../../lib/func'; | ||
|
||
interface BeforeInstallPromptEvent extends Event { | ||
prompt: () => Promise<'accepted' | 'dismissed'>; | ||
userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }>; | ||
} | ||
|
||
export interface UseInstallPWA { | ||
isAvailable: boolean; | ||
isCurrentPWA: boolean; | ||
promptToInstall: (() => Promise<null | 'accepted' | 'dismissed'>) | null; | ||
browserName: BrowserName; | ||
} | ||
|
||
let installEvent: BeforeInstallPromptEvent | null = null; | ||
globalThis?.addEventListener?.( | ||
'beforeinstallprompt', | ||
(e: BeforeInstallPromptEvent) => { | ||
e.preventDefault(); | ||
installEvent = e; | ||
}, | ||
{ once: true }, | ||
); | ||
|
||
export const useInstallPWA = (): UseInstallPWA => { | ||
const isCurrentPWA = isPWA(); | ||
const isAvailable = !!installEvent; | ||
const browserName = getCurrentBrowserName(); | ||
|
||
const promptToInstall = async () => { | ||
if (installEvent) { | ||
await installEvent.prompt?.(); | ||
const { outcome } = await installEvent.userChoice; | ||
return outcome; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
return { isCurrentPWA, isAvailable, promptToInstall, browserName }; | ||
}; |
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
Oops, something went wrong.