Skip to content

Commit

Permalink
feat: prompt for reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Grafikart committed Oct 15, 2024
1 parent d4cebb7 commit 1364cfd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
50 changes: 50 additions & 0 deletions src/components/ReloadPrompt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// @ts-ignore
import { useRegisterSW } from "virtual:pwa-register/react";
import { Button, Snackbar } from "@mui/material";
import Slide from "@mui/material/Slide";

export function ReloadPrompt() {
const {
offlineReady: [offlineReady, setOfflineReady],
needRefresh: [needRefresh, setNeedRefresh],
updateServiceWorker,
} = useRegisterSW();

const refresh = () => {
updateServiceWorker(true).then(() => {
window.location.reload();
});
};

if (offlineReady) {
return (
<Snackbar
anchorOrigin={{ vertical: "bottom", horizontal: "right" }}
open={offlineReady}
onClose={() => setNeedRefresh(false)}
message="L'application peut maintenant fonctionner hors ligne"
TransitionComponent={Slide}
action={
<Button size="small" onClick={() => setOfflineReady(false)}>
Fermer
</Button>
}
/>
);
}

return (
<Snackbar
anchorOrigin={{ vertical: "bottom", horizontal: "right" }}
open={needRefresh}
onClose={() => setNeedRefresh(false)}
message="Une nouvelle version de l'application est disponible"
TransitionComponent={Slide}
action={
<Button size="small" onClick={refresh}>
Actualiser
</Button>
}
/>
);
}
3 changes: 2 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";
import { OidcProvider } from "./components/OidcProvider.tsx";

import { ReloadPrompt } from "./components/ReloadPrompt.tsx";

const root = ReactDOM.createRoot(document.getElementById("root") as HTMLElement);

Expand All @@ -16,6 +16,7 @@ root.render(
<ThemeProvider theme={theme}>
<CssBaseline enableColorScheme />
<App />
<ReloadPrompt />
</ThemeProvider>
</StyledEngineProvider>
</React.StrictMode>
Expand Down
4 changes: 2 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export default defineConfig({
VitePWA({
workbox: {
maximumFileSizeToCacheInBytes: 15_000_000,
sourcemap: true
sourcemap: true,
},
registerType: "autoUpdate",
registerType: "prompt",
}),
],
server: {
Expand Down

0 comments on commit 1364cfd

Please sign in to comment.