2
0

️ Reset remembered state if the typebot is updated (#1675)

Wipe stored chat state if a typebot is updated.

Resolves #1508 
Resolves #1427  



https://github.com/user-attachments/assets/7668d6a7-2916-4158-b715-1cccaeaf6ddf

---------

Co-authored-by: Baptiste Arnaud <baptiste.arnaud95@gmail.com>
This commit is contained in:
Abhirup Basu
2024-08-16 21:40:47 +05:30
committed by GitHub
parent 29ab812512
commit 17684ee5b7
12 changed files with 48 additions and 7 deletions

View File

@ -1148,6 +1148,9 @@
}, },
"settings": { "settings": {
"$ref": "#/components/schemas/settings" "$ref": "#/components/schemas/settings"
},
"publishedAt": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -1986,6 +1989,9 @@
}, },
"settings": { "settings": {
"$ref": "#/components/schemas/settings" "$ref": "#/components/schemas/settings"
},
"publishedAt": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -5869,6 +5875,9 @@
}, },
"theme": { "theme": {
"$ref": "#/components/schemas/theme" "$ref": "#/components/schemas/theme"
},
"updatedAt": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -5879,7 +5888,8 @@
"edges", "edges",
"variables", "variables",
"settings", "settings",
"theme" "theme",
"updatedAt"
], ],
"title": "Typebot V5" "title": "Typebot V5"
}, },
@ -7483,6 +7493,9 @@
}, },
"theme": { "theme": {
"$ref": "#/components/schemas/theme" "$ref": "#/components/schemas/theme"
},
"updatedAt": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -7493,7 +7506,8 @@
"edges", "edges",
"variables", "variables",
"settings", "settings",
"theme" "theme",
"updatedAt"
], ],
"title": "Typebot V6" "title": "Typebot V6"
}, },

View File

@ -11,3 +11,5 @@ There are 2 storage options:
- **Local storage**: The chat state will be saved in the user's web browser. It will be available only on the same device and web browser. - **Local storage**: The chat state will be saved in the user's web browser. It will be available only on the same device and web browser.
- **Session storage**: The chat state will be saved in the user's web browser. It will be available only on the same device and web browser, but it will be deleted when the user closes the current tab or the web browser. - **Session storage**: The chat state will be saved in the user's web browser. It will be available only on the same device and web browser, but it will be deleted when the user closes the current tab or the web browser.
Note that if you publish a new version of your typebot, saved sessions will be automatically resetted. It means that if the user has a saved session, if he refreshes the bot and that a new version is published, the bot will restart from the beginning.

View File

@ -96,6 +96,7 @@ export const startChat = async ({
id: typebot.id, id: typebot.id,
theme: typebot.theme, theme: typebot.theme,
settings: typebot.settings, settings: typebot.settings,
publishedAt: typebot.publishedAt,
}, },
messages, messages,
input, input,

View File

@ -31,5 +31,6 @@ export const findPublicTypebot = ({ publicId }: Props) =>
}, },
}, },
}, },
updatedAt: true,
}, },
}) })

View File

@ -18,5 +18,6 @@ export const findTypebot = ({ id, userId }: Props) =>
theme: true, theme: true,
variables: true, variables: true,
isArchived: true, isArchived: true,
updatedAt: true,
}, },
}) })

View File

@ -278,6 +278,7 @@ export const startSession = async ({
theme: sanitizeAndParseTheme(typebot.theme, { theme: sanitizeAndParseTheme(typebot.theme, {
variables: initialState.typebotsQueue[0].typebot.variables, variables: initialState.typebotsQueue[0].typebot.variables,
}), }),
publishedAt: typebot.updatedAt,
}, },
dynamicTheme: parseDynamicTheme(newSessionState), dynamicTheme: parseDynamicTheme(newSessionState),
logs: startLogs.length > 0 ? startLogs : undefined, logs: startLogs.length > 0 ? startLogs : undefined,
@ -296,6 +297,7 @@ export const startSession = async ({
theme: sanitizeAndParseTheme(typebot.theme, { theme: sanitizeAndParseTheme(typebot.theme, {
variables: initialState.typebotsQueue[0].typebot.variables, variables: initialState.typebotsQueue[0].typebot.variables,
}), }),
publishedAt: typebot.updatedAt,
}, },
messages, messages,
input, input,

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/js", "name": "@typebot.io/js",
"version": "0.3.7", "version": "0.3.8",
"description": "Javascript library to display typebots on your website", "description": "Javascript library to display typebots on your website",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -150,8 +150,25 @@ export const Bot = (props: BotProps & { class?: string }) => {
const initialChatInStorage = getInitialChatReplyFromStorage( const initialChatInStorage = getInitialChatReplyFromStorage(
data.typebot.id data.typebot.id
) )
if (initialChatInStorage) { if (
setInitialChatReply(initialChatInStorage) initialChatInStorage &&
initialChatInStorage.typebot.publishedAt &&
data.typebot.publishedAt
) {
if (
new Date(initialChatInStorage.typebot.publishedAt).getTime() ===
new Date(data.typebot.publishedAt).getTime()
) {
setInitialChatReply(initialChatInStorage)
} else {
// Restart chat by resetting remembered state
wipeExistingChatStateInStorage(data.typebot.id)
setInitialChatReply(data)
setInitialChatReplyInStorage(data, {
typebotId: data.typebot.id,
storage,
})
}
} else { } else {
setInitialChatReply(data) setInitialChatReply(data)
setInitialChatReplyInStorage(data, { setInitialChatReplyInStorage(data, {

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/nextjs", "name": "@typebot.io/nextjs",
"version": "0.3.7", "version": "0.3.8",
"description": "Convenient library to display typebots on your Next.js website", "description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/react", "name": "@typebot.io/react",
"version": "0.3.7", "version": "0.3.8",
"description": "Convenient library to display typebots on your React app", "description": "Convenient library to display typebots on your React app",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@ -12,6 +12,7 @@ export const leadGenerationTypebot: StartTypebot = {
version: '3', version: '3',
id: 'clckrl4q5000t3b6sabwokaar', id: 'clckrl4q5000t3b6sabwokaar',
events: null, events: null,
publishedAt: new Date(),
groups: [ groups: [
{ {
id: 'clckrl4q5000g3b6skizhd262', id: 'clckrl4q5000g3b6skizhd262',

View File

@ -170,6 +170,7 @@ const startTypebotPick = {
variables: true, variables: true,
settings: true, settings: true,
theme: true, theme: true,
updatedAt: true,
} as const } as const
export const startTypebotSchema = z.preprocess( export const startTypebotSchema = z.preprocess(
preprocessTypebot, preprocessTypebot,
@ -381,6 +382,7 @@ export const startChatResponseSchema = z
id: z.string(), id: z.string(),
theme: themeSchema, theme: themeSchema,
settings: settingsSchema, settings: settingsSchema,
publishedAt: z.coerce.date().optional(),
}), }),
}) })
.merge(chatResponseBaseSchema) .merge(chatResponseBaseSchema)