2022-04-11 05:14:50 +00:00
|
|
|
import React, { useState, useMemo, useEffect } from "react";
|
2021-03-10 07:08:20 +00:00
|
|
|
import { useDispatch } from "react-redux";
|
|
|
|
|
import { useSelector } from "store";
|
2022-06-15 15:37:41 +00:00
|
|
|
import { getUserApplicationsWorkspaces } from "selectors/applicationSelectors";
|
2021-03-10 07:08:20 +00:00
|
|
|
import { isPermitted, PERMISSION_TYPE } from "./permissionHelpers";
|
2022-04-12 10:50:01 +00:00
|
|
|
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
|
2021-05-04 06:04:23 +00:00
|
|
|
import { AppState } from "reducers";
|
2022-03-10 14:39:05 +00:00
|
|
|
import Button, { Category, Size } from "components/ads/Button";
|
|
|
|
|
import { StyledDialog, ButtonWrapper, SpinnerWrapper } from "./ForkModalStyles";
|
2021-05-04 06:04:23 +00:00
|
|
|
import { getIsFetchingApplications } from "selectors/applicationSelectors";
|
2021-03-12 07:44:16 +00:00
|
|
|
import { useLocation } from "react-router";
|
|
|
|
|
import Spinner from "components/ads/Spinner";
|
|
|
|
|
import { IconSize } from "components/ads/Icon";
|
2021-10-18 14:03:44 +00:00
|
|
|
import { matchViewerForkPath } from "constants/routes";
|
2022-03-10 14:39:05 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
|
|
|
|
import { Dropdown } from "components/ads";
|
|
|
|
|
import {
|
|
|
|
|
CANCEL,
|
|
|
|
|
createMessage,
|
|
|
|
|
FORK,
|
|
|
|
|
FORK_APP_MODAL_EMPTY_TITLE,
|
|
|
|
|
FORK_APP_MODAL_LOADING_TITLE,
|
|
|
|
|
FORK_APP_MODAL_SUCCESS_TITLE,
|
|
|
|
|
} from "@appsmith/constants/messages";
|
2022-04-11 05:14:50 +00:00
|
|
|
import { getAllApplications } from "actions/applicationActions";
|
2021-03-10 07:08:20 +00:00
|
|
|
|
2021-05-04 06:04:23 +00:00
|
|
|
type ForkApplicationModalProps = {
|
|
|
|
|
applicationId: string;
|
|
|
|
|
// if a trigger is passed
|
|
|
|
|
// it renders that component
|
|
|
|
|
trigger?: React.ReactNode;
|
|
|
|
|
isModalOpen?: boolean;
|
|
|
|
|
setModalClose?: (isOpen: boolean) => void;
|
|
|
|
|
};
|
2021-03-12 07:44:16 +00:00
|
|
|
|
2021-05-04 06:04:23 +00:00
|
|
|
function ForkApplicationModal(props: ForkApplicationModalProps) {
|
2021-05-13 08:35:39 +00:00
|
|
|
const { isModalOpen, setModalClose } = props;
|
2022-06-15 15:37:41 +00:00
|
|
|
const [workspace, selectWorkspace] = useState<{
|
2022-03-10 14:39:05 +00:00
|
|
|
label: string;
|
|
|
|
|
value: string;
|
|
|
|
|
}>({ label: "", value: "" });
|
2021-03-10 07:08:20 +00:00
|
|
|
const dispatch = useDispatch();
|
2022-06-15 15:37:41 +00:00
|
|
|
const userWorkspaces = useSelector(getUserApplicationsWorkspaces);
|
2021-03-12 07:44:16 +00:00
|
|
|
const forkingApplication = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.applications.forkingApplication,
|
|
|
|
|
);
|
2021-05-04 06:04:23 +00:00
|
|
|
|
2022-04-11 05:14:50 +00:00
|
|
|
useEffect(() => {
|
2022-06-15 15:37:41 +00:00
|
|
|
if (!userWorkspaces.length) {
|
2022-04-11 05:14:50 +00:00
|
|
|
dispatch(getAllApplications());
|
|
|
|
|
}
|
2022-06-15 15:37:41 +00:00
|
|
|
}, [userWorkspaces.length]);
|
2022-04-11 05:14:50 +00:00
|
|
|
|
2021-05-04 06:04:23 +00:00
|
|
|
const isFetchingApplications = useSelector(getIsFetchingApplications);
|
2021-03-12 07:44:16 +00:00
|
|
|
const { pathname } = useLocation();
|
2021-10-18 14:03:44 +00:00
|
|
|
|
|
|
|
|
const showBasedOnURL = matchViewerForkPath(pathname);
|
2021-03-10 07:08:20 +00:00
|
|
|
|
|
|
|
|
const forkApplication = () => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FORK_APPLICATION_INIT,
|
|
|
|
|
payload: {
|
|
|
|
|
applicationId: props.applicationId,
|
2022-06-15 15:37:41 +00:00
|
|
|
workspaceId: workspace?.value,
|
2021-03-10 07:08:20 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-15 15:37:41 +00:00
|
|
|
const workspaceList = useMemo(() => {
|
|
|
|
|
const filteredUserWorkspaces = userWorkspaces.filter((item) => {
|
2021-03-10 07:08:20 +00:00
|
|
|
const permitted = isPermitted(
|
2022-06-15 15:37:41 +00:00
|
|
|
item.workspace.userPermissions ?? [],
|
2021-03-10 07:08:20 +00:00
|
|
|
PERMISSION_TYPE.CREATE_APPLICATION,
|
|
|
|
|
);
|
|
|
|
|
return permitted;
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-15 15:37:41 +00:00
|
|
|
if (filteredUserWorkspaces.length) {
|
|
|
|
|
selectWorkspace({
|
|
|
|
|
label: filteredUserWorkspaces[0].workspace.name,
|
|
|
|
|
value: filteredUserWorkspaces[0].workspace.id,
|
2022-03-10 14:39:05 +00:00
|
|
|
});
|
2021-03-10 07:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
2022-06-15 15:37:41 +00:00
|
|
|
return filteredUserWorkspaces.map((workspace) => {
|
2021-03-10 07:08:20 +00:00
|
|
|
return {
|
2022-06-15 15:37:41 +00:00
|
|
|
label: workspace.workspace.name,
|
|
|
|
|
value: workspace.workspace.id,
|
2021-03-10 07:08:20 +00:00
|
|
|
};
|
|
|
|
|
});
|
2022-06-15 15:37:41 +00:00
|
|
|
}, [userWorkspaces]);
|
2021-03-10 07:08:20 +00:00
|
|
|
|
2022-03-10 14:39:05 +00:00
|
|
|
const modalHeading = isFetchingApplications
|
|
|
|
|
? createMessage(FORK_APP_MODAL_LOADING_TITLE)
|
2022-06-15 15:37:41 +00:00
|
|
|
: !workspaceList.length
|
2022-03-10 14:39:05 +00:00
|
|
|
? createMessage(FORK_APP_MODAL_EMPTY_TITLE)
|
|
|
|
|
: createMessage(FORK_APP_MODAL_SUCCESS_TITLE);
|
|
|
|
|
|
2021-03-10 07:08:20 +00:00
|
|
|
return (
|
|
|
|
|
<StyledDialog
|
2021-04-28 10:28:39 +00:00
|
|
|
canOutsideClickClose
|
2021-03-10 07:08:20 +00:00
|
|
|
className={"fork-modal"}
|
2022-03-31 05:16:04 +00:00
|
|
|
headerIcon={{ name: "fork-2", bgColor: Colors.GEYSER_LIGHT }}
|
2021-05-04 06:04:23 +00:00
|
|
|
isOpen={isModalOpen || showBasedOnURL}
|
|
|
|
|
setModalClose={setModalClose}
|
2022-03-10 14:39:05 +00:00
|
|
|
title={modalHeading}
|
2021-05-04 06:04:23 +00:00
|
|
|
trigger={props.trigger}
|
2021-03-10 07:08:20 +00:00
|
|
|
>
|
2022-03-10 14:39:05 +00:00
|
|
|
{isFetchingApplications ? (
|
2021-03-12 07:44:16 +00:00
|
|
|
<SpinnerWrapper>
|
|
|
|
|
<Spinner size={IconSize.XXXL} />
|
|
|
|
|
</SpinnerWrapper>
|
2022-03-10 14:39:05 +00:00
|
|
|
) : (
|
2022-06-15 15:37:41 +00:00
|
|
|
!!workspaceList.length && (
|
2022-03-10 14:39:05 +00:00
|
|
|
<>
|
|
|
|
|
<Dropdown
|
2022-03-31 05:16:04 +00:00
|
|
|
boundary="viewport"
|
2022-03-10 14:39:05 +00:00
|
|
|
dropdownMaxHeight={"200px"}
|
|
|
|
|
fillOptions
|
2022-06-15 15:37:41 +00:00
|
|
|
onSelect={(_, dropdownOption) => selectWorkspace(dropdownOption)}
|
|
|
|
|
options={workspaceList}
|
|
|
|
|
selected={workspace}
|
2022-03-10 14:39:05 +00:00
|
|
|
showLabelOnly
|
|
|
|
|
width={"100%"}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<ButtonWrapper>
|
|
|
|
|
<Button
|
|
|
|
|
category={Category.tertiary}
|
|
|
|
|
disabled={forkingApplication}
|
|
|
|
|
onClick={() => setModalClose && setModalClose(false)}
|
|
|
|
|
size={Size.large}
|
2022-04-12 13:53:57 +00:00
|
|
|
tag="button"
|
2022-03-10 14:39:05 +00:00
|
|
|
text={createMessage(CANCEL)}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
2022-06-15 15:37:41 +00:00
|
|
|
className="t--fork-app-to-workspace-button"
|
2022-03-10 14:39:05 +00:00
|
|
|
isLoading={forkingApplication}
|
|
|
|
|
onClick={forkApplication}
|
|
|
|
|
size={Size.large}
|
2022-04-12 13:53:57 +00:00
|
|
|
tag="button"
|
2022-03-10 14:39:05 +00:00
|
|
|
text={createMessage(FORK)}
|
|
|
|
|
/>
|
|
|
|
|
</ButtonWrapper>
|
|
|
|
|
</>
|
|
|
|
|
)
|
2021-03-12 07:44:16 +00:00
|
|
|
)}
|
2021-03-10 07:08:20 +00:00
|
|
|
</StyledDialog>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2021-03-10 07:08:20 +00:00
|
|
|
|
|
|
|
|
export default ForkApplicationModal;
|