diff --git a/README.md b/README.md index 3ac0e38..f854a37 100644 --- a/README.md +++ b/README.md @@ -352,11 +352,12 @@ Objects implementing this interface can be passed to `renderChat` or to `TockCon #### `LocalStorageSettings` -| Property name | Type | Description | -|------------------------|------------|---------------------------------------------------------------------------------------------------------------------------------------------| -| `enableMessageHistory` | `boolean?` | If set to `true`, the most recent messages of a conversation will be persisted in the local storage. Defaults to `false`. | -| `maxMessageCount` | `number?` | When message history is enabled, sets the max number of messages to store. Defaults to 10. | -| `prefix` | `string?` | Prefix for local storage keys allowing communication with different bots from the same domain (used for both `userId` and message history). | +| Property name | Type | Description | +|------------------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `enableMessageHistory` | `boolean?` | If set to `true`, the most recent messages of a conversation will be persisted in the local storage. Defaults to `false`. | +| `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). | +| `maxMessageCount` | `number?` | When message history is enabled, sets the max number of messages to store. Defaults to 10. | +| `prefix` | `string?` | Prefix for local storage keys allowing communication with different bots from the same domain (used for both `userId` and message history). | #### `NetworkSettings` diff --git a/src/settings/TockSettings.tsx b/src/settings/TockSettings.tsx index 021d7dd..6de9e66 100644 --- a/src/settings/TockSettings.tsx +++ b/src/settings/TockSettings.tsx @@ -6,6 +6,7 @@ export interface LocalStorageSettings { prefix: string; enableMessageHistory: boolean; maxMessageCount: number; + historyMaxAge: number; } export interface NetworkSettings { @@ -28,6 +29,7 @@ export const defaultSettings: TockSettings = { prefix: '', enableMessageHistory: false, maxMessageCount: 10, + historyMaxAge: -1, }, network: { disableSse: false, diff --git a/src/useTock.ts b/src/useTock.ts index 68f0b1f..c69222a 100644 --- a/src/useTock.ts +++ b/src/useTock.ts @@ -179,6 +179,10 @@ export const useTock0: ( localStoragePrefix, 'tockMessageHistory', ); + const messageHistoryLastTime = retrievePrefixedLocalStorageKey( + localStoragePrefix, + 'tockLastMessageTimestamp', + ); const savedHistory = window.localStorage.getItem(messageHistoryLSKeyName); let history: Message[]; @@ -195,6 +199,7 @@ export const useTock0: ( messageHistoryLSKeyName, JSON.stringify(history), ); + window.localStorage.setItem(messageHistoryLastTime, '' + Date.now()); }, [localStoragePrefix, localStorageMaxMessages], ); @@ -652,6 +657,10 @@ export const useTock0: ( localStoragePrefix, 'tockHandledResponses', ); + const messageHistoryLastTimeKey = retrievePrefixedLocalStorageKey( + localStoragePrefix, + 'tockLastMessageTimestamp', + ); const serializedHistory = storageAvailable('localStorage') && localStorageEnabled @@ -659,6 +668,19 @@ export const useTock0: ( : undefined; if (serializedHistory) { + const historyMaxAge = localStorageSettings.historyMaxAge; + if (historyMaxAge > 0) { + const lastMessageTime = +( + window.localStorage.getItem(messageHistoryLastTimeKey) ?? 0 + ); + if ((Date.now() - lastMessageTime) / 1000 > historyMaxAge) { + window.localStorage.removeItem(messageHistoryLSKey); + window.localStorage.removeItem(quickReplyHistoryLSKey); + window.localStorage.removeItem(messageHistoryLastTimeKey); + return null; + } + } + const messages = JSON.parse(serializedHistory); const quickReplies = JSON.parse( window.localStorage.getItem(quickReplyHistoryLSKey) || '[]',