-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.tsx
409 lines (392 loc) · 12.1 KB
/
Main.tsx
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import React, { ComponentType, ReactElement, useEffect, useState } from 'react';
import { NavLink, Route, useHistory, useLocation } from 'react-router-dom';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Helmet } from 'react-helmet';
import { ipcRenderer } from 'electron';
import { v4 as uuidv4 } from 'uuid';
import classNames from 'classnames';
import MarkdownToHtml from './markdown/MarkdownToHtml';
import UnixTimestamp from './timestamp/UnixTimestamp';
import HtmlPreview from './html/HtmlPreview';
import QrCodeGenerator from './qrcode/QrCodeGenerator';
import Base64 from './base64/Base64';
import DiffText from './diff/TextDiff';
import SqlFormatter from './sql/SqlFormatter';
import JsonFormatter from './json/JsonFormatter';
import QRCodeReader from './qrcode/QrCodeReader';
import RegexTester from './regex/RegexTester';
import JwtDebugger from './jwt/JwtDebugger';
import CustomScript from './custom-script/CustomScript';
import Auto from './auto/Auto';
import CronEditor from './cron/Cron';
import JsConsole from './notebook/JavaScript';
import HtmlEntityCodec from './html/HtmlEntityCodec';
import UrlCodec from './url/UrlCodec';
import BackSlashCodec from './text/BackSlash';
import LoremIpsum from './text/LoremIpsum';
interface MenuItem {
path: string;
name: string;
show: boolean;
icon: ReactElement<any, any>;
Component: ComponentType;
}
const defaultRoutes: MenuItem[] = [
{
icon: <FontAwesomeIcon icon="robot" />,
path: '/auto',
name: 'Auto Detection',
show: true,
Component: Auto,
},
{
icon: <FontAwesomeIcon icon="clock" />,
path: '/unix-converter',
name: 'Unix Time Converter',
show: true,
Component: UnixTimestamp,
},
{
icon: <FontAwesomeIcon icon="retweet" />,
path: '/cron-editor',
name: 'Cron Editor',
show: true,
Component: CronEditor,
},
{
icon: <FontAwesomeIcon icon="registered" />,
path: '/regex-tester',
name: 'Regex Tester',
show: true,
Component: RegexTester,
},
{
icon: <FontAwesomeIcon icon="random" />,
path: '/lorem-ipsum',
name: 'Lorem Ipsum Generator',
show: true,
Component: LoremIpsum,
},
{
icon: <FontAwesomeIcon icon={['fab', 'markdown']} />,
path: '/markdown-to-html',
name: 'Markdown to HTML',
show: true,
Component: MarkdownToHtml,
},
{
icon: <FontAwesomeIcon icon={['fab', 'html5']} />,
path: '/html-preview',
name: 'HTML Preview',
show: true,
Component: HtmlPreview,
},
{
icon: <FontAwesomeIcon icon="qrcode" />,
path: '/qrcode-generator',
name: 'QRCode Generator',
show: true,
Component: QrCodeGenerator,
},
{
icon: <FontAwesomeIcon icon="camera" />,
path: '/qrcode-reader',
name: 'QRCode Reader',
show: true,
Component: QRCodeReader,
},
{
icon: <FontAwesomeIcon icon="code" />,
path: '/base64-encoder',
name: 'Base64 Encoder',
show: true,
Component: Base64,
},
{
icon: <FontAwesomeIcon icon="exchange-alt" />,
path: '/text-diff',
name: 'Text Diff',
show: true,
Component: DiffText,
},
{
icon: <FontAwesomeIcon icon={['fab', 'js-square']} />,
path: '/json-formatter',
name: 'JSON Formatter',
show: true,
Component: JsonFormatter,
},
{
icon: <FontAwesomeIcon icon="database" />,
path: '/sql-formatter',
name: 'SQL Formatter',
show: true,
Component: SqlFormatter,
},
{
icon: <FontAwesomeIcon icon="key" />,
path: '/jwt-debugger',
name: 'JWT Debugger',
show: true,
Component: JwtDebugger,
},
{
icon: <FontAwesomeIcon icon={['fab', 'js']} />,
path: '/js-console',
name: 'Js Console',
show: false,
Component: JsConsole,
},
{
icon: <FontAwesomeIcon icon="file-code" />,
path: '/html-entity-encoder',
name: 'HTML Entity Encoder',
show: false,
Component: HtmlEntityCodec,
},
{
icon: <FontAwesomeIcon icon="link" />,
path: '/url-encoder',
name: 'URL Encoder',
show: false,
Component: UrlCodec,
},
{
icon: <FontAwesomeIcon icon="slash" transform={{ rotate: 42 }} />,
path: '/back-slash-encoder',
name: 'Backslash Encoder',
show: false,
Component: BackSlashCodec,
},
];
const Main = () => {
const [allRoutes, setAllRoutes] = useState<MenuItem[]>([]);
const [routes, setRoutes] = useState<MenuItem[]>([]);
const [search, setSearch] = useState('');
const [editMenu, setEditMenu] = useState(false);
const [activeMenuItemPath, setActiveMenuItemPath] = useState('');
const [activeMenuItemName, setActiveMenuItemName] = useState('');
const history = useHistory();
const location = useLocation();
const handleSearch = (e: { target: { value: string } }) => {
setSearch(e.target.value);
};
useEffect(() => {
if (search.trim()) {
setRoutes(
allRoutes.filter(({ name }) => name.match(new RegExp(search, 'gi')))
);
} else if (editMenu) {
setRoutes(allRoutes);
} else {
setRoutes(allRoutes.filter((r) => r.show));
}
}, [search, editMenu]);
useEffect(() => {
const routeMap: Record<string, boolean> = allRoutes.reduce(
(a, b) => ({ ...a, [b.path]: b.show }),
{}
);
setRoutes(allRoutes.filter((r) => editMenu || routeMap[r.path]));
if (allRoutes.length) {
ipcRenderer.invoke('set-store', { key: 'left-menu', value: routeMap });
}
}, [allRoutes]);
useEffect(() => {
setActiveMenuItemPath('');
}, [location]);
useEffect(() => {
const routeMap: Record<string, boolean> = defaultRoutes.reduce(
(a, b) => ({ ...a, [b.path]: b.show }),
{}
);
ipcRenderer
.invoke('get-store', { key: 'left-menu' })
.then((map) => {
if (map) {
const routeList = defaultRoutes.map((r) => ({
...r,
show:
map[r.path] === true || map[r.path] === false
? map[r.path]
: routeMap[r.path],
}));
setAllRoutes(routeList);
} else {
setAllRoutes(defaultRoutes);
}
return null;
})
.catch(console.error);
ipcRenderer.on('hotkey-pressed', () => {
history.push('/auto', { auto: true });
});
}, []);
const handleAddNewMenuItem = () => {
const id = uuidv4();
const routeList = [
...allRoutes,
{
icon: <FontAwesomeIcon icon="slash" transform={{ rotate: 42 }} />,
path: `/custom-script-${id}`,
name: `Custom Script ${id.slice(0, 5)}`,
show: true,
Component: CustomScript,
},
];
setAllRoutes(routeList);
};
const handleDeleteMenuItem = (path: MenuItem['path']) => {
setAllRoutes(allRoutes.filter((r) => r.path !== activeMenuItemPath));
setActiveMenuItemPath('');
setActiveMenuItemName('');
ipcRenderer.invoke('delete-store', path);
};
const handleSaveMenuItemEdit = () => {
setAllRoutes(
allRoutes.map((r) =>
r.path === activeMenuItemPath ? { ...r, name: activeMenuItemName } : r
)
);
setActiveMenuItemPath('');
setActiveMenuItemName('');
};
return (
<div className="absolute inset-0 flex flex-col overflow-hidden">
<main className="relative flex flex-1 min-h-0">
{/* Left sidebar */}
<nav className="flex flex-col flex-shrink-0 w-1/4 overflow-x-hidden overflow-y-auto bg-gray-300">
{/* Search */}
<div className="flex items-center justify-between px-2 mx-3 mt-6 text-gray-400 bg-gray-200 rounded-md focus-within:text-gray-600 focus-within:ring-2 focus-within:ring-blue-500">
<FontAwesomeIcon icon="search" className="mr-1" />
<input
type="text"
className="w-full p-1 bg-gray-200 border-none rounded-r-md focus:ring-0"
value={search}
onChange={handleSearch}
placeholder="Search..."
/>
{search && (
<FontAwesomeIcon
icon="times-circle"
onClick={() => setSearch('')}
className="mr-2 cursor-pointer"
/>
)}
<FontAwesomeIcon
icon={editMenu ? 'check' : 'sliders-h'}
onClick={() => {
setEditMenu(!editMenu);
if (editMenu) handleSaveMenuItemEdit();
}}
className={classNames({
'text-gray-400 cursor-pointer hover:text-gray-600': true,
'text-blue-500 hover:text-blue-600': editMenu,
})}
/>
</div>
<div
className="px-2 my-6"
role="menu"
aria-orientation="horizontal"
aria-labelledby="options-menu"
>
{routes.map(({ path, name, icon, show }) => (
<section
key={path}
className="flex items-center justify-between space-x-2"
>
{(activeMenuItemPath === path && (
<input
className="flex items-center justify-start flex-1 px-3 py-1 mb-1 space-x-1 rounded-lg"
value={activeMenuItemName}
onChange={(evt) => {
setActiveMenuItemName(evt.target.value);
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleSaveMenuItemEdit();
}
}}
/>
)) || (
<NavLink
to={path}
className="flex items-center justify-start flex-1 px-3 py-1 mb-1 space-x-1 rounded-lg"
activeClassName="bg-blue-400 text-white"
>
<span className="w-6">{icon}</span>
{name}
</NavLink>
)}
{editMenu && (
<>
{location.pathname === path && path.startsWith('/custom-') && (
<>
{(!activeMenuItemPath && (
<button
type="button"
onClick={() => {
setActiveMenuItemPath(path);
setActiveMenuItemName(name);
}}
className="w-10 btn"
>
edit
</button>
)) || (
<button
type="button"
onClick={() => handleDeleteMenuItem(path)}
className="w-12 btn text-white bg-red-500"
>
x
</button>
)}
</>
)}
<input
type="checkbox"
checked={show}
onChange={() =>
setAllRoutes(
allRoutes.map((r) =>
r.path === path ? { ...r, show: !show } : r
)
)
}
className="w-4 h-4 rounded cursor-pointer"
/>
</>
)}
</section>
))}
{editMenu && (
<button
type="button"
className="btn"
onClick={handleAddNewMenuItem}
>
+ Add your script
</button>
)}
</div>
</nav>
{/* Main content */}
<section className="relative flex flex-col w-3/4 bg-gray-200">
<div className="h-full px-6 my-6 overflow-x-hidden overflow-y-auto">
{allRoutes.map(({ path, name, Component }) => (
<Route key={path} exact path={path}>
<Component />
<Helmet>
<title>{name}</title>
</Helmet>
</Route>
))}
</div>
</section>
</main>
</div>
);
};
export default Main;