-
Notifications
You must be signed in to change notification settings - Fork 357
feat: add toast component to sample app to handle client notifications #3204
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
examples/SampleApp/src/components/ToastComponent/Toast.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { Dimensions, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; | ||
| import { useToastState } from '../../hooks/useToastState'; | ||
| import Animated, { Easing, SlideInDown, SlideOutDown } from 'react-native-reanimated'; | ||
| import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
| import { useTheme } from 'stream-chat-react-native'; | ||
| import type { Notification } from 'stream-chat'; | ||
|
|
||
| const { width } = Dimensions.get('window'); | ||
|
|
||
| const severityIconMap: Record<Notification['severity'], string> = { | ||
| error: '❌', | ||
| success: '✅', | ||
| warning: '⚠️', | ||
| info: 'ℹ️', | ||
| }; | ||
|
|
||
| export const Toast = () => { | ||
| const { closeToast, notifications } = useToastState(); | ||
| const { top } = useSafeAreaInsets(); | ||
| const { | ||
| theme: { | ||
| colors: { overlay, white_smoke }, | ||
| }, | ||
| } = useTheme(); | ||
|
|
||
| return ( | ||
| <SafeAreaView style={[styles.container, { top }]} pointerEvents='box-none'> | ||
| {notifications.map((notification) => ( | ||
| <Animated.View | ||
| key={notification.id} | ||
| entering={SlideInDown.easing(Easing.bezierFn(0.25, 0.1, 0.25, 1.0))} | ||
| exiting={SlideOutDown} | ||
| style={[styles.toast, { backgroundColor: overlay }]} | ||
| > | ||
| <View style={[styles.icon, { backgroundColor: overlay }]}> | ||
| <Text style={[styles.iconText, { color: white_smoke }]}> | ||
| {severityIconMap[notification.severity]} | ||
| </Text> | ||
| </View> | ||
| <View style={styles.content}> | ||
| <Text style={[styles.message, { color: white_smoke }]}>{notification.message}</Text> | ||
| </View> | ||
| <TouchableOpacity onPress={() => closeToast(notification.id)}> | ||
| <Text style={[styles.close, { color: white_smoke }]}>✕</Text> | ||
| </TouchableOpacity> | ||
| </Animated.View> | ||
| ))} | ||
| </SafeAreaView> | ||
| ); | ||
| }; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| position: 'absolute', | ||
| right: 16, | ||
| left: 16, | ||
| alignItems: 'flex-end', | ||
| }, | ||
| toast: { | ||
| width: width * 0.9, | ||
| borderRadius: 12, | ||
| padding: 12, | ||
| marginBottom: 8, | ||
| flexDirection: 'row', | ||
| justifyContent: 'space-between', | ||
| alignItems: 'center', | ||
| shadowColor: '#000', | ||
| shadowOpacity: 0.2, | ||
| shadowRadius: 4, | ||
| elevation: 5, | ||
| }, | ||
| content: { | ||
| flex: 1, | ||
| marginHorizontal: 8, | ||
| }, | ||
| message: { | ||
| fontSize: 14, | ||
| fontWeight: '600', | ||
| }, | ||
| close: { | ||
| fontSize: 16, | ||
| }, | ||
| icon: { | ||
| width: 20, | ||
| height: 20, | ||
| borderRadius: 12, | ||
| justifyContent: 'center', | ||
| alignItems: 'center', | ||
| }, | ||
| iconText: { | ||
| fontWeight: 'bold', | ||
| includeFontPadding: false, | ||
| }, | ||
| warning: { | ||
| backgroundColor: 'yellow', | ||
| }, | ||
| }); |
39 changes: 39 additions & 0 deletions
39
examples/SampleApp/src/hooks/useClientNotificationsToastHandler.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { Notification } from 'stream-chat'; | ||
| import { useClientNotifications } from 'stream-chat-react-native'; | ||
|
|
||
| import { useEffect, useMemo, useRef } from 'react'; | ||
| import { useToastState } from './useToastState'; | ||
|
|
||
| export const usePreviousNotifications = (notifications: Notification[]) => { | ||
| const prevNotifications = useRef<Notification[]>(notifications); | ||
|
|
||
| const difference = useMemo(() => { | ||
| const prevIds = new Set(prevNotifications.current.map((notification) => notification.id)); | ||
| const newIds = new Set(notifications.map((notification) => notification.id)); | ||
| return { | ||
| added: notifications.filter((notification) => !prevIds.has(notification.id)), | ||
| removed: prevNotifications.current.filter((notification) => !newIds.has(notification.id)), | ||
| }; | ||
| }, [notifications]); | ||
|
|
||
| useEffect(() => { | ||
| prevNotifications.current = notifications; | ||
| }, [notifications]); | ||
|
|
||
| return difference; | ||
| }; | ||
|
|
||
| /** | ||
| * This hook is used to open and close the toast notifications when the notifications are added or removed. | ||
| * @returns {void} | ||
| */ | ||
| export const useClientNotificationsToastHandler = () => { | ||
| const { notifications } = useClientNotifications(); | ||
| const { openToast, closeToast } = useToastState(); | ||
| const { added, removed } = usePreviousNotifications(notifications); | ||
|
|
||
| useEffect(() => { | ||
| added.forEach(openToast); | ||
| removed.forEach((notification) => closeToast(notification.id)); | ||
| }, [added, closeToast, openToast, removed]); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { Notification } from 'stream-chat'; | ||
| import { useStableCallback, useStateStore } from 'stream-chat-react-native'; | ||
| import type { ToastState } from '../store/toast-store'; | ||
| import { toastStore, openToast, closeToast } from '../store/toast-store'; | ||
|
|
||
| const selector = ({ notifications }: ToastState) => ({ | ||
| notifications, | ||
| }); | ||
|
|
||
| export const useToastState = () => { | ||
| const { notifications } = useStateStore(toastStore, selector); | ||
|
|
||
| const openToastInternal = useStableCallback((notificationData: Notification) => { | ||
| openToast(notificationData); | ||
| }); | ||
|
|
||
| const closeToastInternal = useStableCallback((id: string) => { | ||
| closeToast(id); | ||
| }); | ||
|
|
||
| return { notifications, openToast: openToastInternal, closeToast: closeToastInternal }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { Notification, StateStore } from 'stream-chat'; | ||
|
|
||
| export type ToastState = { | ||
| notifications: Notification[]; | ||
| }; | ||
|
|
||
| const INITIAL_STATE: ToastState = { | ||
| notifications: [], | ||
| }; | ||
|
|
||
| export const toastStore = new StateStore<ToastState>(INITIAL_STATE); | ||
|
|
||
| export const openToast = (notification: Notification) => { | ||
| if (!notification.id) { | ||
| console.warn('Notification must have an id to be opened!'); | ||
| return; | ||
| } | ||
| const { notifications } = toastStore.getLatestValue(); | ||
|
|
||
| // Prevent duplicate notifications | ||
| if (notifications.some((n) => n.id === notification.id)) { | ||
| console.warn('Notification with the same id already exists!'); | ||
| return; | ||
| } | ||
|
|
||
| toastStore.partialNext({ | ||
| notifications: [...notifications, notification], | ||
| }); | ||
| }; | ||
|
|
||
| export const closeToast = (id: string) => { | ||
| if (!id) { | ||
| console.warn('Notification id is required to be closed!'); | ||
| return; | ||
| } | ||
| const { notifications } = toastStore.getLatestValue(); | ||
| toastStore.partialNext({ | ||
| notifications: notifications.filter((notification) => notification.id !== id), | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { NotificationManagerState } from 'stream-chat'; | ||
|
|
||
| import { useStateStore } from './useStateStore'; | ||
|
|
||
| import { useChatContext } from '../contexts/chatContext/ChatContext'; | ||
|
|
||
| const selector = (state: NotificationManagerState) => ({ | ||
| notifications: state.notifications, | ||
| }); | ||
|
|
||
| /** | ||
| * This hook is used to get the notifications from the client. | ||
| * @returns {Object} - An object containing the notifications. | ||
| * @returns {Notification[]} notifications - The notifications. | ||
| */ | ||
| export const useClientNotifications = () => { | ||
| const { client } = useChatContext(); | ||
| const { notifications } = useStateStore(client.notifications.store, selector); | ||
|
|
||
| return { notifications }; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.