Skip to content

Commit 46efdf4

Browse files
committed
chore(ui): improve installation support
1 parent 7437f45 commit 46efdf4

File tree

4 files changed

+40
-70
lines changed

4 files changed

+40
-70
lines changed

ui/src/contexts/DeviceContext.tsx

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
import React, { createContext, FC, PropsWithChildren, useContext, useState } from 'react'
2-
import { useAddToHomescreenPrompt } from '../hooks'
32
import { isDisplayMode } from '../helpers'
43

4+
interface IBeforeInstallPromptEvent extends Event {
5+
readonly platforms: string[]
6+
readonly userChoice: Promise<{
7+
outcome: 'accepted' | 'dismissed'
8+
platform: string
9+
}>
10+
prompt(): Promise<void>
11+
}
12+
513
interface DeviceContextType {
614
isInstalled: boolean
7-
isInstallable: boolean
8-
promptToInstall: () => void
15+
beforeInstallPromptEvent: IBeforeInstallPromptEvent | null
916
}
1017

1118
const DeviceContext = createContext<DeviceContextType>({
1219
isInstalled: false,
13-
isInstallable: false,
14-
promptToInstall: () => {return},
20+
beforeInstallPromptEvent: null,
1521
})
1622

1723
const DeviceProvider: FC<PropsWithChildren> = ({ children }) => {
18-
const [prompt, promptToInstall] = useAddToHomescreenPrompt()
19-
const [isInstalled] = useState(isDisplayMode('standalone'))
20-
const [isInstallable, setInstallableState] = useState(false)
21-
22-
React.useEffect(
23-
() => {
24-
if (prompt) {
25-
setInstallableState(true)
26-
}
27-
},
28-
[prompt]
29-
)
30-
31-
return <DeviceContext.Provider value={{ isInstalled, isInstallable, promptToInstall }}>{children}</DeviceContext.Provider>
24+
const [beforeInstallPromptEvent, setBeforeInstallPromptEventState] = useState<IBeforeInstallPromptEvent | null>(null)
25+
const [isInstalled, setInstalledState] = useState(isDisplayMode('standalone'))
26+
27+
React.useEffect(() => {
28+
const ready = (e: IBeforeInstallPromptEvent) => {
29+
e.preventDefault()
30+
setBeforeInstallPromptEventState(e)
31+
}
32+
const installed = () => setInstalledState(true)
33+
window.addEventListener('beforeinstallprompt', ready as any)
34+
window.addEventListener('appinstalled', installed)
35+
return () => {
36+
window.removeEventListener('beforeinstallprompt', ready as any)
37+
window.removeEventListener('appinstalled', installed)
38+
}
39+
}, [])
40+
41+
return <DeviceContext.Provider value={{ isInstalled, beforeInstallPromptEvent }}>{children}</DeviceContext.Provider>
3242
}
3343

3444
export { DeviceProvider }

ui/src/hooks/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './useAddToHomeScreenPrompt'
21
export * from './useApi'
32
export * from './useConfirmModal'
43
export * from './useDeviceSubcriptionStatus'

ui/src/hooks/useAddToHomeScreenPrompt.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

ui/src/settings/preferences/InstallationBox.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,27 @@ const Installed = () => (
3030

3131
const Uninstallable = () => (
3232
<p>
33-
Oh! readflow can&apos;t be installed on this device.
33+
Sadness! readflow can&apos;t be installed on this device from here.
3434
<br />
35-
Maybe your device device configuration does not allow it. Sad.
35+
Maybe your device&apos;s browser allows you to do this, but only by using the built-in application menu.
3636
</p>
3737
)
3838

3939
const InstallationBox = () => {
40-
const {isInstalled, isInstallable, promptToInstall} = useDevice()
40+
const { isInstalled, beforeInstallPromptEvent } = useDevice()
41+
42+
const install = React.useCallback(() => {
43+
if (beforeInstallPromptEvent) {
44+
beforeInstallPromptEvent.prompt()
45+
}
46+
}, [beforeInstallPromptEvent])
4147

4248
return (
4349
<Box title="Installation">
4450
{(() => {
4551
switch (true) {
46-
case !isInstalled && isInstallable:
47-
return <Install onClick={promptToInstall} />
52+
case !isInstalled && beforeInstallPromptEvent != null:
53+
return <Install onClick={install} />
4854
case isInstalled:
4955
return <Installed />
5056
default:

0 commit comments

Comments
 (0)