File size: 31,571 Bytes
9cd6ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
import { useState } from "react";
import { useIntl } from "react-intl";

import { Input } from "@/components/input";
import { Label } from "@/components/label";
import { Collapse } from "@/components/collapse";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faGripVertical } from "@fortawesome/free-solid-svg-icons";
import { ChevronDownIcon } from "@heroicons/react/solid";
import { ChevronRightIcon } from "@heroicons/react/solid";
import classNames from "classnames";
import { TrashIcon } from "@heroicons/react/solid";
import { useUpdateEffect } from "react-use";
import { PlusIcon } from "@heroicons/react/solid";
import { ColorPicker } from "@/components/color-picker";
import { Switch } from "@/components/switch";
import { PaperAirplaneIcon } from "@heroicons/react/solid";
import { rgbaToHex } from "@/utils";
import { useMessage } from "@/components/message-editor/useMessage";

export default function EmbedsEditor() {
  const intl = useIntl();
  const [message, setMessage] = useState({
    content: "",
    embeds: [],
    buttons: [],
  });
  const [webhookUrl, setWebhookUrl] = useState("");

  const { post } = useMessage();

  return (
    <div className="bg-dark-600 w-full flex-col lg:flex-row flex min-h-screen">
      <div className="w-full lg:w-1/2 mx-auto flex flex-col justify-center px-6 lg:px-4 py-10 max-w-2xl">
        <div className="grid grid-cols-2 gap-x-6 gap-y-5 w-full">
          <div className="bg-dark-400 rounded-lg p-4 w-full col-span-2 flex items-center justify-between gap-6">
            <Input
              value={webhookUrl}
              placeholder="Webhook URL"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-100 focus:ring-[3px] ring-blue ring-opacity-80"
              onChange={(url) => setWebhookUrl(url as string)}
            />
            <button
              className="bg-blue hover:bg-opacity-80 text-white font-semibold flex items-center gap-2 px-4 py-3 rounded-md text-sm whitespace-nowrap"
              onClick={() => post(webhookUrl, message)}
            >
              <PaperAirplaneIcon className="w-4 rotate-90" />
              Send message
            </button>
          </div>
          <Form message={message} setMessage={setMessage} />
        </div>
      </div>
      <div className="bg-dark-500 border-l-[2px] border-dark-400 p-10 top-0 sticky w-full lg:w-1/2 h-screen">
        <p className="text-red bg-red bg-opacity-10 px-4 py-3 rounded-md text-center font-semibold">
          preview will be here
        </p>
      </div>
    </div>
  );
}

const Form = ({
  message,
  setMessage,
}: {
  message: any;
  setMessage: (e: any) => void;
}) => {
  return (
    <>
      <Collapse
        open={true}
        title="Author and Content"
        parentClassName="col-span-2"
        onOpenClassName="rounded-b-none"
        className="bg-blue rounded-lg p-4 text-white font-sans text-base uppercase tracking-wider font-semibold"
      >
        <div className="bg-dark-400 rounded-b-lg p-5 grid grid-cols-2 gap-4">
          <div>
            <Label className="mb-2.5">
              Username
              <span className="font-sans font-regular opacity-50 text-xs ml-1">
                {message?.username?.length ?? 0}/80
              </span>
            </Label>
            <Input
              value={message?.username}
              placeholder="Username"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-100 focus:ring-[3px] ring-blue ring-opacity-80"
              onChange={(username) => setMessage({ ...message, username })}
            />
          </div>
          <div>
            <Label className="mb-2.5">Avatar URL</Label>
            <Input
              value={message?.avatar_url}
              placeholder="Avatar URL"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-100 focus:ring-[3px] ring-blue ring-opacity-80"
              onChange={(avatar_url) => setMessage({ ...message, avatar_url })}
            />
          </div>
          <div className="col-span-2">
            <Label className="mb-2.5">
              CONTENT{" "}
              <span className="font-sans font-regular opacity-50 text-xs ml-1">
                {message?.content?.length ?? 0}/2000
              </span>
            </Label>
            <Input
              type="textarea"
              value={message.content}
              placeholder="Content"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-100 focus:ring-[3px] ring-blue ring-opacity-80"
              onChange={(content) => setMessage({ ...message, content })}
            />
          </div>
        </div>
      </Collapse>
      <Collapse
        open={true}
        title={
          <p>
            Embeds{" "}
            <span className="text-sm opacity-60 ml-2">
              {message?.embeds?.length ?? 0} / 10
            </span>
          </p>
        }
        parentClassName="col-span-2"
        onOpenClassName="rounded-b-none"
        className="bg-blue rounded-lg p-4 text-white font-sans text-base uppercase tracking-wider font-semibold"
      >
        <div className="bg-dark-400 rounded-b-lg p-5 grid grid-cols-1 gap-4">
          <Embeds
            embeds={message?.embeds}
            setEmbeds={(embeds) => setMessage({ ...message, embeds })}
          />
        </div>
      </Collapse>
      <Collapse
        open={true}
        title={
          <p>
            Link Buttons
            <span className="text-sm opacity-60 ml-2">
              {message?.buttons?.length ?? 0} / 25
            </span>
          </p>
        }
        parentClassName="col-span-2"
        onOpenClassName="rounded-b-none"
        className="bg-blue rounded-lg p-4 text-white font-sans text-base uppercase tracking-wider font-semibold"
      >
        <div className="bg-dark-400 rounded-b-lg p-5 grid grid-cols-1 gap-4">
          <Buttons
            buttons={message?.buttons}
            setButtons={(buttons) => setMessage({ ...message, buttons })}
          />
        </div>
      </Collapse>
    </>
  );
};

