Coder Social home page Coder Social logo

ukjinjang / react-channel-plugin Goto Github PK

View Code? Open in Web Editor NEW
33.0 1.0 9.0 1.33 MB

Channel IO plugin wrapper for React

Home Page: https://ukjinjang.github.io/react-channel-plugin/

License: MIT License

JavaScript 6.14% TypeScript 91.24% HTML 2.62%
react react-hook react-channel-plugin channel-io channel-talk react-hooks typescript

react-channel-plugin's Introduction

Logo

react-channel-plugin

npm CircleCI codecov Known Vulnerabilities minzip dependency-count tree-shaking npm-download license

Channel IO (Channel Talk) plugin wrapper for React.

If you want to use Channel IO plugin without React, please refer channel-web-sdk-loader.

Installation

$ yarn add react-channel-plugin

Getting started

Example code of react channel plugin.

import React from 'react';
import {
  ReactChannelIO,
  useChannelIOApi,
  useChannelIOEvent,
} from 'react-channel-plugin';
import { CHANNEL_ID_PLUGIN_KEY } from './config';

const App = () => {
  return (
    <ReactChannelIO pluginKey={CHANNEL_ID_PLUGIN_KEY} language="en" autoBoot>
      <AppPage />
    </ReactChannelIO>
  );
};

const AppPage = () => {
  const { showMessenger } = useChannelIOApi();

  useChannelIOEvent('onShowMessenger', () => {
    console.log('Messenger opened!');
  });

  return (
    <button onClick={showMessenger}>
      <span>Open</span>
    </button>
  );
};

API

React provider and hooks for Channel IO API.

ReactChannelIO

<ReactChannelIO /> is React Context provider, which will provides context (APIs and event listeners) to react-channel-plugin hooks. Also it receives Channel IO plugin options and initialize Channel IO instance. Make sure place <ReactChannelIO /> upper than hooks used at your app.

Props

/**
 * Please refer ChannelIO offical docs.
 * - ref: https://developers.channel.io/docs/web-boot-option
 */
type ChannelIOBootOption = {};

interface ReactChannelIOProps extends ChannelIOBootOption {
  /**
   * Indicates whether ChannelIO should be automatically booted or not.
   * If `true` no need to call `boot` manually.
   *
   * - Default: `false`
   */
  autoBoot?: boolean;

  /**
   * Timeout before call `boot`.
   * Only work when `autoBoot` set as `true`.
   *
   * - Default: `1000`
   */
  autoBootTimeout?: number;

  /**
   * Need to reboot channel plugin when boot option changed?
   *
   * - Default: `true`
   */
  rebootOnOptionChanged?: boolean;

  /**
   * Since ChannelIO does not support `customLauncherSelector` after plugin booted,
   * add onClick event listener at element which has `customLauncherSelector`
   * whenever DOM tree mutated. (observed by `MutationObserver`)
   *
   * - Default: `true`
   */
  useCustomLauncherSelectorTweak?: boolean;

  /**
   * Print debug logs via `console.debug`.
   * Set `false` when use plugin at production env.
   */
  verbose?: boolean;

  /**
   * Emitted when channel plugin booted.
   */
  onBoot?: (err?: any, user?: ChannelIOUser) => void;
}

Example

import React from 'react';
import { ReactChannelIO } from 'react-channel-plugin';
import { CHANNEL_ID_PLUGIN_KEY } from './config';

const App = () => {
  const userProfile = { ... };

  return (
    <ReactChannelIO
      pluginKey={CHANNEL_ID_PLUGIN_KEY}
      language="en"
      profile={userProfile}
      autoBoot
      autoBootTimeout={2000}
    >
      <span>Child component of the ReactChannelIO</span>
    </ReactChannelIO>
  );
};

useChannelIOApi

Provides API of Channel IO as React hook. Please refer official docs to see detail description of each API.

  • boot
  • shutdown
  • showMessenger
  • hideMessenger
  • openChat
  • openSupportBot
  • track
  • clearCallbacks
  • updateUser
  • addTags
  • removeTags
  • setPage
  • resetPage
  • showChannelButton
  • hideChannelButton
  • setAppearance

Example

import { useChannelIOApi } from 'react-channel-plugin';

