Skip to content

Commit a5937bd

Browse files
correi-ffrancoisno
authored andcommitted
resolves #106 Cannot parametrize the max number of messages in history
1 parent c54b1e1 commit a5937bd

File tree

6 files changed

+60
-28
lines changed

6 files changed

+60
-28
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ A `TockTheme` can be used as a value of a `ThemeProvider` of [`emotion-theming`]
257257
| `timeoutBetweenMessage` | `number?` | Timeout between message |
258258
| `widgets` | `any?` | Custom display component |
259259
| `disableSse` | `boolean?` | Disable SSE (not even trying) |
260-
| `accessibility` | `TockAccessibility?` | Object for overriding role and label accessibility attributes |
261-
| `localStorage` | `boolean?` | Enable history local storage |
260+
| `accessibility` | `TockAccessibility` | Object for overriding role and label accessibility attributes |
261+
| ~~`localStorage`~~ | ~~`boolean?`~~ | Enable history local storage (@deprecated) |
262+
| `localStorageHistory` | `TockLocalStorage?` | Object for history local storage |
262263

263264
#### `TockAccessibility`
264265

@@ -275,6 +276,13 @@ A `TockTheme` can be used as a value of a `ThemeProvider` of [`emotion-theming`]
275276
| `sendButtonLabel` | `string?` | Message of the send message button image aria-label attribute (overrides 'Send a message') |
276277
| `clearButtonLabel` | `string?` | Message of the clear messages button image aria-label attribute (overrides 'Clear messages') |
277278

279+
#### `TockLocalStorage`
280+
281+
| Property name | Type | Description |
282+
|-----------------------------------|------------------------|-----------------------------------------------------------------------------|
283+
| `enable` | `boolean?` | Enable history local storage |
284+
| `maxNumberMessages` | `number?` | Max number of messages in the history local storage (default 10) |
285+
278286
#### `CarouselAccessibility`
279287

280288
| Property name | Type | Description |
@@ -313,12 +321,12 @@ renderChat(
313321
);
314322
```
315323

316-
#### Local storage
324+
#### Local storage history
317325

318-
The optional `localStorage` makes it possible to provide a history session of messages.
326+
The optional `localStorageHistory` makes it possible to provide a history session of messages.
319327
This history loads at the creation of the chat and is stored in the locale storage of the browser.
320328

321-
The `localStorage` parameter is a boolean, by default set to false.
329+
The `localStorageHistory` parameter is an object, by default not set (enable then to false).
322330

323331
Example:
324332

@@ -328,7 +336,11 @@ renderChat(
328336
'<TOCK_BOT_API_URL>',
329337
undefined,
330338
{},
331-
{ localStorage: true },
339+
{ localStorageHistory: {
340+
enable: true,
341+
maxNumberMessages: 15, // by default 10 messages max
342+
}
343+
},
332344
);
333345
```
334346

src/TockLocalStorage.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface TockLocalStorage {
2+
enable?: boolean;
3+
maxNumberMessages?: number;
4+
}
5+
6+
export default TockLocalStorage;

src/TockOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import TockAccessibility from 'TockAccessibility';
2+
import TockLocalStorage from 'TockLocalStorage';
23

