Skip to content

Commit

Permalink
feat: add duration option to NotificationBar
Browse files Browse the repository at this point in the history
  • Loading branch information
Cowjiang committed Jan 25, 2024
1 parent e07d435 commit cb997ea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ function App() {
const navigate = useNavigate()

const {isLoading, isAdmin, setLoading, checkAdmin, restartAsAdmin} = useAppStore()
const {getServiceState} = useZeroTierStore()
const {getServiceState, getServiceStartType} = useZeroTierStore()

const {setNotification} = useNotification()

useEffect(() => {
Promise.all([checkAdmin(), getServiceState()]).finally(() => setLoading(false))
Promise.all([
checkAdmin(),
getServiceState(),
getServiceStartType()
]).finally(() => setLoading(false))
}, [])

useEffect(() => {
Expand Down
15 changes: 14 additions & 1 deletion src/components/NotificationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type NotificationBarOptions = {
children?: ReactNode
hideCloseButton?: boolean
animate?: boolean
duration?: number
onClose?: () => void
}

Expand Down Expand Up @@ -73,15 +74,27 @@ function NotificationProvider({children}: { children: ReactNode }) {
const [options, setOptions] = useState<NotificationBarProps>(initialOptions)
const [hidden, setHidden] = useState(true)

const [autoCloseTimer, setAutoCloseTimer] = useState<number | undefined>(undefined)

const setNotification = (options: NotificationBarOptions) => {
clearTimer()
setOptions({...initialOptions, ...options})
setTimeout(() => setHidden(false), options.animate ? 250 : 0)
setTimeout(() => setHidden(false), 0)
options.duration && setAutoCloseTimer(setTimeout(closeNotification, options.duration))
}
const closeNotification = () => {
clearTimer()
setHidden(true)
setTimeout(() => setOptions(initialOptions), options.animate ? 250 : 0)
}

const clearTimer = () => {
if (autoCloseTimer) {
clearTimeout(autoCloseTimer)
setAutoCloseTimer(undefined)
}
}

return (
<NotificationContext.Provider value={{setNotification, closeNotification}}>
{children}
Expand Down

0 comments on commit cb997ea

Please sign in to comment.