-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathWidgetView.tsx
More file actions
58 lines (53 loc) · 1.95 KB
/
WidgetView.tsx
File metadata and controls
58 lines (53 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import type { FieldNames, FormState, WidgetDrawer } from '@lifi/widget'
import { LiFiWidget, WidgetSkeleton } from '@lifi/widget'
import type { JSX } from 'react'
import { useCallback, useEffect, useRef } from 'react'
import { useFormValues } from '../../store/editTools/useFormValues.js'
import { useSkeletonToolValues } from '../../store/editTools/useSkeletonToolValues.js'
import { useConfig } from '../../store/widgetConfig/useConfig.js'
import { usePlaygroundWidgetMode } from '../../store/widgetConfig/useConfigValues.js'
import { CheckoutWidgetView } from './CheckoutWidgetView.js'
import { WidgetViewContainer } from './WidgetViewContainer.js'
export function WidgetView(): JSX.Element {
const { config } = useConfig()
const { playgroundWidgetMode } = usePlaygroundWidgetMode()
const drawerRef = useRef<WidgetDrawer>(null)
const formRef = useRef<FormState>(null)
const { isSkeletonShown, isSkeletonSideBySide } = useSkeletonToolValues()
const { formValues } = useFormValues()
const toggleDrawer = useCallback(() => {
drawerRef?.current?.toggleDrawer()
}, [])
useEffect(() => {
if (formRef.current && formValues) {
Object.entries(formValues).forEach(([fieldName, fieldValue]) => {
if (fieldName !== 'formUpdateKey') {
formRef.current?.setFieldValue(fieldName as FieldNames, fieldValue, {
setUrlSearchParam: true,
})
}
})
}
}, [formValues])
if (playgroundWidgetMode === 'checkout') {
return (
<WidgetViewContainer>
<CheckoutWidgetView />
</WidgetViewContainer>
)
}
return (
<WidgetViewContainer toggleDrawer={toggleDrawer}>
{!isSkeletonShown || isSkeletonSideBySide ? (
<LiFiWidget
config={config}
ref={drawerRef}
integrator="li.fi-playground"
formRef={formRef}
open
/>
) : null}
{isSkeletonShown ? <WidgetSkeleton config={config} /> : null}
</WidgetViewContainer>
)
}