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";
|
2022-10-20 06:03:33 +00:00
|
|
|
import {
|
|
|
|
|
isPermitted,
|
|
|
|
|
PERMISSION_TYPE,
|
|
|
|
|
} from "@appsmith/utils/permissionHelpers";
|
2022-04-12 10:50:01 +00:00
|
|
|
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
feat: import changes batch 2 (#15722)
* Remove treedropdown from ads
* Change Treedropdown imports
* Remove Notification Banner, change imports
* Remove Toggle from ads
* Change toggle imports
* explicitly declare function argument types
* Remove Menu from ads
* Change menu imports
* Remove Spinner from ads
* Change spinner imports
* Remove Radio, import changes
* test: updated flaky test under default meta (#15707)
* updated flaky test
* Updated tests
* updated tests
* updated the tests
* updated tests
* Update constants.ts
* add more typecasting
* Remove ListSegmentHeader, import changes
* Remove TagInputComponent, import changes
* Remove Switch, import changes
* Remove SearchInput, change imports
* Rename TagInputComponent to TagInput
* Remove ProgressiveImage, import changes
* import changes for SearchVariant
* Remove menu divider, import changes
* Remove TableDropdown, import changes
* Remove Switcher
* Remove StatusBar, import changes
* Remove showcase carousel
* Remove RectangularSwitcher, import change
* Add types to TableDropdown's args
* Remove MultiSwitch, import change
* Remove GifPlayerComponent, import change
* Remove DraggableList, import change
* Remove DisplayImageUpload, import change
* Remove DatePickerComponent, import change
* Remove CopyToClipBoard, import change
* Remove ColorSelector, import change
* Remove TabItemBackgroundFill, NumberedStep, ColorPickerComponent
* GifPlayerComponent -> GifPlayer
* change named import
* Remove FormFieldError, change imports
* Update to new version of Tree Dropdown
* Fix issue with ads/index.ts
* Test file fix
* Fix issue with merge?!?!??
* update design system to 1.0.18
* Bump ds version
* bump ds version
* bump ds version
Co-authored-by: NandanAnantharamu <67676905+NandanAnantharamu@users.noreply.github.com>
Co-authored-by: Albin <albin@appsmith.com>
2022-09-02 08:38:17 +00:00
|
|
|
import {
|
|
|
|
|
Button,
|
|
|
|
|
Category,
|
|
|
|
|
Dropdown,
|
|
|
|
|
IconSize,
|
|
|
|
|
Size,
|
|
|
|
|
Spinner,
|
|
|
|
|
} from "design-system";
|
2022-03-10 14:39:05 +00:00
|
|
|
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";
|
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 {
|
|
|
|
|
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-08-22 05:09:39 +00:00
|
|
|
onSelect={(
|
|
|
|
|
_: string,
|
|
|
|
|
dropdownOption: React.SetStateAction<{
|
|
|
|
|
label: string;
|
|
|
|
|
value: string;
|
|
|
|
|
}>,
|
|
|
|
|
) => selectWorkspace(dropdownOption)}
|
2022-06-15 15:37:41 +00:00
|
|
|
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;
|