Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[M2-5266] Changed the notification center behavior #407

Merged
merged 7 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/shared/constants/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const Theme = {
accentGreen30: 'rgba(15, 123, 108, 0.30)',
accentYellow: '#DFAC03',
accentYellow30: 'rgba(223, 172, 3, 0.30)',
accentYellow30AnalogueWithoutOpacity: 'rgb(245, 230, 179)', // Formula: Color * alpha + Background * (1 - alpha)
accentOrange: '#D9730D',
accentOrange30: 'rgba(217, 115, 13, 0.30)',
inverseOnSurface: '#F0F0F4',
Expand Down
2 changes: 2 additions & 0 deletions src/shared/ui/NotificationCenter/ui/ErrorNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export const ErrorNotification = forwardRef((props: Props, ref) => {
</Box>
);
});

ErrorNotification.displayName = 'ErrorNotification';
2 changes: 2 additions & 0 deletions src/shared/ui/NotificationCenter/ui/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export const Notification = forwardRef<HTMLDivElement, Props>(
}
},
);

Notification.displayName = 'Notification';
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type Props = PropsWithChildren<{
}>;

export const NotificationAnimation = ({ notifications, refMap }: Props) => {
const transitions = useTransition(notifications, {
const lastNotification = notifications[notifications.length - 1];

const transitions = useTransition(lastNotification, {
from: { height: '0px' },
enter: (item) => async (next) => {
await next({ height: `${refMap.get(item)?.offsetHeight ?? 72}px` });
Expand Down
21 changes: 17 additions & 4 deletions src/shared/ui/NotificationCenter/ui/NotificationCenter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo } from 'react';
import { forwardRef, useCallback, useEffect, useMemo } from 'react';

import Box from '@mui/material/Box';

Expand All @@ -8,7 +8,11 @@ import { Notification as TNotification } from '../lib/types';

import { eventEmitter, useForceUpdate } from '~/shared/utils';

export const NotificationCenter = () => {
type Props = {
isIntersecting?: boolean;
};

export const NotificationCenter = forwardRef<HTMLDivElement, Props>((props, ref) => {
const forceUpdate = useForceUpdate();
const refMap: WeakMap<TNotification, HTMLDivElement> = useMemo(() => new WeakMap(), []); // Collect refs for each notification element to calculate their height

Expand Down Expand Up @@ -45,11 +49,20 @@ export const NotificationCenter = () => {
}, [onNotificationAdded, onNotificationRemoved]);

return (
<Box id="app-notification-container" width="100%">
<Box
ref={ref}
id="app-notification-container"
width="100%"
position="sticky"
top={0}
zIndex={10}
>
<NotificationAnimation
notifications={notificationCenterStore.notifications}
refMap={refMap}
/>
</Box>
);
};
});

NotificationCenter.displayName = 'NotificationCenter';
2 changes: 2 additions & 0 deletions src/shared/ui/NotificationCenter/ui/SuccessNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ export const SuccessNotification = forwardRef((props: Props, ref) => {
</Box>
);
});

SuccessNotification.displayName = 'SuccessNotification';
4 changes: 3 additions & 1 deletion src/shared/ui/NotificationCenter/ui/WarningNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export const WarningNotification = forwardRef((props: Props, ref) => {
padding="12px 16px"
gap="12px"
minHeight="72px"
bgcolor={Theme.colors.light.accentYellow30}
bgcolor={Theme.colors.light.accentYellow30AnalogueWithoutOpacity}
>
<ErrorRoundedIcon sx={{ color: Theme.colors.light.accentYellow }} />
<Markdown markdown={props.message} />
</Box>
);
});

WarningNotification.displayName = 'WarningNotification';
1 change: 1 addition & 0 deletions src/shared/utils/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './useIsPublic';
export * from './useFlowType';
export * from './useForceUpdate';
export * from './usePrevious';
export * from './useIntersectionObserver';
37 changes: 37 additions & 0 deletions src/shared/utils/hooks/useIntersectionObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useEffect, useMemo, useRef, useState } from 'react';

type Return = {
containerRef: React.RefObject<HTMLDivElement>;
targetRef: React.RefObject<HTMLDivElement>;
isIntersecting: boolean;
};

export const useIntersectionObserver = (): Return => {
const containerRef = useRef<HTMLDivElement | null>(null);
const targetRef = useRef<HTMLDivElement | null>(null);

const [isIntersecting, setIsIntersecting] = useState<boolean>(false);

const observer = useMemo(() => {
return new IntersectionObserver(
([notificationCenter]) => {
setIsIntersecting(notificationCenter.isIntersecting);
},
{
root: containerRef.current,
rootMargin: '0px',
threshold: 1,
},
);
}, []);

useEffect(() => {
observer.observe(targetRef.current as Element);

return () => {
observer.disconnect();
};
}, [observer]);

return { containerRef, targetRef, isIntersecting };
};
Loading