File size: 11,473 Bytes
9cd6ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0fd9a7
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
import { useContext, useState } from "react";
import Image from "next/image";
import { useTour } from "@reactour/tour";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faDownload } from "@fortawesome/free-solid-svg-icons";
import saveAsPng from "save-svg-as-png";

import { EditorType, IconItem } from "types/editor";
import { Icon } from "./icon";
import { EditorTabs } from "components/editor-icons/comps/tabs";
import { ListIcons } from "components/editor-icons/comps/list";
import { Search } from "components/search";
import { ClydeMessage } from "components/clyde/message";
import { Switch } from "components/switch";
import { Shapes } from "@/components/editor-icons/comps/shapes";
import { Icons } from "components/editor-icons/comps/icons";
import { useUser } from "utils/auth";
import { PremiumContext } from "components/premium/premium";

import DefaultAvatar from "assets/images/avatars/default-avatar.svg";
import DefaultAvatar2 from "assets/images/avatars/default-avatar-2.svg";
import BackgroundTransparent from "assets/images/editor/transparent_bg.svg";
import { API } from "@/utils/api";
import { Premium } from "../premium";
import { FormattedMessage, useIntl } from "react-intl";

export default function Editor({
  editor,
  onChange,
}: {
  editor: EditorType;
  onChange: (e: EditorType) => void;
}) {
  const intl = useIntl();
  const { user } = useUser();
  const { setOpen } = useContext(PremiumContext);
  const { setIsOpen } = useTour();

  const [currentTab, setCurrentTab] = useState(0);
  const [multiIcons, setMultiIcons] = useState(false);
  const [search, setSearch] = useState("");

  const renderTabs = (currentTab: number) => {
    switch (currentTab) {
      case 0:
        return (
          <>
            <Search search={search} setSearch={setSearch} />
            <div className="flex items-center justify-start gap-2 mt-4 lg:mt-3 mb-5">
              <Switch
                value={multiIcons}
                size="small"
                onChange={!user?.id ? () => setOpen(true) : setMultiIcons}
              />
              <p className="text-dark-100 text-sm font-semibold tracking-wider select-none flex items-center justify-start gap-2">
                <FormattedMessage id="iconsEditor.editor.listIcons.multiIcons" />
                {/* {!user?.id ? <Premium tooltip={false} /> : ""} */}
              </p>
            </div>
            <ListIcons
              onCustomText={() => {
                // if (!user?.id) return setOpen(true);
                onChange({
                  ...editor,
                  icons: multiIcons
                    ? [
                        ...editor.icons,
                        {
                          colour: "#ffffff",
                          custom_text: {
                            enabled: true,
                            text: "10",
                            size: 160,
                          },
                        },
                      ]
                    : [
                        {
                          ...editor?.icons[0],
                          component: undefined,
                          position: undefined,
                          colour: "#ffffff",
                          custom_text: {
                            enabled: true,
                            text: "10",
                            size: 160,
                          },
                        },
                      ],
                });
                if (!multiIcons) setCurrentTab(2);
              }}
              onCustomUpload={(image) => {
                onChange({
                  ...editor,
                  icons: multiIcons
                    ? [
                        ...editor.icons,
                        {
                          colour: "#ffffff",
                          image,
                        },
                      ]
                    : [
                        {
                          ...editor?.icons[0],
                          custom_text: undefined,
                          component: undefined,
                          position: undefined,
                          colour: "#ffffff",
                          image,
                        },
                      ],
                });
                if (!multiIcons) setCurrentTab(2);
              }}
              onSelect={(icon: IconItem) => {
                onChange({
                  ...editor,
                  icons: multiIcons
                    ? [
                        ...editor.icons,
                        {
                          component: icon.name,
                          colour: icon?.defaultColor ?? "#fff",
                        },
                      ]
                    : [
                        {
                          ...editor?.icons[0],
                          colour: icon?.defaultColor ?? "#fff",
                          component: icon.name,
                          position: undefined,
                          custom_text: undefined,
                        },
                      ],
                });
              }}
            />
          </>
        );
      case 1:
        return (
          <>
            <Shapes
              editor={editor}
              onChange={(newShape: any) =>
                onChange({ ...editor, shape: { ...editor.shape, ...newShape } })
              }
            />
          </>
        );
      case 2:
        return (
          <>
            <Icons
              editor={editor}
              onChange={onChange}
              onStep={(tabIndex: number, allowMultiple?: boolean) => {
                setCurrentTab(tabIndex);
                setMultiIcons(user?.id ? allowMultiple || false : false);
              }}
            />
          </>
        );
      default:
        return null;
    }
  };

  const handleSaveIcon = () => {
    const svg = document.getElementById("discotools-selected-svg");

    saveAsPng.saveSvgAsPng(svg, "discotools-xyz-icon.png", {
      scale: 1.25,
      encoderOptions: 1,
    });
    // API.download();
  };

  const previewAvatar = () => {
    if (user?.id) {
      if (user?.avatar)
        return `https://cdn.discordapp.com/avatars/${user?.id}/${user?.avatar}.png`;
      else
        return `https://cdn.discordapp.com/embed/avatars/${
          user.discriminator % 5
        }.png`;
    }
    return DefaultAvatar.src;
  };

  const renderColor = () => {
    if (editor?.shape?.gradient?.enabled) {
      const color = editor?.shape?.gradient?.colours?.[0]?.value;
      if (color.includes("rgba")) {
        const rgba = color?.split(",");
        return `rgb(${rgba[0].split("(")[1]}, ${rgba[1]}, ${rgba[2]})`;
      }
      return color;
    }
    if (editor?.shape?.colour?.includes("rgba")) {
      const rgba = editor?.shape?.colour?.split(",");
      return `rgb(${rgba[0].split("(")[1]}, ${rgba[1]}, ${rgba[2]})`;
    }
    if (editor?.shape?.colour) {
      return editor.shape.colour;
    }
    return "#fff";
  };

  return (
    <div className="relative mt-20 max-w-6xl mx-auto z-1">
      <ClydeMessage
        message={intl.formatMessage({ id: "helper.title" })}
        auto
        onClick={() => setIsOpen(true)}
      />
      <div
        className="bg-dark-500 shadow-lg grid grid-cols-1 lg:grid-cols-5 rounded-2xl lg:max-h-[700px] lg:h-[700px]"
        onClick={(e) => {
          // e.preventDefault();
          // e.stopPropagation();
        }}
      >
        <div className="lg:col-span-3 flex flex-col justify-start overflow-hidden">
          <EditorTabs current={currentTab} onChange={setCurrentTab} />
          <div className="px-6 pb-6 pt-4 w-full overflow-y-auto h-full">
            {renderTabs(currentTab)}
          </div>
        </div>
        <div className="bg-dark-500 rounded-b-2xl lg:rounded-r-2xl col-span-2 flex flex-col flex-1 h-full">
          <header
            className="relative p-5 lg:p-8 flex items-center lg:rounded-tr-2xl justify-center bg-repeat h-[300px] w-full z-10 transition-all duration-200 first-step"
            style={{
              backgroundImage: `url(${BackgroundTransparent.src})`,
            }}
          >
            <Icon
              editor={editor}
              id="discotools-selected-svg"
              onChange={onChange}
              onChangeTab={(i: number) => setCurrentTab(i)}
            />
          </header>
          <main className="lg:border-l border-dark-300 flex flex-1 flex-col px-5 py-6 justify-between gap-6">
            <div>
              <div className="grid grid-cols-1 gap-5">
                <div className="flex items-start gap-3 justify-start">
                  <img
                    src={previewAvatar()}
                    width={32}
                    height={32}
                    alt="Default Avatar for user"
                    className="rounded-full object-cover w-10 h-10"
                  />
                  <div className="text-left">
                    <div className="flex items-center justify-start gap-1">
                      <p
                        className="font-semibold text-base"
                        style={{ color: renderColor() }}
                      >
                        Captain Astro
                      </p>
                      <Icon size={24} editor={editor} />

                      <p className="text-white text-opacity-50 text-[10px]">
                        Today at 11:23 pm
                      </p>
                    </div>
                    <p className="text-white text-opacity-60 text-sm mt-0.5">
                      <FormattedMessage id="iconsEdtior.editor.preview.text1" />
                    </p>
                  </div>
                </div>
                <div className="flex items-start gap-3 justify-start">
                  <Image
                    src={DefaultAvatar2}
                    width={32}
                    height={32}
                    alt="Second user without custom roles icons"
                    className="rounded-full object-cover w-10 h-10"
                  />
                  <div className="text-left">
                    <div className="flex items-center justify-start gap-1">
                      <p className="font-semibold text-base text-white">
                        Rookie
                      </p>
                      <p className="text-white text-opacity-50 text-[10px] ml-1">
                        Today at 11:23 pm
                      </p>
                    </div>
                    <p className="text-white text-opacity-60 text-sm mt-0.5">
                      <FormattedMessage
                        id="iconsEdtior.editor.preview.text2"
                        values={{
                          user: (t) => user?.username ?? "Captain Astro",
                        }}
                      />
                    </p>
                  </div>
                </div>
              </div>
            </div>
            <button
              className="text-white font-medium bg-blue hover:bg-blue tracking-wide text-base px-5 py-2.5 rounded justify-center flex items-center gap-2"
              onClick={handleSaveIcon}
            >
              <FontAwesomeIcon icon={faDownload} className="w-4" />
              <FormattedMessage id="iconsEdtior.editor.preview.download" />
            </button>
          </main>
        </div>
      </div>
    </div>
  );
}