import * as React from "react"; import { useState, useEffect } from "@theia/core/shared/react"; import { createUseStyles } from "react-jss"; import Modal from "react-modal"; import { useCommonStyles } from "../StorjWidget/StorjWidget.style"; import { LocalStorageKey, RCloneEndPoints } from "../../constants"; import { IRCloneFile } from "../StorjWidget/StorjWidget.interface"; import fetchRClone from "../../helpers/fetchRClone"; import BackButton from "../BackButton"; const useStyles = createUseStyles({ contentContainer: { position: "relative", height: "100%", display: "flex", flexDirection: "column", }, confirmButton: { backgroundColor: "#F57C00 !important", color: "#fff !important", }, actionsContainer: { alignSelf: "flex-end", marginTop: "auto", }, commonButton: { borderRadius: 2, textTransform: "uppercase", height: 32, }, innerContent: { display: "flex", maxHeight: 300, overflow: (props: any) => (props.fileListLoading ? "hidden" : "auto"), background: "var(--theia-input-background)", color: "var(--theia-input-foreground)", padding: 16, borderRadius: 6, }, header: { display: "flex", flexDirection: "column", width: "100%", }, selectedPathContainer: { display: "flex", marginTop: 16, alignItems: "center", }, selectedPath: { flex: 1, marginLeft: 16, background: "var(--theia-input-background)", color: "var(--theia-input-foreground)", height: 24, borderRadius: 12, padding: "4px 24px", display: "flex", alignItems: "center", fontSize: 12, }, predefinedPath: { opacity: "40%", }, fileRow: { cursor: "pointer", lineHeight: "32px", "&:hover": { textDecoration: "underline", }, }, fileRowSelected: { composes: "$fileRow", background: "var(--theia-selection-background)", }, loadingContainer: { display: "flex", justifyContent: "center", alignItems: "center", }, fileActionsContainer: { marginBottom: 16, display: "flex", }, homeButton: { marginLeft: 16, "&:hover": { textDecoration: "underline", }, cursor: "pointer", }, }); type Props = { isOpen: boolean; onClose: Function; onClickConfirm: (selectedPath: string, selectedFile: string) => void; canSelectFiles: boolean; }; const RemotePathSelectorModal = (props: Props) => { const { isOpen: isOpenProp, onClose, onClickConfirm, canSelectFiles } = props; const selectedRemoteName = localStorage.getItem( LocalStorageKey.SELECTED_REMOTE ); const selectedBucket = localStorage.getItem(LocalStorageKey.SELECTED_BUCKET); const [isOpen, setIsOpen] = useState(isOpenProp); const predefinedPath = `${selectedRemoteName}:${selectedBucket}`; const [selectedPath, setSelectedPath] = useState(""); const [fileListLoading, setFileListLoading] = useState(false); const [selectedFile, setSelectedFile] = useState(""); const [fileList, setFileList] = useState([]); const classes = useStyles({ fileListLoading }); const commonClasses = useCommonStyles(); useEffect(() => { setIsOpen(isOpenProp); }, [isOpenProp]); useEffect(() => { if (isOpen && !selectedFile) { getFileList(); } }, [selectedPath, isOpen, selectedFile]); function closeModal() { setIsOpen(false); onClose(); } function handleClickConfirm() { onClickConfirm(`${predefinedPath}/${selectedPath}`, selectedFile); closeModal(); } const getFileList = async () => { setFileListLoading(true); try { const options: RequestInit = { body: JSON.stringify({ fs: predefinedPath, remote: selectedPath }), }; const res = await fetchRClone(RCloneEndPoints.getFilesList, options); const json = await res.json(); setFileList(json.list); } catch (error) { console.error(error); } finally { setFileListLoading(false); } }; const handleClickPath = (file: IRCloneFile) => { if (!file.IsDir && !canSelectFiles) { return; } setSelectedPath(file.Path); if (!file.IsDir) { setSelectedFile(file.Name); } else { setSelectedFile(""); } }; const handleClickHome = () => { setSelectedPath(""); setSelectedFile(""); }; const handleClickBack = () => { if (!selectedPath) { return; } const pathArr = selectedPath.split("/"); pathArr.pop(); if (selectedFile) { pathArr.pop(); } setSelectedPath(pathArr.join("/")); setSelectedFile(""); }; return (

SELECT A DIRECTORY TO SYNC

Current Path

{`${predefinedPath}/`} {selectedPath}
{fileListLoading ? (
) : fileList.length === 0 ? (

This directory is empty.

) : ( fileList.map((file, index) => { return (
handleClickPath(file)} > ); }) )}
Name Modified
{file.Name} {new Intl.DateTimeFormat("en").format( new Date(file.ModTime) )}
); }; export default RemotePathSelectorModal;