enzostvs HF staff commited on
Commit
f480cae
·
1 Parent(s): da67388

rework snippet card

Browse files
components/editor/main/snippet/index.tsx CHANGED
@@ -3,11 +3,29 @@ import { useCopyToClipboard } from "react-use";
3
  import classNames from "classnames";
4
  import Highlight from "react-highlight";
5
  import { Options } from "redaxios";
6
- import { BiSolidCopy } from "react-icons/bi";
 
 
 
 
 
7
 
8
  import { ApiRoute } from "@/utils/type";
9
 
10
- const LANGUAGES = ["curl", "javascript", "python"];
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  export const Snippet = ({
13
  endpoint,
@@ -18,11 +36,10 @@ export const Snippet = ({
18
  parameters?: Record<string, any>;
19
  body?: Options | undefined;
20
  }) => {
21
- const [language, setLanguage] = useState<string>(LANGUAGES[0]);
22
  const [_, copyToClipboard] = useCopyToClipboard();
23
  const [isCopied, setIsCopied] = useState<boolean>(false);
24
 
25
- const generateRequestFromEndpoint = () => {
26
  const { method, path } = endpoint;
27
 
28
  const needBody = ["post", "put", "patch", "delete"].includes(
@@ -94,8 +111,8 @@ export const Snippet = ({
94
  return "";
95
  };
96
 
97
- const handleCopyToClipboard = () => {
98
- copyToClipboard(generateRequestFromEndpoint());
99
  setIsCopied(true);
100
  setTimeout(() => {
101
  setIsCopied(false);
@@ -104,48 +121,37 @@ export const Snippet = ({
104
 
105
  return (
106
  <div className="mt-8 grid grid-cols-1 gap-4 w-full">
107
- <p className="text-slate-200 uppercase text-xs font-semibold">Snippet</p>
108
- <div className="bg-slate-950/50 rounded-xl overflow-hidden">
109
- <ul className="bg-slate-950 flex items-center justify-start">
110
- {LANGUAGES.map((lang, key) => (
111
- <li
112
- key={key}
113
- className={classNames(
114
- "py-4 text-slate-300 text-xs font-semibold px-6 border-r border-slate-900 cursor-pointer hover:bg-slate-900/80 transition-all duration-75",
115
- {
116
- "bg-slate-900/50 hover:!bg-slate-900/50": lang === language,
117
- }
118
- )}
119
- onClick={() => setLanguage(lang)}
120
  >
121
- {lang}
122
- </li>
123
- ))}
124
- </ul>
125
- <main className="px-6 py-6">
126
- <Highlight
127
- className={`${language} text-xs font-code !bg-transparent !p-0 !whitespace-pre-wrap break-all !leading-relaxed`}
128
- >
129
- {generateRequestFromEndpoint()}
130
- </Highlight>
131
- <div
132
- className="flex justify-end relative"
133
- onClick={handleCopyToClipboard}
134
- >
135
- <BiSolidCopy className="text-slate-500 cursor-pointer hover:text-slate-300 transition-all duration-75" />
136
  <div
137
- className={classNames(
138
- "bg-indigo-500/60 text-slate-100 text-xs font-semibold absolute bottom-0 right-0 z-10 rounded-lg px-2 py-1 pointer-events-none -translate-y-full transition-all duration-200",
139
- {
140
- "opacity-0": !isCopied,
141
- }
142
- )}
143
  >
144
- Copied!
 
 
 
 
 
 
 
 
 
 
145
  </div>
146
- </div>
147
- </main>
148
- </div>
149
  </div>
150
  );
151
  };
 
3
  import classNames from "classnames";
4
  import Highlight from "react-highlight";
5
  import { Options } from "redaxios";
6
+ import {
7
+ BiSolidCopy,
8
+ BiLogoJavascript,
9
+ BiLogoPython,
10
+ BiCodeCurly,
11
+ } from "react-icons/bi";
12
 
13
  import { ApiRoute } from "@/utils/type";
14
 
15
+ const LANGUAGES = [
16
+ {
17
+ value: "curl",
18
+ icon: <BiCodeCurly className=" text-xl text-slate-300" />,
19
+ },
20
+ {
21
+ value: "javascript",
22
+ icon: <BiLogoJavascript className=" text-xl text-yellow-500" />,
23
+ },
24
+ {
25
+ value: "python",
26
+ icon: <BiLogoPython className=" text-xl text-blue-500" />,
27
+ },
28
+ ];
29
 
30
  export const Snippet = ({
31
  endpoint,
 
36
  parameters?: Record<string, any>;
37
  body?: Options | undefined;
38
  }) => {
 
39
  const [_, copyToClipboard] = useCopyToClipboard();
40
  const [isCopied, setIsCopied] = useState<boolean>(false);
41
 
42
+ const generateRequestFromEndpoint = (language: string) => {
43
  const { method, path } = endpoint;
44
 
45
  const needBody = ["post", "put", "patch", "delete"].includes(
 
111
  return "";
112
  };
113
 
114
+ const handleCopyToClipboard = (language: string) => {
115
+ copyToClipboard(generateRequestFromEndpoint(language));
116
  setIsCopied(true);
117
  setTimeout(() => {
118
  setIsCopied(false);
 
121
 
122
  return (
123
  <div className="mt-8 grid grid-cols-1 gap-4 w-full">
124
+ {LANGUAGES.map((lang, key) => (
125
+ <div key={key} className="bg-slate-950/50 rounded-xl overflow-hidden">
126
+ <header className="bg-slate-950 flex items-center justify-start px-5 py-4 uppercase gap-2">
127
+ {lang.icon}
128
+ <p className="text-slate-300 text-xs font-semibold">{lang.value}</p>
129
+ </header>
130
+ <main className="px-6 py-6">
131
+ <Highlight
132
+ className={`${lang.value} text-xs font-code !bg-transparent !p-0 !whitespace-pre-wrap break-all !leading-relaxed`}
 
 
 
 
133
  >
134
+ {generateRequestFromEndpoint(lang.value)}
135
+ </Highlight>
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  <div
137
+ className="flex justify-end relative"
138
+ onClick={() => handleCopyToClipboard(lang.value)}
 
 
 
 
139
  >
140
+ <BiSolidCopy className="text-slate-500 cursor-pointer hover:text-slate-300 transition-all duration-75" />
141
+ <div
142
+ className={classNames(
143
+ "bg-indigo-500/60 text-slate-100 text-xs font-semibold absolute bottom-0 right-0 z-10 rounded-lg px-2 py-1 pointer-events-none -translate-y-full transition-all duration-200",
144
+ {
145
+ "opacity-0": !isCopied,
146
+ }
147
+ )}
148
+ >
149
+ Copied!
150
+ </div>
151
  </div>
152
+ </main>
153
+ </div>
154
+ ))}
155
  </div>
156
  );
157
  };