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"; import * as SVG from "~/common/svg"; import * as Window from "~/common/window"; import { css } from "@emotion/react"; import { Boundary } from "~/components/system/components/fragments/Boundary"; import { PopoverNavigation } from "~/components/system/components/PopoverNavigation"; import { LoaderSpinner } from "~/components/system/components/Loaders"; import { dispatchCustomEvent } from "~/common/custom-events"; import { CheckBox } from "~/components/system/components/CheckBox"; import { Table } from "~/components/core/Table"; import { FileTypeIcon } from "~/components/core/FileTypeIcon"; import { ButtonPrimary, ButtonWarning } from "~/components/system/components/Buttons"; import { TabGroup } from "~/components/core/TabGroup"; import SlateMediaObjectPreview from "~/components/core/SlateMediaObjectPreview"; import FilePreviewBubble from "~/components/core/FilePreviewBubble"; const STYLES_CONTAINER_HOVER = css` display: flex; :hover { color: ${Constants.system.brand}; } `; const STYLES_ICON_BOX = css` height: 32px; width: 32px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; margin-left: 16px; `; const STYLES_CANCEL_BOX = css` height: 16px; width: 16px; background-color: ${Constants.system.brand}; border-radius: 3px; position: relative; right: 3px; cursor: pointer; box-shadow: 0 0 0 1px ${Constants.system.brand}; `; const STYLES_HEADER_LINE = css` display: flex; align-items: center; margin-top: 80px; margin-bottom: 30px; `; const STYLES_LINK = css` display: inline; cursor: pointer; transition: 200ms ease all; font-size: 0.9rem; padding: 12px 0px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 320px; @media (max-width: ${Constants.sizes.tablet}px) { max-width: 120px; } `; const STYLES_VALUE = css` font-size: 0.9rem; padding: 12px 0px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; `; const STYLES_ICON_BOX_HOVER = css` display: inline-flex; align-items: center; padding: 8px; cursor: pointer; :hover { color: ${Constants.system.brand}; } `; const STYLES_ICON_BOX_BACKGROUND = css` display: inline-flex; align-items: center; justify-content: center; height: 25px; width: 25px; cursor: pointer; background-color: rgba(255, 255, 255, 0.75); border-radius: 3px; position: absolute; bottom: 8px; right: 8px; `; const STYLES_ACTION_BAR = css` display: flex; align-items: center; justify-content: space-between; box-shadow: 0 0 0 1px ${Constants.system.lightBorder} inset, 0 0 4px 2px ${Constants.system.shadow}; border-radius: 4px; padding: 12px 32px; box-sizing: border-box; background-color: ${Constants.system.foreground}; position: fixed; bottom: 12px; width: calc(100vw - ${Constants.sizes.sidebar}px + 32px); max-width: ${Constants.sizes.desktop}px; @media (max-width: ${Constants.sizes.mobile}px) { width: calc(100vw - 48px); } `; const STYLES_RIGHT = css` flex-shrink: 0; display: flex; align-items: center; `; const STYLES_LEFT = css` width: 100%; min-width: 10%; display: flex; align-items: center; `; const STYLES_FILES_SELECTED = css` font-family: ${Constants.font.semiBold}; @media (max-width: ${Constants.sizes.mobile}px) { display: none; } `; const STYLES_COPY_INPUT = css` pointer-events: none; position: absolute; opacity: 0; `; const STYLES_IMAGE_GRID = css` display: grid; grid-template-columns: repeat(auto-fit, minmax(214px, 1fr)); margin: 0 -27px; @media (max-width: ${Constants.sizes.mobile}px) { display: grid; grid-template-columns: 1fr 1fr; margin: 0px -12px; } `; 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 ${Constants.system.lightBorder} inset, 0 0 40px 0 ${Constants.system.shadow}; cursor: pointer; position: relative; @media (max-width: ${Constants.sizes.mobile}px) { width: 144px; height: 144px; margin: 12px auto; } `; const STYLES_MOBILE_HIDDEN = css` @media (max-width: ${Constants.sizes.mobile}px) { display: none; } `; const delay = (ms) => new Promise((resolve) => window.setTimeout(resolve, ms)); let mounted = false; export default class DataView extends React.Component { _mounted = false; state = { menu: null, loading: {}, checked: {}, startIndex: 0, view: "grid", viewLimit: 40, scrollDebounce: false, }; async componentDidMount() { 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); } window.addEventListener("scroll", this._handleScroll); await this._handleCheckScroll(); } 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); window.removeEventListener("scroll", this._handleCheckScroll); window.removeEventListener("remote-update-carousel", this._handleUpdate); } _handleScroll = (e) => { const windowHeight = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight; const body = document.body; const html = document.documentElement; const docHeight = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight ); const windowBottom = windowHeight + window.pageYOffset; if (windowBottom >= docHeight - 600) { this.setState({ viewLimit: this.state.viewLimit + 30 }); } }; _handleCheckScroll = Window.debounce(this._handleScroll, 200); _handleCheckBox = (e) => { let checked = this.state.checked; if (e.target.value === false) { delete checked[e.target.name]; this.setState({ checked }); return; } this.setState({ checked: { ...this.state.checked, [e.target.name]: true }, }); }; _handleDelete = async (cid) => { const message = `Are you sure you want to delete these files? They will be deleted from your slates as well`; if (!window.confirm(message)) { return; } dispatchCustomEvent({ name: "state-global-carousel-loading", detail: { loading: "deleting" } }); let cids; if (cid) { cids = [cid]; } else { cids = Object.keys(this.state.checked).map((id) => { let index = parseInt(id); return this.props.viewer.library[0].children[index].ipfs.replace("/ipfs/", ""); }); } this._handleLoading({ cids }); const response = await Actions.deleteBucketItems({ cids }); if (!response) { dispatchCustomEvent({ name: "create-alert", detail: { alert: { message: "We're having trouble connecting right now. Please try again later", }, }, }); this._handleLoading({ cids }); dispatchCustomEvent({ name: "state-global-carousel-loading", detail: { loading: false } }); return; } if (response.error) { dispatchCustomEvent({ name: "create-alert", detail: { alert: { decorator: response.decorator } }, }); this._handleLoading({ cids }); dispatchCustomEvent({ name: "state-global-carousel-loading", detail: { loading: false } }); return; } this._handleLoading({ cids }); this.setState({ checked: {} }); dispatchCustomEvent({ name: "create-alert", detail: { alert: { message: "Files successfully deleted!", status: "INFO" }, }, }); dispatchCustomEvent({ name: "state-global-carousel-loading", detail: { loading: false } }); }; _handleSelect = (index) => { System.dispatchCustomEvent({ name: "slate-global-open-carousel", detail: { index }, }); }; _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 Actions.addFileToSlate({ slate, data: [{ title: data.name, ...data }], }); if (!addResponse) { dispatchCustomEvent({ name: "create-alert", detail: { alert: { message: "We're having trouble connecting right now. Please try again later", }, }, }); return null; } if (addResponse.error) { dispatchCustomEvent({ name: "create-alert", detail: { alert: { decorator: addResponse.decorator } }, }); return null; } const { added, skipped } = addResponse; let message = Strings.formatAsUploadMessage(added, skipped, true); dispatchCustomEvent({ name: "create-alert", detail: { alert: { message, status: !added ? null : "INFO" }, }, }); 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 response = await Actions.removeFileFromSlate({ slateId: slate.id, ids: [id] }); if (!response) { System.dispatchCustomEvent({ name: "state-global-carousel-loading", detail: { loading: false }, }); dispatchCustomEvent({ name: "create-alert", detail: { alert: { message: "We're having trouble connecting right now and weren't able to delete that. Please try again later", }, }, }); return null; } if (response.error) { System.dispatchCustomEvent({ name: "state-global-carousel-loading", detail: { loading: false }, }); dispatchCustomEvent({ name: "create-alert", detail: { alert: { decorator: response.decorator, }, }, }); return null; } System.dispatchCustomEvent({ name: "state-global-carousel-loading", detail: { loading: false }, }); }; _handleCopy = (e, value) => { e.stopPropagation(); this._handleHide(); this.setState({ copyValue: value }, () => { this._ref.select(); document.execCommand("copy"); }); }; _handleHide = (e) => { this.setState({ menu: null }); }; _handleLoading = ({ cids }) => { let loading = this.state.loading; for (let cid of cids) { System.dispatchCustomEvent({ name: "state-global-carousel-loading", detail: { loading: !this.state.loading[cid] }, }); loading[cid] = !this.state.loading[cid]; } this.setState({ loading }); }; _handleClick = (e) => { this.setState({ [e.target.name]: e.target.value }); }; _handleAddToSlate = (e) => { let userFiles = this.props.viewer.library[0].children; let files = Object.keys(this.state.checked).map((index) => userFiles[index]); this.props.onAction({ type: "SIDEBAR", value: "SIDEBAR_ADD_FILE_TO_SLATE", data: { files }, }); this._handleUncheckAll(); }; _handleUncheckAll = () => { this.setState({ checked: {} }); }; render() { let numChecked = Object.keys(this.state.checked).length || 0; const header = (