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

fix: move useLocation within useNotify hook to cater for latest react-router-dom limitations #1017

Merged
merged 1 commit into from
Jan 19, 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
26 changes: 15 additions & 11 deletions src/components/NotificationProvider/NotificationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const NotifyContext = createContext<NotificationHelper>({
success: () => undefined,
info: () => undefined,
queue: () => undefined,
setDeduplicated: () => undefined,
});

export const NotificationProvider: FC<NotifyProviderProps> = ({ children }) => {
const [notification, setNotification] = useState<NotificationType | null>(
null
);
const { state, pathname } = useLocation() as QueuedNotification;

const clear = () => notification !== null && setNotification(null);

Expand All @@ -41,15 +41,6 @@ export const NotificationProvider: FC<NotifyProviderProps> = ({ children }) => {
return value;
};

useEffect(() => {
if (state?.queuedNotification) {
setDeduplicated(state.queuedNotification);
window.history.replaceState({}, "");
} else {
clear();
}
}, [state, pathname]);

const helper: NotificationHelper = {
notification,
clear,
Expand All @@ -58,6 +49,7 @@ export const NotificationProvider: FC<NotifyProviderProps> = ({ children }) => {
setDeduplicated(failure(title, error, message, actions)),
info: (message, title) => setDeduplicated(info(message, title)),
success: (message) => setDeduplicated(success(message)),
setDeduplicated,
};

return (
Expand All @@ -66,7 +58,19 @@ export const NotificationProvider: FC<NotifyProviderProps> = ({ children }) => {
};

export function useNotify() {
return useContext(NotifyContext);
const ctx = useContext(NotifyContext);
const { state, pathname } = useLocation() as QueuedNotification;

useEffect(() => {
if (state?.queuedNotification) {
ctx.setDeduplicated(state.queuedNotification);
window.history.replaceState({}, "");
} else {
ctx.clear();
}
}, [state, pathname]);

return ctx;
}

export const NotificationConsumer: FC = () => {
Expand Down
1 change: 1 addition & 0 deletions src/components/NotificationProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export interface NotificationHelper {
info: (message: ReactNode, title?: string) => NotificationType;
success: (message: ReactNode) => NotificationType;
queue: (notification: NotificationType) => QueuedNotification;
setDeduplicated: (value: NotificationType) => NotificationType;
}
Loading