Skip to content

Commit

Permalink
Improve Code Formatting and Copy on Quickstart page (#705)
Browse files Browse the repository at this point in the history
Co-authored-by: gitstart-lotus <[email protected]>
  • Loading branch information
gitstart-lotus and gitstart authored Mar 19, 2023
1 parent ccefe73 commit 33238c9
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 11 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"react-query-devtools": "2.6.3",
"react-router-dom": "6.3.0",
"react-spinners": "0.13.4",
"react-syntax-highlighter": "^15.5.0",
"react-toastify": "9.0.8",
"sql-formatter": "^12.0.5",
"universal-cookie": "4.0.4",
Expand Down
63 changes: 54 additions & 9 deletions frontend/src/pages/Quickstart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import React, { FC, useState, useEffect, useMemo } from "react";
import { PageLayout } from "../components/base/PageLayout";
import { useQuery } from "react-query";
import useGlobalStore from "../stores/useGlobalstore";
import CopyText from "../components/base/CopytoClipboard";
import quickStartCheck from "../helpers/quickStartCheck";
import { toast } from "react-toastify";
import SyntaxHighlighter from "react-syntax-highlighter";
import { docco } from "react-syntax-highlighter/dist/esm/styles/hljs";
import { CheckCircleOutlined, CopyOutlined } from "@ant-design/icons";
import { Tooltip } from "antd";

interface Props {
text: string;
subText: string;
Expand All @@ -16,8 +20,9 @@ interface Props {
highlighted?: boolean;
}

const CodeExample = () => {
const CodeExample = ({ complete }: { complete: boolean }) => {
const [selectedTab, setSelectedTab] = useState("curl");
const [copySuccess, setCopySuccess] = useState(false);

const codeExamples = {
curl: {
Expand Down Expand Up @@ -66,6 +71,18 @@ const CodeExample = () => {
setSelectedTab(tab);
};

const copyToClipBoard = async (copyMe) => {
try {
await navigator.clipboard.writeText(copyMe);
setCopySuccess(true);
setTimeout(() => {
setCopySuccess(false);
}, 3000);
} catch (err) {
setCopySuccess(false);
}
};

return (
<div className="p-4 pt-0 bg-[#F5F5F5] w-full">
<div className="flex space-x-4 mb-12">
Expand All @@ -83,12 +100,40 @@ const CodeExample = () => {
</button>
))}
</div>
<CopyText
textToCopy={codeExamples[selectedTab].code}
className=" text-sm mx-8"
showIcon
language={codeExamples[selectedTab].language}
/>
<div
className={`relative rounded-md border p-4 ${
complete ? "bg-[#E3FFF1]" : "bg-slate-100"
}`}
>
<SyntaxHighlighter
customStyle={{
...docco,
marginBottom: "0px",
background: "transparent",
}}
language={codeExamples[selectedTab].language}
>
{codeExamples[selectedTab].code}
</SyntaxHighlighter>
<div className="absolute bottom-4 right-4 cursor-pointer">
<Tooltip
placement="right"
title={
copySuccess ? (
<div className="copiedTag">
<CheckCircleOutlined className="checkedIcon" /> Copied
</div>
) : (
<span>Copy Code</span>
)
}
>
<CopyOutlined
onClick={() => copyToClipBoard(codeExamples[selectedTab].code)}
/>
</Tooltip>
</div>
</div>
</div>
);
};
Expand Down Expand Up @@ -179,7 +224,7 @@ const quickStartItem = ({
<div className="absolute bottom-0 left-0 h-1 w-full bg-primary" />
)}
</div>
<CodeExample />
<CodeExample complete={complete} />
</>
);
};
Expand Down
Loading

0 comments on commit 33238c9

Please sign in to comment.