2
0
Files
bot/apps/builder/src/features/settings/components/GeneralSettingsForm.tsx
Baptiste Arnaud 0dc276c18f Restore chat state when user is remembered (#1333)
Closes #993

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Added a detailed explanation page for the "Remember user" setting in
the app documentation.
- Introduced persistence of chat state across sessions, with options for
local or session storage.
- Enhanced bot functionality to store and retrieve initial chat replies
and manage bot open state with improved storage handling.
- Added a new callback for chat state persistence to bot component
props.

- **Improvements**
- Updated the general settings form to clarify the description of the
"Remember user" feature.
- Enhanced custom CSS handling and progress value persistence in bot
components.
- Added conditional transition disabling in various components for
smoother user experiences.
- Simplified the handling of `onTransitionEnd` across multiple bubble
components.

- **Refactor**
- Renamed `inputIndex` to `chunkIndex` or `index` in various components
for consistency.
	- Removed unused ESLint disable comments related to reactivity rules.
	- Adjusted import statements and cleaned up code across several files.

- **Bug Fixes**
- Fixed potential issues with undefined callbacks by introducing
optional chaining in component props.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-03-07 15:39:09 +01:00

130 lines
4.0 KiB
TypeScript

import {
FormControl,
FormLabel,
HStack,
Stack,
Tag,
Text,
useColorModeValue,
} from '@chakra-ui/react'
import { Settings } from '@typebot.io/schemas'
import React from 'react'
import { isDefined } from '@typebot.io/lib'
import { SwitchWithLabel } from '@/components/inputs/SwitchWithLabel'
import { SwitchWithRelatedSettings } from '@/components/SwitchWithRelatedSettings'
import { DropdownList } from '@/components/DropdownList'
import { MoreInfoTooltip } from '@/components/MoreInfoTooltip'
import {
defaultSettings,
rememberUserStorages,
} from '@typebot.io/schemas/features/typebot/settings/constants'
type Props = {
generalSettings: Settings['general'] | undefined
onGeneralSettingsChange: (generalSettings: Settings['general']) => void
}
export const GeneralSettingsForm = ({
generalSettings,
onGeneralSettingsChange,
}: Props) => {
const keyBg = useColorModeValue(undefined, 'gray.600')
const toggleRememberUser = (isEnabled: boolean) =>
onGeneralSettingsChange({
...generalSettings,
rememberUser: {
...generalSettings?.rememberUser,
isEnabled,
},
})
const handleInputPrefillChange = (isInputPrefillEnabled: boolean) =>
onGeneralSettingsChange({
...generalSettings,
isInputPrefillEnabled,
})
const handleHideQueryParamsChange = (isHideQueryParamsEnabled: boolean) =>
onGeneralSettingsChange({
...generalSettings,
isHideQueryParamsEnabled,
})
const updateRememberUserStorage = (
storage: NonNullable<
NonNullable<Settings['general']>['rememberUser']
>['storage']
) =>
onGeneralSettingsChange({
...generalSettings,
rememberUser: {
...generalSettings?.rememberUser,
storage,
},
})
return (
<Stack spacing={6}>
<SwitchWithLabel
label="Prefill input"
initialValue={
generalSettings?.isInputPrefillEnabled ??
defaultSettings.general.isInputPrefillEnabled
}
onCheckChange={handleInputPrefillChange}
moreInfoContent="Inputs are automatically pre-filled whenever their associated variable has a value"
/>
<SwitchWithLabel
label="Hide query params on bot start"
initialValue={
generalSettings?.isHideQueryParamsEnabled ??
defaultSettings.general.isHideQueryParamsEnabled
}
onCheckChange={handleHideQueryParamsChange}
moreInfoContent="If your URL contains query params, they will be automatically hidden when the bot starts."
/>
<SwitchWithRelatedSettings
label={'Remember user'}
moreInfoContent="If enabled, the chat state will be restored if the user comes back after exiting."
initialValue={
generalSettings?.rememberUser?.isEnabled ??
(isDefined(generalSettings?.isNewResultOnRefreshEnabled)
? !generalSettings?.isNewResultOnRefreshEnabled
: false)
}
onCheckChange={toggleRememberUser}
>
<FormControl as={HStack} justifyContent="space-between">
<FormLabel mb="0">
Storage:&nbsp;
<MoreInfoTooltip>
<Stack>
<Text>
Choose{' '}
<Tag size="sm" bgColor={keyBg}>
session
</Tag>{' '}
to remember the user as long as he does not closes the tab or
the browser.
</Text>
<Text>
Choose{' '}
<Tag size="sm" bgColor={keyBg}>
local
</Tag>{' '}
to remember the user forever on the same device.
</Text>
</Stack>
</MoreInfoTooltip>
</FormLabel>
<DropdownList
currentItem={generalSettings?.rememberUser?.storage ?? 'session'}
onItemSelect={updateRememberUserStorage}
items={rememberUserStorages}
></DropdownList>
</FormControl>
</SwitchWithRelatedSettings>
</Stack>
)
}