const AppPage = () => {
  const { showMessenger, updateUser } = useChannelIOApi();

  return (
    <>
      <button onClick={showMessenger}>
        <span>Open</span>
      </button>

      <button
        onClick={() => {
          updateUser({
            profile: {
              name: 'John Doe',
              email: '[email protected]',
              mobileNumber: '+821012345678',
            },
          });
        }}
      >
        <span>Update user</span>
      </button>
    </>
  );
};

useChannelIOEvent

Provides event callbacks from Channel IO as React hook. Provide callback method name as first parameter of hook method. Please refer official docs to see detail description of each callback.

  • onShowMessenger
  • onHideMessenger
  • onBadgeChanged
  • onChatCreated
  • onFollowUpChanged
  • onUrlClicked

Example

import { useChannelIOApi } from 'react-channel-plugin';

const AppPage = () => {
  useChannelIOEvent('onShowMessenger', () => {
    console.log('messenger opened!');
  });

  useChannelIOEvent('onFollowUpChanged', profile => {
    console.log('profile updated:', profile);
  });

  return null;
};

Playground

Playground for react-channel-plugin.

https://ukjinjang.github.io/react-channel-plugin

Unit Test

To run unit test components that use react-channel plugin's hook with react-testing-library, pass ReactChannelIO provider to wrapper option of render method.

import '@testing-library/jest-dom/extend-expect';
import { render } from '@testing-library/react';

// ...

render(<ComponentWithChannelHook {...props} />, {
  wrapper: ({ children }) => {
    return (
      <ReactChannelIO
        children={children}
        pluginKey={CHANNEL_ID_PLUGIN_KEY}
        {...pluginProps}
      />
    );
  },
});

Brower compatibility

Browser (last 2 versions)
Google Chrome
MS Edge (Chromium)
Mozilla Firefox
Electron

Issues

react-channel-plugin is a light-weight wrapper of Channel IO JavaScript SDK. Because of this, the issue you're having likely isn't a react-channel-plugin issue, but an issue with Channel IO service itself. So please check it again, before submit new issue.

react-channel-plugin's People

Contributors

dependabot[bot] avatar kotarella1110 avatar snyk-bot avatar ukjinjang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-channel-plugin's Issues

No longer contributing?

As of December 15, There is some big changes to the ChannelTalk API schema.

https://developers.channel.io/docs/upcoming-changes-to-open-api-231215-~

The library does not currently support new versions.
So I'm seeing a lot of warnings right now, and we know you're aware of that.

image

However, we're still seeing pretty high numbers, with 2k weekly downloads.

What do you think about doing more work to keep up with the new version for a lot of developers?
I'd be happy to contribute. I'm here for you.

It showing some depriciated warnings

Hi There,
I used this plugin with the react its working fine and great but showing following console warnings.