const Embeds = ({
  embeds,
  setEmbeds,
}: {
  embeds: any[];
  setEmbeds: (e: any) => void;
}) => {
  const [open, setOpen] = useState(0);

  const onDelete = (i: number) => {
    const newEmbeds = embeds ?? [];
    newEmbeds.splice(i, 1);
    setEmbeds(newEmbeds);
  };

  const setEmbed = (embed: any, index: number) => {
    const newEmbeds = embeds ?? [];
    newEmbeds[index] = embed;
    setEmbeds(newEmbeds);
  };

  useUpdateEffect(() => setOpen(embeds?.length - 1), [embeds]);

  return (
    <>
      {embeds?.map((embed: any, i: number) => (
        <div key={i} className="flex">
          <div
            className="h-full w-[3px] rounded-l-lg"
            style={{ backgroundColor: embed?.color ?? "#fff" }}
          />
          <div className="bg-[#2f3136] rounded-r-lg w-full">
            <div
              className={classNames(
                "px-4 pt-4 pb-2 flex justify-between items-center text-white font-semibold cursor-pointer",
                {
                  "!pb-4": open !== i,
                }
              )}
              onClick={() => setOpen(open === i ? -1 : i)}
            >
              <div className="flex items-center justify-start gap-1 flex-1">
                <ChevronRightIcon
                  className={classNames("w-5", {
                    "rotate-90": open === i,
                  })}
                />
                <p>Embed {i + 1}</p>
              </div>
              <div
                className="flex items-center justify-end gap-2"
                onClick={(e) => {
                  e.preventDefault();
                  e.stopPropagation();
                }}
              >
                <TrashIcon
                  className="w-5 hover:text-danger"
                  onClick={() => onDelete(i)}
                />
              </div>
            </div>
            {open === i && (
              <div className="px-4 pb-4 grid grid-cols-2 gap-2">
                <Author
                  author={embed?.author}
                  setAuthor={(author) => setEmbed({ ...embed, author }, i)}
                />
                <Body
                  embed={embed}
                  setEmbed={(embed) => setEmbed({ ...embed }, i)}
                />
                <Fields
                  fields={embed?.fields}
                  setFields={(fields) => setEmbed({ ...embed, fields }, i)}
                />
                <Images
                  embed={embed}
                  setEmbed={(newEmbed) => setEmbed({ ...newEmbed }, i)}
                />
                <Footer
                  embed={embed}
                  setEmbed={(newEmbed) => setEmbed({ ...newEmbed }, i)}
                />
              </div>
            )}
          </div>
          {embeds?.length > 1 && (
            <div className="flex items-center justify-center">
              <FontAwesomeIcon
                icon={faGripVertical}
                className="w-8 text-dark-100"
              />
            </div>
          )}
        </div>
      ))}
      <div className="w-full flex gap-3">
        <button
          className="bg-transparent flex items-center gap-2 border-2 border-dashed border-darkGreen group rounded-md text-darkGreen hover:border-solid bg-opacity-10 hover:text-white transition-all duration-200 px-4 py-3 text-sm font-semibold relative z-1"
          onClick={() => {
            setEmbeds([
              ...(embeds ?? []),
              {
                color: "#ff00ff",
              },
            ]);
          }}
        >
          <PlusIcon className="w-4" />
          Add Embed
          <div className="absolute left-0 h-full top-0 bg-darkGreen z-[-1] group-hover:w-full w-0 transition-all duration-200" />
        </button>
        <button
          className="bg-transparent flex items-center gap-2 border-2 border-dashed border-danger group rounded-md text-danger hover:border-solid bg-opacity-10 hover:text-white transition-all duration-200 px-4 py-3 text-sm font-semibold relative z-1"
          onClick={() => setEmbeds([])}
        >
          <TrashIcon className="w-4" />
          Clear embeds
          <div className="absolute left-0 h-full top-0 bg-danger z-[-1] group-hover:w-full w-0 transition-all duration-200" />
        </button>
      </div>
    </>
  );
};
const Buttons = ({
  buttons,
  setButtons,
}: {
  buttons: any[];
  setButtons: (e: any) => void;
}) => {
  const [open, setOpen] = useState(0);

  const onDelete = (i: number) => {
    const newButtons = buttons ?? [];
    newButtons.splice(i, 1);
    setButtons(newButtons);
  };

  const setButton = (button: any, index: number) => {
    const newButtons = buttons ?? [];
    newButtons[index] = button;
    setButtons(newButtons);
  };

  useUpdateEffect(() => setOpen(buttons?.length - 1), [buttons]);

  return (
    <>
      {buttons?.map((button: any, i: number) => (
        <div key={i} className="flex">
          <div className="bg-[#2f3136] rounded-r-lg w-full">
            <div
              className={classNames(
                "px-4 pt-4 pb-2 flex justify-between items-center text-white font-semibold cursor-pointer",
                {
                  "!pb-4": open !== i,
                }
              )}
              onClick={() => setOpen(open === i ? -1 : i)}
            >
              <div className="flex items-center justify-start gap-1 flex-1">
                <ChevronRightIcon
                  className={classNames("w-5", {
                    "rotate-90": open === i,
                  })}
                />
                <p>Button {i + 1}</p>
              </div>
              <div
                className="flex items-center justify-end gap-2"
                onClick={(e) => {
                  e.preventDefault();
                  e.stopPropagation();
                }}
              >
                <TrashIcon
                  className="w-5 hover:text-danger"
                  onClick={() => onDelete(i)}
                />
              </div>
            </div>
            {open === i && (
              <div className="px-4 pb-4 grid grid-cols-2 gap-2">
                <div className="col-span-2">
                  <Label className="mb-2.5 !text-xs">
                    Title
                    <span className="font-sans font-regular opacity-50 text-xs ml-1">
                      {button?.label?.length ?? 0}/80
                    </span>
                  </Label>
                  <Input
                    value={button?.label}
                    placeholder="Label"
                    className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
                    onChange={(label) => setButton({ ...button, label }, i)}
                  />
                </div>
                <div className="col-span-2">
                  <Label className="mb-2.5 !text-xs">URL</Label>
                  <Input
                    value={button?.url}
                    placeholder="Label"
                    className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
                    onChange={(url) => setButton({ ...button, url }, i)}
                  />
                </div>
                <div className="col-span-2 grid grid-cols-2 gap-4 mt-1">
                  <div>
                    <Label className="mb-2.5 !text-xs">Native Emoji</Label>
                    <Input
                      value={button?.emoji?.name}
                      placeholder="Native Emoji"
                      className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
                      onChange={(name) =>
                        setButton(
                          {
                            ...button,
                            emoji: { ...button?.emoji?.button, name },
                          },
                          i
                        )
                      }
                    />
                  </div>
                  <div>
                    <Label className="mb-2.5 !text-xs">Custom Emoji</Label>
                    <Input
                      value={button?.emoji?.id}
                      placeholder="Custom Emoji"
                      className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
                      onChange={(id) =>
                        setButton(
                          {
                            ...button,
                            emoji: { ...button?.emoji?.button, id },
                          },
                          i
                        )
                      }
                    />
                  </div>
                </div>
              </div>
            )}
          </div>
          {buttons?.length > 1 && (
            <div className="flex items-center justify-center">
              <FontAwesomeIcon
                icon={faGripVertical}
                className="w-8 text-dark-100"
              />
            </div>
          )}
        </div>
      ))}
      <div className="w-full flex gap-3">
        <button
          className="bg-transparent flex items-center gap-2 border-2 border-dashed border-darkGreen group rounded-md text-darkGreen hover:border-solid bg-opacity-10 hover:text-white transition-all duration-200 px-4 py-3 text-sm font-semibold relative z-1"
          onClick={() => {
            setButtons([
              ...(buttons ?? []),
              {
                label: "New Button",
                type: 2,
                style: 5,
              },
            ]);
          }}
        >
          <PlusIcon className="w-4" />
          Add button
          <div className="absolute left-0 h-full top-0 bg-darkGreen z-[-1] group-hover:w-full w-0 transition-all duration-200" />
        </button>
        <button
          className="bg-transparent flex items-center gap-2 border-2 border-dashed border-danger group rounded-md text-danger hover:border-solid bg-opacity-10 hover:text-white transition-all duration-200 px-4 py-3 text-sm font-semibold relative z-1"
          onClick={() => setButtons([])}
        >
          <TrashIcon className="w-4" />
          Clear buttons
          <div className="absolute left-0 h-full top-0 bg-danger z-[-1] group-hover:w-full w-0 transition-all duration-200" />
        </button>
      </div>
    </>
  );
};

