Skip to content

Commit

Permalink
frontend: muiAlert; update close handler
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Sep 7, 2024
1 parent c103b52 commit cbe086a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/frontend/src/components/Alert/Alert.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MuiAlert, { AlertProps as MuiAlertProps } from '@mui/material/Alert'
import React, { FC } from 'react'
import React, { FC, useState, useEffect } from 'react'
import clsx from 'clsx'
import { makeStyles } from '@mui/styles'
import { prettifyErrorMessage } from '#utils/index.js'
Expand All @@ -23,10 +23,19 @@ type AlertProps = {
export const Alert: FC<AlertProps & MuiAlertProps> = props => {
const { text, className, children, severity, onClose, maxWidth } = props
const styles = useStyles({ maxWidth })
const show = text ?? children
const [show, setShow] = useState(!!(text ?? children))

useEffect(() => {
setShow(!!(text ?? children))
}, [text, children])

function handleClose () {
onClose?.()
setShow(false)
}

return show ? (
<MuiAlert severity={severity} onClose={onClose} className={clsx(styles.root, className)}>
<MuiAlert severity={severity} onClose={handleClose} className={clsx(styles.root, className)}>
{children ?? prettifyErrorMessage(text ?? '')}
</MuiAlert>
) : null
Expand Down

0 comments on commit cbe086a

Please sign in to comment.