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

[RFC] Global notifications #4630

Open
Janpot opened this issue Jan 23, 2025 · 0 comments · May be fixed by #4628
Open

[RFC] Global notifications #4630

Janpot opened this issue Jan 23, 2025 · 0 comments · May be fixed by #4628
Labels
RFC Request For Comments

Comments

@Janpot
Copy link
Member

Janpot commented Jan 23, 2025

Which problem do we want to solve

How do we solve the problem

API

We maintain API parity for the existing solution but manage the state outside of react. a Global api to create a notifications instance that includes open / close methods, and a provider that must be rendered by the user. The user can create their own instance, or use a global one we also provide.

interface NotificationsApi {
  // Original methods
  open(...)
  close(...)
  Provider: React.ComponentType<{}>
}

// Allow users creating their own instance
export function createNotifications(options): NotificationsApi;

// Provide global default instance
const { open, close, Provider } = createNotifications()
export { open, close, Provider }

// backwards compatibility
export function useNotifications() {
  return { open, close }
}

User can then use it in their zustand store as such:

import { create } from 'zustand'
import * as notifications from '@toolpad/core/notifications'

const useStore = create((set) => ({
  bears: 0,
  increasePopulation: () => {
    set((state) => ({ bears: state.bears + 1 }))
    notifications.show('One more bear', {})
  },
  removeAllBears: () => set({ bears: 0 }),
  updateBears: (newBears) => set({ bears: newBears }),
}))

Edge-case

When no UI is rendered, notifications are queued. As soon as UI is rendered queued notifications are shown. It’s the responsibility of the user to have the provider rendered. If it turns out problematic we can consider adding a warning in dev if we detect:

  • The UI has unmounted and notifications are enqueue
  • The UI has never mounted and notifications have been enqueued for longer than X milliseconds.

but let's await user feedback first.

Implementation

#4628

@Janpot Janpot added RFC Request For Comments status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 23, 2025
@Janpot Janpot linked a pull request Jan 23, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request For Comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant