1.0.0 • Published 2 months ago

@sirenapp/react-inbox v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Siren React Inbox

Overview

The @sirenapp/react-inbox sdk is a comprehensive and customizable React UI kit for displaying and managing notifications. This documentation provides comprehensive information on how to install, configure, and use the sdk effectively.

1. Installation

You can install the react sdk from npm

npm @sirenapp/react-inbox

or from yarn

yarn @sirenapp/react-inbox

Prerequisites

  • React v16.8+

2. Configuration

2.1 Initialization

Initialize the sdk with user token and recipient id. Wrap the provider around your App's root.

import { SirenProvider } from "@sirenapp/react-inbox";

const config = {
  userToken: "your_user_token",
  recipientId: "your_recipient_id",
};

<SirenProvider config={config}>{/* Your app components */}</SirenProvider>;

2.2 Configure notification inbox

Once the provider is configured, next step is to configure the notification inbox

Inbox is a paginated list view for displaying notifications.

import { SirenInbox } from '@sirenapp/react-inbox';

<SirenInbox />

Props for the notification inbox

Below are optional props available for the inbox component:

PropDescriptionTypeDefault value
themeObject for custom themesTheme{}
titleTitle of the notification inboxstring"Notifications"
loadMoreLabelText shown on the load more componentstring"Load More"
hideHeaderToggle to hide or show the header sectionbooleanfalse
hideClearAllToggle to hide or show the clear all buttonbooleanfalse
hideBadgeToggle to hide or show the badgebooleanfalse
darkModeToggle to enable dark modebooleanfalse
itemsPerFetchNumber of notifications fetch per api request (have a max cap of 50)number20
windowViewOnlyToggle to enable fit-to-screen window or modal viewbooleanfalse
notificationIconOption to use custom notification IconJSX Elementnull
cardPropsProps for customizing the notification cards{ hideAvatar: boolean }{ hideAvatar: false }
customNotificationCardFunction for rendering custom notification cards(notification)=> JSX Elementnull
onNotificationCardClickCustom click handler for notification cards(notification)=> void()=>null
listEmptyComponentCustom component for empty notification listJSX Elementnull
customHeaderCustom header componentJSX Elementnull
customFooterCustom footer componentJSX Elementnull
customLoaderCustom loader componentJSX Elementnull
loadMoreComponentCustom load more componentJSX Elementnull
customErrorWindowCustom error windowJSX Elementnull
onErrorCallback for handling errors(error: SirenErrorType)=> voidnull

3. Customization

3.1 Themes

Here are the available theme options:

type Theme = {
  dark: ThemeProps,
  light: ThemeProps,
};

type ThemeProps = {
  notificationIcon?: {
    size?: number,
  },
  colors?: {
    primaryColor?: string,
    textColor?: string,
    neutralColor?: string,
    borderColor?: string,
    highlightedCardColor?: string,
    dateColor?: string,
    deleteIcon?: string,
    timerIcon?: string,
    clearAllIcon?: string,
    infiniteLoader?: string,
    windowShadowColor?: string
  },
  badgeStyle?: {
    color?: string,
    textColor?: string,
  },
  window?: {
    borderColor?: string,
  },
  windowHeader?: {
    background?: string,
    titleColor?: string,
    headerActionColor?: string,
    borderColor?: string,
  },
  windowContainer?: {
    background?: string,
  },
  notificationCard?: {
    borderColor?: string,
    background?: string,
    titleColor?: string,
    descriptionColor?: string,
  },
  loadMoreButton: {
    color?: string,
    background?: string,
  }
};

3.2 Style options

Here are the custom style options for the notification inbox

Please note that the badgeStyle, window shadow and border props are only applicable for modal view

 type CustomStyle = {
  notificationIcon?: {
    size?: number,
  },
  window?: {
    width?: DimensionValue,
    borderRadius?: number,
  },
  windowHeader?: {
    height?: DimensionValue,
    titleFontWeight?:TextStyle["fontWeight"],
    titleSize?: number,
    titlePadding?: number,
  },
  windowContainer?: {
    padding?: number,
    contentHeight?: DimensionValue,
  },
  notificationCard?: {
    padding?: number,
    borderWidth?: number,
    avatarSize?: number,
    titleFontWeight?: TextStyle["fontWeight"],
    titleSize?: number,
    descriptionSize?: number,
    dateSize?: number,
  },
  loadMoreButton?: {
    fontSize?: number,
    fontWeight?: TextStyle["fontWeight"]
  },
  badgeStyle?: {
    size?: number,
    textSize?: number,
    top?: number;
    left?: number
  },
  deleteIcon?:{
    size?: number
  }
  dateIcon?:{
    size?: number
  }
  clearAllIcon?:{
    size?: number
  }
}

4. Hooks

useSiren is a hook that provides utility functions for modifying notifications.

import { useSiren } from "@sirenapp/react-inbox";

function MyComponent() {
  const {
    markAsRead,
    deleteNotification,
    markAllNotificationsAsReadByDate,
    clearAllNotificationByDate,
    markNotificationsAsViewed,
  } = useSiren();

  function handleMarkAsRead(id) {
    markAsRead(id);
  }

  function handleDeleteNotification(id) {
    deleteNotification(id);
  }

  function handleMarkAllNotificationsAsReadByDate(untilDate) {
    markNotificationsAsReadByDate(untilDate);
  }

  function handleClearAllNotificationByDate(untilDate) {
    deleteNotificationsByDate(untilDate);
  }

  function handleMarkNotificationsAsViewed(untilDate) {
    markNotificationsAsViewed(untilDate);
  }

  return {
    /* Your component logic */
  };
}

useSiren functions

FunctionsParametersTypeDescription
markNotificationsAsReadByDatestartDateISO date stringSets the read status of notifications to true until the given date.
markAsReadidstringSet read status of a notification to true
deleteNotificationidstringDelete a notification by id
deleteNotificationsByDatestartDateISO date stringDelete all notifications until given date
markNotificationsAsViewedstartDateISO date stringSets the viewed status of notifications to true until the given date

5. Error codes

Given below are all possible error codes thrown by sdk.

Error codeDescription
INVALID_TOKENThe token passed in the provider is invalid
INVALID_RECIPIENT_IDThe recipient id passed in the provider is invalid
TOKEN_VERIFICATION_FAILEDVerification of the given tokens has failed
GENERIC_API_ERROROccurrence of an unexpected api error
OUTSIDE_SIREN_CONTEXTAttempting to invoke the functions outside the siren inbox context
MISSING_PARAMETERThe required parameter is missing

Example

Here's a basic example to help you get started

import React from 'react';
import {SirenInbox,SirenProvider} from '@sirenapp/react-inbox';

function App(): React.JSX.Element {

  const config = {
    userToken: 'your_user_token',
    recipientId: 'your_recipient_id',
  };

  return (
    <SirenProvider config={config}>
      <MyContainer />
    </SirenProvider>
  );
}

export default App;

export function MyContainer(): React.JSX.Element {

  return (
    <div>
      <SirenInbox
        title="Notifications"
        hideHeader={false}
        darkMode={false}
        cardProps={{hideAvatar: false}}
      />
    </div>
  );
}
1.0.0

2 months ago