const Author = ({
  author,
  setAuthor,
}: {
  author: any;
  setAuthor: (a: any) => void;
}) => {
  const [open, setOpen] = useState(true);
  return (
    <div className="col-span-2">
      <div
        className="text-white text-sm font-semibold tracking-wider flex items-center justify-start gap-1 cursor-pointer hover:bg-dark-400 px-2 py-1.5 rounded"
        onClick={() => setOpen(!open)}
      >
        <ChevronRightIcon
          className={classNames("w-5", {
            "rotate-90": open,
          })}
        />
        Author
      </div>
      {open && (
        <div className="grid grid-cols-2 gap-4 mt-2 pl-3">
          <div className="col-span-2">
            <Label className="mb-2.5 !text-xs">
              Author
              <span className="font-sans font-regular opacity-50 text-xs ml-1">
                {author?.name?.length ?? 0}/256
              </span>
            </Label>
            <Input
              value={author?.name}
              placeholder="Author name"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(name) => setAuthor({ ...author, name })}
            />
          </div>
          <div>
            <Label className="mb-2.5 !text-xs">Author URL</Label>
            <Input
              value={author?.url}
              placeholder="Author URL"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(url) => setAuthor({ ...author, url })}
            />
          </div>
          <div>
            <Label className="mb-2.5 !text-xs">Author Icon URL</Label>
            <Input
              value={author?.icon_url}
              placeholder="Author Icon URL"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(icon_url) => setAuthor({ ...author, icon_url })}
            />
          </div>
        </div>
      )}
    </div>
  );
};

