diff --git a/src/App.tsx b/src/App.tsx index 145dbbaa5b..c58fd86f2a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,71 +1,88 @@ -import React, { useState, useEffect, useRef } from "react" +import React, { useState, useEffect, useRef } from "react"; import { createIframeClient, CompilationFileSources, CompilationResult, - Status -} from "@remixproject/plugin" + Status, +} from "@remixproject/plugin"; -import { AppContext } from "./AppContext" -import { Routes } from "./routes" -import { useLocalStorage } from "./hooks/useLocalStorage" -import { createDocumentation } from "./utils/utils" +import { AppContext } from "./AppContext"; +import { Routes } from "./routes"; +import { useLocalStorage } from "./hooks/useLocalStorage"; +import { createDocumentation } from "./utils/utils"; -import "./App.css" -import { ContractName, Documentation } from "./types" +import "./App.css"; +import { ContractName, Documentation } from "./types"; -const devMode = { port: 8080 } +const devMode = { port: 8080 }; export const getNewContractNames = (compilationResult: CompilationResult) => { - const compiledContracts = compilationResult.contracts - let result: string[] = [] + const compiledContracts = compilationResult.contracts; + let result: string[] = []; for (const file of Object.keys(compiledContracts)) { - const newContractNames = Object.keys(compiledContracts[file]) - result = [...result, ...newContractNames] + const newContractNames = Object.keys(compiledContracts[file]); + result = [...result, ...newContractNames]; } - return result -} + return result; +}; -const sampleMap = new Map() +const sampleMap = new Map(); const App = () => { - const [clientInstance, setClientInstance] = useState(undefined as any) - const [contracts, setContracts] = useState(sampleMap) - const [sites, setSites] = useLocalStorage('sites', []) - const clientInstanceRef = useRef(clientInstance) - clientInstanceRef.current = clientInstance - const contractsRef = useRef(contracts) - contractsRef.current = contracts + const [clientInstance, setClientInstance] = useState(undefined as any); + const [contracts, setContracts] = useState(sampleMap); + const [sites, setSites] = useLocalStorage("sites", []); + const clientInstanceRef = useRef(clientInstance); + clientInstanceRef.current = clientInstance; + const contractsRef = useRef(contracts); + contractsRef.current = contracts; useEffect(() => { - console.log("Remix EthDoc loading...") - const client = createIframeClient({ devMode }) + console.log("Remix EthDoc loading..."); + const client = createIframeClient({ devMode }); const loadClient = async () => { - await client.onload() - setClientInstance(client) - console.log("Remix EthDoc Plugin has been loaded") - - client.solidity.on("compilationFinished", (fileName: string, source: CompilationFileSources, languageVersion: string, data: CompilationResult) => { - console.log("New compilation received") - - const existingMap = contractsRef.current; - const newContractsMapWithDocumentation = createDocumentation(fileName, data) - const newMap = new Map([...existingMap, ...newContractsMapWithDocumentation]) - - console.log("New Map", newMap) - - const status: Status = { key: 'succeed', type: 'success', title: 'New documentation ready' } - clientInstanceRef.current.emit('statusChanged', status) - setContracts(newMap) - }) - } - - loadClient() - }, []) - + await client.onload(); + setClientInstance(client); + console.log("Remix EthDoc Plugin has been loaded"); + + client.solidity.on( + "compilationFinished", + ( + fileName: string, + source: CompilationFileSources, + languageVersion: string, + data: CompilationResult + ) => { + console.log("New compilation received"); + + const existingMap = contractsRef.current; + const newContractsMapWithDocumentation = createDocumentation( + fileName, + data + ); + const newMap = new Map([ + ...existingMap, + ...newContractsMapWithDocumentation, + ]); + + console.log("New Map", newMap); + + const status: Status = { + key: "succeed", + type: "success", + title: "New documentation ready", + }; + clientInstanceRef.current.emit("statusChanged", status); + setContracts(newMap); + } + ); + }; + + loadClient(); + }, []); return ( { contracts, setContracts, sites, - setSites - }}> + setSites, + }} + > - ) -} + ); +}; -export default App +export default App; diff --git a/src/AppContext.tsx b/src/AppContext.tsx index 68e626433a..c243fa5103 100644 --- a/src/AppContext.tsx +++ b/src/AppContext.tsx @@ -1,17 +1,23 @@ -import React from "react" -import { PluginApi, IRemixApi, Api, PluginClient, CompilationResult } from "@remixproject/plugin" +import React from "react"; +import { + PluginApi, + IRemixApi, + Api, + PluginClient, + CompilationResult, +} from "@remixproject/plugin"; -import { ContractName, Documentation, PublishedSite } from "./types" +import { ContractName, Documentation, PublishedSite } from "./types"; export const AppContext = React.createContext({ clientInstance: {} as PluginApi> & PluginClient>, contracts: new Map(), setContracts: (contracts: Map) => { - console.log("Calling Set Contract Names") + console.log("Calling Set Contract Names"); }, sites: [], setSites: (sites: PublishedSite[]) => { - console.log("Calling Set Sites") - } -}) + console.log("Calling Set Sites"); + }, +}); diff --git a/src/hooks/useLocalStorage.tsx b/src/hooks/useLocalStorage.tsx index cca4a83a72..d677de7813 100644 --- a/src/hooks/useLocalStorage.tsx +++ b/src/hooks/useLocalStorage.tsx @@ -1,4 +1,4 @@ -import { useState } from "react" +import { useState } from "react"; export function useLocalStorage(key: string, initialValue: any) { // State to store our value @@ -6,15 +6,15 @@ export function useLocalStorage(key: string, initialValue: any) { const [storedValue, setStoredValue] = useState(() => { try { // Get from local storage by key - const item = window.localStorage.getItem(key) + const item = window.localStorage.getItem(key); // Parse stored json or if none return initialValue - return item ? JSON.parse(item) : initialValue + return item ? JSON.parse(item) : initialValue; } catch (error) { // If error also return initialValue - console.log(error) - return initialValue + console.log(error); + return initialValue; } - }) + }); // Return a wrapped version of useState's setter function that ... // ... persists the new value to localStorage. @@ -22,16 +22,16 @@ export function useLocalStorage(key: string, initialValue: any) { try { // Allow value to be a function so we have same API as useState const valueToStore = - value instanceof Function ? value(storedValue) : value + value instanceof Function ? value(storedValue) : value; // Save state - setStoredValue(valueToStore) + setStoredValue(valueToStore); // Save to local storage - window.localStorage.setItem(key, JSON.stringify(valueToStore)) + window.localStorage.setItem(key, JSON.stringify(valueToStore)); } catch (error) { // A more advanced implementation would handle the error case - console.log(error) + console.log(error); } - } + }; - return [storedValue, setValue] + return [storedValue, setValue]; } diff --git a/src/index.tsx b/src/index.tsx index fd29146ba3..e0434dd0ac 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,11 +1,11 @@ -import React from 'react'; -import ReactDOM from 'react-dom'; -import App from './App'; -import { Routes } from './routes' +import React from "react"; +import ReactDOM from "react-dom"; +import App from "./App"; +import { Routes } from "./routes"; ReactDOM.render( , - document.getElementById('root') -); \ No newline at end of file + document.getElementById("root") +); diff --git a/src/routes.tsx b/src/routes.tsx index fa47e5582e..8fad895972 100644 --- a/src/routes.tsx +++ b/src/routes.tsx @@ -1,36 +1,31 @@ -import React from "react" +import React from "react"; import { - BrowserRouter as Router, - Switch, - Route, - RouteProps, -} from "react-router-dom" + BrowserRouter as Router, + Switch, + Route, + RouteProps, +} from "react-router-dom"; -import { ErrorView, HomeView } from "./views" +import { ErrorView, HomeView } from "./views"; interface Props extends RouteProps { - component: any // TODO: new (props: any) => React.Component - from: string + component: any; // TODO: new (props: any) => React.Component + from: string; } const CustomRoute = ({ component: Component, ...rest }: Props) => { - return ( - ( - - )} - /> - ) -} + return ( + } /> + ); +}; export const Routes = () => ( - - - - - - - - -) + + + + + + + + +); diff --git a/src/utils/faker.ts b/src/utils/faker.ts index adf2876370..bd0df7fd5d 100644 --- a/src/utils/faker.ts +++ b/src/utils/faker.ts @@ -1,30 +1,30 @@ -import { CompiledContract, ABIParameter } from '@remixproject/plugin' +import { CompiledContract, ABIParameter } from "@remixproject/plugin"; -import sampleData from './sample-data/sample-artifact.json' -import sampleDataWithComments from './sample-data/sample-artifact-with-comments.json' +import sampleData from "./sample-data/sample-artifact.json"; +import sampleDataWithComments from "./sample-data/sample-artifact-with-comments.json"; export const buildFakeArtifact: () => CompiledContract = () => { - const result = sampleData as never as CompiledContract - return result -} + const result = (sampleData as never) as CompiledContract; + return result; +}; export const buildFakeArtifactWithComments: () => CompiledContract = () => { - const result = sampleDataWithComments as never as CompiledContract - return result -} + const result = (sampleDataWithComments as never) as CompiledContract; + return result; +}; export const buildFakeABIParameter: () => ABIParameter = () => { - return { - internalType: "address", - name: "allocator", - type: "address" - } -} + return { + internalType: "address", + name: "allocator", + type: "address", + }; +}; export const buildFakeABIParameterWithDocumentation: () => ABIParameter = () => { - return { - internalType: "address", - name: "allocator", - type: "address" - } -} \ No newline at end of file + return { + internalType: "address", + name: "allocator", + type: "address", + }; +}; diff --git a/src/utils/index.ts b/src/utils/index.ts index de804ef3f9..2648b019f2 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,2 +1,2 @@ -export * from './utils' -export * from './publisher' \ No newline at end of file +export * from "./utils"; +export * from "./publisher"; diff --git a/src/utils/publisher.test.ts b/src/utils/publisher.test.ts index 4a5e9f1b7a..25a76b8d08 100644 --- a/src/utils/publisher.test.ts +++ b/src/utils/publisher.test.ts @@ -1,19 +1,19 @@ -import { publish } from './publisher'; +import { publish } from "./publisher"; -const open = require('open') +// tslint:disable-next-line +const open = require("open"); -jest.setTimeout(10000) +jest.setTimeout(10000); -describe('Publisher tests', () => { +describe("Publisher tests", () => { + test("it can publish", async () => { + const result = await publish("hello 123"); - test('it can publish', async () => { - const result = await publish("hello 123") + expect(result).toBeDefined(); + }); - expect(result).toBeDefined() - }) - - test('it can publish html', async () => { - const result = await publish(` + test("it can publish html", async () => { + const result = await publish(` @@ -27,14 +27,14 @@ describe('Publisher tests', () => {
Content custom
- `) + `); - // Uncomment for testing + // Uncomment for testing - // const url = `https://ipfs.io/ipfs/${result}`; + // const url = `https://ipfs.io/ipfs/${result}`; - // await open(url, { app: ['google chrome', '--incognito'] }); + // await open(url, { app: ['google chrome', '--incognito'] }); - expect(result).toBeDefined() - }) -}) + expect(result).toBeDefined(); + }); +}); diff --git a/src/utils/publisher.ts b/src/utils/publisher.ts index d15bacab64..07cc6c98ca 100644 --- a/src/utils/publisher.ts +++ b/src/utils/publisher.ts @@ -1,13 +1,18 @@ import { HTMLContent } from "./types"; -const IpfsClient = require('ipfs-mini') +// tslint:disable-next-line +const IpfsClient = require("ipfs-mini"); export const publish = async (content: HTMLContent) => { - const ipfs = new IpfsClient({ host: 'ipfs.infura.io', port: 5001, protocol: 'https' }); + const ipfs = new IpfsClient({ + host: "ipfs.infura.io", + port: 5001, + protocol: "https", + }); - const documentHash = await ipfs.add(content) + const documentHash = await ipfs.add(content); - console.log("Document hash", documentHash) + console.log("Document hash", documentHash); - return documentHash -} + return documentHash; +}; diff --git a/src/utils/template.ts b/src/utils/template.ts index 78ed582dab..aaeedfe57c 100644 --- a/src/utils/template.ts +++ b/src/utils/template.ts @@ -1,5 +1,12 @@ -import { FunctionDocumentation, TemplateDoc, MethodDoc, ContractDoc, ContractDocumentation, ParameterDocumentation } from "./types" -type HTMLContent = string +import { + FunctionDocumentation, + TemplateDoc, + MethodDoc, + ContractDoc, + ContractDocumentation, + ParameterDocumentation, +} from "./types"; +type HTMLContent = string; export const htmlTemplate = (content: HTMLContent) => ` @@ -16,9 +23,13 @@ export const htmlTemplate = (content: HTMLContent) => ` ${content} -` +`; -export const template = (name: string, contractDoc: ContractDocumentation, functions: FunctionDocumentation[]) => ` +export const template = ( + name: string, + contractDoc: ContractDocumentation, + functions: FunctionDocumentation[] +) => `