Skip to content

resolves #190: add historyMaxAge parameter #191

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

Open
wants to merge 3 commits into
base: master
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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
2 changes: 2 additions & 0 deletions src/settings/TockSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface LocalStorageSettings {
prefix: string;
enableMessageHistory: boolean;
maxMessageCount: number;
historyMaxAge: number;
}

export interface NetworkSettings {
Expand All @@ -28,6 +29,7 @@ export const defaultSettings: TockSettings = {
prefix: '',
enableMessageHistory: false,
maxMessageCount: 10,
historyMaxAge: -1,
},
network: {
disableSse: false,
Expand Down
22 changes: 22 additions & 0 deletions src/useTock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export const useTock0: (
localStoragePrefix,
'tockMessageHistory',
);
const messageHistoryLastTime = retrievePrefixedLocalStorageKey(
localStoragePrefix,
'tockLastMessageTimestamp',
);

const savedHistory = window.localStorage.getItem(messageHistoryLSKeyName);
let history: Message[];
Expand All @@ -195,6 +199,7 @@ export const useTock0: (
messageHistoryLSKeyName,
JSON.stringify(history),
);
window.localStorage.setItem(messageHistoryLastTime, '' + Date.now());
},
[localStoragePrefix, localStorageMaxMessages],
);
Expand Down Expand Up @@ -652,13 +657,30 @@ export const useTock0: (
localStoragePrefix,
'tockHandledResponses',
);
const messageHistoryLastTimeKey = retrievePrefixedLocalStorageKey(
localStoragePrefix,
'tockLastMessageTimestamp',
);

const serializedHistory =
storageAvailable('localStorage') && localStorageEnabled
? window.localStorage.getItem(messageHistoryLSKey)
: 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) || '[]',
Expand Down