const Body = ({
  embed,
  setEmbed,
}: {
  embed: any;
  setEmbed: (a: any) => void;
}) => {
  const [open, setOpen] = useState(true);
  return (
    <div className="col-span-2">
      <div
        className="text-white text-sm font-semibold tracking-wider flex items-center justify-start gap-1 cursor-pointer hover:bg-dark-400 px-2 py-1.5 rounded"
        onClick={() => setOpen(!open)}
      >
        <ChevronRightIcon
          className={classNames("w-5", {
            "rotate-90": open,
          })}
        />
        Body
      </div>
      {open && (
        <div className="grid grid-cols-2 gap-x-4 gap-y-3 mt-2 pl-3">
          <div className="col-span-2">
            <Label className="mb-2.5 !text-xs">
              Title
              <span className="font-sans font-regular opacity-50 text-xs ml-1">
                {embed?.title?.length ?? 0}/256
              </span>
            </Label>
            <Input
              value={embed?.title}
              placeholder="Title"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(title) => setEmbed({ ...embed, title })}
            />
          </div>
          <div className="col-span-2">
            <Label className="mb-2.5 !text-xs">
              Description{" "}
              <span className="font-sans font-regular opacity-50 text-xs ml-1">
                {embed?.description?.length ?? 0}/4096
              </span>
            </Label>
            <Input
              type="textarea"
              value={embed.description}
              placeholder="Content"
              className="bg-dark-600 bg-opacity-80 rounded-md p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(description) => setEmbed({ ...embed, description })}
            />
          </div>
          <div>
            <Label className="mb-2.5 !text-xs">Title URL</Label>
            <Input
              value={embed?.url}
              placeholder="Title URL"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(url) => setEmbed({ ...embed, url })}
            />
          </div>
          <div>
            <Label className="mb-2.5 !text-xs">Embed Color</Label>
            <div className="flex gap-3">
              <Input
                value={embed?.color}
                placeholder="Embed color"
                className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
                onChange={(color) => setEmbed({ ...embed, color })}
              />
              <ColorPicker
                data={embed}
                full
                className="h-[42px] w-[44px] min-w-[44px] rounded"
                value={embed?.color}
                gradients={false}
                onChange={(c: any, datas) => {
                  setEmbed({ ...embed, color: rgbaToHex(c) });
                }}
              />
            </div>
          </div>
        </div>
      )}
    </div>
  );
};

