-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from kachun333/dev
Dev
- Loading branch information
Showing
9 changed files
with
189 additions
and
7 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
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,49 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import * as serviceWorkerRegistration from "serviceWorkerRegistration"; | ||
|
||
interface UseServiceWorkerRes { | ||
hasUpdate: boolean; | ||
isLoading: boolean; | ||
updateApp: () => void; | ||
} | ||
|
||
function useServiceWorker(): UseServiceWorkerRes { | ||
const [hasUpdate, setHasUpdate] = useState(false); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const serviceWorkerRef = useRef<ServiceWorker | null>(null); | ||
|
||
useEffect(() => { | ||
function onHasUpdate(registration: ServiceWorkerRegistration) { | ||
serviceWorkerRef.current = registration.waiting; | ||
setHasUpdate(true); | ||
} | ||
|
||
serviceWorkerRegistration.register({ | ||
onUpdate: onHasUpdate, | ||
onHasWaiting: onHasUpdate, | ||
}); | ||
}, []); | ||
|
||
function updateApp() { | ||
setIsLoading(true); | ||
if (!serviceWorkerRef.current) return; | ||
const sw = serviceWorkerRef.current; | ||
// Add listener for state change of service worker | ||
sw.onstatechange = () => { | ||
if (sw?.state === "activated" && navigator.serviceWorker.controller) { | ||
// Reload page if waiting was successfully skipped | ||
window.location.reload(); | ||
} | ||
}; | ||
sw.postMessage({ type: "SKIP_WAITING" }); | ||
setHasUpdate(false); | ||
} | ||
|
||
return { | ||
hasUpdate, | ||
isLoading, | ||
updateApp, | ||
}; | ||
} | ||
|
||
export default useServiceWorker; |
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,26 @@ | ||
import RocketLaunch from "@mui/icons-material/RocketLaunch"; | ||
import { CircularProgress } from "@mui/material"; | ||
import React from "react"; | ||
import * as S from "./index.styled"; | ||
|
||
interface SnackbarObj { | ||
avatar: React.ReactNode; | ||
title: string; | ||
caption: string; | ||
} | ||
|
||
export const HAS_UPDATE_SNACKBAR: SnackbarObj = { | ||
avatar: ( | ||
<S.StyledAvatar alt="update app icon"> | ||
<RocketLaunch /> | ||
</S.StyledAvatar> | ||
), | ||
title: "Update Available", | ||
caption: "Click here for updates", | ||
}; | ||
|
||
export const UPDATING_SNACKBAR: SnackbarObj = { | ||
avatar: <CircularProgress />, | ||
title: "Updating", | ||
caption: "Just a moment...", | ||
}; |
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,57 @@ | ||
import { Avatar, Typography } from "@mui/material"; | ||
import Paper from "@mui/material/Paper"; | ||
import { styled } from "@mui/material/styles"; | ||
|
||
const initialBottom = 32; | ||
const FABHeight = 56; | ||
const marginTopFAB = 8; | ||
const translateY = marginTopFAB + FABHeight; | ||
|
||
export const StyledPaper = styled(Paper, { | ||
shouldForwardProp: (prop) => prop !== "show" && prop !== "hasFAB", | ||
})<{ show: boolean; hasFAB: boolean }>(({ theme, show, hasFAB }) => { | ||
const opacity = show ? 1 : 0; | ||
const styleWithFAB = hasFAB && { | ||
transform: `translate(-50%, -${translateY}px)`, | ||
transition: `transform ${theme.transitions.duration.leavingScreen}ms ${theme.transitions.easing.sharp}`, | ||
}; | ||
|
||
return { | ||
zIndex: 2000, // highest in App | ||
cursor: "pointer", | ||
width: 320, | ||
padding: `${theme.spacing(1.5)} ${theme.spacing(2)}`, | ||
position: "fixed", | ||
bottom: initialBottom, | ||
left: "50%", | ||
opacity, | ||
transform: "translate(-50%)", | ||
transition: ` | ||
opacity ${theme.transitions.duration.enteringScreen}ms ${theme.transitions.easing.easeIn}, | ||
transform ${theme.transitions.duration.enteringScreen}ms ${theme.transitions.easing.sharp} | ||
`, | ||
|
||
[theme.breakpoints.down("sm")]: { | ||
width: "95%", | ||
...styleWithFAB, | ||
}, | ||
}; | ||
}); | ||
|
||
export const StyledContainer = styled("div")({ | ||
display: "flex", | ||
alignItems: "center", | ||
}); | ||
|
||
export const StyledTextContainer = styled("div")({ | ||
flex: 1, | ||
}); | ||
|
||
export const StyledCaption = styled(Typography)({ | ||
/* so that caption is nicely align */ | ||
marginTop: -4, | ||
}); | ||
|
||
export const StyledAvatar = styled(Avatar)(({ theme }) => ({ | ||
backgroundColor: theme.palette.primary.main, | ||
})); |
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,40 @@ | ||
import { Typography } from "@mui/material"; | ||
import useScrollTrigger from "@mui/material/useScrollTrigger"; | ||
import React, { useEffect, useState } from "react"; | ||
import useServiceWorker from "./hooks/useServiceWorker"; | ||
import { HAS_UPDATE_SNACKBAR, UPDATING_SNACKBAR } from "./index.constants"; | ||
import * as S from "./index.styled"; | ||
|
||
const ServiceWorker: React.FC = () => { | ||
const trigger = useScrollTrigger(); | ||
const { isLoading, hasUpdate, updateApp } = useServiceWorker(); | ||
const [showUpdatePrompt, setShowUpdatePrompt] = useState(false); | ||
|
||
useEffect(() => { | ||
const id = setTimeout(() => setShowUpdatePrompt(hasUpdate), 500); | ||
return () => clearTimeout(id); | ||
}, [hasUpdate]); | ||
|
||
if (!hasUpdate) return null; | ||
const snackbarObj = isLoading ? UPDATING_SNACKBAR : HAS_UPDATE_SNACKBAR; | ||
return ( | ||
<S.StyledPaper | ||
show={showUpdatePrompt} | ||
hasFAB={trigger} | ||
onClick={updateApp} | ||
elevation={10} | ||
> | ||
<S.StyledContainer> | ||
<S.StyledTextContainer> | ||
<Typography variant="h6">{snackbarObj.title}</Typography> | ||
<Typography variant="caption" component="div"> | ||
{snackbarObj.caption} | ||
</Typography> | ||
</S.StyledTextContainer> | ||
{snackbarObj.avatar} | ||
</S.StyledContainer> | ||
</S.StyledPaper> | ||
); | ||
}; | ||
|
||
export default ServiceWorker; |
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