otterscan/src/address/Contract.tsx

59 lines
1.4 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect } from "react";
2021-07-25 07:10:42 +00:00
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
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-24 23:33:45 +00:00
import { sourcifySourceFile } from "../url";
import hljsDefineSolidity from "highlightjs-solidity";
hljsDefineSolidity(hljs);
type ContractProps = {
checksummedAddress: string;
2021-07-24 23:33:45 +00:00
networkId: number;
filename: string;
source: any;
};
2021-07-24 23:33:45 +00:00
const Contract: React.FC<ContractProps> = ({
checksummedAddress,
networkId,
filename,
source,
}) => {
const [content, setContent] = useState<string>(source.content);
useEffect(() => {
2021-07-24 23:33:45 +00:00
if (source.content) {
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-24 23:33:45 +00:00
readContent();
}, [checksummedAddress, networkId, filename, source.content]);
2021-07-23 18:36:31 +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
>
{content ?? ""}
</SyntaxHighlighter>
);
};
export default React.memo(Contract);