Skip to content

Commit 3ba52c7

Browse files
committed
feat: remove persistentMenu, coverComponent, blockInputs, enableEmojiPicker, enableAttachments, enableUserInput and enableAnimations from WebchatProps and pass it inside theme
1 parent 4d76902 commit 3ba52c7

11 files changed

+116
-158
lines changed

packages/botonic-react/src/dev-app.jsx

+1-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import merge from 'lodash.merge'
21
import React from 'react'
32
import { createRoot } from 'react-dom/client'
43

@@ -56,13 +55,6 @@ export class DevApp extends WebchatApp {
5655
getComponent(host, optionsAtRuntime = {}) {
5756
let {
5857
theme = {},
59-
persistentMenu,
60-
coverComponent,
61-
blockInputs,
62-
enableEmojiPicker,
63-
enableAttachments,
64-
enableUserInput,
65-
enableAnimations,
6658
storage,
6759
storageKey,
6860
onInit,
@@ -73,14 +65,7 @@ export class DevApp extends WebchatApp {
7365
hostId,
7466
...webchatOptions
7567
} = optionsAtRuntime
76-
theme = merge(this.theme, theme)
77-
persistentMenu = persistentMenu || this.persistentMenu
78-
coverComponent = coverComponent || this.coverComponent
79-
blockInputs = blockInputs || this.blockInputs
80-
enableEmojiPicker = enableEmojiPicker || this.enableEmojiPicker
81-
enableAttachments = enableAttachments || this.enableAttachments
82-
enableUserInput = enableUserInput || this.enableUserInput
83-
enableAnimations = enableAnimations || this.enableAnimations
68+
theme = super.createInitialTheme(optionsAtRuntime)
8469
storage = storage || this.storage
8570
storageKey = storageKey || this.storageKey
8671
this.onInit = onInit || this.onInit
@@ -97,13 +82,6 @@ export class DevApp extends WebchatApp {
9782
host={this.host}
9883
shadowDOM={this.shadowDOM}
9984
theme={theme}
100-
persistentMenu={persistentMenu}
101-
coverComponent={coverComponent}
102-
blockInputs={blockInputs}
103-
enableEmojiPicker={enableEmojiPicker}
104-
enableAttachments={enableAttachments}
105-
enableUserInput={enableUserInput}
106-
enableAnimations={enableAnimations}
10785
storage={storage}
10886
storageKey={storageKey}
10987
getString={(stringId, session) => this.bot.getString(stringId, session)}

packages/botonic-react/src/index-types.ts

-7
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,6 @@ export interface WebchatProps {
121121

122122
shadowDOM?: any
123123
theme?: WebchatTheme
124-
persistentMenu?: PersistentMenuOptionsTheme
125-
coverComponent?: CoverComponentOptions
126-
blockInputs?: BlockInputOption[]
127-
enableEmojiPicker?: boolean
128-
enableAttachments?: boolean
129-
enableUserInput?: boolean
130-
enableAnimations?: boolean
131124
storage?: Storage | null
132125
storageKey?: string | (() => string)
133126
defaultDelay?: number

packages/botonic-react/src/webchat-app.tsx

+81-27
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ export class WebchatApp {
8888
server,
8989
}: WebchatArgs) {
9090
this.theme = theme
91-
// TODO: Review if is possible store persistentMenu, coverComponent, blockInputs, enableEmojiPicker, enableAttachments, enableUserInput, enableAnimations, etc as part of theme
9291
this.persistentMenu = persistentMenu
9392
this.coverComponent = coverComponent
9493
this.blockInputs = blockInputs
@@ -371,17 +370,86 @@ export class WebchatApp {
371370
return this.webchatRef.current?.updateWebchatSettings(settings)
372371
}
373372

374-
// eslint-disable-next-line complexity
373+
createInitialTheme(optionsAtRuntime: WebchatArgs = {}) {
374+
const theme = merge(defaultTheme, this.theme, optionsAtRuntime.theme)
375+
376+
if (theme.animations === undefined) {
377+
theme.animations = {}
378+
}
379+
380+
theme.animations.enable =
381+
theme.animations.enable ??
382+
optionsAtRuntime.enableAnimations ??
383+
this.enableAnimations
384+
385+
theme.coverComponent =
386+
theme.coverComponent ??
387+
optionsAtRuntime.coverComponent ??
388+
this.coverComponent
389+
390+
theme.userInput = this.createInitialThemUserInput(theme, optionsAtRuntime)
391+
392+
return theme
393+
}
394+
395+
createInitialThemUserInput(
396+
theme: WebchatTheme,
397+
optionsAtRuntime: WebchatArgs = {}
398+
) {
399+
if (theme.userInput === undefined) {
400+
theme.userInput = {}
401+
}
402+
403+
console.log(
404+
'WebchatApp theme.userInput.enable',
405+
theme.userInput.enable,
406+
optionsAtRuntime.enableUserInput,
407+
this.enableUserInput
408+
)
409+
theme.userInput.enable =
410+
theme.userInput.enable ??
411+
optionsAtRuntime.enableUserInput ??
412+
this.enableUserInput
413+
414+
theme.userInput.persistentMenu =
415+
theme.userInput.persistentMenu ??
416+
optionsAtRuntime.persistentMenu ??
417+
this.persistentMenu
418+
419+
theme.userInput.blockInputs =
420+
theme.userInput.blockInputs ??
421+
optionsAtRuntime.blockInputs ??
422+
this.blockInputs
423+
424+
if (theme.userInput.emojiPicker === undefined) {
425+
theme.userInput.emojiPicker = {}
426+
}
427+
428+
theme.userInput.emojiPicker.enable =
429+
theme.userInput.emojiPicker.enable ??
430+
optionsAtRuntime.enableEmojiPicker ??
431+
this.enableEmojiPicker
432+
433+
if (theme.userInput.attachments === undefined) {
434+
theme.userInput.attachments = {}
435+
}
436+
437+
theme.userInput.attachments.enable =
438+
theme.userInput.attachments.enable ??
439+
optionsAtRuntime.enableAttachments ??
440+
this.enableAttachments
441+
442+
theme.userInput.blockInputs =
443+
theme.userInput.blockInputs ??
444+
optionsAtRuntime.blockInputs ??
445+
this.blockInputs
446+
447+
return theme.userInput
448+
}
449+
375450
getComponent(host: HTMLDivElement, optionsAtRuntime: WebchatArgs = {}) {
376451
let {
377-
theme = defaultTheme,
378-
persistentMenu,
379-
coverComponent,
380-
blockInputs,
381-
enableAttachments,
382-
enableUserInput,
383-
enableAnimations,
384-
enableEmojiPicker,
452+
theme = {},
385453
defaultDelay,
386454
defaultTyping,
387455
storage,
@@ -398,14 +466,7 @@ export class WebchatApp {
398466
hostId,
399467
...webchatOptions
400468
} = optionsAtRuntime
401-
theme = merge(this.theme, theme)
402-
persistentMenu = persistentMenu || this.persistentMenu
403-
coverComponent = coverComponent || this.coverComponent
404-
blockInputs = blockInputs || this.blockInputs
405-
enableEmojiPicker = enableEmojiPicker || this.enableEmojiPicker
406-
enableAttachments = enableAttachments || this.enableAttachments
407-
enableUserInput = enableUserInput || this.enableUserInput
408-
enableAnimations = enableAnimations || this.enableAnimations
469+
theme = this.createInitialTheme(optionsAtRuntime)
409470
defaultDelay = defaultDelay || this.defaultDelay
410471
defaultTyping = defaultTyping || this.defaultTyping
411472
server = server || this.server
@@ -429,29 +490,22 @@ export class WebchatApp {
429490
host={this.host}
430491
shadowDOM={this.shadowDOM}
431492
theme={theme as WebchatTheme}
432-
persistentMenu={persistentMenu}
433-
coverComponent={coverComponent}
434-
blockInputs={blockInputs}
435-
enableEmojiPicker={enableEmojiPicker}
436-
enableAttachments={enableAttachments}
437-
enableUserInput={enableUserInput}
438-
enableAnimations={enableAnimations}
439493
storage={this.storage}
440494
storageKey={this.storageKey}
441495
defaultDelay={defaultDelay}
442496
defaultTyping={defaultTyping}
443497
onInit={(...args: [any]) => this.onInitWebchat(...args)}
444498
onOpen={(...args: [any]) => this.onOpenWebchat(...args)}
445499
onClose={(...args: [any]) => this.onCloseWebchat(...args)}
446-
onUserInput={(...args: [any]) => this.onUserInput(...args)} // TODO: Review this function, and his params
500+
onUserInput={(...args: [any]) => this.onUserInput(...args)}
447501
onStateChange={(args: OnStateChangeArgs) => {
448502
this.onStateChange(args)
449503
}}
450504
onTrackEvent={(
451505
request: ActionRequest,
452506
eventName: string,
453507
args?: EventArgs
454-
) => this.onTrackEventWebchat(request, eventName, args)} //TODO: Review if this implementation is correct
508+
) => this.onTrackEventWebchat(request, eventName, args)}
455509
server={server}
456510
/>
457511
)

packages/botonic-react/src/webchat/input-panel/attachment.tsx

+4-14
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,20 @@ import { ConditionalAnimation } from '../components/conditional-animation'
88

99
interface AttachmentProps {
1010
accept: string
11-
enableAttachments?: boolean
1211
onChange: (event: any) => void
1312
}
1413

15-
export const Attachment = ({
16-
accept,
17-
enableAttachments,
18-
onChange,
19-
}: AttachmentProps) => {
20-
const { getThemeProperty } = useContext(WebchatContext)
14+
export const Attachment = ({ accept, onChange }: AttachmentProps) => {
15+
const { webchatState } = useContext(WebchatContext)
2116

2217
const fileInputRef = useRef<HTMLInputElement | null>(null)
2318

24-
const CustomAttachments = getThemeProperty(
25-
WEBCHAT.CUSTOM_PROPERTIES.customAttachments
26-
)
19+
const CustomAttachments = webchatState.theme.userInput?.attachments?.custom
2720

2821
const isAttachmentsEnabled = () => {
2922
const hasCustomAttachments = !!CustomAttachments
3023
return (
31-
getThemeProperty(
32-
WEBCHAT.CUSTOM_PROPERTIES.enableAttachments,
33-
enableAttachments
34-
) ?? hasCustomAttachments
24+
webchatState.theme.userInput?.attachments?.enable || hasCustomAttachments
3525
)
3626
}
3727
const attachmentsEnabled = isAttachmentsEnabled()

packages/botonic-react/src/webchat/input-panel/emoji-picker.tsx

+4-14
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,18 @@ import { Icon } from '../components/common'
77
import { ConditionalAnimation } from '../components/conditional-animation'
88

99
interface EmojiPickerProps {
10-
enableEmojiPicker?: boolean
1110
onClick: () => void
1211
}
1312

14-
export const EmojiPicker = ({
15-
enableEmojiPicker,
16-
onClick,
17-
}: EmojiPickerProps) => {
18-
const { getThemeProperty } = useContext(WebchatContext)
13+
export const EmojiPicker = ({ onClick }: EmojiPickerProps) => {
14+
const { webchatState } = useContext(WebchatContext)
1915

20-
const CustomEmojiPicker = getThemeProperty(
21-
WEBCHAT.CUSTOM_PROPERTIES.customEmojiPicker,
22-
undefined
23-
)
16+
const CustomEmojiPicker = webchatState.theme.userInput?.emojiPicker?.custom
2417

2518
const isEmojiPickerEnabled = () => {
2619
const hasCustomEmojiPicker = !!CustomEmojiPicker
2720
return (
28-
getThemeProperty(
29-
WEBCHAT.CUSTOM_PROPERTIES.enableEmojiPicker,
30-
enableEmojiPicker
31-
) ?? hasCustomEmojiPicker
21+
webchatState.theme.userInput?.emojiPicker?.enable || hasCustomEmojiPicker
3222
)
3323
}
3424
const emojiPickerEnabled = isEmojiPickerEnabled()

packages/botonic-react/src/webchat/input-panel/index.tsx

+2-13
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,13 @@ import { UserInputContainer } from './styles'
1515
import { Textarea } from './textarea'
1616

1717
interface InputPanelProps {
18-
persistentMenu: any
19-
enableEmojiPicker?: boolean
20-
enableAttachments?: boolean
2118
handleAttachment: (event: any) => void
2219
textareaRef: React.MutableRefObject<HTMLTextAreaElement | undefined>
2320
host: HTMLElement
2421
onUserInput?: (event: any) => Promise<void>
2522
}
2623

2724
export const InputPanel = ({
28-
persistentMenu,
29-
enableEmojiPicker,
30-
enableAttachments,
3125
handleAttachment,
3226
textareaRef,
3327
host,
@@ -96,23 +90,18 @@ export const InputPanel = ({
9690
/>
9791
)}
9892

99-
<PersistentMenu onClick={handleMenu} persistentMenu={persistentMenu} />
93+
<PersistentMenu onClick={handleMenu} />
10094

10195
<Textarea
10296
host={host}
103-
persistentMenu={persistentMenu}
10497
textareaRef={textareaRef}
10598
sendChatEvent={sendChatEvent}
10699
sendTextAreaText={sendTextAreaText}
107100
/>
108101

109-
<EmojiPicker
110-
enableEmojiPicker={enableEmojiPicker}
111-
onClick={handleEmojiClick}
112-
/>
102+
<EmojiPicker onClick={handleEmojiClick} />
113103

114104
<Attachment
115-
enableAttachments={enableAttachments}
116105
onChange={handleAttachment}
117106
accept={getFullMimeWhitelist().join(',')}
118107
/>

packages/botonic-react/src/webchat/input-panel/persistent-menu.tsx

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
import React, { useContext } from 'react'
22

33
import LogoMenu from '../../assets/menuButton.svg'
4-
import { ROLES, WEBCHAT } from '../../constants'
4+
import { ROLES } from '../../constants'
55
import { WebchatContext } from '../../webchat/context'
66
import { Icon } from '../components/common'
77
import { ConditionalAnimation } from '../components/conditional-animation'
88

99
interface PersistentMenuProps {
10-
persistentMenu: any
1110
onClick: () => void
1211
}
1312

14-
export const PersistentMenu = ({
15-
onClick,
16-
persistentMenu,
17-
}: PersistentMenuProps) => {
18-
const { getThemeProperty } = useContext(WebchatContext)
13+
export const PersistentMenu = ({ onClick }: PersistentMenuProps) => {
14+
const { webchatState } = useContext(WebchatContext)
1915

20-
const persistentMenuOptions = getThemeProperty(
21-
WEBCHAT.CUSTOM_PROPERTIES.persistentMenu,
22-
persistentMenu
23-
)
16+
const persistentMenuOptions = webchatState.theme.userInput?.persistentMenu
2417

25-
const CustomMenuButton = getThemeProperty(
26-
WEBCHAT.CUSTOM_PROPERTIES.customMenuButton
27-
)
18+
const CustomMenuButton = webchatState.theme.userInput?.menuButton?.custom
2819

2920
return (
3021
<>

packages/botonic-react/src/webchat/input-panel/send-button.tsx

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@ interface SendButtonProps {
1111
}
1212

1313
export const SendButton = ({ onClick }: SendButtonProps) => {
14-
const { getThemeProperty } = useContext(WebchatContext)
14+
const { webchatState } = useContext(WebchatContext)
1515

16-
const sendButtonEnabled = getThemeProperty(
17-
WEBCHAT.CUSTOM_PROPERTIES.enableSendButton
18-
)
16+
const sendButtonEnabled = webchatState.theme.userInput?.sendButton?.enable
1917

20-
const CustomSendButton = getThemeProperty(
21-
WEBCHAT.CUSTOM_PROPERTIES.customSendButton
22-
)
18+
const CustomSendButton = webchatState.theme.userInput?.sendButton?.custom
2319

2420
return (
2521
<>

0 commit comments

Comments
 (0)