fix alert issues (#20416)

This commit is contained in:
Sweta Agarwalla 2025-03-26 00:11:59 +05:30 committed by GitHub
parent 246c8d4721
commit fd3f2a2a78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 8 deletions

View File

@ -31,12 +31,19 @@ const AlertBar = ({
return getIconAndClassName(type); return getIconAndClassName(type);
}, [type]); }, [type]);
const fullWidthAlert = expanded ? 'full-width-alert' : '';
return ( return (
<Alert <Alert
closable closable
showIcon showIcon
afterClose={resetAlert} afterClose={resetAlert}
className={classNames('alert-container', className, animationClass)} className={classNames(
'alert-container',
className,
animationClass,
fullWidthAlert
)}
closeIcon={ closeIcon={
<CrossIcon <CrossIcon
className="alert-close-icon" className="alert-close-icon"

View File

@ -75,13 +75,17 @@
display: flex; display: flex;
align-items: center; align-items: center;
border-radius: 12px; border-radius: 12px;
margin: 0 16px; margin: 0 18px;
} }
&.hide-alert { &.hide-alert {
animation: resize-hide-animation 0.2s ease-in-out forwards; animation: resize-hide-animation 0.2s ease-in-out forwards;
} }
&.full-width-alert {
margin: 0px;
}
&.info { &.info {
background-color: @alert-info-bg; background-color: @alert-info-bg;
border-color: @alert-info; border-color: @alert-info;

View File

@ -21,6 +21,7 @@ import React, {
ReactNode, ReactNode,
useEffect, useEffect,
useMemo, useMemo,
useState,
} from 'react'; } from 'react';
import { useLocation } from 'react-router-dom'; import { useLocation } from 'react-router-dom';
import { useAlertStore } from '../../hooks/useAlertStore'; import { useAlertStore } from '../../hooks/useAlertStore';
@ -60,8 +61,9 @@ const PageLayoutV1: FC<PageLayoutProp> = ({
mainContainerClassName = '', mainContainerClassName = '',
pageContainerStyle = {}, pageContainerStyle = {},
}: PageLayoutProp) => { }: PageLayoutProp) => {
const { alert, resetAlert } = useAlertStore(); const { alert, resetAlert, isErrorTimeOut } = useAlertStore();
const location = useLocation(); const location = useLocation();
const [prevPath, setPrevPath] = useState<string | undefined>();
const contentWidth = useMemo(() => { const contentWidth = useMemo(() => {
if (leftPanel && rightPanel) { if (leftPanel && rightPanel) {
@ -76,12 +78,18 @@ const PageLayoutV1: FC<PageLayoutProp> = ({
}, [leftPanel, rightPanel, leftPanelWidth, rightPanelWidth]); }, [leftPanel, rightPanel, leftPanelWidth, rightPanelWidth]);
useEffect(() => { useEffect(() => {
if (alert && alert.type === 'error') { if (prevPath !== location.pathname) {
setTimeout(() => { if (isErrorTimeOut) {
resetAlert(); resetAlert();
}, 3000); }
} }
}, [location.pathname, resetAlert]); }, [location.pathname, resetAlert, isErrorTimeOut]);
useEffect(() => {
setTimeout(() => {
setPrevPath(location.pathname);
}, 3000);
}, [location.pathname]);
return ( return (
<Fragment> <Fragment>

View File

@ -21,6 +21,7 @@ export type AlertType = {
interface AlertStore { interface AlertStore {
alert: AlertType | undefined; alert: AlertType | undefined;
timeoutId: ReturnType<typeof setTimeout> | null; timeoutId: ReturnType<typeof setTimeout> | null;
isErrorTimeOut: boolean;
animationClass: string; animationClass: string;
addAlert: (alert: AlertType, timer?: number) => void; addAlert: (alert: AlertType, timer?: number) => void;
resetAlert: VoidFunction; resetAlert: VoidFunction;
@ -30,9 +31,15 @@ export const useAlertStore = create<AlertStore>()((set, get) => ({
alert: undefined, alert: undefined,
animationClass: '', animationClass: '',
timeoutId: null, timeoutId: null,
isErrorTimeOut: false,
addAlert: (alert: AlertType, timer?: number) => { addAlert: (alert: AlertType, timer?: number) => {
const { timeoutId } = get(); const { timeoutId } = get();
set({ alert, animationClass: 'show-alert' }); set({ alert, animationClass: 'show-alert' });
if (alert.type === 'error') {
setTimeout(() => {
set({ isErrorTimeOut: true });
}, 5000);
}
if (timeoutId) { if (timeoutId) {
clearTimeout(timeoutId); clearTimeout(timeoutId);
@ -49,6 +56,6 @@ export const useAlertStore = create<AlertStore>()((set, get) => ({
} }
}, },
resetAlert: () => { resetAlert: () => {
set({ alert: undefined }); set({ alert: undefined, isErrorTimeOut: false });
}, },
})); }));