const Images = ({
  embed,
  setEmbed,
}: {
  embed: any;
  setEmbed: (a: any) => void;
}) => {
  const [open, setOpen] = useState(true);
  return (
    <div className="col-span-2">
      <div
        className="text-white text-sm font-semibold tracking-wider flex items-center justify-start gap-1 cursor-pointer hover:bg-dark-400 px-2 py-1.5 rounded"
        onClick={() => setOpen(!open)}
      >
        <ChevronRightIcon
          className={classNames("w-5", {
            "rotate-90": open,
          })}
        />
        Images
      </div>
      {open && (
        <div className="grid grid-cols-2 gap-x-4 gap-y-3 mt-2 pl-3">
          <div>
            <Label className="mb-2.5 !text-xs">Image URL</Label>
            <Input
              value={embed?.image?.url}
              placeholder="Image URL"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(url) => setEmbed({ ...embed, image: { url } })}
            />
          </div>
          <div>
            <Label className="mb-2.5 !text-xs">Thumbnail URL</Label>
            <Input
              value={embed?.thumbnail?.url}
              placeholder="Thumbnail URL"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(url) => setEmbed({ ...embed, thumbnail: { url } })}
            />
          </div>
        </div>
      )}
    </div>
  );
};

const Fields = ({
  fields,
  setFields,
}: {
  fields: any;
  setFields: (a: any) => void;
}) => {
  const [open, setOpen] = useState(true);
  return (
    <div className="col-span-2">
      <div
        className="text-white text-sm font-semibold tracking-wider flex items-end justify-start gap-1 cursor-pointer hover:bg-dark-400 px-2 py-1.5 rounded"
        onClick={() => setOpen(!open)}
      >
        <ChevronRightIcon
          className={classNames("w-5", {
            "rotate-90": open,
          })}
        />
        Fields
        <span className="opacity-50 text-xs">{fields?.length ?? 0}/25</span>
      </div>
      {open && (
        <div className="grid grid-cols-2 gap-4 mt-2 pl-3">
          {fields?.map((field: any, i: number) => (
            <Field
              key={i}
              index={i}
              field={field}
              setField={(newField) => {
                const newFields = fields ?? [];
                newFields[i] = newField;
                setFields(newFields);
              }}
              onDelete={() => {
                const newFields = fields ?? [];
                newFields.splice(i, 1);
                setFields(newFields);
              }}
            />
          ))}
          <div className="w-full flex gap-3 col-span-2">
            <button
              className="bg-transparent flex items-center gap-1 border-2 border-dashed border-darkGreen group rounded-md text-darkGreen hover:border-solid bg-opacity-10 hover:text-white transition-all duration-200 px-2.5 py-1.5 text-[13px] font-semibold relative z-1"
              onClick={() => {
                setFields([
                  ...(fields ?? []),
                  {
                    name: "Name",
                    value: "Value",
                    inline: true,
                  },
                ]);
              }}
            >
              <PlusIcon className="w-3" />
              Add Field
              <div className="absolute left-0 h-full top-0 bg-darkGreen z-[-1] group-hover:w-full w-0 transition-all duration-200" />
            </button>
            <button
              className="bg-transparent flex items-center gap-1 border-2 border-dashed border-danger group rounded-md text-danger hover:border-solid bg-opacity-10 hover:text-white transition-all duration-200 px-2.5 py-1.5 text-[13px] font-semibold relative z-1"
              onClick={() => setFields([])}
            >
              <TrashIcon className="w-3" />
              Clear Fields
              <div className="absolute left-0 h-full top-0 bg-danger z-[-1] group-hover:w-full w-0 transition-all duration-200" />
            </button>
          </div>
        </div>
      )}
    </div>
  );
};

