Skip to content

Commit 886db81

Browse files
authored
resolves #190: add historyMaxAge parameter (#191)
* resolves #190: add historyMaxAge parameter * clean up type conversion * fix comparison
1 parent 8422a3a commit 886db81

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,12 @@ Objects implementing this interface can be passed to `renderChat` or to `TockCon
352352

353353
#### `LocalStorageSettings`
354354

355-
| Property name | Type | Description |
356-
|------------------------|------------|---------------------------------------------------------------------------------------------------------------------------------------------|
357-
| `enableMessageHistory` | `boolean?` | If set to `true`, the most recent messages of a conversation will be persisted in the local storage. Defaults to `false`. |
358-
| `maxMessageCount` | `number?` | When message history is enabled, sets the max number of messages to store. Defaults to 10. |
359-
| `prefix` | `string?` | Prefix for local storage keys allowing communication with different bots from the same domain (used for both `userId` and message history). |
355+
| Property name | Type | Description |
356+
|------------------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
357+
| `enableMessageHistory` | `boolean?` | If set to `true`, the most recent messages of a conversation will be persisted in the local storage. Defaults to `false`. |
358+
| `historyMaxAge` | `number?` | If set to a positive value, represents the number of seconds before the message history is cleared (the timeout is reset after each message received). |
359+
| `maxMessageCount` | `number?` | When message history is enabled, sets the max number of messages to store. Defaults to 10. |
360+
| `prefix` | `string?` | Prefix for local storage keys allowing communication with different bots from the same domain (used for both `userId` and message history). |
360361

361362
#### `NetworkSettings`
362363

src/settings/TockSettings.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface LocalStorageSettings {
66
prefix: string;
77
enableMessageHistory: boolean;
88
maxMessageCount: number;
9+
historyMaxAge: number;
910
}
1011

1112
export interface NetworkSettings {
@@ -28,6 +29,7 @@ export const defaultSettings: TockSettings = {
2829
prefix: '',
2930
enableMessageHistory: false,
3031
maxMessageCount: 10,
32+
historyMaxAge: -1,
3133
},
3234
network: {
3335
disableSse: false,

src/useTock.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ export const useTock0: (
193193
localStoragePrefix,
194194
'tockMessageHistory',
195195
);
196+
const messageHistoryLastTime = retrievePrefixedLocalStorageKey(
197+
localStoragePrefix,
198+
'tockLastMessageTimestamp',
199+
);
196200

197201
const savedHistory = window.localStorage.getItem(messageHistoryLSKeyName);
198202
let history: Message[];
@@ -209,6 +213,7 @@ export const useTock0: (
209213
messageHistoryLSKeyName,
210214
JSON.stringify(history),
211215
);
216+
window.localStorage.setItem(messageHistoryLastTime, '' + Date.now());
212217
},
213218
[localStoragePrefix, localStorageMaxMessages],
214219
);
@@ -668,13 +673,30 @@ export const useTock0: (
668673
localStoragePrefix,
669674
'tockHandledResponses',
670675
);
676+
const messageHistoryLastTimeKey = retrievePrefixedLocalStorageKey(
677+
localStoragePrefix,
678+
'tockLastMessageTimestamp',
679+
);
671680

672681
const serializedHistory =
673682
storageAvailable('localStorage') && localStorageEnabled
674683
? window.localStorage.getItem(messageHistoryLSKey)
675684
: undefined;
676685

677686
if (serializedHistory) {
687+
const historyMaxAge = localStorageSettings.historyMaxAge;
688+
if (historyMaxAge > 0) {
689+
const lastMessageTime = +(
690+
window.localStorage.getItem(messageHistoryLastTimeKey) ?? 0
691+
);
692+
if ((Date.now() - lastMessageTime) / 1000 > historyMaxAge) {
693+
window.localStorage.removeItem(messageHistoryLSKey);
694+
window.localStorage.removeItem(quickReplyHistoryLSKey);
695+
window.localStorage.removeItem(messageHistoryLastTimeKey);
696+
return null;
697+
}
698+
}
699+
678700
const messages = JSON.parse(serializedHistory);
679701
const quickReplies = JSON.parse(
680702
window.localStorage.getItem(quickReplyHistoryLSKey) || '[]',

0 commit comments

Comments
 (0)