forked from aeharding/voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add versions and update page (aeharding#13)
* Add versions and update page * Fix annoying broken transitions bug, polish updates page * Hide pesky installation badge for various configurations
- Loading branch information
Showing
11 changed files
with
310 additions
and
14 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 |
---|---|---|
|
@@ -55,6 +55,12 @@ | |
"env": { | ||
"node": true | ||
} | ||
}, | ||
{ | ||
"files": ["src/**"], | ||
"globals": { | ||
"APP_VERSION": true | ||
} | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useContext } from "react"; | ||
import { BeforeInstallPromptContext } from "../../BeforeInstallPromptProvider"; | ||
import { | ||
isAppleDeviceInstallable, | ||
isInstalled, | ||
isTouchDevice, | ||
} from "../../helpers/device"; | ||
|
||
export default function useShouldInstall() { | ||
const { event } = useContext(BeforeInstallPromptContext); | ||
|
||
if (isInstalled()) return false; | ||
if (isAppleDeviceInstallable()) return true; | ||
if (!isTouchDevice()) return false; | ||
if (event) return true; | ||
|
||
return false; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { | ||
IonBackButton, | ||
IonBadge, | ||
IonButtons, | ||
IonContent, | ||
IonHeader, | ||
IonLabel, | ||
IonList, | ||
IonPage, | ||
IonRefresher, | ||
IonRefresherContent, | ||
IonTitle, | ||
IonToolbar, | ||
} from "@ionic/react"; | ||
import { MaxWidthContainer } from "../../features/shared/AppContent"; | ||
import { InsetIonItem, SettingLabel } from "../profile/ProfileFeedItemsPage"; | ||
import { useContext, useEffect } from "react"; | ||
import { PageContentIonSpinner } from "../shared/UserPage"; | ||
import styled from "@emotion/styled"; | ||
import { UpdateContext } from "./update/UpdateContext"; | ||
|
||
const UpToDateText = styled.div` | ||
margin: auto; | ||
text-align: center; | ||
padding: 5rem 1rem; | ||
color: var(--ion-color-medium); | ||
`; | ||
|
||
const Container = styled.div` | ||
height: 100%; | ||
width: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
`; | ||
|
||
export default function UpdateAppPage() { | ||
const { status, checkForUpdates, updateServiceWorker } = | ||
useContext(UpdateContext); | ||
|
||
useEffect(() => { | ||
checkForUpdates(); | ||
}, [checkForUpdates]); | ||
|
||
return ( | ||
<IonPage className="grey-bg"> | ||
<IonHeader> | ||
<IonToolbar> | ||
<IonButtons slot="start"> | ||
<IonBackButton defaultHref="/settings" text="Settings" /> | ||
</IonButtons> | ||
|
||
<IonTitle>Updates</IonTitle> | ||
</IonToolbar> | ||
</IonHeader> | ||
<IonContent> | ||
<IonRefresher | ||
slot="fixed" | ||
onIonRefresh={async (e) => { | ||
try { | ||
await checkForUpdates(); | ||
} finally { | ||
e.detail.complete(); | ||
} | ||
}} | ||
> | ||
<IonRefresherContent /> | ||
</IonRefresher> | ||
<Container> | ||
<MaxWidthContainer> | ||
<IonList inset color="primary"> | ||
<InsetIonItem> | ||
<IonLabel>Current version</IonLabel> | ||
<SettingLabel slot="end" color="medium"> | ||
{APP_VERSION} | ||
</SettingLabel> | ||
</InsetIonItem> | ||
<InsetIonItem | ||
href="https://github.com/aeharding/wefwef/releases" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
<IonLabel>Release notes</IonLabel> | ||
</InsetIonItem> | ||
</IonList> | ||
|
||
{status === "outdated" && ( | ||
<IonList inset color="primary"> | ||
<InsetIonItem | ||
detail | ||
onClick={async () => { | ||
await updateServiceWorker(); | ||
location.reload(); | ||
}} | ||
> | ||
<IonLabel>Install new update</IonLabel> | ||
<IonBadge color="danger">1</IonBadge> | ||
</InsetIonItem> | ||
</IonList> | ||
)} | ||
</MaxWidthContainer> | ||
|
||
{status === "loading" && <PageContentIonSpinner />} | ||
{status === "not-enabled" && ( | ||
<UpToDateText>Not installed.</UpToDateText> | ||
)} | ||
{status === "error" && ( | ||
<UpToDateText> | ||
Error checking for updates. | ||
<br /> | ||
<br /> | ||
Are you connected to the internet? | ||
</UpToDateText> | ||
)} | ||
{status === "current" && ( | ||
<UpToDateText>wefwef is up to date</UpToDateText> | ||
)} | ||
</Container> | ||
</IonContent> | ||
</IonPage> | ||
); | ||
} |
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,91 @@ | ||
import React, { createContext, useEffect, useRef, useState } from "react"; | ||
import { useInterval } from "usehooks-ts"; | ||
import { useRegisterSW } from "virtual:pwa-register/react"; | ||
import usePageVisibility from "../../../helpers/usePageVisibility"; | ||
|
||
type UpdateStatus = | ||
| "not-enabled" | ||
| "loading" | ||
| "current" | ||
| "outdated" | ||
| "error"; | ||
|
||
interface IUpdateContext { | ||
// used for determining whether page needs to be scrolled up first | ||
checkForUpdates: () => Promise<void>; | ||
updateServiceWorker: () => Promise<void>; | ||
status: UpdateStatus; | ||
} | ||
|
||
export const UpdateContext = createContext<IUpdateContext>({ | ||
checkForUpdates: async () => {}, | ||
updateServiceWorker: async () => {}, | ||
|
||
status: "loading", | ||
}); | ||
|
||
export function UpdateContextProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const [status, setStatus] = useState<UpdateStatus>("not-enabled"); | ||
const pageVisibility = usePageVisibility(); | ||
|
||
const registration = useRef<ServiceWorkerRegistration>(); | ||
|
||
const registerSW = useRegisterSW({ | ||
onRegistered(r) { | ||
setStatus("loading"); | ||
if (!r) return; | ||
|
||
registration.current = r; | ||
checkForUpdates(); | ||
}, | ||
onRegisterError() { | ||
setStatus("error"); | ||
}, | ||
}); | ||
|
||
useInterval(() => { | ||
checkForUpdates(); | ||
}, 1_000 * 60 * 60); | ||
|
||
useEffect(() => { | ||
if (!pageVisibility) return; | ||
|
||
checkForUpdates(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [pageVisibility]); | ||
|
||
async function checkForUpdates() { | ||
const r = registration.current; | ||
|
||
if (!r) { | ||
if (status === "not-enabled") return; | ||
setStatus("error"); | ||
return; | ||
} | ||
|
||
try { | ||
await r.update(); | ||
} catch (error) { | ||
setStatus("error"); | ||
throw error; | ||
} | ||
|
||
setStatus(!!(r.waiting || r.installing) ? "outdated" : "current"); | ||
} | ||
|
||
return ( | ||
<UpdateContext.Provider | ||
value={{ | ||
status, | ||
checkForUpdates, | ||
updateServiceWorker: registerSW.updateServiceWorker, | ||
}} | ||
> | ||
{children} | ||
</UpdateContext.Provider> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
/// <reference types="vite/client" /> | ||
/// <reference types="@emotion/react/types/css-prop" /> | ||
/// <reference types="vite-plugin-pwa/react" /> | ||
|
||
declare const APP_VERSION: string; |
Oops, something went wrong.