const Field = ({
  field,
  index,
  setField,
  onDelete,
}: {
  field: any;
  index: number;
  setField: (e: any) => void;
  onDelete: () => void;
}) => {
  const [open, setOpen] = useState(true);
  return (
    <div className="border-2 border-dashed border-dark-200 rounded-lg col-span-2 grid grid-cols-2 gap-4 p-3">
      <div
        className="col-span-2 flex items-center justify-between gap-1 flex-1 text-sm text-white font-semibold cursor-pointer w-full"
        onClick={() => setOpen(!open)}
      >
        <div className="flex items-center justify-start gap-1">
          <ChevronRightIcon
            className={classNames("w-4", {
              "rotate-90": open,
            })}
          />
          <p>Field #{index + 1}</p>
        </div>
        <div
          className="flex items-center justify-end gap-2"
          onClick={(e) => {
            e.preventDefault();
            e.stopPropagation();
          }}
        >
          <TrashIcon className="w-4 hover:text-danger" onClick={onDelete} />
        </div>
      </div>
      {open && (
        <>
          <div className="col-span-2 flex items-center justify-start gap-4">
            <div className="w-full">
              <Label className="mb-2.5 !text-xs">
                Title
                <span className="font-sans font-regular opacity-50 text-xs ml-1">
                  {field?.name?.length ?? 0}/256
                </span>
              </Label>
              <Input
                value={field?.name}
                placeholder="Field Name"
                className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
                onChange={(name) => setField({ ...field, name })}
              />
            </div>
            <div>
              <Label className="mb-2.5 !text-xs">Inline</Label>
              <Switch
                value={field?.inline}
                onChange={() => setField({ ...field, inline: !field?.inline })}
              />
            </div>
          </div>
          <div className="col-span-2">
            <Label className="mb-2.5 !text-xs">
              Field value{" "}
              <span className="font-sans font-regular opacity-50 text-xs ml-1">
                {field?.value?.length ?? 0}/1024
              </span>
            </Label>
            <Input
              type="textarea"
              value={field.value}
              placeholder="Content"
              className="bg-dark-600 bg-opacity-80 rounded-md p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(value) => setField({ ...field, value })}
            />
          </div>
        </>
      )}
    </div>
  );
};

const Footer = ({
  embed,
  setEmbed,
}: {
  embed: any;
  setEmbed: (a: any) => void;
}) => {
  const [open, setOpen] = useState(true);
  return (
    <div className="col-span-2">
      <div
        className="text-white text-sm font-semibold tracking-wider flex items-center justify-start gap-1 cursor-pointer hover:bg-dark-400 px-2 py-1.5 rounded"
        onClick={() => setOpen(!open)}
      >
        <ChevronRightIcon
          className={classNames("w-5", {
            "rotate-90": open,
          })}
        />
        Body
      </div>
      {open && (
        <div className="grid grid-cols-2 gap-x-4 gap-y-3 mt-2 pl-3">
          <div className="col-span-2">
            <Label className="mb-2.5 !text-xs">
              Footer Text
              <span className="font-sans font-regular opacity-50 text-xs ml-1">
                {embed?.footer?.name ?? 0}/256
              </span>
            </Label>
            <Input
              value={embed?.footer?.text}
              placeholder="Footer Text"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(text) =>
                setEmbed({ ...embed, footer: { ...embed?.footer, text } })
              }
            />
          </div>
          <div>
            <Label className="mb-2.5 !text-xs">Footer Icon Url</Label>
            <Input
              value={embed?.footer?.icon_url}
              placeholder="Footer Icon URL"
              className="bg-dark-600 bg-opacity-80 rounded-md p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(icon_url) =>
                setEmbed({ ...embed, footer: { ...embed?.footer, icon_url } })
              }
            />
          </div>
          <div>
            <Label className="mb-2.5 !text-xs">Timestamp</Label>
            <Input
              value={embed?.timestamp}
              placeholder="Timestamp"
              className="bg-dark-600 bg-opacity-80 rounded p-3 text-sm text-white placeholder-dark-200 focus:ring-[3px] ring-white ring-opacity-50"
              onChange={(timestamp) =>
                setEmbed({ ...embed, footer: { ...embed?.footer, timestamp } })
              }
            />
          </div>
        </div>
      )}
    </div>
  );
};