slate/scenes/SceneSettingsDeveloper.js

339 lines
10 KiB
JavaScript
Raw Normal View History

import * as React from "react";
import * as Actions from "~/common/actions";
2021-04-19 22:30:11 +03:00
import * as Window from "~/common/window";
import * as Constants from "~/common/constants";
import * as System from "~/components/system";
import * as SVG from "~/common/svg";
import * as Events from "~/common/custom-events";
2020-11-30 08:24:22 +03:00
import { css } from "@emotion/react";
2021-04-19 22:30:11 +03:00
import { TabGroup, PrimaryTabGroup, SecondaryTabGroup } from "~/components/core/TabGroup";
import ScenePage from "~/components/core/ScenePage";
2020-08-22 07:25:34 +03:00
import ScenePageHeader from "~/components/core/ScenePageHeader";
2021-04-19 22:30:11 +03:00
import SquareButtonGray from "~/components/core/SquareButtonGray";
import APIDocsGetV1 from "~/components/api-docs/v1/get";
import APIDocsGetSlateV1 from "~/components/api-docs/v1/get-slate.js";
import APIDocsUpdateSlateV1 from "~/components/api-docs/v1/update-slate.js";
import APIDocsUploadToSlateV1 from "~/components/api-docs/v1/upload.js";
2021-03-22 21:17:42 +03:00
2021-04-19 22:30:11 +03:00
const STYLES_API_KEY = css`
height: 40px;
2021-04-19 22:30:11 +03:00
border-radius: 4px;
cursor: copy;
background-color: ${Constants.system.white};
outline: none;
border: none;
width: 380px;
font-family: ${Constants.font.code};
padding: 0 16px;
2021-04-19 22:30:11 +03:00
font-size: 14px;
`;
const STYLES_KEY_CONTAINER = css`
height: 40px;
display: flex;
flex-direction: row;
align-items: center;
margin-bottom: 8px;
`;
class Key extends React.Component {
2021-04-19 22:30:11 +03:00
_input;
2021-04-19 22:30:11 +03:00
state = { visible: false, copying: false };
_handleDelete = async (id) => {
await this.props.onDelete(id);
};
2021-04-19 22:30:11 +03:00
_handleCopy = async () => {
this._input.select();
document.execCommand("copy");
await this.setState({ copying: true });
await Window.delay(1000);
await this.setState({ copying: false });
};
render() {
return (
<div css={STYLES_KEY_CONTAINER}>
2021-04-19 22:30:11 +03:00
<input
ref={(c) => {
this._input = c;
}}
2021-04-19 22:30:11 +03:00
value={this.state.copying ? "Copied!" : this.props.data.key}
readOnly
type={this.state.visible || this.state.copying ? "text" : "password"}
css={STYLES_API_KEY}
onClick={this._handleCopy}
onMouseEnter={() => this.setState({ visible: true })}
onMouseLeave={() => this.setState({ visible: false })}
/>
<SquareButtonGray
onClick={() => this._handleDelete(this.props.data.id)}
style={{
marginLeft: 8,
}}
>
2021-04-19 22:30:11 +03:00
<SVG.Trash height="16px" />
</SquareButtonGray>
</div>
);
}
}
export default class SceneSettingsDeveloper extends React.Component {
2021-04-19 22:30:11 +03:00
_bucketCID;
state = {
loading: false,
2021-03-24 07:10:26 +03:00
language: "javascript",
docs: "GET",
2021-04-19 22:30:11 +03:00
copying: false,
tab: 0,
};
_handleCopy = async () => {
this._bucketCID.select();
document.execCommand("copy");
await this.setState({ copying: true });
await Window.delay(1000);
await this.setState({ copying: false });
};
_handleSave = async (e) => {
this.setState({ loading: true });
const response = await Actions.generateAPIKey();
2020-09-12 01:25:33 +03:00
Events.hasError(response);
this.setState({ loading: false });
};
_handleDelete = async (id) => {
this.setState({ loading: true });
2021-04-19 22:30:11 +03:00
if (
!window.confirm(
"Are you sure you want to revoke this API key? Any services using it will no longer be able to access your Slate account"
)
) {
this.setState({ loading: false });
return;
}
const response = await Actions.deleteAPIKey({ id });
Events.hasError(response);
2020-10-31 02:12:20 +03:00
this.setState({ loading: false });
};
2021-03-22 21:17:42 +03:00
//handles language changes
_handleChangeLanguage = (newLanguage) => {
this.setState({ language: newLanguage });
};
async componentDidMount() {
2021-04-20 23:35:30 +03:00
// if (!this.props.viewer.keys) {
// return;
// }
// if (!this.props.viewer.keys.length) {
// return;
// }
// console.log(this.props.viewer.keys);
// const response = await fetch("/api/v1/get-slate", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// Authorization: `Basic ${this.props.viewer.keys[0].key}`,
// },
// });
// const json = await response.json();
}
render() {
2021-03-24 07:10:26 +03:00
let APIKey = "YOUR-API-KEY-HERE";
let lang = this.state.language;
if (this.props.viewer.keys) {
if (this.props.viewer.keys.length) {
2021-03-22 21:17:42 +03:00
APIKey = this.props.viewer.keys[0].key;
}
}
2021-03-24 07:10:26 +03:00
let slateId = "YOUR-SLATE-ID-VALUE";
if (this.props.viewer.slates) {
if (this.props.viewer.slates.length) {
slateId = this.props.viewer.slates[0].id;
}
}
2021-04-09 10:28:03 +03:00
let userBucketCID = this.props.viewer?.userBucketCID;
2021-04-19 22:30:11 +03:00
if (userBucketCID) {
userBucketCID = userBucketCID.replace("/ipfs/", "");
}
2021-04-08 23:41:10 +03:00
return (
<ScenePage>
{/*
2021-03-22 21:17:42 +03:00
<div css={STYLES_SIDEBAR}>
2021-03-24 07:10:26 +03:00
<span css={STYLES_LINK} onClick={() => this._changeDocs("INTRO")}>
2021-03-22 21:17:42 +03:00
Introduction
</span>
2021-03-24 07:10:26 +03:00
<span css={STYLES_LABEL}>api</span>
2021-03-22 21:17:42 +03:00
<div>
2021-03-24 07:10:26 +03:00
<span
css={STYLES_LINK}
style={{ color: this.state.docs === "GET" ? Constants.system.brand : null }}
onClick={() => this._changeDocs("GET")}
>
2021-03-22 21:17:42 +03:00
Get all slates
</span>
2021-03-24 07:10:26 +03:00
<span
css={STYLES_LINK}
style={{ color: this.state.docs === "GET_SLATE" ? Constants.system.brand : null }}
onClick={() => this._changeDocs("GET_SLATE")}
>
2021-03-22 21:17:42 +03:00
Get slate by ID
</span>
2021-03-24 07:10:26 +03:00
<span
css={STYLES_LINK}
style={{
color: this.state.docs === "UPLOAD_TO_SLATE" ? Constants.system.brand : null,
}}
onClick={() => this._changeDocs("UPLOAD_TO_SLATE")}
>
2021-03-22 21:17:42 +03:00
Upload to slate by ID
</span>
2021-03-24 07:10:26 +03:00
<span
css={STYLES_LINK}
style={{ color: this.state.docs === "UPDATE_SLATE" ? Constants.system.brand : null }}
onClick={() => this._changeDocs("UPDATE_SLATE")}
>
2021-03-22 21:17:42 +03:00
Update slate
</span>
</div>
2021-03-24 07:10:26 +03:00
<span css={STYLES_LABEL}>guides</span>
2021-03-22 21:17:42 +03:00
</div>
*/}
2021-04-19 22:30:11 +03:00
<ScenePageHeader title="API Keys">
You can use your API keys to access your account information outside of Slate and upload
files to Slate. You can have a maximum of 10 keys at any given time.
2020-08-22 07:25:34 +03:00
</ScenePageHeader>
2021-04-08 23:41:10 +03:00
{userBucketCID && (
2021-04-19 22:30:11 +03:00
<div style={{ marginTop: 34, marginBottom: 24 }}>
<System.DescriptionGroup
style={{ maxWidth: 640 }}
label="Bucket CID"
description={
"This is your bucket CID. Use this to access your Slate files on other platforms"
}
/>
<input
value={this.state.copying ? "Copied!" : userBucketCID}
css={STYLES_API_KEY}
style={{ textOverflow: "ellipsis" }}
type="text"
readOnly
ref={(c) => {
this._bucketCID = c;
}}
onClick={this._handleCopy}
/>
</div>
2021-04-08 23:41:10 +03:00
)}
<br />
2021-04-19 22:30:11 +03:00
<System.DescriptionGroup style={{ maxWidth: 640, marginBottom: 24 }} label="API Keys" />
{this.props.viewer.keys.map((k) => {
return <Key key={k.id} data={k} onDelete={this._handleDelete} />;
})}
<div style={{ marginTop: 24 }}>
2021-04-19 22:30:11 +03:00
{this.props.viewer.keys.length < 10 ? (
<System.ButtonPrimary onClick={this._handleSave} loading={this.state.loading}>
Generate
</System.ButtonPrimary>
) : (
<System.ButtonDisabled>Generate</System.ButtonDisabled>
)}
{this.props.viewer.keys.length === 0 ? (
2021-03-24 07:10:26 +03:00
<ScenePageHeader title="">
Generate an API key to have it appear in the code examples
</ScenePageHeader>
) : null}
</div>
2021-04-19 22:30:11 +03:00
2021-03-25 22:05:09 +03:00
{/*
2021-03-24 07:10:26 +03:00
<div css={STYLES_LANGUAGE_CONTAINER}>
<div
css={STYLES_LANGUAGE_TILE}
style={{ color: this.state.language === "javascript" ? Constants.system.brand : null }}
onClick={() => this._handleChangeLanguage("javascript")}
>
<span style={{ marginBottom: 32 }}>JS ICON</span>
<span>Node.js</span>
</div>
<div
css={STYLES_LANGUAGE_TILE}
style={{ color: this.state.language === "python" ? Constants.system.brand : null }}
onClick={() => this._handleChangeLanguage("python")}
>
<span style={{ marginBottom: 32 }}>PY ICON</span>
<span>Python3</span>
</div>
</div>
2021-03-25 22:05:09 +03:00
<APIDocsGet language={lang} APIKey={APIKey} />
<APIDocsUpdateSlate language={lang} APIKey={APIKey} slateId={slateId} />
<APIDocsUploadToSlate language={lang} APIKey={APIKey} slateId={slateId} />
2021-03-25 22:05:09 +03:00
*/}
2021-04-19 22:30:11 +03:00
<ScenePageHeader title="Developer Documentation" style={{ marginTop: 96 }}>
Slate is currently on v2.0 of the API. While prior versions are still supported, we
recommend using the most up to date version.
</ScenePageHeader>
<SecondaryTabGroup
tabs={["Version 2.0", "Version 1.0"]}
value={this.state.tab}
onChange={(tab) => this.setState({ tab })}
/>
{this.state.tab === 0 ? (
<></>
) : (
<>
<APIDocsGetV1
2021-04-19 22:30:11 +03:00
language={lang}
APIKey={APIKey}
onLanguageChange={this._handleChangeLanguage}
/>
<APIDocsGetSlateV1
2021-04-19 22:30:11 +03:00
language={lang}
APIKey={APIKey}
slateId={slateId}
onLanguageChange={this._handleChangeLanguage}
/>
<APIDocsUpdateSlateV1
2021-04-19 22:30:11 +03:00
language={lang}
APIKey={APIKey}
slateId={slateId}
onLanguageChange={this._handleChangeLanguage}
/>
<APIDocsUploadToSlateV1
2021-04-19 22:30:11 +03:00
language={lang}
APIKey={APIKey}
slateId={slateId}
onLanguageChange={this._handleChangeLanguage}
/>
</>
2021-04-19 22:30:11 +03:00
)}
</ScenePage>
);
}
}