-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
282 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
import * as React from "react"; | ||
import { ChakraProvider } from "@chakra-ui/react"; | ||
import { Home } from "@/pages/Home"; | ||
import { theme } from "./theme"; | ||
import { SpeedInsights } from "@vercel/speed-insights/react"; | ||
import { Analytics } from "@vercel/analytics/react"; | ||
import { | ||
NotificationProvider, | ||
useSendNotification, | ||
} from "./components/Notifications"; | ||
import { useEffect } from "react"; | ||
import { ServiceWorkerStatus, onceSW } from "./utils/serviceWorker"; | ||
|
||
export const App = () => ( | ||
<ChakraProvider theme={theme}> | ||
<Analytics /> | ||
<SpeedInsights /> | ||
<Home /> | ||
</ChakraProvider> | ||
); | ||
function InnerApp() { | ||
const send = useSendNotification(); | ||
|
||
useEffect(() => { | ||
onceSW(ServiceWorkerStatus.Initialized, () => | ||
send({ content: "Cached for faster loading", timeout: 2500 }) | ||
); | ||
onceSW(ServiceWorkerStatus.Pending, () => | ||
send({ content: "An update. Close all tabs for this page to finish it" }) | ||
); | ||
onceSW(ServiceWorkerStatus.Updated, () => | ||
send({ content: "Update finished", timeout: 3000 }) | ||
); | ||
}, [send]); | ||
|
||
return ( | ||
<> | ||
<Home /> | ||
</> | ||
); | ||
} | ||
|
||
export function App() { | ||
return ( | ||
<ChakraProvider theme={theme}> | ||
<Analytics /> | ||
<SpeedInsights /> | ||
<NotificationProvider> | ||
<InnerApp /> | ||
</NotificationProvider> | ||
</ChakraProvider> | ||
); | ||
} |
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,142 @@ | ||
import { Fields } from "@/utils"; | ||
import { Box, Container, Slide, chakra } from "@chakra-ui/react"; | ||
import { | ||
ReactNode, | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
import { FaXmark as FaXmarkRaw } from "react-icons/fa6"; | ||
|
||
const FaXmark = chakra(FaXmarkRaw); | ||
|
||
export class Notification { | ||
readonly id: number; | ||
readonly content: ReactNode; | ||
readonly timeout: number; | ||
|
||
constructor({ id, content, timeout }: Fields<Notification>) { | ||
this.id = id; | ||
this.content = content; | ||
this.timeout = timeout; | ||
} | ||
} | ||
|
||
const NotificationContext = createContext<{ | ||
notifications: Notification[]; | ||
sendNotification: (n: Notification) => void; | ||
removeNotification: (id: number) => void; | ||
}>({ | ||
notifications: [], | ||
sendNotification: () => {}, | ||
removeNotification: () => {}, | ||
}); | ||
|
||
let NOTIFICATIONS = 0; | ||
|
||
function NotificationsContainer() { | ||
const [status, setStatus] = useState<number>(0); | ||
const { | ||
notifications: [notification], | ||
removeNotification, | ||
} = useContext(NotificationContext); | ||
|
||
useEffect(() => { | ||
if (notification && status === 0) { | ||
setStatus(1); | ||
setTimeout(() => setStatus(2), notification.timeout); | ||
} | ||
}, [status, notification, removeNotification]); | ||
|
||
if (!notification) { | ||
return undefined; | ||
} | ||
|
||
return ( | ||
<Slide | ||
direction="bottom" | ||
in={status === 1} | ||
style={{ zIndex: 99999999 }} | ||
onAnimationEnd={() => { | ||
if (status === 2) { | ||
removeNotification(notification.id); | ||
setStatus(0); | ||
} | ||
}} | ||
onClick={() => { | ||
setStatus(2); | ||
}} | ||
> | ||
<Box | ||
textAlign="center" | ||
cursor="pointer" | ||
borderTopWidth={1} | ||
borderColor="chakra-border-color" | ||
background="chakra-body-bg" | ||
transition="background 0.3s ease-in-out" | ||
_hover={{ | ||
_dark: { | ||
background: "gray.700", | ||
}, | ||
_light: { | ||
background: "gray.100", | ||
}, | ||
}} | ||
py={5} | ||
> | ||
<Container>{notification.content}</Container> | ||
</Box> | ||
<FaXmark | ||
position="absolute" | ||
right={4} | ||
top="50%" | ||
transform="translateY(-50%)" | ||
fill="chakra-placeholder-color" | ||
aria-label="Close mark" | ||
/> | ||
</Slide> | ||
); | ||
} | ||
|
||
export function NotificationProvider({ children }: { children: ReactNode }) { | ||
const [notifications, setNotifications] = useState<Notification[]>([]); | ||
const sendNotification = useCallback( | ||
(n: Notification) => setNotifications((ns) => [...ns, n]), | ||
[] | ||
); | ||
const removeNotification = useCallback( | ||
(id: number) => setNotifications((ns) => ns.filter((n) => n.id !== id)), | ||
[] | ||
); | ||
|
||
return ( | ||
<NotificationContext.Provider | ||
value={{ | ||
notifications, | ||
sendNotification, | ||
removeNotification, | ||
}} | ||
> | ||
{children} | ||
<NotificationsContainer /> | ||
</NotificationContext.Provider> | ||
); | ||
} | ||
|
||
export function useSendNotification() { | ||
const { sendNotification } = useContext(NotificationContext); | ||
|
||
return useCallback( | ||
(n: Omit<Fields<Notification>, "id" | "timeout"> & { timeout?: number }) => | ||
sendNotification( | ||
new Notification({ | ||
...n, | ||
id: NOTIFICATIONS++, | ||
timeout: n.timeout ?? 10000, | ||
}) | ||
), | ||
[sendNotification] | ||
); | ||
} |
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,50 @@ | ||
export enum ServiceWorkerStatus { | ||
Unknown = 0, | ||
Initialized, | ||
Pending, | ||
Activated, | ||
Updated, | ||
} | ||
|
||
const STATUS_KEY = "chainIdleServiceWorkerStatus"; | ||
const PREV_STATUS = Number(localStorage.getItem(STATUS_KEY)); | ||
let STATUS = ServiceWorkerStatus.Unknown; | ||
|
||
let LISTENERS: { handler: () => void; status: ServiceWorkerStatus }[] = []; | ||
|
||
function statusEq(want: ServiceWorkerStatus, received: ServiceWorkerStatus) { | ||
return ( | ||
(want === ServiceWorkerStatus.Updated && | ||
received === ServiceWorkerStatus.Activated && | ||
PREV_STATUS === ServiceWorkerStatus.Pending) || | ||
want === received | ||
); | ||
} | ||
|
||
function onSW(status: ServiceWorkerStatus) { | ||
STATUS = status; | ||
localStorage.setItem(STATUS_KEY, status.toString()); | ||
|
||
LISTENERS = LISTENERS.filter((h) => { | ||
if (statusEq(h.status, status)) { | ||
h.handler(); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
|
||
export const registrationConfig = { | ||
onInitialized: () => onSW(ServiceWorkerStatus.Initialized), | ||
onPending: () => onSW(ServiceWorkerStatus.Pending), | ||
onActivated: () => onSW(ServiceWorkerStatus.Activated), | ||
}; | ||
|
||
export function onceSW(status: ServiceWorkerStatus, handler: () => void) { | ||
if (statusEq(status, STATUS)) { | ||
handler(); | ||
return; | ||
} | ||
|
||
LISTENERS.push({ handler, status }); | ||
} |