|
1 | 1 | // Copyright Contributors to the Amundsen project.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -export * from './Alert'; |
5 |
| -export * from './AlertList'; |
| 4 | +import * as React from 'react'; |
| 5 | +import SanitizedHTML from 'react-sanitized-html'; |
| 6 | +import { Modal } from 'react-bootstrap'; |
| 7 | + |
| 8 | +import { IconSizes } from 'interfaces'; |
| 9 | +import { NoticeSeverity } from 'config/config-types'; |
| 10 | +import { AlertIcon, InformationIcon } from 'components/SVGIcons'; |
| 11 | +import { DefinitionList } from 'components/DefinitionList'; |
| 12 | + |
| 13 | +import './styles.scss'; |
| 14 | + |
| 15 | +const SEVERITY_TO_COLOR_MAP = { |
| 16 | + [NoticeSeverity.INFO]: '#3a97d3', // cyan50 |
| 17 | + [NoticeSeverity.WARNING]: '#FF8D1F', // $amber70 |
| 18 | + [NoticeSeverity.ALERT]: '#b8072c', // $red70 |
| 19 | +}; |
| 20 | +const SEVERITY_TO_SEVERITY_CLASS = { |
| 21 | + [NoticeSeverity.INFO]: 'is-info', |
| 22 | + [NoticeSeverity.WARNING]: 'is-warning', |
| 23 | + [NoticeSeverity.ALERT]: 'is-alert', |
| 24 | +}; |
| 25 | +const OPEN_PAYLOAD_CTA = 'See details'; |
| 26 | +const PAYLOAD_MODAL_TITLE = 'Summary'; |
| 27 | +const PAYLOAD_MODAL_CLOSE_BTN = 'Close'; |
| 28 | + |
| 29 | +const DEFINITION_WIDTH = 150; |
| 30 | + |
| 31 | +export interface AlertProps { |
| 32 | + /** Message to show in the alert */ |
| 33 | + message: string | React.ReactNode; |
| 34 | + /** Severity of the alert (info, warning, or alert) */ |
| 35 | + severity?: NoticeSeverity; |
| 36 | + /** Link passed to set as the action (for routing links) */ |
| 37 | + actionLink?: React.ReactNode; |
| 38 | + /** Text of the link action */ |
| 39 | + actionText?: string; |
| 40 | + /** Href for the link action */ |
| 41 | + actionHref?: string; |
| 42 | + /** Callback to call when the action is clicked */ |
| 43 | + onAction?: (event: React.MouseEvent<HTMLButtonElement>) => void; |
| 44 | + /** Optional extra info to render in a modal */ |
| 45 | + payload?: Record<string, string>; |
| 46 | +} |
| 47 | + |
| 48 | +const Alert: React.FC<AlertProps> = ({ |
| 49 | + message, |
| 50 | + severity = NoticeSeverity.WARNING, |
| 51 | + onAction, |
| 52 | + actionText, |
| 53 | + actionHref, |
| 54 | + actionLink, |
| 55 | + payload, |
| 56 | +}: AlertProps) => { |
| 57 | + const [showPayloadModal, setShowPayloadModal] = React.useState(false); |
| 58 | + let action: null | React.ReactNode = null; |
| 59 | + |
| 60 | + const handleSeeDetails = (e: React.MouseEvent<HTMLButtonElement>) => { |
| 61 | + onAction?.(e); |
| 62 | + setShowPayloadModal(true); |
| 63 | + }; |
| 64 | + const handleModalClose = () => { |
| 65 | + setShowPayloadModal(false); |
| 66 | + }; |
| 67 | + |
| 68 | + if (payload) { |
| 69 | + action = ( |
| 70 | + <button |
| 71 | + type="button" |
| 72 | + className="btn btn-link btn-payload" |
| 73 | + onClick={handleSeeDetails} |
| 74 | + > |
| 75 | + {OPEN_PAYLOAD_CTA} |
| 76 | + </button> |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + if (actionText && onAction) { |
| 81 | + action = ( |
| 82 | + <button type="button" className="btn btn-link" onClick={onAction}> |
| 83 | + {actionText} |
| 84 | + </button> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + if (actionText && actionHref) { |
| 89 | + action = ( |
| 90 | + <a className="action-link" href={actionHref}> |
| 91 | + {actionText} |
| 92 | + </a> |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + if (actionLink) { |
| 97 | + action = actionLink; |
| 98 | + } |
| 99 | + |
| 100 | + let iconComponent: React.ReactNode = null; |
| 101 | + |
| 102 | + if (severity === NoticeSeverity.INFO) { |
| 103 | + iconComponent = ( |
| 104 | + <InformationIcon |
| 105 | + fill={SEVERITY_TO_COLOR_MAP[severity]} |
| 106 | + size={IconSizes.REGULAR} |
| 107 | + /> |
| 108 | + ); |
| 109 | + } else { |
| 110 | + iconComponent = ( |
| 111 | + <AlertIcon |
| 112 | + stroke={SEVERITY_TO_COLOR_MAP[severity]} |
| 113 | + size={IconSizes.SMALL} |
| 114 | + /> |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + // If we receive a string, we want to sanitize any html inside |
| 119 | + const formattedMessage = |
| 120 | + typeof message === 'string' ? <SanitizedHTML html={message} /> : message; |
| 121 | + |
| 122 | + const payloadDefinitions = payload |
| 123 | + ? Object.keys(payload).map((key) => ({ |
| 124 | + term: key, |
| 125 | + description: payload[key], |
| 126 | + })) |
| 127 | + : null; |
| 128 | + |
| 129 | + return ( |
| 130 | + <div className={`alert ${SEVERITY_TO_SEVERITY_CLASS[severity]}`}> |
| 131 | + {iconComponent} |
| 132 | + <p className="alert-message">{formattedMessage}</p> |
| 133 | + {action && <span className="alert-action">{action}</span>} |
| 134 | + {payloadDefinitions && ( |
| 135 | + <Modal |
| 136 | + className="alert-payload-modal" |
| 137 | + show={showPayloadModal} |
| 138 | + onHide={handleModalClose} |
| 139 | + > |
| 140 | + <Modal.Header closeButton onHide={handleModalClose}> |
| 141 | + <Modal.Title>{PAYLOAD_MODAL_TITLE}</Modal.Title> |
| 142 | + </Modal.Header> |
| 143 | + <Modal.Body> |
| 144 | + <DefinitionList |
| 145 | + definitions={payloadDefinitions} |
| 146 | + termWidth={DEFINITION_WIDTH} |
| 147 | + /> |
| 148 | + </Modal.Body> |
| 149 | + <Modal.Footer> |
| 150 | + <button |
| 151 | + className="btn btn-primary payload-modal-close" |
| 152 | + type="button" |
| 153 | + onClick={handleModalClose} |
| 154 | + > |
| 155 | + {PAYLOAD_MODAL_CLOSE_BTN} |
| 156 | + </button> |
| 157 | + </Modal.Footer> |
| 158 | + </Modal> |
| 159 | + )} |
| 160 | + </div> |
| 161 | + ); |
| 162 | +}; |
| 163 | + |
| 164 | +export default Alert; |
0 commit comments