@magicbell/react-headless
Version:
Hooks to build a notification inbox
36 lines (30 loc) • 1.03 kB
text/typescript
import { uniqBy } from 'ramda';
import INotificationStore from '../../../types/INotificationStore.js';
type Props = Omit<INotificationStore, 'context'>;
type Options = {
reset: boolean;
prepend: boolean;
};
/**
* Function to set props on a notification store. The notifications are merged
* unless `options.reset` is true.
*
* @param store Notifications store
* @param props Props to set to the store
* @param options.reset Do not prepend notifications to the store
* @returns Store
*/
export default function setStoreProps(
store: INotificationStore,
props: Props,
options: Partial<Options> = { reset: false },
): INotificationStore {
const { notifications = [], ...meta } = props;
const allNotifications = options.reset
? notifications
: uniqBy(
(notification) => notification.id,
options.prepend ? [...notifications, ...store.notifications] : [...store.notifications, ...notifications],
);
return { context: store.context, notifications: allNotifications, ...meta };
}