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

Add ability to use a React Node as useNotify message #8580

Merged
merged 2 commits into from
Jan 16, 2023
Merged

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Jan 16, 2023

You may want to notification message that contains HTML or other React components. To do so, you can pass a React node as the first argument of the notify function.

This allows e.g. using MUI's <Alert> component to display a notification with a custom icon, color, or action.

Capture d’écran 2023-01-16 à 11 31 59

import { useSubscribe } from "@react-admin/ra-realtime";
import { useNotify, useDataProvider } from "react-admin";
import { Alert } from "@mui/material";

export const ConnectionWatcher = () => {
  const notify = useNotify();
  const dataProvider = useDataProvider();
  useSubscribe("connectedUsers", (event) => {
    if (event.type === "connected") {
      dataProvider
        .getOne("agents", { id: event.payload.agentId })
        .then(({ data }) => {
          notify(
            <Alert severity="info">
                Agent ${data.firstName} ${data.lastName} just logged in
            </Alert>
            );
        });
    }
    if (event.type === "disconnected") {
      dataProvider
        .getOne("agents", { id: event.payload.agentId })
        .then(({ data }) => {
          notify(
            <Alert severity="info">
                Agent ${data.firstName} ${data.lastName} just logged out
            </Alert>
          );
        });
    }
  });
  return null;
};

Note that if you use this ability to pass a React node, the message will not be translated - you'll have to translate it yourself using useTranslate.

@djhi djhi added this to the 4.8.0 milestone Jan 16, 2023
@djhi djhi merged commit a09f0a2 into next Jan 16, 2023
@djhi djhi deleted the useNotify-node branch January 16, 2023 11:26
@sidescraper
Copy link

Interesting example!
But ra-realtime is an enterprise feature. Can this also be used in open react-admin?
thanks for a hint

@fzaninotto
Copy link
Member Author

yes, this is a new capacity added to useNotify, which is part of the open-source core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants