use location.origin instead of hardcoded https://siasky.net

This commit is contained in:
Karol Wypchlo 2020-02-27 12:16:05 +01:00
parent 2180a60c5a
commit a0039b1502
4 changed files with 95 additions and 91 deletions

3
src/LocationContext.js Normal file
View File

@ -0,0 +1,3 @@
import { createContext } from "react";
export default createContext(null);

View File

@ -1,10 +1,11 @@
import React, { useState } from "react"; import React, { useState, useContext } from "react";
import classNames from "classnames"; import classNames from "classnames";
import { Light as SyntaxHighlighter } from "react-syntax-highlighter"; import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
import { javascript, go, python, bash } from "react-syntax-highlighter/dist/esm/languages/hljs"; import { javascript, go, python, bash } from "react-syntax-highlighter/dist/esm/languages/hljs";
import Colors from "./Colors"; import Colors from "./Colors";
import * as snippets from "./Code"; import * as snippets from "./Code";
import "./CodeExamples.scss"; import "./CodeExamples.scss";
import LocationContext from "../../LocationContext";
SyntaxHighlighter.registerLanguage("javascript", javascript); SyntaxHighlighter.registerLanguage("javascript", javascript);
SyntaxHighlighter.registerLanguage("go", go); SyntaxHighlighter.registerLanguage("go", go);
@ -13,6 +14,9 @@ SyntaxHighlighter.registerLanguage("bash", bash);
export default function CodeExamples() { export default function CodeExamples() {
const [active, setActive] = useState(1); 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 ( return (
<div className="code-examples"> <div className="code-examples">
@ -34,25 +38,25 @@ export default function CodeExamples() {
<div className="code-examples-body"> <div className="code-examples-body">
{active === 1 && ( {active === 1 && (
<SyntaxHighlighter wrapLines showLineNumbers={true} language="bash" style={Colors}> <SyntaxHighlighter wrapLines showLineNumbers={true} language="bash" style={Colors}>
{snippets.curl} {interpolateSnippet(snippets.curl)}
</SyntaxHighlighter> </SyntaxHighlighter>
)} )}
{active === 2 && ( {active === 2 && (
<SyntaxHighlighter wrapLines showLineNumbers={true} language="python" style={Colors}> <SyntaxHighlighter wrapLines showLineNumbers={true} language="python" style={Colors}>
{snippets.python} {interpolateSnippet(snippets.python)}
</SyntaxHighlighter> </SyntaxHighlighter>
)} )}
{active === 3 && ( {active === 3 && (
<SyntaxHighlighter wrapLines showLineNumbers={true} language="javascript" style={Colors}> <SyntaxHighlighter wrapLines showLineNumbers={true} language="javascript" style={Colors}>
{snippets.node} {interpolateSnippet(snippets.node)}
</SyntaxHighlighter> </SyntaxHighlighter>
)} )}
{active === 4 && ( {active === 4 && (
<SyntaxHighlighter wrapLines showLineNumbers={true} language="go" style={Colors}> <SyntaxHighlighter wrapLines showLineNumbers={true} language="go" style={Colors}>
{snippets.go} {interpolateSnippet(snippets.go)}
</SyntaxHighlighter> </SyntaxHighlighter>
)} )}
</div> </div>

View File

@ -1,4 +1,4 @@
import React, { Component } from "react"; import React, { useState, useContext } from "react";
import classNames from "classnames"; import classNames from "classnames";
import Dropzone from "react-dropzone"; import Dropzone from "react-dropzone";
import Reveal from "react-reveal/Reveal"; import Reveal from "react-reveal/Reveal";
@ -6,30 +6,28 @@ import shortid from "shortid";
import { Button, UploadFile } from "../"; import { Button, UploadFile } from "../";
import { Deco3, Deco4, Deco5, Folder, DownArrow } from "../../svg"; import { Deco3, Deco4, Deco5, Folder, DownArrow } from "../../svg";
import "./HomeUpload.scss"; import "./HomeUpload.scss";
import LocationContext from "../../LocationContext";
export default class HomeUpload extends Component { export default function HomeUpload() {
state = { files: [] }; const [files, setFiles] = useState([]);
const location = useContext(LocationContext);
handleDrop = async acceptedFiles => { const handleDrop = async acceptedFiles => {
this.setState({ setFiles(previousFiles => [...acceptedFiles.map(file => ({ file, status: "uploading" })), ...previousFiles]);
files: [...acceptedFiles.map(file => ({ file, status: "uploading" })), ...this.state.files]
});
const onComplete = (file, status, skylink) => { const onComplete = (file, status, skylink) => {
this.setState(state => { setFiles(previousFiles => {
const index = state.files.findIndex(f => f.file === file); const index = previousFiles.findIndex(f => f.file === file);
return { return [
files: [ ...previousFiles.slice(0, index),
...state.files.slice(0, index),
{ {
...state.files[index], ...previousFiles[index],
status, status,
url: `https://siasky.net/${skylink}` url: `${location.origin}/${skylink}`
}, },
...state.files.slice(index + 1) ...previousFiles.slice(index + 1)
] ];
};
}); });
}; };
@ -39,7 +37,7 @@ export default class HomeUpload extends Component {
fd.append("file", file); fd.append("file", file);
const uuid = shortid.generate(); 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(); const { skylink } = await response.json();
onComplete(file, "complete", skylink); onComplete(file, "complete", skylink);
@ -49,7 +47,7 @@ export default class HomeUpload extends Component {
}); });
}; };
handleSkylink = event => { const handleSkylink = event => {
event.preventDefault(); event.preventDefault();
const skylink = event.target.skylink.value.replace("sia://", ""); const skylink = event.target.skylink.value.replace("sia://", "");
@ -59,14 +57,13 @@ export default class HomeUpload extends Component {
} }
}; };
render() {
return ( return (
<Reveal effect="active"> <Reveal effect="active">
<div className="home-upload"> <div className="home-upload">
<div className="home-upload-white fadeInUp delay4"> <div className="home-upload-white fadeInUp delay4">
<div className="home-upload-split"> <div className="home-upload-split">
<div className="home-upload-box "> <div className="home-upload-box ">
<Dropzone onDrop={acceptedFiles => this.handleDrop(acceptedFiles)}> <Dropzone onDrop={handleDrop}>
{({ getRootProps, getInputProps, isDragActive }) => ( {({ getRootProps, getInputProps, isDragActive }) => (
<> <>
<div <div
@ -95,7 +92,7 @@ export default class HomeUpload extends Component {
<h3 id="skylink-retrieve-title">Have a Skylink?</h3> <h3 id="skylink-retrieve-title">Have a Skylink?</h3>
<p>Paste the link to retrieve your file</p> <p>Paste the link to retrieve your file</p>
<form className="home-upload-retrieve-form" onSubmit={this.handleSkylink}> <form className="home-upload-retrieve-form" onSubmit={handleSkylink}>
<input name="skylink" type="text" placeholder="sia://" aria-labelledby="skylink-retrieve-title" /> <input name="skylink" type="text" placeholder="sia://" aria-labelledby="skylink-retrieve-title" />
<button type="submit" aria-label="Retrieve file"> <button type="submit" aria-label="Retrieve file">
<DownArrow /> <DownArrow />
@ -105,9 +102,9 @@ export default class HomeUpload extends Component {
</div> </div>
</div> </div>
{this.state.files.length > 0 && ( {files.length > 0 && (
<div className="home-uploaded-files"> <div className="home-uploaded-files">
{this.state.files.map((file, i) => { {files.map((file, i) => {
return <UploadFile key={i} {...file} />; return <UploadFile key={i} {...file} />;
})} })}
</div> </div>
@ -126,4 +123,3 @@ export default class HomeUpload extends Component {
</Reveal> </Reveal>
); );
} }
}

View File

@ -2,12 +2,13 @@ import React from "react";
import SEO from "../components/seo"; import SEO from "../components/seo";
import { App } from "../components"; import { App } from "../components";
import "../global.scss"; import "../global.scss";
import LocationContext from "../LocationContext";
const IndexPage = () => ( const IndexPage = ({ location }) => (
<> <LocationContext.Provider value={location}>
<SEO /> <SEO />
<App /> <App />
</> </LocationContext.Provider>
); );
export default IndexPage; export default IndexPage;