Skip to content
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

refactor: convert some Livechat files to TS #30425

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/livechat/src/definitions/agents.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Agent = { name: string; username: string; status?: string; [key: string]: unknown };
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import i18next from 'i18next';

import { getDateFnsLocale } from './locale';

export const normalizeAgent = (agentData) => agentData && { name: agentData.name, username: agentData.username, status: agentData.status };
export const normalizeAgent = (agentData: Agent) =>
agentData && { name: agentData.name, username: agentData.username, status: agentData.status };

export const normalizeQueueAlert = async (queueInfo) => {
export const normalizeQueueAlert = async (queueInfo: { spot: number; estimatedWaitTimeSeconds: number }) => {
if (!queueInfo) {
return;
}
const formatDistance = await import('date-fns/formatDistance');
const { default: formatDistance } = await import('date-fns/formatDistance');
const { spot, estimatedWaitTimeSeconds } = queueInfo;
const locale = getDateFnsLocale();
const locale = await getDateFnsLocale();
const estimatedWaitTime =
estimatedWaitTimeSeconds && formatDistance(new Date().setSeconds(estimatedWaitTimeSeconds), new Date(), { locale });
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import store from '../store';
* To normalize Language String and return language code
* @param {String} languageString
*/
export const normalizeLanguageString = (languageString) => {
export const normalizeLanguageString = (languageString: string) => {
let [languageCode, countryCode] = languageString.split ? languageString.split(/[-_]/) : [];
if (!languageCode || languageCode.length !== 2) {
return 'en';
}
languageCode = languageCode.toLowerCase();

if (!countryCode || countryCode.length !== 2) {
countryCode = null;
countryCode = '';
} else {
countryCode = countryCode.toUpperCase();
}
Expand All @@ -23,7 +23,7 @@ export const normalizeLanguageString = (languageString) => {
/**
* To get browser Language of user
*/
export const browserLanguage = () => navigator.userLanguage || navigator.language;
export const browserLanguage = () => (navigator as any).userLanguage || navigator.language;

/**
* This is configured langauge
Expand Down Expand Up @@ -101,6 +101,5 @@ export const getDateFnsLocale = () => {
fullLanguage = fullLanguage.toLowerCase();
const [languageCode] = fullLanguage.split ? fullLanguage.split(/[-_]/) : [];
const locale = [fullLanguage, languageCode, 'en-US'].find((lng) => supportedLocales.indexOf(lng) > -1);
// eslint-disable-next-line import/no-dynamic-require
return import(`date-fns/locale/${locale}/index.js`);
};
3 changes: 3 additions & 0 deletions packages/livechat/src/store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type StoreState = {
showConnecting?: any;
limitTextLength?: any;
displayOfflineForm?: boolean;
language?: string;
};
online?: boolean;
departments: Department[];
Expand All @@ -42,6 +43,7 @@ type StoreState = {
guest: any;
theme: any;
visible: boolean;
language?: string;
};
gdpr: {
accepted: boolean;
Expand All @@ -66,6 +68,7 @@ type StoreState = {
lastReadMessageId?: any;
triggerAgent?: any;
queueInfo?: any;
connecting?: boolean;
};

export const initialState = (): StoreState => ({
Expand Down
Loading