ProjectSelectionModal.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { RouteComponentProps, withRouter } from "react-router";
  2. import styled from "styled-components";
  3. import React, { useContext, useEffect, useState } from "react";
  4. import Modal from "components/porter/Modal";
  5. import Text from "components/porter/Text";
  6. import Spacer from "components/porter/Spacer";
  7. import { Context } from "shared/Context";
  8. import { DetailedClusterType, ProjectType } from "shared/types";
  9. import gradient from "assets/gradient.png";
  10. import { pushFiltered } from "shared/routing";
  11. import SearchBar from "components/porter/SearchBar";
  12. import { search } from "shared/search";
  13. import _ from 'lodash';
  14. import { useMemo } from 'react';
  15. import api from "shared/api";
  16. import Button from "components/porter/Button";
  17. import Container from "components/porter/Container";
  18. type Props = RouteComponentProps & {
  19. closeModal: () => void;
  20. projects: ProjectType[];
  21. currentProject: ProjectType;
  22. }
  23. const ProjectSelectionModal: React.FC<Props> = ({
  24. closeModal,
  25. projects,
  26. currentProject,
  27. ...props
  28. }) => {
  29. const context = useContext(Context);
  30. const { setCurrentProject, setCurrentCluster, user } = context;
  31. const [searchValue, setSearchValue] = useState("");
  32. const [clusters, setClusters] = useState<DetailedClusterType[]>([]);
  33. const [loading, setLoading] = useState<boolean>(true);
  34. const [error, setError] = useState<string>("");
  35. const filteredProjects = useMemo(() => {
  36. const filteredBySearch = projects.filter((project) => {
  37. return project.id === Number(searchValue) || project.name.toLowerCase().includes(searchValue.toLowerCase());
  38. });
  39. // sort and return all the projects
  40. const sortedProjects = _.sortBy(filteredBySearch, 'name');
  41. // move the selected project to the top
  42. const selectedProjectIndex = sortedProjects.findIndex(project => project.id === currentProject.id);
  43. if (selectedProjectIndex !== -1) {
  44. const selectedProject = sortedProjects.splice(selectedProjectIndex, 1)[0];
  45. sortedProjects.unshift(selectedProject);
  46. }
  47. return sortedProjects;
  48. }, [projects, searchValue, currentProject]);
  49. useEffect(() => {
  50. if (typeof window !== 'undefined') {
  51. const handleKeyDown = (e: KeyboardEvent) => {
  52. if (e.key === "Escape" || e.keyCode === 27) {
  53. closeModal();
  54. }
  55. };
  56. window.addEventListener('keydown', handleKeyDown);
  57. return () => {
  58. window.removeEventListener('keydown', handleKeyDown);
  59. };
  60. }
  61. }, [closeModal]);
  62. const updateClusterList = async (projectId: number) => {
  63. try {
  64. setLoading(true)
  65. const res = await api.getClusters(
  66. "<token>",
  67. {},
  68. { id: projectId }
  69. );
  70. if (res.data) {
  71. setClusters(res.data);
  72. setLoading(false);
  73. setError("");
  74. return res.data;
  75. } else {
  76. setLoading(false);
  77. setError("Response data missing");
  78. }
  79. } catch (err) {
  80. setError(err.toString());
  81. }
  82. };
  83. const renderBlockList = () => {
  84. return filteredProjects.map((project: ProjectType, i: number) => {
  85. return (
  86. <IdContainer
  87. key={i}
  88. selected={project.id === currentProject.id}
  89. onClick={async () => {
  90. setCurrentProject(project);
  91. const clusters_list = await updateClusterList(project.id);
  92. console.log(clusters_list);
  93. if (clusters_list?.length > 0) {
  94. setCurrentCluster(clusters_list[0]);
  95. if (project.simplified_view_enabled) {
  96. pushFiltered(props, "/onboarding/source", ["project_id"], {});
  97. }
  98. else {
  99. pushFiltered(props, "/applications", ["project_id"], {});
  100. }
  101. } else {
  102. pushFiltered(props, "/onboarding", ["project_id"], {});
  103. }
  104. closeModal();
  105. }}
  106. >
  107. {/* <BlockIcon src={gradient} /> */}
  108. <BlockTitle>{project.name}</BlockTitle>
  109. <BlockDescription>
  110. Project ID: {project.id}
  111. </BlockDescription>
  112. </IdContainer>
  113. );
  114. });
  115. };
  116. return (
  117. <Modal closeModal={closeModal} width={'600px'}>
  118. <Text size={16} style={{ marginRight: '10px' }}>
  119. Switch Project
  120. </Text>
  121. <Spacer y={1} />
  122. <Container row spaced>
  123. <SearchBar
  124. value={searchValue}
  125. setValue={(x) => {
  126. setSearchValue(x);
  127. }}
  128. placeholder="Search projects..."
  129. width="100%"
  130. autoFocus={true}
  131. />
  132. <Spacer inline x={1} />
  133. {user.isPorterUser && <Button onClick={() =>
  134. pushFiltered(props, "/new-project", ["project_id"], {
  135. new_project: true,
  136. })} height="30px" width="130px">
  137. <I className="material-icons">add</I> New Project
  138. </Button>}
  139. </Container>
  140. <Spacer y={1} />
  141. <ScrollableContent> {/* Wrap the block list */}
  142. {/* <BlockList>
  143. {renderBlockList()}
  144. </BlockList> */}
  145. {renderBlockList()}
  146. <Spacer height="15px" />
  147. </ScrollableContent>
  148. </Modal >
  149. )
  150. }
  151. export default withRouter(ProjectSelectionModal);
  152. const IdContainer = styled.div`
  153. color: #ffffff;
  154. border-radius: 5px;
  155. padding: 5px;
  156. display: block;
  157. width: 100%;
  158. border-radius: 5px;
  159. background:${(props) => props.theme.clickable.bg};
  160. border: 1px solid ${({ theme }) => theme.border};
  161. margin-bottom: 10px;
  162. margin-top: 5px;
  163. border: ${props => props.selected ? "2px solid #8590ff" : "1px solid #494b4f"};
  164. :hover {
  165. border: ${({ selected }) => (!selected && "1px solid #7a7b80")};
  166. }
  167. cursor: pointer;
  168. animation: fadeIn 0.3s 0s;
  169. @keyframes fadeIn {
  170. from {
  171. opacity: 0;
  172. }
  173. to {
  174. opacity: 1;
  175. }
  176. }
  177. `;
  178. const BlockDescription = styled.div`
  179. color: #ffffff66;
  180. margin-left: -10px;
  181. margin-top: 4px;
  182. text-align: center;
  183. font-weight: default;
  184. font-size: 13px;
  185. padding: 0px 25px;
  186. height: 2.4em;
  187. font-size: 12px;
  188. display: -webkit-box;
  189. overflow: hidden;
  190. -webkit-line-clamp: 2;
  191. -webkit-box-orient: vertical;
  192. `;
  193. const BlockTitle = styled.div`
  194. margin-top: 12px;
  195. width: 100%;
  196. margin-left: -10px;
  197. text-align: center;
  198. font-size: 16px;
  199. justify-content: center;
  200. white-space: nowrap;
  201. overflow: hidden;
  202. text-overflow: ellipsis;
  203. `;
  204. const ScrollableContent = styled.div`
  205. overflow-y: auto; /* Enable vertical scrolling */
  206. height: calc(100vh - 500px); /* Set the maximum height */
  207. padding-right: 15px; /* Add some right padding to account for scrollbar */
  208. `;
  209. const I = styled.i`
  210. color: white;
  211. font-size: 14px;
  212. display: flex;
  213. align-items: center;
  214. margin-right: 5px;
  215. justify-content: center;
  216. `;