From dfcbc1a15004f4911f7cec87ab85ab7758046701 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Mon, 6 Sep 2021 02:09:12 -0300 Subject: [PATCH] Extract hook --- src/address/Contract.tsx | 27 +++------------------------ src/useSourcify.ts | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/address/Contract.tsx b/src/address/Contract.tsx index e946391..1a30583 100644 --- a/src/address/Contract.tsx +++ b/src/address/Contract.tsx @@ -1,11 +1,10 @@ -import React, { useState, useEffect } from "react"; +import React from "react"; import { Light as SyntaxHighlighter } from "react-syntax-highlighter"; import hljs from "highlight.js"; import docco from "react-syntax-highlighter/dist/esm/styles/hljs/docco"; -import { sourcifySourceFile } from "../url"; - import hljsDefineSolidity from "highlightjs-solidity"; +import { useContract } from "../useSourcify"; hljsDefineSolidity(hljs); type ContractProps = { @@ -21,27 +20,7 @@ const Contract: React.FC = ({ filename, source, }) => { - const [content, setContent] = useState(source.content); - useEffect(() => { - if (source.content) { - return; - } - - 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); - } - }; - readContent(); - }, [checksummedAddress, networkId, filename, source.content]); + const content = useContract(checksummedAddress, networkId, filename, source); return ( { + const [content, setContent] = useState(source.content); + + useEffect(() => { + if (source.content) { + return; + } + + 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); + } + }; + readContent(); + }, [checksummedAddress, networkId, filename, source.content]); + + return content; +};