2
0

Remember result in either local or session storage (#514)

Closes #513
This commit is contained in:
Baptiste Arnaud
2023-05-16 14:58:56 +02:00
committed by GitHub
parent 0ae2a4ba8b
commit 27b009dd76
11 changed files with 129 additions and 50 deletions

View File

@ -7,9 +7,9 @@ import { setIsMobile } from '@/utils/isMobileSignal'
import { BotContext, InitialChatReply, OutgoingLog } from '@/types'
import { ErrorMessage } from './ErrorMessage'
import {
getExistingResultIdFromSession,
setResultInSession,
} from '@/utils/sessionStorage'
getExistingResultIdFromStorage,
setResultInStorage,
} from '@/utils/storage'
import { setCssVariablesValue } from '@/utils/setCssVariablesValue'
import immutableCss from '../assets/immutable.css'
@ -52,7 +52,7 @@ export const Bot = (props: BotProps & { class?: string }) => {
isPreview: props.isPreview ?? false,
resultId: isNotEmpty(props.resultId)
? props.resultId
: getExistingResultIdFromSession(typebotIdFromProps),
: getExistingResultIdFromStorage(typebotIdFromProps),
startGroupId: props.startGroupId,
prefilledVariables: {
...prefilledVariables,
@ -76,7 +76,10 @@ export const Bot = (props: BotProps & { class?: string }) => {
if (!data) return setError(new Error("Error! Couldn't initiate the chat."))
if (data.resultId && typebotIdFromProps)
setResultInSession(typebotIdFromProps, data.resultId)
setResultInStorage(data.typebot.settings.general.rememberUser?.storage)(
typebotIdFromProps,
data.resultId
)
setInitialChatReply(data)
setCustomCss(data.typebot.theme.customCss ?? '')

View File

@ -1,20 +0,0 @@
const sessionStorageKey = 'resultId'
export const getExistingResultIdFromSession = (typebotId?: string) => {
if (!typebotId) return
try {
return (
sessionStorage.getItem(`${sessionStorageKey}-${typebotId}`) ?? undefined
)
} catch {
/* empty */
}
}
export const setResultInSession = (typebotId: string, resultId: string) => {
try {
return sessionStorage.setItem(`${sessionStorageKey}-${typebotId}`, resultId)
} catch {
/* empty */
}
}

View File

@ -0,0 +1,29 @@
const sessionStorageKey = 'resultId'
export const getExistingResultIdFromStorage = (typebotId?: string) => {
if (!typebotId) return
try {
return (
sessionStorage.getItem(`${sessionStorageKey}-${typebotId}`) ??
localStorage.getItem(`${sessionStorageKey}-${typebotId}`) ??
undefined
)
} catch {
/* empty */
}
}
export const setResultInStorage =
(storageType: 'local' | 'session' = 'session') =>
(typebotId: string, resultId: string) => {
try {
;(storageType === 'session' ? localStorage : sessionStorage).removeItem(
`${sessionStorageKey}-${typebotId}`
)
return (
storageType === 'session' ? sessionStorage : localStorage
).setItem(`${sessionStorageKey}-${typebotId}`, resultId)
} catch {
/* empty */
}
}