Skip to content

Commit

Permalink
UI: Convert AppUpdate.jsx to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
previnder committed Jan 15, 2025
1 parent a58a317 commit b3521ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
44 changes: 22 additions & 22 deletions ui/src/AppUpdate.jsx → ui/src/AppUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { ButtonClose } from './components/Button';
import Modal from './components/Modal';
import { useIsMobile } from './hooks';

export const forceSwUpdate = async () => {
export async function forceSwUpdate() {
if ('serviceWorker' in navigator) {
console.log('Force updating service worker');
const registration = await navigator.serviceWorker.ready;
return registration.update();
}
};
}

const AppUpdate = () => {
function AppUpdate() {
const [serviceWorkerWaiting, setServiceWorkerWaiting] = useState(false);

useEffect(() => {
Expand All @@ -26,29 +26,29 @@ const AppUpdate = () => {
};
}, []);

// const newSw = useRef();
useEffect(() => {
let effectCancelled = false;

const detectSwUpdate = async () => {
const registration = await navigator.serviceWorker.ready;
registration.addEventListener('updatefound', (e) => {
registration.addEventListener('updatefound', () => {
const newSW = registration.installing;
newSW.addEventListener('statechange', (e) => {
if (newSW.state == 'installed') {
if (!effectCancelled) {
// New service worker is installed, but waiting activation
// newSw.current = newSw;
setServiceWorkerWaiting(true);
if (newSW) {
newSW.addEventListener('statechange', () => {
if (newSW.state == 'installed') {
if (!effectCancelled) {
setServiceWorkerWaiting(true);
}
}
}
});
});
}
});
};

if ('serviceWorker' in navigator) detectSwUpdate();

return () => (effectCancelled = true);
if ('serviceWorker' in navigator) {
detectSwUpdate();
}
return () => {
effectCancelled = true;
};
}, []);

const handleReload = async () => {
Expand Down Expand Up @@ -100,8 +100,8 @@ const AppUpdate = () => {
</div>
<div className="modal-card-content">
<p>
A new version of this app is available. Reload the page to update. It won't take more
than a second.
A new version of this app is available. Reload the page to update. It won&apos;t take
more than a second.
</p>
</div>
<div className="modal-card-actions">
Expand All @@ -116,7 +116,7 @@ const AppUpdate = () => {
}

return null;
};
}

const localStorageKey = 'update_prompt_displayed_at';

Expand All @@ -140,7 +140,7 @@ function shouldDisplayUpdatePrompt() {
}

function updateLocalStoragePromptDisplayedTimestamp() {
window.localStorage.setItem(localStorageKey, Math.round(Date.now() / 1000));
window.localStorage.setItem(localStorageKey, Math.round(Date.now() / 1000).toString());
}

export default AppUpdate;
6 changes: 6 additions & 0 deletions ui/src/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ export function isDeviceIos() {
return /iphone|ipad|ipod/.test(userAgent);
}

/**
* Reports whether the device is running as a standalone application (basically,
* reports whether it's running as a PWA).
*
* @returns void
*/
export function isDeviceStandalone() {
// return window.navigator && 'standalone' in window.navigator;
return navigator.standalone || window.matchMedia('(display-mode: standalone)').matches;
Expand Down

0 comments on commit b3521ac

Please sign in to comment.