-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathScript.tsx
More file actions
142 lines (136 loc) · 5.2 KB
/
Script.tsx
File metadata and controls
142 lines (136 loc) · 5.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { useEffect, useState } from 'react'
import { Dialog } from '@headlessui/react'
import { Form } from '@remix-run/react'
import { XIcon } from '~/components/icons.js'
import { Button, CopyButton, InfoWithTooltip } from '~/components/index.js'
import { removeItem } from '~/lib/utils.js'
type ScriptModalProps = {
title: string
tooltip?: string
buttonCodeTooltip?: string
defaultType?: string
scriptForDisplay: string
isOpen: boolean
onClose: () => void
}
const selectableTypes = ['banner', 'widget', 'button']
const buttonCode = "<span class='_wm_tools_button'></span>"
export const ScriptModal = ({
title,
tooltip,
buttonCodeTooltip,
defaultType,
scriptForDisplay,
isOpen,
onClose
}: ScriptModalProps) => {
const [types, setTypes] = useState([defaultType])
const [processedScript, setProcessedScript] = useState(scriptForDisplay)
const chooseType = (selected: string) => {
if (types.indexOf(selected) != -1) {
const remaining = removeItem(types, selected)
setTypes([...remaining])
} else {
setTypes([...types, selected])
}
}
useEffect(() => {
const script = scriptForDisplay.replace('[elements]', types.join('|'))
setProcessedScript(script)
}, [types, scriptForDisplay])
return (
<Dialog as="div" className="relative z-10" onClose={onClose} open={isOpen}>
<div className="fixed inset-0 bg-slate-500 bg-opacity-75 transition-opacity" />
<div className="fixed inset-0 z-10 overflow-y-auto">
<div className="flex min-h-full items-center justify-center p-4 text-center">
<Dialog.Panel className="relative transform overflow-hidden rounded-lg max-w-full transition-all bg-white px-4 pb-4 pt-5 text-left shadow-xl w-full max-w-xl">
<div className="absolute right-0 top-0 pr-4 pt-4">
<button
type="button"
className="text-gray-400 hover:text-gray-500 focus:outline-none"
onClick={onClose}
>
<span className="sr-only">Close</span>
<XIcon className="h-8 w-8" aria-hidden="true" />
</button>
</div>
<div>
<Dialog.Title
as="h3"
className="flex font-semibold leading-6 text-lg text-center"
>
<span>{title}</span>
<InfoWithTooltip tooltip={tooltip} />
</Dialog.Title>
<div className="mt-2">
<div className="flex items-center m-6 mb-0 p-2">
<span className="flex">Include: </span>
{selectableTypes.map((type) => {
return (
<div className="flex items-center ml-2" key={type}>
<input
type="checkbox"
className="flex w-5 h-5 mr-2"
defaultChecked={defaultType == type}
onClick={() => chooseType(type)}
/>
<span className="flex">{type}</span>
</div>
)
})}
</div>
<div className="flex">
<code className="flex m-6 p-2 max-w-md border border-tealish whitespace-pre-wrap break-all break-words overflow-x-auto block">
{processedScript}
</code>
<div className="flex">
<CopyButton
aria-label="copy script"
className="h-7 w-7"
size="sm"
value={processedScript}
variant="input"
></CopyButton>
</div>
</div>
{types.includes('button') && (
<div className="flex flex-col">
<span className="flex w-full ml-4">
Button code{' '}
<InfoWithTooltip tooltip={buttonCodeTooltip} />
</span>
<div className="flex">
<code className="flex m-6 my-2 p-2 max-w-md w-md border border-tealish whitespace-pre-wrap break-all break-words overflow-x-auto block">
{buttonCode}
</code>
<div className="flex">
<CopyButton
aria-label="copy button"
className="h-7 w-7"
size="sm"
value={buttonCode}
variant="input"
></CopyButton>
</div>
</div>
</div>
)}
<Form method="post" replace preventScrollReset>
<div className="flex justify-end space-x-4">
<Button
aria-label="close modal"
type="reset"
onClick={onClose}
>
Close
</Button>
</div>
</Form>
</div>
</div>
</Dialog.Panel>
</div>
</div>
</Dialog>
)
}