2021-07-25 07:03:27 +00:00
|
|
|
import React, { useState, useEffect } from "react";
|
2021-07-25 07:10:42 +00:00
|
|
|
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
|
2021-07-23 18:10:08 +00:00
|
|
|
import hljs from "highlight.js";
|
2021-07-25 07:10:42 +00:00
|
|
|
import docco from "react-syntax-highlighter/dist/esm/styles/hljs/docco";
|
2021-07-25 07:03:27 +00:00
|
|
|
|
2021-07-24 23:33:45 +00:00
|
|
|
import { sourcifySourceFile } from "../url";
|
2021-07-25 07:03:27 +00:00
|
|
|
|
|
|
|
import hljsDefineSolidity from "highlightjs-solidity";
|
2021-07-23 18:10:08 +00:00
|
|
|
hljsDefineSolidity(hljs);
|
|
|
|
|
|
|
|
type ContractProps = {
|
|
|
|
checksummedAddress: string;
|
2021-07-24 23:33:45 +00:00
|
|
|
networkId: number;
|
|
|
|
filename: string;
|
|
|
|
source: any;
|
2021-07-23 18:10:08 +00:00
|
|
|
};
|
|
|
|
|
2021-07-24 23:33:45 +00:00
|
|
|
const Contract: React.FC<ContractProps> = ({
|
|
|
|
checksummedAddress,
|
|
|
|
networkId,
|
|
|
|
filename,
|
|
|
|
source,
|
|
|
|
}) => {
|
|
|
|
const [content, setContent] = useState<string>(source.content);
|
2021-07-23 18:10:08 +00:00
|
|
|
useEffect(() => {
|
2021-07-24 23:33:45 +00:00
|
|
|
if (source.content) {
|
2021-07-23 18:10:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-24 23:33:45 +00:00
|
|
|
const readContent = async () => {
|
|
|
|
const normalizedFilename = filename.replaceAll("@", "_");
|
|
|
|
const url = sourcifySourceFile(
|
|
|
|
checksummedAddress,
|
|
|
|
networkId,
|
|
|
|
normalizedFilename
|
|
|
|
);
|
|
|
|
const res = await fetch(url);
|
|
|
|
if (res.ok) {
|
|
|
|
const _content = await res.text();
|
|
|
|
setContent(_content);
|
2021-07-23 18:10:08 +00:00
|
|
|
}
|
|
|
|
};
|
2021-07-24 23:33:45 +00:00
|
|
|
readContent();
|
|
|
|
}, [checksummedAddress, networkId, filename, source.content]);
|
2021-07-23 18:36:31 +00:00
|
|
|
|
2021-07-23 18:10:08 +00:00
|
|
|
return (
|
2021-07-25 07:10:42 +00:00
|
|
|
<SyntaxHighlighter
|
|
|
|
className="w-full h-full border font-code text-base"
|
|
|
|
language="solidity"
|
|
|
|
style={docco}
|
|
|
|
showLineNumbers
|
|
|
|
>
|
2021-07-25 07:03:27 +00:00
|
|
|
{content ?? ""}
|
|
|
|
</SyntaxHighlighter>
|
2021-07-23 18:10:08 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(Contract);
|