-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
177 lines (164 loc) · 6.23 KB
/
App.tsx
File metadata and controls
177 lines (164 loc) · 6.23 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { BeefreeExample } from './BeefreeExample'
import i18nEnUS from './i18n/en-US.json'
import i18nItIT from './i18n/it-IT.json'
import i18nEsES from './i18n/es-ES.json'
import i18nFrFR from './i18n/fr-FR.json'
import i18nDeDE from './i18n/de-DE.json'
import i18nPtBR from './i18n/pt-BR.json'
import i18nIdID from './i18n/id-ID.json'
import i18nJaJP from './i18n/ja-JP.json'
import i18nZhCN from './i18n/zh-CN.json'
import i18nZhHK from './i18n/zh-HK.json'
import i18nCsCZ from './i18n/cs-CZ.json'
import i18nNbNO from './i18n/nb-NO.json'
import i18nDaDK from './i18n/da-DK.json'
import i18nSvSE from './i18n/sv-SE.json'
import i18nPlPL from './i18n/pl-PL.json'
import i18nHuHU from './i18n/hu-HU.json'
import i18nRuRU from './i18n/ru-RU.json'
import i18nKoKR from './i18n/ko-KR.json'
import i18nNlNL from './i18n/nl-NL.json'
import i18nFiFI from './i18n/fi-FI.json'
import i18nRoRO from './i18n/ro-RO.json'
import i18nSlSI from './i18n/sl-SI.json'
import type { BuilderType } from './BeefreeExample'
const UI_LANGUAGES = [
'en-US', 'it-IT', 'es-ES', 'fr-FR', 'de-DE', 'pt-BR',
'id-ID', 'ja-JP', 'zh-CN', 'zh-HK', 'cs-CZ', 'nb-NO',
'da-DK', 'sv-SE', 'pl-PL', 'hu-HU', 'ru-RU', 'ko-KR',
'nl-NL', 'fi-FI', 'ro-RO', 'sl-SI',
]
const I18N_MAP: Record<string, typeof i18nEnUS> = {
'en-US': i18nEnUS,
'it-IT': i18nItIT,
'es-ES': i18nEsES,
'fr-FR': i18nFrFR,
'de-DE': i18nDeDE,
'pt-BR': i18nPtBR,
'id-ID': i18nIdID,
'ja-JP': i18nJaJP,
'zh-CN': i18nZhCN,
'zh-HK': i18nZhHK,
'cs-CZ': i18nCsCZ,
'nb-NO': i18nNbNO,
'da-DK': i18nDaDK,
'sv-SE': i18nSvSE,
'pl-PL': i18nPlPL,
'hu-HU': i18nHuHU,
'ru-RU': i18nRuRU,
'ko-KR': i18nKoKR,
'nl-NL': i18nNlNL,
'fi-FI': i18nFiFI,
'ro-RO': i18nRoRO,
'sl-SI': i18nSlSI,
}
const REACT_LOGO_DATA_URI = 'data:image/svg+xml,%3csvg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'-11.5 -10.232 23 20.463\'%3e%3ccircle r=\'2.05\' fill=\'white\'/%3e%3cg stroke=\'white\' fill=\'none\'%3e%3cellipse rx=\'11\' ry=\'4.2\'/%3e%3cellipse rx=\'11\' ry=\'4.2\' transform=\'rotate(60)\'/%3e%3cellipse rx=\'11\' ry=\'4.2\' transform=\'rotate(120)\'/%3e%3c/g%3e%3c/svg%3e'
export type ToastType = 'success' | 'error' | 'info'
interface ToastState {
message: string
title?: string
type: ToastType
}
export const App = () => {
const [selectedBuilderType, setSelectedBuilderType] = useState<BuilderType>('emailBuilder')
const [selectedBuilderLanguage, setSelectedBuilderLanguage] = useState('en-US')
const [isShared, setIsShared] = useState(false)
const [toast, setToast] = useState<ToastState | null>(null)
const [toastExiting, setToastExiting] = useState(false)
const toastTimers = useRef<ReturnType<typeof setTimeout>[]>([])
const appStrings = useMemo(() => {
const messages = I18N_MAP[selectedBuilderLanguage] ?? i18nEnUS
return { ...messages.app, builderTypes: messages.app.builderTypes as Record<string, string> }
}, [selectedBuilderLanguage])
const clearToastTimers = useCallback(() => {
toastTimers.current.forEach(clearTimeout)
toastTimers.current = []
}, [])
const showToast = useCallback((message: string, type: ToastType = 'info', title?: string, durationMs = 5000) => {
clearToastTimers()
setToastExiting(false)
setToast({ message, type, title })
toastTimers.current.push(
setTimeout(() => setToastExiting(true), durationMs),
setTimeout(() => {
setToast(null)
setToastExiting(false)
}, durationMs + 400),
)
}, [clearToastTimers])
useEffect(() => {
const timer = setTimeout(() => {
showToast(appStrings.welcomeMessage, 'success', appStrings.welcomeTitle)
}, 500)
return () => {
clearTimeout(timer)
clearToastTimers()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return (
<main className="main">
<header>
<div className="left-side">
<img src="assets/logo.svg" height="40" alt="Beefree SDK" />
</div>
<div className="right-side">
<div className="header-controls">
<div className="header-select-group">
<label htmlFor="headerBuilderType">{appStrings.builderLabel}</label>
<select
id="headerBuilderType"
value={selectedBuilderType}
onChange={e => setSelectedBuilderType(e.target.value as BuilderType)}
>
<option value="emailBuilder">{appStrings.builderTypes.emailBuilder}</option>
<option value="pageBuilder">{appStrings.builderTypes.pageBuilder}</option>
<option value="popupBuilder">{appStrings.builderTypes.popupBuilder}</option>
<option value="fileManager">{appStrings.builderTypes.fileManager}</option>
</select>
</div>
<div className="header-select-group">
<label htmlFor="headerLanguage">{appStrings.languageLabel}</label>
<select
id="headerLanguage"
value={selectedBuilderLanguage}
onChange={e => setSelectedBuilderLanguage(e.target.value)}
>
{UI_LANGUAGES.map(lang => (
<option key={lang} value={lang}>{lang}</option>
))}
</select>
</div>
<button
className={`header-coediting-btn${isShared ? ' active' : ''}`}
onClick={() => setIsShared(s => !s)}
disabled={selectedBuilderType === 'fileManager'}
>
{appStrings.coEditing}
</button>
</div>
<div className="react-brand">
<img src={REACT_LOGO_DATA_URI} alt="React" />
<span>React</span>
</div>
</div>
</header>
<div className="content">
<BeefreeExample
builderType={selectedBuilderType}
builderLanguage={selectedBuilderLanguage}
isShared={isShared}
onIsSharedChange={setIsShared}
onNotify={showToast}
/>
</div>
{toast && (
<div className={`toast toast-${toast.type}${toastExiting ? ' toast-exit' : ''}`}>
{toast.title && <h3>{toast.title}</h3>}
<p>{toast.message}</p>
</div>
)}
</main>
)
}