From a0039b1502277a88ec38e640584d6b2e685b7e0f Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Thu, 27 Feb 2020 12:16:05 +0100 Subject: [PATCH] use location.origin instead of hardcoded https://siasky.net --- src/LocationContext.js | 3 + src/components/CodeExamples/CodeExamples.js | 14 +- src/components/HomeUpload/HomeUpload.js | 162 ++++++++++---------- src/pages/index.js | 7 +- 4 files changed, 95 insertions(+), 91 deletions(-) create mode 100644 src/LocationContext.js diff --git a/src/LocationContext.js b/src/LocationContext.js new file mode 100644 index 00000000..577570f0 --- /dev/null +++ b/src/LocationContext.js @@ -0,0 +1,3 @@ +import { createContext } from "react"; + +export default createContext(null); diff --git a/src/components/CodeExamples/CodeExamples.js b/src/components/CodeExamples/CodeExamples.js index c2f28b25..9334888f 100644 --- a/src/components/CodeExamples/CodeExamples.js +++ b/src/components/CodeExamples/CodeExamples.js @@ -1,10 +1,11 @@ -import React, { useState } from "react"; +import React, { useState, useContext } from "react"; import classNames from "classnames"; import { Light as SyntaxHighlighter } from "react-syntax-highlighter"; import { javascript, go, python, bash } from "react-syntax-highlighter/dist/esm/languages/hljs"; import Colors from "./Colors"; import * as snippets from "./Code"; import "./CodeExamples.scss"; +import LocationContext from "../../LocationContext"; SyntaxHighlighter.registerLanguage("javascript", javascript); SyntaxHighlighter.registerLanguage("go", go); @@ -13,6 +14,9 @@ SyntaxHighlighter.registerLanguage("bash", bash); export default function CodeExamples() { const [active, setActive] = useState(1); + const location = useContext(LocationContext); + const interpolateRegExp = new RegExp("https://siasky.net", "g"); + const interpolateSnippet = snippet => snippet.replace(interpolateRegExp, location.origin); return (
@@ -34,25 +38,25 @@ export default function CodeExamples() {
{active === 1 && ( - {snippets.curl} + {interpolateSnippet(snippets.curl)} )} {active === 2 && ( - {snippets.python} + {interpolateSnippet(snippets.python)} )} {active === 3 && ( - {snippets.node} + {interpolateSnippet(snippets.node)} )} {active === 4 && ( - {snippets.go} + {interpolateSnippet(snippets.go)} )}
diff --git a/src/components/HomeUpload/HomeUpload.js b/src/components/HomeUpload/HomeUpload.js index 48da244f..4620aee8 100644 --- a/src/components/HomeUpload/HomeUpload.js +++ b/src/components/HomeUpload/HomeUpload.js @@ -1,4 +1,4 @@ -import React, { Component } from "react"; +import React, { useState, useContext } from "react"; import classNames from "classnames"; import Dropzone from "react-dropzone"; import Reveal from "react-reveal/Reveal"; @@ -6,30 +6,28 @@ import shortid from "shortid"; import { Button, UploadFile } from "../"; import { Deco3, Deco4, Deco5, Folder, DownArrow } from "../../svg"; import "./HomeUpload.scss"; +import LocationContext from "../../LocationContext"; -export default class HomeUpload extends Component { - state = { files: [] }; +export default function HomeUpload() { + const [files, setFiles] = useState([]); + const location = useContext(LocationContext); - handleDrop = async acceptedFiles => { - this.setState({ - files: [...acceptedFiles.map(file => ({ file, status: "uploading" })), ...this.state.files] - }); + const handleDrop = async acceptedFiles => { + setFiles(previousFiles => [...acceptedFiles.map(file => ({ file, status: "uploading" })), ...previousFiles]); const onComplete = (file, status, skylink) => { - this.setState(state => { - const index = state.files.findIndex(f => f.file === file); + setFiles(previousFiles => { + const index = previousFiles.findIndex(f => f.file === file); - return { - files: [ - ...state.files.slice(0, index), - { - ...state.files[index], - status, - url: `https://siasky.net/${skylink}` - }, - ...state.files.slice(index + 1) - ] - }; + return [ + ...previousFiles.slice(0, index), + { + ...previousFiles[index], + status, + url: `${location.origin}/${skylink}` + }, + ...previousFiles.slice(index + 1) + ]; }); }; @@ -39,7 +37,7 @@ export default class HomeUpload extends Component { fd.append("file", file); const uuid = shortid.generate(); - const response = await fetch(`/skynet/skyfile/${uuid}`, { method: "POST", body: fd }); + const response = await fetch(`${location.origin}/skynet/skyfile/${uuid}`, { method: "POST", body: fd }); const { skylink } = await response.json(); onComplete(file, "complete", skylink); @@ -49,7 +47,7 @@ export default class HomeUpload extends Component { }); }; - handleSkylink = event => { + const handleSkylink = event => { event.preventDefault(); const skylink = event.target.skylink.value.replace("sia://", ""); @@ -59,71 +57,69 @@ export default class HomeUpload extends Component { } }; - render() { - return ( - -
-
-
-
- this.handleDrop(acceptedFiles)}> - {({ getRootProps, getInputProps, isDragActive }) => ( - <> -
- -

Upload your Files

- Drop your files here to pin to Skynet -
- -
- - - )} -
-
- -
-
- -

Paste the link to retrieve your file

- -
- - -
-
-
+ return ( + +
+
+
+
+ + {({ getRootProps, getInputProps, isDragActive }) => ( + <> +
+ +

Upload your Files

+ Drop your files here to pin to Skynet +
+ +
+ + + )} +
- {this.state.files.length > 0 && ( -
- {this.state.files.map((file, i) => { - return ; - })} +
+
+ +

Paste the link to retrieve your file

+ +
+ + +
- )} +
-

- Upon uploading a file, Skynet generates a 46 byte link called a Skylink. This link can then - be shared with anyone to retrieve the file on any Skynet Webportal. -

- - - - + {files.length > 0 && ( +
+ {files.map((file, i) => { + return ; + })} +
+ )}
- - ); - } + +

+ Upon uploading a file, Skynet generates a 46 byte link called a Skylink. This link can then + be shared with anyone to retrieve the file on any Skynet Webportal. +

+ + + + +
+ + ); } diff --git a/src/pages/index.js b/src/pages/index.js index 039a6f93..1212dcf7 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -2,12 +2,13 @@ import React from "react"; import SEO from "../components/seo"; import { App } from "../components"; import "../global.scss"; +import LocationContext from "../LocationContext"; -const IndexPage = () => ( - <> +const IndexPage = ({ location }) => ( + - + ); export default IndexPage;