Skip to content

Commit

Permalink
fix(ui): improve PWA install support
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Sep 16, 2024
1 parent 83869ba commit 7437f45
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
33 changes: 18 additions & 15 deletions ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Store } from 'redux'

import {
CurrentUserProvider,
DeviceProvider,
GraphQLProvider,
LocalConfigurationProvider,
MessageProvider,
Expand Down Expand Up @@ -37,21 +38,23 @@ export default function App({ store, history /*, theme*/ }: Props) {
<ConnectedRouter history={history}>
<AuthenticationProvider>
<GraphQLProvider>
<LocalConfigurationProvider>
<MessageProvider>
<ModalProvider>
<NavbarProvider>
<ScrollMemoryProvider>
<CurrentUserProvider>
<AppLayout>
<Routes />
</AppLayout>
</CurrentUserProvider>
</ScrollMemoryProvider>
</NavbarProvider>
</ModalProvider>
</MessageProvider>
</LocalConfigurationProvider>
<DeviceProvider>
<LocalConfigurationProvider>
<MessageProvider>
<ModalProvider>
<NavbarProvider>
<ScrollMemoryProvider>
<CurrentUserProvider>
<AppLayout>
<Routes />
</AppLayout>
</CurrentUserProvider>
</ScrollMemoryProvider>
</NavbarProvider>
</ModalProvider>
</MessageProvider>
</LocalConfigurationProvider>
</DeviceProvider>
</GraphQLProvider>
</AuthenticationProvider>
</ConnectedRouter>
Expand Down
36 changes: 36 additions & 0 deletions ui/src/contexts/DeviceContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { createContext, FC, PropsWithChildren, useContext, useState } from 'react'
import { useAddToHomescreenPrompt } from '../hooks'
import { isDisplayMode } from '../helpers'

interface DeviceContextType {
isInstalled: boolean
isInstallable: boolean
promptToInstall: () => void
}

const DeviceContext = createContext<DeviceContextType>({
isInstalled: false,
isInstallable: false,
promptToInstall: () => {return},
})

const DeviceProvider: FC<PropsWithChildren> = ({ children }) => {
const [prompt, promptToInstall] = useAddToHomescreenPrompt()
const [isInstalled] = useState(isDisplayMode('standalone'))
const [isInstallable, setInstallableState] = useState(false)

React.useEffect(
() => {
if (prompt) {
setInstallableState(true)
}
},
[prompt]
)

return <DeviceContext.Provider value={{ isInstalled, isInstallable, promptToInstall }}>{children}</DeviceContext.Provider>
}

export { DeviceProvider }

export const useDevice = () => useContext(DeviceContext)
1 change: 1 addition & 0 deletions ui/src/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './CurrentUserContext'
export * from './DeviceContext'
export * from './GraphQLContext'
export * from './LocalConfigurationContext'
export * from './MessageContext'
Expand Down
4 changes: 2 additions & 2 deletions ui/src/hooks/useAddToHomeScreenPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useAddToHomescreenPrompt(): [
null
)

const promptToInstall = () => {
const promptToInstall = React.useCallback(() => {
if (prompt) {
return prompt.prompt()
}
Expand All @@ -26,7 +26,7 @@ export function useAddToHomescreenPrompt(): [
'Tried installing before browser sent "beforeinstallprompt" event'
)
)
}
}, [prompt])

React.useEffect(() => {
const ready = (e: IBeforeInstallPromptEvent) => {
Expand Down
18 changes: 3 additions & 15 deletions ui/src/settings/preferences/InstallationBox.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { useState } from 'react'
import React from 'react'

import { Box, Button } from '../../components'
import { isDisplayMode } from '../../helpers'
import { useAddToHomescreenPrompt } from '../../hooks'
import { useDevice } from '../../contexts'

interface InstallProps {
onClick?: (e: any) => void
Expand Down Expand Up @@ -38,18 +37,7 @@ const Uninstallable = () => (
)

const InstallationBox = () => {
const [prompt, promptToInstall] = useAddToHomescreenPrompt()
const [isInstalled] = useState(isDisplayMode('standalone'))
const [isInstallable, setInstallableState] = useState(false)

React.useEffect(
() => {
if (prompt) {
setInstallableState(true)
}
},
[prompt]
)
const {isInstalled, isInstallable, promptToInstall} = useDevice()

return (
<Box title="Installation">
Expand Down

0 comments on commit 7437f45

Please sign in to comment.