@kustomer/chat-react-native
Version:
Kustomer Chat SDK wrapper for React Native
157 lines (156 loc) • 5.97 kB
JavaScript
;
import { NativeModules, Platform } from 'react-native';
import { KustomerDisplayMode } from './types';
function getLinkingErrorText(moduleName) {
return `The package '@kustomer/chat-react-native' doesn't seem to be linked.\n` + `Looking for module '${moduleName}'. Make sure: \n\n` + Platform.select({
ios: "- You have run 'pod install'\n",
default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
}
function getLinkingErrorProxy(moduleName) {
return new Proxy({}, {
get() {
// throw new Error(getLinkingErrorText(moduleName));
console.error(new Error(getLinkingErrorText(moduleName)));
return null;
}
});
}
const RNKustomerChatProvider = NativeModules.RNKustomerChatProvider ?? getLinkingErrorProxy('RNKustomerChatProvider');
export const KustomerChatProvider = RNKustomerChatProvider;
const RNKustomerAuthProvider = NativeModules.RNKustomerAuthProvider ?? getLinkingErrorProxy('RNKustomerAuthProvider');
export const KustomerAuthProvider = RNKustomerAuthProvider;
const RNKustomerPushProvider = NativeModules.RNKustomerPushProvider ?? getLinkingErrorProxy('RNKustomerPushProvider');
export const KustomerPushProvider = RNKustomerPushProvider;
const RNKustomerKbProvider = NativeModules.RNKustomerKbProvider ?? getLinkingErrorProxy('RNKustomerKbProvider');
export const KustomerKbProvider = RNKustomerKbProvider;
const RNKustomerChat = NativeModules.RNKustomerChat ?? getLinkingErrorProxy('RNKustomerChat');
export const KustomerChat = {
// Explicitly define all methods instead of using spread operator
close(animated = true) {
return RNKustomerChat.close(animated);
},
logOut() {
return RNKustomerChat.logOut();
},
logIn(jwt) {
return RNKustomerChat.logIn(jwt);
},
logOutThenLogIn(jwt) {
return RNKustomerChat.logOutThenLogIn(jwt);
},
isLoggedIn(idType, id) {
return RNKustomerChat.isLoggedIn(idType, id);
},
getStatus() {
return RNKustomerChat.getStatus();
},
getStatusSync() {
return RNKustomerChat.getStatusSync();
},
isVisible() {
return RNKustomerChat.isVisible();
},
isVisibleSync() {
return RNKustomerChat.isVisibleSync();
},
show(displayMode = KustomerDisplayMode.default) {
return RNKustomerChat.show(displayMode);
},
openConversation(conversationId, animated = true) {
return Platform.OS === 'ios' ? RNKustomerChat.openConversation(conversationId, animated) : RNKustomerChat.openConversation(conversationId);
},
startNewConversation(initialMessage, customDescribe, animated = Platform.OS === 'ios') {
return RNKustomerChat.startNewConversation(initialMessage, customDescribe, animated);
},
openChatAssistant(chatAssistantId, startDialog, initialMessages, customDescribe, animated = Platform.OS === 'ios') {
return RNKustomerChat.openChatAssistant(chatAssistantId, startDialog, initialMessages, customDescribe, animated);
},
isConfigured() {
return RNKustomerChat.isConfigured();
},
getConstants() {
return RNKustomerChat.getConstants();
},
changeActiveAssistant(options) {
return RNKustomerChat.changeActiveAssistant(options);
},
changeBusinessSchedule(scheduleId) {
return RNKustomerChat.changeBusinessSchedule(scheduleId);
},
isChatAvailable() {
return RNKustomerChat.isChatAvailable ? RNKustomerChat.isChatAvailable() : RNKustomerChatProvider.isChatAvailable();
},
isChatAvailableSync() {
return RNKustomerChat.isChatAvailableSync();
},
getUnreadCount() {
return RNKustomerChat.getUnreadCount();
},
showKbArticle(articleId) {
return RNKustomerChat.showKbArticle(articleId);
},
showKbCategory(categoryId) {
return RNKustomerChat.showKbCategory(categoryId);
},
start() {
return RNKustomerChat.start();
},
getOpenConversationCount() {
return RNKustomerChat.getOpenConversationCount();
},
unsetPushToken() {
return RNKustomerChat.unsetPushToken();
},
onRemoteMessage(message) {
return RNKustomerChat.onRemoteMessage(message);
},
setPushToken(token) {
return RNKustomerChat.setPushToken(token);
},
configure(apiKey, options = {}) {
return RNKustomerChat.configure(apiKey, options).then(() => {
if (options?.disablePush || options?.skipFCMPushRegistration) return;
try {
const firebaseMessaging = require('@react-native-firebase/messaging').default;
async function setToken(token) {
const v = await Platform.select({
ios: firebaseMessaging().getAPNSToken(),
android: Promise.resolve(token)
});
RNKustomerChat.setPushToken(v);
}
Platform.select({
ios: firebaseMessaging().getAPNSToken(),
android: firebaseMessaging().getToken()
}).then(RNKustomerChat.setPushToken);
firebaseMessaging().onTokenRefresh(setToken);
firebaseMessaging().onMessage(message => {
KustomerChat.onRemoteMessage(message);
});
} catch (e) {
if (options?.disablePush !== true) console.warn('Kustomer: @react-native-firebase/messaging is not installed. Push notifications will not work.');
return;
}
});
},
describeCustomer(attributes, customAttributes) {
const deprecatedProperties = ['emails', 'phones', 'socials'];
for (const key in attributes) {
if (deprecatedProperties.includes(key)) {
console.warn(`Kustomer: The attribute ${key} is deprecated. Please use shared${key.charAt(0).toUpperCase()}${key.slice(1)} instead.`);
}
}
return RNKustomerChatProvider.describeCustomer({
...attributes,
custom: customAttributes
});
},
describeConversation(conversationId, attributes) {
return RNKustomerChatProvider.describeConversation(conversationId, attributes);
},
isKustomerNotification(message) {
return message.data?.name?.includes('kustomer') ?? false;
}
};
//# sourceMappingURL=NativeModules.js.map