Skip to content

Commit

Permalink
feat(snack-bar): add deduplication support for notifications
Browse files Browse the repository at this point in the history
Introduced the `dedupeKey` property to prevent duplicate snack bar notifications. Modified related components and hooks to handle deduplication logic efficiently. This ensures a cleaner and more user-friendly notification experience.
  • Loading branch information
AMoreaux committed Jan 15, 2025
1 parent 1baa030 commit 64d25ed
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const VerifyEffect = () => {
useEffect(() => {
if (isDefined(errorMessage)) {
enqueueSnackBar(errorMessage, {
dedupeKey: 'verify-failed-dedupe-key',
variant: SnackBarVariant.Error,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const VerifyEmailEffect = () => {
const verifyEmailToken = async () => {
if (!email || !emailVerificationToken) {
enqueueSnackBar(`Invalid email verification link.`, {
dedupeKey: 'email-verification-link-dedupe-key',
variant: SnackBarVariant.Error,
});
return navigate(AppPath.SignInUp);
Expand All @@ -39,12 +40,14 @@ export const VerifyEmailEffect = () => {
);

enqueueSnackBar('Email verified.', {
dedupeKey: 'email-verified-dedupe-key',
variant: SnackBarVariant.Success,
});

navigate(`${AppPath.Verify}?loginToken=${loginToken.token}`);
} catch (error) {
enqueueSnackBar('Email verification failed.', {
dedupeKey: 'email-verification-failed-dedupe-key',
variant: SnackBarVariant.Error,
});
setIsError(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type SnackBarProps = Pick<ComponentPropsWithoutRef<'div'>, 'id'> & {
onClose?: () => void;
role?: 'alert' | 'status';
variant?: SnackBarVariant;
dedupeKey?: string;
};

const StyledContainer = styled.div`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback } from 'react';
import { useRecoilCallback } from 'recoil';
import { v4 as uuidv4 } from 'uuid';
import { isDefined } from '~/utils/isDefined';

import { SnackBarManagerScopeInternalContext } from '@/ui/feedback/snack-bar-manager/scopes/scope-internal-context/SnackBarManagerScopeInternalContext';
import {
Expand Down Expand Up @@ -29,6 +30,15 @@ export const useSnackBar = () => {
({ set }) =>
(newValue) =>
set(snackBarInternalScopedState({ scopeId }), (prev) => {
if (
isDefined(newValue.dedupeKey) &&
prev.queue.some(
(snackBar) => snackBar.dedupeKey === newValue.dedupeKey,
)
) {
return prev;
}

if (prev.queue.length >= prev.maxQueue) {
return {
...prev,
Expand Down

0 comments on commit 64d25ed

Please sign in to comment.