Skip to content

Commit

Permalink
Added reload prompt for all pages
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Apr 14, 2024
1 parent 4498f66 commit 0d054b4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
33 changes: 0 additions & 33 deletions src/frontend/src/ReloadPrompt.tsx

This file was deleted.

2 changes: 2 additions & 0 deletions src/frontend/src/pages/ui/RootPage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { type PropsWithChildren, type FC } from 'react';
import { Outlet, ScrollRestoration } from 'react-router-dom';
import { ReloadPrompt } from '@/widgets/pwa';

export const RootPage: FC<PropsWithChildren> = ({ children }) => (
<>
<ScrollRestoration />
<ReloadPrompt />
<Outlet />
{children}
</>
Expand Down
2 changes: 0 additions & 2 deletions src/frontend/src/widgets/layout/ui/PrivateLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type Theme, useMediaQuery, useScrollTrigger } from '@mui/material';
import { type PropsWithChildren, type FC, type ReactElement } from 'react';
import { useAuthStatusCheckEffect } from '@/features/auth';
import { ReloadPrompt } from '@/ReloadPrompt';
import { APP_BAR_HEIGHT_SM, APP_BAR_HEIGHT_XS } from '@/shared/constants';
import { useToggle } from '@/shared/hooks';
import { AppShell } from '@/shared/ui';
Expand Down Expand Up @@ -36,7 +35,6 @@ export const PrivateLayout: FC<Props> = ({ children, subheader }) => {
subheader={subheader}
subheaderElevation={pageScrolled ? 1 : 0}
>
<ReloadPrompt />
{children}
</AppShell>
);
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/widgets/pwa/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ui';
33 changes: 33 additions & 0 deletions src/frontend/src/widgets/pwa/ui/ReloadPrompt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useRegisterSW } from 'virtual:pwa-register/react';
import { Button, Snackbar } from '@mui/material';
import { type FC } from 'react';

export const ReloadPrompt: FC = () => {
const registerSw = useRegisterSW({
onRegisteredSW: async (_, registration) => {
await registration?.update();
},
});

const [needRefresh] = registerSw.needRefresh;

const handleReload = (): void => {
registerSw.updateServiceWorker(true);
};

return (
<Snackbar
open={needRefresh}
message="New content available, click on reload button to update"
action={
<Button variant="text" onClick={handleReload}>
Reload
</Button>
}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
/>
);
};
1 change: 1 addition & 0 deletions src/frontend/src/widgets/pwa/ui/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ReloadPrompt';

0 comments on commit 0d054b4

Please sign in to comment.