2021-07-24 23:33:45 +00:00
|
|
|
import React, { useState } from "react";
|
2021-07-23 18:10:08 +00:00
|
|
|
import Highlight from "react-highlight";
|
|
|
|
|
|
|
|
import "highlight.js/styles/stackoverflow-light.css";
|
|
|
|
import hljs from "highlight.js";
|
|
|
|
import hljsDefineSolidity from "highlightjs-solidity";
|
2021-07-24 23:33:45 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import { sourcifySourceFile } from "../url";
|
2021-07-23 18:10:08 +00:00
|
|
|
hljsDefineSolidity(hljs);
|
|
|
|
hljs.initHighlightingOnLoad();
|
|
|
|
|
|
|
|
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-24 23:33:45 +00:00
|
|
|
<Highlight className="w-full h-full border focus:outline-none font-code text-base">
|
|
|
|
{content}
|
|
|
|
</Highlight>
|
2021-07-23 18:10:08 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(Contract);
|