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

feat: adding types for frontChat #25

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions examples/boot/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {boot} from '../../lib/helpers/boot';
import {FrontChatCmdType} from '../../lib/types/front-chat-types';

/*
* Constants.
Expand All @@ -17,9 +18,9 @@ boot(document.body)
// option to the 'init' command. The below callback is just an example of waiting for the chat widget to
// be initialized, before performing another action.
const onInitCompleted = () => {
frontChat('show');
frontChat(FrontChatCmdType.Show);
};

frontChat('init', {chatId, onInitCompleted});
frontChat(FrontChatCmdType.Init, {chatId, onInitCompleted});
})
.catch(console.error);
4 changes: 3 additions & 1 deletion examples/react-embed-front-chat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {useEffect, useRef} from 'react';

import {FrontChatCmdType} from '../../lib/types/front-chat-types';

/*
* Constants.
*/
Expand Down Expand Up @@ -33,7 +35,7 @@ export function App() {
scriptTag.setAttribute('type', 'text/javascript');
scriptTag.setAttribute('src', scriptSrc);
scriptTag.onload = () => {
iframe.contentWindow?.FrontChat?.('init', {
iframe.contentWindow?.FrontChat?.(FrontChatCmdType.Init, {
chatId,
useDefaultLauncher: false,
shouldShowWindowOnLaunch: true,
Expand Down
3 changes: 2 additions & 1 deletion examples/react-use-front-chat-boot/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {useEffect, useState} from 'react';

import {useFrontChatBoot} from '../../lib/hooks/use-front-chat-boot';
import {FrontChatCmdType} from '../../lib/types/front-chat-types';

/*
* Constants.
Expand Down Expand Up @@ -34,7 +35,7 @@ export function App() {
return;
}

frontChat('show');
frontChat(FrontChatCmdType.Show);
setIsWindowVisible;
}, [frontChat, isInitialized, isWindowVisible]);

Expand Down
3 changes: 2 additions & 1 deletion examples/react-verified-user/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {createHmac} from 'crypto';
import {useEffect, useState} from 'react';

import {useFrontChatBoot} from '../../lib/hooks/use-front-chat-boot';
import {FrontChatCmdType} from '../../lib/types/front-chat-types';

/*
* Constants.
Expand Down Expand Up @@ -52,7 +53,7 @@ export function App() {
return;
}

frontChat('show');
frontChat(FrontChatCmdType.Show);
setIsWindowVisible;
}, [frontChat, isInitialized, isWindowVisible]);

Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/initialize/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {FrontChatCmdType} from '../../types/front-chat-types';
import {boot} from '../boot';

/*
Expand All @@ -7,5 +8,5 @@ import {boot} from '../boot';
export async function initialize(chatId: string, element?: HTMLElement) {
const frontChat = await boot(element);

frontChat('init', {chatId});
frontChat(FrontChatCmdType.Init, {chatId});
}
13 changes: 9 additions & 4 deletions lib/hooks/use-front-chat-boot/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {useEffect, useRef, useState} from 'react';

import {boot} from '../../helpers/boot';
import {type FrontChat, type FrontChatOptions, type FrontChatParams} from '../../types/front-chat-types';
import {
type FrontChat,
FrontChatCmdType,
type FrontChatOptions,
type FrontChatParams
} from '../../types/front-chat-types';

/*
* Types.
Expand Down Expand Up @@ -53,15 +58,15 @@ export function useFrontChatBoot(element?: HTMLElement, options?: FrontChatOptio
return;
}

if (cmdType === 'init') {
if (cmdType === FrontChatCmdType.Init) {
const onInitCompleted = () => {
setStatus(FrontChatStatusesEnum.INITIALIZED);
};

return window.FrontChat(cmdType, {...params, onInitCompleted});
}

if (cmdType === 'shutdown') {
if (cmdType === FrontChatCmdType.Shutdown) {
setStatus(FrontChatStatusesEnum.READY);
}

Expand All @@ -73,7 +78,7 @@ export function useFrontChatBoot(element?: HTMLElement, options?: FrontChatOptio
return;
}

frontChat('init', params);
frontChat(FrontChatCmdType.Init, params);
};

return {
Expand Down
26 changes: 24 additions & 2 deletions lib/types/front-chat-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,33 @@ declare global {
* Types.
*/

export enum FrontChatCommand {
Init = 'init',
Show = 'show',
Hide = 'hide',
Shutdown = 'shutdown'
}

export type FrontChatParams = {
chatId?: string;
userId?: string;
userHash?: string;
shouldShowWindowOnLaunch?: boolean;
shouldExpandOnShowWindow?: boolean;
shouldHideCloseButton?: boolean;
shouldHideExpandButton?: boolean;
welcomeMessageAppearance?: 'hidden' | 'always';
isMobileWebview?: boolean;
useDefaultLauncher?: boolean;
onInitCompleted?: () => void;
// Allow any other key with a value of string, boolean, object, or undefined
[key: string]: string | boolean | object | undefined;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be number as well? If so, maybe unknown is a better fit.

Suggested change
[key: string]: string | boolean | object | undefined;
[key: string]: unknown;

};

export interface FrontChatOptions {
nonce?: string;
}

type UnbindHandler = () => void;

export type FrontChatParams = Record<string, string | boolean | object>;
export type FrontChat = (cmdType: string, params?: FrontChatParams) => UnbindHandler | undefined;
export type FrontChat = (cmdType: FrontChatCmdType, params?: FrontChatParams) => UnbindHandler | undefined;