1
1
import { Session } from '@botonic/core'
2
- import { useReducer , useRef } from 'react'
2
+ import merge from 'lodash.merge'
3
+ import { useEffect , useReducer , useRef } from 'react'
3
4
4
5
import { Reply } from '../../components'
5
6
import { Webview } from '../../components/index-types'
6
7
import { WebchatMessage } from '../../index-types'
8
+ import { msgToBotonic } from '../../msg-to-botonic'
9
+ import { initSession , shouldKeepSessionOnReload } from '../../util'
7
10
import { defaultTheme } from '../theme/default-theme'
8
11
import { WebchatTheme } from '../theme/types'
12
+ import { BotonicStorage } from '../use-botonic-storage'
9
13
import { WebchatAction } from './actions'
10
14
import { ClientInput , DevSettings , ErrorMessage , WebchatState } from './types'
11
15
import { webchatReducer } from './webchat-reducer'
12
16
13
- function getWebchatInitialState ( initialTheme : WebchatTheme ) : WebchatState {
17
+ function getWebchatInitialState (
18
+ initialTheme : WebchatTheme ,
19
+ botonicStorage ?: BotonicStorage ,
20
+ initialSession ?: Partial < Session > ,
21
+ devSettings ?: DevSettings
22
+ ) : WebchatState {
23
+ const initialStorageSession = initSession ( botonicStorage ?. session )
24
+ const session = merge ( initialSession , initialStorageSession )
25
+ const lastMessageUpdate = botonicStorage ?. lastMessageUpdate
26
+ const themeUpdates = botonicStorage ?. themeUpdates || { }
27
+ const lastRoutePath = botonicStorage ?. lastRoutePath
28
+ console . log ( 'getWebchatInitialState devSettings' , devSettings )
29
+
14
30
return {
15
31
replies : [ ] ,
16
32
messagesJSON : [ ] ,
@@ -19,20 +35,20 @@ function getWebchatInitialState(initialTheme: WebchatTheme): WebchatState {
19
35
typing : false ,
20
36
webview : null ,
21
37
webviewParams : null ,
22
- session : { user : undefined } ,
23
- lastRoutePath : undefined ,
38
+ session : session as Partial < Session > ,
39
+ lastRoutePath : lastRoutePath ,
24
40
handoff : false ,
25
41
theme : initialTheme ,
26
- themeUpdates : { } ,
42
+ themeUpdates : themeUpdates ,
27
43
error : { } ,
28
44
online : true ,
29
- devSettings : { keepSessionOnReload : false } ,
45
+ devSettings : devSettings || { keepSessionOnReload : false } ,
30
46
isWebchatOpen : false ,
31
47
isEmojiPickerOpen : false ,
32
48
isPersistentMenuOpen : false ,
33
49
isCoverComponentOpen : false ,
34
50
isCustomComponentRendered : false ,
35
- lastMessageUpdate : undefined ,
51
+ lastMessageUpdate : lastMessageUpdate ,
36
52
currentAttachment : undefined ,
37
53
numUnreadMessages : 0 ,
38
54
isLastMessageVisible : true ,
@@ -77,9 +93,20 @@ export interface UseWebchat {
77
93
inputPanelRef : React . MutableRefObject < HTMLDivElement | null >
78
94
}
79
95
80
- export function useWebchat ( theme ?: WebchatTheme ) : UseWebchat {
96
+ export function useWebchat (
97
+ theme ?: WebchatTheme ,
98
+ botonicStorage ?: BotonicStorage ,
99
+ initialSession ?: Partial < Session > ,
100
+ devSettings ?: DevSettings
101
+ ) : UseWebchat {
81
102
const initialTheme = theme || defaultTheme
82
- const webchatInitialState = getWebchatInitialState ( initialTheme )
103
+ console . log ( 'useWebchat devSettings' , devSettings )
104
+ const webchatInitialState = getWebchatInitialState (
105
+ initialTheme ,
106
+ botonicStorage ,
107
+ initialSession ,
108
+ devSettings
109
+ )
83
110
84
111
const [ webchatState , webchatDispatch ] = useReducer (
85
112
webchatReducer ,
@@ -245,6 +272,31 @@ export function useWebchat(theme?: WebchatTheme): UseWebchat {
245
272
} )
246
273
}
247
274
275
+ useEffect ( ( ) => {
276
+ // const keepSessionOnReload = shouldKeepSessionOnReload({
277
+ // devSettings,
278
+ // initialDevSettings,
279
+ // })
280
+
281
+ const keepSessionOnReload = devSettings ?. keepSessionOnReload
282
+ console . log ( 'useWebchat useEffect keepSessionOnReload' , keepSessionOnReload )
283
+ if ( keepSessionOnReload ) {
284
+ const messagesJSON = botonicStorage ?. messages || [ ]
285
+ for ( const message of messagesJSON ) {
286
+ addMessage ( message )
287
+
288
+ const newMessageComponent = msgToBotonic (
289
+ { ...message , delay : 0 , typing : 0 } ,
290
+ initialTheme . message ?. customTypes
291
+ )
292
+ if ( newMessageComponent ) {
293
+ //@ts -ignore
294
+ addMessageComponent ( newMessageComponent )
295
+ }
296
+ }
297
+ }
298
+ } , [ ] )
299
+
248
300
return {
249
301
addMessage,
250
302
addMessageComponent,
0 commit comments