2020-08-12 11:22:28 +03:00
|
|
|
import * as React from "react";
|
|
|
|
import * as Constants from "~/common/constants";
|
|
|
|
import * as Strings from "~/common/strings";
|
|
|
|
import * as System from "~/components/system";
|
|
|
|
import * as Actions from "~/common/actions";
|
2020-09-08 00:45:58 +03:00
|
|
|
import * as SVG from "~/common/svg";
|
2020-08-12 11:22:28 +03:00
|
|
|
|
|
|
|
import { css } from "@emotion/react";
|
2020-09-08 00:45:58 +03:00
|
|
|
import { Boundary } from "~/components/system/components/fragments/Boundary";
|
|
|
|
import { PopoverNavigation } from "~/components/system/components/PopoverNavigation";
|
2020-09-08 08:36:01 +03:00
|
|
|
import { LoaderSpinner } from "~/components/system/components/Loaders";
|
2020-09-09 07:12:54 +03:00
|
|
|
import { dispatchCustomEvent } from "~/common/custom-events";
|
|
|
|
import { generateLayout } from "~/components/core/Slate";
|
2020-08-12 11:22:28 +03:00
|
|
|
|
2020-08-20 23:33:08 +03:00
|
|
|
import SlateMediaObject from "~/components/core/SlateMediaObject";
|
2020-09-08 00:45:58 +03:00
|
|
|
import SlateMediaObjectPreview from "~/components/core/SlateMediaObjectPreview";
|
2020-08-12 11:22:28 +03:00
|
|
|
|
|
|
|
const COLUMNS_SCHEMA = [
|
|
|
|
{
|
2020-09-08 00:45:58 +03:00
|
|
|
key: "name",
|
|
|
|
name: <span style={{ fontSize: "0.9rem" }}>Name</span>,
|
|
|
|
width: "100%",
|
2020-08-12 11:22:28 +03:00
|
|
|
},
|
|
|
|
{
|
2020-09-08 00:45:58 +03:00
|
|
|
key: "size",
|
|
|
|
name: <span style={{ fontSize: "0.9rem" }}>Size</span>,
|
|
|
|
width: "104px",
|
2020-08-12 11:22:28 +03:00
|
|
|
},
|
|
|
|
{
|
2020-09-08 00:45:58 +03:00
|
|
|
key: "more",
|
|
|
|
name: <div />,
|
|
|
|
width: "64px",
|
2020-08-12 11:22:28 +03:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const STYLES_LINK = css`
|
|
|
|
cursor: pointer;
|
|
|
|
transition: 200ms ease all;
|
2020-09-08 00:45:58 +03:00
|
|
|
font-size: 0.9rem;
|
|
|
|
padding: 12px 0px;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
white-space: nowrap;
|
2020-08-12 11:22:28 +03:00
|
|
|
|
|
|
|
:hover {
|
|
|
|
color: ${Constants.system.brand};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-09-08 00:45:58 +03:00
|
|
|
const STYLES_VALUE = css`
|
|
|
|
font-size: 0.9rem;
|
|
|
|
padding: 12px 0px;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
white-space: nowrap;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_TABLE_VALUE = {
|
|
|
|
fontFamily: Constants.font.medium,
|
|
|
|
padding: "0px 24px",
|
|
|
|
};
|
|
|
|
|
|
|
|
const STYLES_TABLE_CONTAINER = css`
|
|
|
|
border: 1px solid rgba(229, 229, 229, 0.75);
|
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_ICON_BOX = css`
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
align-items: center;
|
|
|
|
padding: 8px;
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
:hover {
|
|
|
|
color: ${Constants.system.brand};
|
|
|
|
}
|
2020-08-12 11:22:28 +03:00
|
|
|
`;
|
|
|
|
|
2020-09-08 00:45:58 +03:00
|
|
|
const STYLES_COPY_INPUT = css`
|
|
|
|
pointer-events: none;
|
|
|
|
position: absolute;
|
|
|
|
opacity: 0;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_IMAGE_GRID = css`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
margin: 0 -27px;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const STYLES_IMAGE_BOX = css`
|
|
|
|
width: 160px;
|
|
|
|
height: 160px;
|
|
|
|
margin: 27px;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
box-shadow: 0px 0px 0px 1px rgba(229, 229, 229, 0.5) inset;
|
|
|
|
cursor: pointer;
|
2020-08-12 11:22:28 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
const delay = (ms) => new Promise((resolve) => window.setTimeout(resolve, ms));
|
|
|
|
|
2020-08-24 09:32:26 +03:00
|
|
|
/*
|
|
|
|
{this.state.loading || isOnNetwork ? null : (
|
|
|
|
<System.ButtonPrimary
|
|
|
|
loading={this.state.loading}
|
|
|
|
style={{ marginRight: 16 }}
|
|
|
|
onClick={() => this._handleMakeDeal(each)}>
|
|
|
|
Store on Filecoin
|
|
|
|
</System.ButtonPrimary>
|
|
|
|
)}
|
|
|
|
*/
|
|
|
|
|
2020-09-09 07:12:54 +03:00
|
|
|
let mounted = false;
|
|
|
|
|
2020-08-12 11:22:28 +03:00
|
|
|
export default class DataView extends React.Component {
|
2020-09-09 07:12:54 +03:00
|
|
|
_mounted = false;
|
|
|
|
|
2020-08-12 11:22:28 +03:00
|
|
|
state = {
|
2020-09-08 00:45:58 +03:00
|
|
|
menu: null,
|
2020-09-08 08:36:01 +03:00
|
|
|
loading: {},
|
2020-08-12 11:22:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2020-09-09 07:12:54 +03:00
|
|
|
if (!mounted) {
|
|
|
|
mounted = true;
|
|
|
|
window.addEventListener("remote-data-deletion", this._handleDataDeletion);
|
|
|
|
window.addEventListener(
|
|
|
|
"remote-slate-object-remove",
|
|
|
|
this._handleRemoteSlateObjectRemove
|
|
|
|
);
|
|
|
|
window.addEventListener(
|
|
|
|
"remote-slate-object-add",
|
|
|
|
this._handleRemoteSlateObjectAdd
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-12 11:22:28 +03:00
|
|
|
await this._handleUpdate();
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:12:54 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
mounted = false;
|
|
|
|
|
|
|
|
window.removeEventListener(
|
|
|
|
"remote-data-deletion",
|
|
|
|
this._handleDataDeletion
|
|
|
|
);
|
|
|
|
window.removeEventListener(
|
|
|
|
"remote-slate-object-remove",
|
|
|
|
this._handleRemoteSlateObjectRemove
|
|
|
|
);
|
|
|
|
window.removeEventListener(
|
|
|
|
"remote-slate-object-add",
|
|
|
|
this._handleRemoteSlateObjectAdd
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-08 07:04:24 +03:00
|
|
|
componentDidUpdate = (prevProps) => {
|
|
|
|
if (prevProps.items !== this.props.items) {
|
|
|
|
this._handleUpdate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-12 11:22:28 +03:00
|
|
|
_handleUpdate = async () => {
|
|
|
|
// NOTE(jim): Hack to handle some race conditions.
|
|
|
|
await delay(200);
|
|
|
|
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "slate-global-create-carousel",
|
|
|
|
detail: {
|
|
|
|
slides: this.props.items.map((each) => {
|
2020-08-25 21:25:26 +03:00
|
|
|
// TODO(jim): Shouldn't really be handled here.
|
2020-08-12 11:22:28 +03:00
|
|
|
const cid = each.ipfs.replace("/ipfs/", "");
|
2020-08-25 21:25:26 +03:00
|
|
|
const url = Strings.getCIDGatewayURL(cid);
|
|
|
|
const data = { ...each, url, cid };
|
2020-08-21 10:07:39 +03:00
|
|
|
|
2020-08-12 11:22:28 +03:00
|
|
|
return {
|
2020-08-25 21:25:26 +03:00
|
|
|
id: data.id,
|
2020-09-09 07:12:54 +03:00
|
|
|
slates: this.props.viewer.slates,
|
2020-08-12 11:22:28 +03:00
|
|
|
cid,
|
2020-08-25 21:25:26 +03:00
|
|
|
data,
|
2020-09-09 07:12:54 +03:00
|
|
|
renderDataControls: true,
|
2020-08-25 21:25:26 +03:00
|
|
|
component: <SlateMediaObject key={data.id} data={data} />,
|
2020-09-09 07:12:54 +03:00
|
|
|
onDataDelete: () => {
|
|
|
|
return dispatchCustomEvent({
|
|
|
|
name: "remote-data-deletion",
|
|
|
|
detail: { cid },
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onRemoveFromSlate: (data) => {
|
|
|
|
return dispatchCustomEvent({
|
|
|
|
name: "remote-slate-object-remove",
|
|
|
|
detail: { ...data },
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onAddToSlate: (data) => {
|
|
|
|
return dispatchCustomEvent({
|
|
|
|
name: "remote-slate-object-add",
|
|
|
|
detail: { ...data },
|
|
|
|
});
|
|
|
|
},
|
2020-08-12 11:22:28 +03:00
|
|
|
};
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleSelect = (index) => {
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "slate-global-open-carousel",
|
|
|
|
detail: { index },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-09-09 07:12:54 +03:00
|
|
|
_handleDataDeletion = (e) => {
|
|
|
|
this._handleDelete(e.detail.cid);
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleRemoteSlateObjectAdd = async ({ detail }) => {
|
|
|
|
const { id, slate, data } = detail;
|
|
|
|
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: { id: slate.id } },
|
|
|
|
});
|
|
|
|
|
|
|
|
const addResponse = await fetch(`/api/slates/add-url`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Accept: "application/json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ slate, data: { title: data.name, ...data } }),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!addResponse || addResponse.error) {
|
|
|
|
alert("We could not add this object to your slate.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.props.onRehydrate();
|
|
|
|
await this._handleUpdate();
|
|
|
|
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: false },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleRemoteSlateObjectRemove = async ({ detail }) => {
|
|
|
|
const { id, slate } = detail;
|
|
|
|
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: { id: slate.id } },
|
|
|
|
});
|
|
|
|
|
|
|
|
const objects = slate.data.objects.filter((o, i) => {
|
|
|
|
return o.id !== id;
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO(jim): This is a brute force way to handle this.
|
|
|
|
const layouts = { lg: generateLayout(objects) };
|
|
|
|
|
|
|
|
const response = await Actions.updateSlate({
|
|
|
|
id: slate.id,
|
|
|
|
data: {
|
|
|
|
name: slate.data.name,
|
|
|
|
objects,
|
|
|
|
layouts,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response) {
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
alert("We failed to remove the object from your Slate.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
alert("We failed to remove the object from your Slate.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.props.onRehydrate();
|
|
|
|
await this._handleUpdate();
|
|
|
|
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: false },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-09-08 00:45:58 +03:00
|
|
|
// _handleMakeDeal = (data) => {
|
|
|
|
// this.props.onAction({
|
|
|
|
// type: "SIDEBAR",
|
|
|
|
// value: "SIDEBAR_FILE_STORAGE_DEAL",
|
|
|
|
// data,
|
|
|
|
// });
|
|
|
|
// };
|
|
|
|
|
|
|
|
_handleCopy = (e, value) => {
|
|
|
|
this._handleHide();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.setState({ copyValue: value }, () => {
|
|
|
|
this._ref.select();
|
|
|
|
document.execCommand("copy");
|
2020-08-20 08:45:43 +03:00
|
|
|
});
|
2020-08-12 11:22:28 +03:00
|
|
|
};
|
|
|
|
|
2020-09-08 00:45:58 +03:00
|
|
|
_handleHide = (e) => {
|
|
|
|
this.setState({ menu: null });
|
|
|
|
};
|
|
|
|
|
2020-09-09 07:12:54 +03:00
|
|
|
_handleRemoteDeletion = async (e) => {
|
|
|
|
await this._handleDelete(e.detail.cid);
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleLoading = ({ cid }) => {
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: !this.state.loading[cid] },
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
loading: { ...this.state.loading, [cid]: !this.state.loading[cid] },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-12 11:22:28 +03:00
|
|
|
_handleDelete = async (cid) => {
|
2020-09-09 07:12:54 +03:00
|
|
|
this._handleLoading({ cid });
|
|
|
|
|
2020-08-25 21:25:26 +03:00
|
|
|
if (
|
|
|
|
!window.confirm(
|
|
|
|
"Are you sure you want to delete this? It will be removed from your Slates too."
|
|
|
|
)
|
|
|
|
) {
|
2020-09-09 07:12:54 +03:00
|
|
|
this._handleLoading({ cid });
|
2020-08-12 11:22:28 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await Actions.deleteBucketItem({ cid });
|
|
|
|
|
|
|
|
if (!response) {
|
2020-09-09 07:12:54 +03:00
|
|
|
this._handleLoading({ cid });
|
2020-08-12 11:22:28 +03:00
|
|
|
alert("TODO: Broken response error");
|
2020-09-09 07:12:54 +03:00
|
|
|
return null;
|
2020-08-12 11:22:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (response.error) {
|
2020-09-09 07:12:54 +03:00
|
|
|
this._handleLoading({ cid });
|
2020-08-12 11:22:28 +03:00
|
|
|
alert("TODO: Bucket delete error");
|
2020-09-09 07:12:54 +03:00
|
|
|
return null;
|
2020-08-12 11:22:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.props.onRehydrate();
|
|
|
|
await this._handleUpdate();
|
2020-09-09 07:12:54 +03:00
|
|
|
this._handleLoading({ cid });
|
|
|
|
return null;
|
2020-08-12 11:22:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_handleClick = (e) => {
|
|
|
|
this.setState({ [e.target.name]: e.target.value });
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2020-09-08 00:45:58 +03:00
|
|
|
if (this.props.view === "grid") {
|
|
|
|
return (
|
|
|
|
<div css={STYLES_IMAGE_GRID}>
|
|
|
|
{this.props.items.map((each, index) => (
|
|
|
|
<div
|
|
|
|
css={STYLES_IMAGE_BOX}
|
|
|
|
onClick={() => this._handleSelect(index)}
|
|
|
|
>
|
|
|
|
<SlateMediaObjectPreview
|
|
|
|
url={`https://${each.ipfs.replace(
|
|
|
|
"/ipfs/",
|
|
|
|
""
|
|
|
|
)}.ipfs.slate.textile.io`}
|
|
|
|
title={each.file || each.name}
|
|
|
|
type={each.type || each.icon}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2020-08-12 11:22:28 +03:00
|
|
|
const columns = COLUMNS_SCHEMA;
|
|
|
|
const rows = this.props.items.map((each, index) => {
|
|
|
|
const cid = each.ipfs.replace("/ipfs/", "");
|
|
|
|
const isOnNetwork = each.networks && each.networks.includes("FILECOIN");
|
|
|
|
|
|
|
|
return {
|
|
|
|
...each,
|
2020-09-08 00:45:58 +03:00
|
|
|
name: (
|
|
|
|
<div css={STYLES_LINK} onClick={() => this._handleSelect(index)}>
|
|
|
|
{each.file || each.name}
|
|
|
|
</div>
|
2020-08-12 11:22:28 +03:00
|
|
|
),
|
2020-09-08 00:45:58 +03:00
|
|
|
size: <div css={STYLES_VALUE}>{Strings.bytesToSize(each.size)}</div>,
|
|
|
|
more: (
|
|
|
|
<div
|
|
|
|
css={STYLES_ICON_BOX}
|
2020-09-08 08:36:01 +03:00
|
|
|
onClick={
|
|
|
|
this.state.loading[cid]
|
|
|
|
? () => {}
|
|
|
|
: () => this.setState({ menu: each.id })
|
|
|
|
}
|
2020-09-08 00:45:58 +03:00
|
|
|
>
|
2020-09-08 08:36:01 +03:00
|
|
|
{this.state.loading[cid] ? (
|
|
|
|
<LoaderSpinner style={{ height: 24, width: 24 }} />
|
|
|
|
) : (
|
|
|
|
<SVG.MoreHorizontal height="24px" />
|
|
|
|
)}
|
|
|
|
|
2020-09-08 00:45:58 +03:00
|
|
|
{this.state.menu === each.id ? (
|
|
|
|
<Boundary
|
|
|
|
captureResize={true}
|
|
|
|
captureScroll={false}
|
|
|
|
enabled
|
|
|
|
onOutsideRectEvent={this._handleHide}
|
|
|
|
>
|
|
|
|
<PopoverNavigation
|
|
|
|
style={{
|
|
|
|
top: "48px",
|
|
|
|
right: "40px",
|
|
|
|
}}
|
|
|
|
navigation={[
|
|
|
|
{
|
|
|
|
text: "Copy CID",
|
|
|
|
onClick: (e) => this._handleCopy(e, cid),
|
|
|
|
},
|
2020-09-08 07:04:24 +03:00
|
|
|
{
|
|
|
|
text: "Copy Link",
|
|
|
|
onClick: (e) =>
|
|
|
|
this._handleCopy(
|
|
|
|
e,
|
|
|
|
`https://${cid}.ipfs.slate.textile.io`
|
|
|
|
),
|
|
|
|
},
|
2020-09-08 00:45:58 +03:00
|
|
|
{
|
|
|
|
text: "Delete",
|
|
|
|
onClick: (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
this.setState({ menu: null }, () =>
|
|
|
|
this._handleDelete(cid)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</Boundary>
|
2020-08-12 11:22:28 +03:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
columns,
|
|
|
|
rows,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2020-09-08 00:45:58 +03:00
|
|
|
<div css={STYLES_TABLE_CONTAINER}>
|
2020-08-12 11:22:28 +03:00
|
|
|
<System.Table
|
|
|
|
data={data}
|
2020-09-08 00:45:58 +03:00
|
|
|
noColor
|
|
|
|
topRowStyle={{
|
|
|
|
...STYLES_TABLE_VALUE,
|
2020-09-08 08:36:01 +03:00
|
|
|
backgroundColor: Constants.system.foreground,
|
2020-09-08 00:45:58 +03:00
|
|
|
fontFamily: Constants.font.semiBold,
|
|
|
|
padding: "12px 24px",
|
|
|
|
}}
|
|
|
|
rowStyle={STYLES_TABLE_VALUE}
|
2020-08-12 11:22:28 +03:00
|
|
|
onAction={this.props.onAction}
|
|
|
|
onNavigateTo={this.props.onNavigateTo}
|
|
|
|
/>
|
2020-09-08 00:45:58 +03:00
|
|
|
<input
|
|
|
|
readOnly
|
|
|
|
ref={(c) => {
|
|
|
|
this._ref = c;
|
|
|
|
}}
|
|
|
|
value={this.state.copyValue}
|
|
|
|
css={STYLES_COPY_INPUT}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-08-12 11:22:28 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|