34
export interface TockOptions {
45
// An initial message to send to the backend to trigger a welcome sequence
@@ -10,7 +11,11 @@ export interface TockOptions {
1011
widgets?: any;
1112
disableSse?: boolean;
1213
accessibility?: TockAccessibility;
14+
/**
15+
* @deprecated since version 22.3.1
16+
*/
1317
localStorage?: boolean;
18+
localStorageHistory?: TockLocalStorage;
1419
}
1520

1621
export default TockOptions;

src/components/Chat/Chat.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ChatInput from '../ChatInput';
44
import Container from '../Container';
55
import Conversation from '../Conversation';
66
import TockAccessibility from '../../TockAccessibility';
7+
import TockLocalStorage from 'TockLocalStorage';
78

89
export interface ChatProps {
910
endPoint: string;
@@ -14,7 +15,7 @@ export interface ChatProps {
1415
extraHeadersProvider?: () => Promise<Record<string, string>>;
1516
disableSse?: boolean;
1617
accessibility?: TockAccessibility;
17-
localStorage?: boolean;
18+
localStorageHistory?: TockLocalStorage;
1819
}
1920

2021
const Chat: (props: ChatProps) => JSX.Element = ({
@@ -26,7 +27,7 @@ const Chat: (props: ChatProps) => JSX.Element = ({
2627
extraHeadersProvider = undefined,
2728
disableSse = false,
2829
accessibility = {},
29-
localStorage = false,
30+
localStorageHistory = {},
3031
}: ChatProps) => {
3132
const {
3233
messages,
@@ -45,7 +46,7 @@ const Chat: (props: ChatProps) => JSX.Element = ({
4546
endPoint,
4647
extraHeadersProvider,
4748
disableSse,
48-
localStorage,
49+
localStorageHistory,
4950
);
5051

5152
useEffect(() => {
@@ -54,15 +55,14 @@ const Chat: (props: ChatProps) => JSX.Element = ({
5455
if (referralParameter) {
5556
sendReferralParameter(referralParameter);
5657
}
57-
const history = window.localStorage.getItem('tockMessageHistory');
58-
if (
59-
messages.length === 0 &&
60-
openingMessage &&
61-
(localStorage === false || !history)
62-
) {
58+
const history =
59+
localStorageHistory?.enable == true
60+
? window.localStorage.getItem('tockMessageHistory')
61+
: undefined;
62+
if (messages.length === 0 && openingMessage && !history) {
6363
sendOpeningMessage(openingMessage);
6464
}
65-
if (localStorage === true && history) {
65+
if (history) {
6666
addHistory(
6767
JSON.parse(history),
6868
JSON.parse(

src/renderChat.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ export const renderChat: (
3434
extraHeadersProvider={options.extraHeadersProvider}
3535
disableSse={options.disableSse}
3636
accessibility={options.accessibility}
37-
localStorage={
38-
options.localStorage && storageAvailable('localStorage')
39-
}
37+
{...(storageAvailable('localStorage') && {
38+
localStorageHistory: {
39+
enable:
40+
options.localStorage ||
41+
options.localStorageHistory?.enable === true,
42+
maxNumberMessages: options.localStorageHistory?.maxNumberMessages,
43+
},
44+
})}
4045
/>
4146
</TockContext>
4247
</ThemeProvider>,

src/useTock.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from './TockContext';
2020
import { Sse } from './Sse';
2121
import useLocalTools, { UseLocalTools } from './useLocalTools';
22+
import TockLocalStorage from 'TockLocalStorage';
2223

2324
export interface UseTock {
2425
messages: Message[];
@@ -91,12 +92,12 @@ const useTock: (
9192
tockEndPoint: string,
9293
extraHeadersProvider?: () => Promise<Record<string, string>>,
9394
disableSse?: boolean,
94-
localStorage?: boolean,
95+
localStorageHistory?: TockLocalStorage,
9596
) => UseTock = (
9697
tockEndPoint: string,
9798
extraHeadersProvider?: () => Promise<Record<string, string>>,
9899
disableSse?: boolean,
99-
localStorage?: boolean,
100+
localStorageHistory?: TockLocalStorage,
100101
) => {
101102
const {
102103
messages,
@@ -106,7 +107,9 @@ const useTock: (
106107
sseInitializing,
107108
}: TockState = useTockState();
108109
const dispatch: Dispatch<TockAction> = useTockDispatch();
109-
const { clearMessages }: UseLocalTools = useLocalTools(localStorage);
110+
const { clearMessages }: UseLocalTools = useLocalTools(
111+
localStorageHistory?.enable ?? false,
112+
);
110113

111114
const startLoading: () => void = () => {
112115
dispatch({
@@ -126,13 +129,14 @@ const useTock: (
126129
message: any,
127130
) => {
128131
let history: any = window.localStorage.getItem('tockMessageHistory');
132+
const maxNumberMessages = localStorageHistory?.maxNumberMessages ?? 10;
129133
if (!history) {
130134
history = [];
131135
} else {
132136
history = JSON.parse(history);
133137
}
134-
if (history.length >= 10) {
135-
history.shift();
138+
if (history.length >= maxNumberMessages) {
139+
history.splice(0, history.length - maxNumberMessages + 1);
136140
}
137141
history.push(message);
138142
window.localStorage.setItem('tockMessageHistory', JSON.stringify(history));
@@ -150,7 +154,7 @@ const useTock: (
150154
type: 'SET_QUICKREPLIES',
151155
quickReplies: quickReplies,
152156
});
153-
if (localStorage) {
157+
if (localStorageHistory?.enable ?? false) {
154158
window.localStorage.setItem(
155159
'tockQuickReplyHistory',
156160
JSON.stringify(quickReplies),
@@ -185,7 +189,7 @@ const useTock: (
185189
type: MessageType.carousel,
186190
} as Carousel;
187191
}
188-
if (localStorage) {
192+
if (localStorageHistory?.enable ?? false) {
189193
recordResponseToLocaleSession(message);
190194
}
191195
return message;
@@ -253,7 +257,7 @@ const useTock: (
253257
message,
254258
type: MessageType.message,
255259
} as TextMessage;
256-
if (localStorage) {
260+
if (localStorageHistory?.enable ?? false) {
257261
recordResponseToLocaleSession(messageToDispatch);
258262
}
259263
dispatch({
@@ -314,7 +318,7 @@ const useTock: (
314318
return Promise.resolve();
315319
} else if (button.payload) {
316320
setQuickReplies([]);
317-
if (localStorage) {
321+
if (localStorageHistory?.enable ?? false) {
318322
recordResponseToLocaleSession({
319323
author: 'user',
320324
message: button.label,

0 commit comments

Comments
 (0)