ch-plugin-core-20230626145524.js:95 Warning: onBoot API is deprecated and will be removed in a future release. Consider using boot callback instead. See https://developers.channel.io/docs/web-channelio#boot for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onShow API is deprecated and will be removed in a future release. Consider using onShowMessenger callback instead. See https://developers.channel.io/docs/web-channelio#onShowMessenger for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onHide API is deprecated and will be removed in a future release. Consider using onHideMessenger callback instead. See https://developers.channel.io/docs/web-channelio#onHideMessenger for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onChangeBadge API is deprecated and will be removed in a future release. Consider using onBadgeChanged callback instead. See https://developers.channel.io/docs/web-channelio#onBadgeChanged for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onCreateChat API is deprecated and will be removed in a future release. Consider using onChatCreated callback instead. See https://developers.channel.io/docs/web-channelio#onChatCreated for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onProfileChanged API is deprecated and will be removed in a future release. Consider using onFollowUpChanged callback instead. See https://developers.channel.io/docs/web-channelio#onFollowUpChanged for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onChangeProfile API is deprecated and will be removed in a future release. Consider using onFollowUpChanged callback instead. See https://developers.channel.io/docs/web-channelio#onFollowUpChanged for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onClickRedirect API is deprecated and will be removed in a future release. Consider using onUrlClicked callback instead. See https://developers.channel.io/docs/web-channelio#onUrlClicked for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onBoot API is deprecated and will be removed in a future release. Consider using boot callback instead. See https://developers.channel.io/docs/web-channelio#boot for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onShow API is deprecated and will be removed in a future release. Consider using onShowMessenger callback instead. See https://developers.channel.io/docs/web-channelio#onShowMessenger for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onHide API is deprecated and will be removed in a future release. Consider using onHideMessenger callback instead. See https://developers.channel.io/docs/web-channelio#onHideMessenger for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onChangeBadge API is deprecated and will be removed in a future release. Consider using onBadgeChanged callback instead. See https://developers.channel.io/docs/web-channelio#onBadgeChanged for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onCreateChat API is deprecated and will be removed in a future release. Consider using onChatCreated callback instead. See https://developers.channel.io/docs/web-channelio#onChatCreated for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onProfileChanged API is deprecated and will be removed in a future release. Consider using onFollowUpChanged callback instead. See https://developers.channel.io/docs/web-channelio#onFollowUpChanged for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onChangeProfile API is deprecated and will be removed in a future release. Consider using onFollowUpChanged callback instead. See https://developers.channel.io/docs/web-channelio#onFollowUpChanged for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onClickRedirect API is deprecated and will be removed in a future release. Consider using onUrlClicked callback instead. See https://developers.channel.io/docs/web-channelio#onUrlClicked for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core.4f497a3e.vendor.js:169 WebSocket connection to 'wss://1.front-ws.channel.io/socket.io/?EIO=4&transport=websocket' failed: WebSocket is closed before the connection is established. doClose @ ch-plugin-core.4f497a3e.vendor.js:169 index.js:63 Array(25) installHook.js:342 Array(25) ch-plugin-core-20230626145524.js:95 Warning: onBoot API is deprecated and will be removed in a future release. Consider using boot callback instead. See https://developers.channel.io/docs/web-channelio#boot for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onShow API is deprecated and will be removed in a future release. Consider using onShowMessenger callback instead. See https://developers.channel.io/docs/web-channelio#onShowMessenger for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onHide API is deprecated and will be removed in a future release. Consider using onHideMessenger callback instead. See https://developers.channel.io/docs/web-channelio#onHideMessenger for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onChangeBadge API is deprecated and will be removed in a future release. Consider using onBadgeChanged callback instead. See https://developers.channel.io/docs/web-channelio#onBadgeChanged for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onCreateChat API is deprecated and will be removed in a future release. Consider using onChatCreated callback instead. See https://developers.channel.io/docs/web-channelio#onChatCreated for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onProfileChanged API is deprecated and will be removed in a future release. Consider using onFollowUpChanged callback instead. See https://developers.channel.io/docs/web-channelio#onFollowUpChanged for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onChangeProfile API is deprecated and will be removed in a future release. Consider using onFollowUpChanged callback instead. See https://developers.channel.io/docs/web-channelio#onFollowUpChanged for more information. i @ ch-plugin-core-20230626145524.js:95 ch-plugin-core-20230626145524.js:95 Warning: onClickRedirect API is deprecated and will be removed in a future release. Consider using onUrlClicked callback instead. See https://developers.channel.io/docs/web-channelio#onUrlClicked for more information.

Possible wrong usages of useRef leads inconsistent behavior of the program

The Problem

Throughout this repository, this pattern repeatly used around.

// ...
const someRef = useRef(someProp);

// ... someRef.current is used

useEffect(() => {
  someRef.current = someProp;
}, [someProp]);

When we change someProp for this component, we expect someProp is used and propagated through the component immediately. However using useRef and useEffect breaks this common expectation. Actually propagation of changed someProp requires additional event queue fetching.

This leads several unexpected and inconsistent behavior of the program.

Actual Issue We Found

One example we found ( and it's the actual reason we wrote this issue ) is the fact that rebootOnOptionChanged is not working as expected. (or... it could work in some environment but not guaranteed.)

In ReactChannelIO.tsx, when channelIOBootOption is changed, it triggers re-render of the component and runs several useEffect hooks. These are the hooks executed.

The optionRef Hook

  useEffect(() => {
    optionRef.current = channelIOBootOption;
  }, [channelIOBootOption]);

The reboot hook

  //
  // Re-boot channel plugin when boot option changed.
  //
  useDeepEffect(() => {
    if (isBooted && rebootOnOptionChanged) {
      debugLogger(verbose, 'Rebooting since option has been changed...');
      void boot().catch(() => void 0);
    }
  }, [channelIOBootOption]);

For the reboot to work properly with the changed channelIOBootOption, optionRef Hook must be executed first which is very hard to guarantee. The actual behavior we faced is that the component reboots Channel IO with old options.

Suggestion

We think you can safely remove most of useRef and just use the passed props directly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.