diff --git a/phone/src/ui/components/SnackBar.tsx b/phone/src/ui/components/SnackBar.tsx new file mode 100644 index 0000000..4d23f16 --- /dev/null +++ b/phone/src/ui/components/SnackBar.tsx @@ -0,0 +1,9 @@ +import React from 'react'; + +export const SnackBar = ({ children }) => { + return ( +
+

{children}

+
+ ) +} \ No newline at end of file diff --git a/phone/src/ui/components/SnackbarProvider.tsx b/phone/src/ui/components/SnackbarProvider.tsx new file mode 100644 index 0000000..7947098 --- /dev/null +++ b/phone/src/ui/components/SnackbarProvider.tsx @@ -0,0 +1,30 @@ +import React, { createContext, useState, useEffect, useMemo, useCallback } from 'react'; +import { SnackBar } from './SnackBar'; + + +export const SnackbarContext = createContext(null); +const AUTO_HIDE = 2000; + +function SnackbarProvider({ children }) { + const [alerts, setAlerts] = useState([]); + + const alertIdx = alerts.join(','); + + useEffect(() => { + const timer = setTimeout(() => setAlerts((alert) => alerts.slice(0, alerts.length - 1)), AUTO_HIDE); + return () => clearTimeout(timer); + }, [alertIdx]) + + const addAlert = useCallback((content) => setAlerts((alerts) => [content, ...alerts]), []); + + const value = useMemo(() => ({ addAlert }), [addAlert]); + + return ( + + {children} + {alerts.map((alert) => {alert})} + + ) +} + +export default SnackbarProvider; \ No newline at end of file diff --git a/phone/src/ui/hooks/useSnackbar.ts b/phone/src/ui/hooks/useSnackbar.ts new file mode 100644 index 0000000..fe6077a --- /dev/null +++ b/phone/src/ui/hooks/useSnackbar.ts @@ -0,0 +1,8 @@ +import { useContext } from 'react'; +import { SnackbarContext } from '../components/SnackbarProvider'; + +interface ISnackBar { + addAlert: (message: string, severity: string) => void; +} + +export const useSnackbar = (): ISnackBar => useContext(SnackbarContext); \ No newline at end of file