Skip to content

Commit

Permalink
fix: move useLocation within useNotify hook to cater for latest react…
Browse files Browse the repository at this point in the history
…-router-dom limitations

Signed-off-by: Mason Hu <[email protected]>
  • Loading branch information
mas-who committed Jan 19, 2024
1 parent a88e811 commit 6baf889
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
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;
}

0 comments on commit 6baf889

Please sign in to comment.