otterscan/src/address/Contract.tsx

55 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-07-24 23:33:45 +00:00
import React, { useState } from "react";
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";
hljsDefineSolidity(hljs);
hljs.initHighlightingOnLoad();
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-24 23:33:45 +00:00
<Highlight className="w-full h-full border focus:outline-none font-code text-base">
{content}
</Highlight>
);
};
export default React.memo(Contract);