Skip to content

Commit

Permalink
refactor updateServiceWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
poulch committed Jul 30, 2024
1 parent f2c2c09 commit 887a540
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
11 changes: 2 additions & 9 deletions src/components/TopWarning/NewVersionAvailable.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { Button, Text } from "@saleor/macaw-ui-next";
import React from "react";
import { FormattedMessage } from "react-intl";
import { useRegisterSW } from "virtual:pwa-register/react";

import { Container } from "./Container";

export const NewVersionAvailable = () => {
const { updateServiceWorker } = useRegisterSW();

const handleReloadClick = () => {
updateServiceWorker();
};

export const NewVersionAvailable = ({ onUpdate }: { onUpdate: () => void }) => {
return (
<Container>
<Text size={1} fontWeight="medium">
Expand All @@ -22,7 +15,7 @@ export const NewVersionAvailable = () => {
/>
</Text>
<Button
onClick={handleReloadClick}
onClick={onUpdate}
size="small"
variant="tertiary"
color="accent1"
Expand Down
6 changes: 2 additions & 4 deletions src/components/TopWarning/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import { YouAreOffline } from "./YouAreOffline";
export const TopWarning = () => {
const {
needRefresh: [needRefresh],
updateServiceWorker,
} = useRegisterSW();

// eslint-disable-next-line no-console
console.log("Has new version", needRefresh);

const isOnline = useNavigatorOnLine();

if (!isOnline) {
return <YouAreOffline />;
}

if (needRefresh) {
return <NewVersionAvailable />;
return <NewVersionAvailable onUpdate={() => updateServiceWorker(true)} />;
}

return null;
Expand Down

0 comments on commit 887a540

Please sign in to comment.