|
| 1 | +/*! |
| 2 | + * Copyright 2024 - Swiss Data Science Center (SDSC) |
| 3 | + * A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and |
| 4 | + * Eidgenössische Technische Hochschule Zürich (ETHZ). |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License |
| 17 | + */ |
| 18 | + |
| 19 | +import cx from "classnames"; |
| 20 | +import { ReactNode, useCallback, useEffect, useMemo } from "react"; |
| 21 | +import { Globe2, Lock } from "react-bootstrap-icons"; |
| 22 | +import { |
| 23 | + Link, |
| 24 | + generatePath, |
| 25 | + useSearchParams, |
| 26 | +} from "react-router-dom-v5-compat"; |
| 27 | +import { Card, CardBody, Col, Row } from "reactstrap"; |
| 28 | + |
| 29 | +import { Loader } from "../../../components/Loader"; |
| 30 | +import Pagination from "../../../components/Pagination"; |
| 31 | +import { TimeCaption } from "../../../components/TimeCaption"; |
| 32 | +import { RtkOrNotebooksError } from "../../../components/errors/RtkErrorAlert"; |
| 33 | +import { ABSOLUTE_ROUTES } from "../../../routing/routes.constants"; |
| 34 | +import type { DataConnector } from "../../projectsV2/api/data-connectors.api"; |
| 35 | +import { |
| 36 | + useGetNamespacesByNamespaceSlugQuery, |
| 37 | + useGetDataConnectorsQuery, |
| 38 | +} from "../../projectsV2/api/projectV2.enhanced-api"; |
| 39 | +import ClampedParagraph from "../../../components/clamped/ClampedParagraph"; |
| 40 | + |
| 41 | +const DEFAULT_PER_PAGE = 12; |
| 42 | +const DEFAULT_PAGE_PARAM = "page"; |
| 43 | + |
| 44 | +interface DataConnectorListDisplayProps { |
| 45 | + namespace?: string; |
| 46 | + pageParam?: string; |
| 47 | + perPage?: number; |
| 48 | + emptyListElement?: ReactNode; |
| 49 | +} |
| 50 | + |
| 51 | +export default function DataConnectorListDisplay({ |
| 52 | + namespace: ns, |
| 53 | + pageParam: pageParam_, |
| 54 | + perPage: perPage_, |
| 55 | + emptyListElement, |
| 56 | +}: DataConnectorListDisplayProps) { |
| 57 | + const pageParam = useMemo( |
| 58 | + () => (pageParam_ ? pageParam_ : DEFAULT_PAGE_PARAM), |
| 59 | + [pageParam_] |
| 60 | + ); |
| 61 | + const perPage = useMemo( |
| 62 | + () => (perPage_ ? perPage_ : DEFAULT_PER_PAGE), |
| 63 | + [perPage_] |
| 64 | + ); |
| 65 | + |
| 66 | + const [searchParams, setSearchParams] = useSearchParams(); |
| 67 | + const onPageChange = useCallback( |
| 68 | + (pageNumber: number) => { |
| 69 | + setSearchParams((prevParams) => { |
| 70 | + if (pageNumber == 1) { |
| 71 | + prevParams.delete(pageParam); |
| 72 | + } else { |
| 73 | + prevParams.set(pageParam, `${pageNumber}`); |
| 74 | + } |
| 75 | + return prevParams; |
| 76 | + }); |
| 77 | + }, |
| 78 | + [pageParam, setSearchParams] |
| 79 | + ); |
| 80 | + |
| 81 | + const page = useMemo(() => { |
| 82 | + const pageRaw = searchParams.get(pageParam); |
| 83 | + if (!pageRaw) { |
| 84 | + return 1; |
| 85 | + } |
| 86 | + try { |
| 87 | + const page = parseInt(pageRaw, 10); |
| 88 | + return page > 0 ? page : 1; |
| 89 | + } catch { |
| 90 | + return 1; |
| 91 | + } |
| 92 | + }, [pageParam, searchParams]); |
| 93 | + |
| 94 | + const { data, error, isLoading } = useGetDataConnectorsQuery({ |
| 95 | + params: { |
| 96 | + namespace: ns, |
| 97 | + page, |
| 98 | + per_page: perPage, |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + useEffect(() => { |
| 103 | + if (data?.totalPages && page > data.totalPages) { |
| 104 | + setSearchParams( |
| 105 | + (prevParams) => { |
| 106 | + if (data.totalPages == 1) { |
| 107 | + prevParams.delete(pageParam); |
| 108 | + } else { |
| 109 | + prevParams.set(pageParam, `${data.totalPages}`); |
| 110 | + } |
| 111 | + return prevParams; |
| 112 | + }, |
| 113 | + { replace: true } |
| 114 | + ); |
| 115 | + } |
| 116 | + }, [data?.totalPages, page, pageParam, setSearchParams]); |
| 117 | + |
| 118 | + if (isLoading) |
| 119 | + return ( |
| 120 | + <div className={cx("d-flex", "justify-content-center", "w-100")}> |
| 121 | + <div className={cx("d-flex", "flex-column")}> |
| 122 | + <Loader /> |
| 123 | + <div>Retrieving projects...</div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ); |
| 127 | + |
| 128 | + if (error || data == null) { |
| 129 | + return <RtkOrNotebooksError error={error} dismissible={false} />; |
| 130 | + } |
| 131 | + |
| 132 | + if (!data.total) { |
| 133 | + return emptyListElement ?? <p>The project list is empty.</p>; |
| 134 | + } |
| 135 | + |
| 136 | + return ( |
| 137 | + <div className={cx("d-flex", "flex-column", "gap-3")}> |
| 138 | + <Row |
| 139 | + className={cx("row-cols-1", "row-cols-md-2", "row-cols-xxl-3", "g-3")} |
| 140 | + > |
| 141 | + {data.dataConnectors?.map((dc) => ( |
| 142 | + <ListDisplayDataConnector key={dc.id} dataConnector={dc} /> |
| 143 | + ))} |
| 144 | + </Row> |
| 145 | + <Pagination |
| 146 | + currentPage={data.page} |
| 147 | + perPage={perPage} |
| 148 | + totalItems={data.total} |
| 149 | + onPageChange={onPageChange} |
| 150 | + className={cx( |
| 151 | + "d-flex", |
| 152 | + "justify-content-center", |
| 153 | + "rk-search-pagination" |
| 154 | + )} |
| 155 | + /> |
| 156 | + </div> |
| 157 | + ); |
| 158 | +} |
| 159 | + |
| 160 | +interface ListDisplayDataConnectorProps { |
| 161 | + dataConnector: DataConnector; |
| 162 | +} |
| 163 | +function ListDisplayDataConnector({ |
| 164 | + dataConnector, |
| 165 | +}: ListDisplayDataConnectorProps) { |
| 166 | + const { data: namespaceData } = useGetNamespacesByNamespaceSlugQuery({ |
| 167 | + namespaceSlug: dataConnector.namespace, |
| 168 | + }); |
| 169 | + |
| 170 | + const { |
| 171 | + name, |
| 172 | + namespace, |
| 173 | + description, |
| 174 | + visibility, |
| 175 | + creation_date: creationDate, |
| 176 | + } = dataConnector; |
| 177 | + |
| 178 | + const namespaceUrl = |
| 179 | + namespaceData && namespaceData.namespace_kind === "group" |
| 180 | + ? generatePath(ABSOLUTE_ROUTES.v2.groups.show.root, { slug: namespace }) |
| 181 | + : generatePath(ABSOLUTE_ROUTES.v2.users.show, { |
| 182 | + username: dataConnector.namespace, |
| 183 | + }); |
| 184 | + |
| 185 | + return ( |
| 186 | + <Col> |
| 187 | + <Card className="h-100" data-cy="project-card"> |
| 188 | + <CardBody className={cx("d-flex", "flex-column")}> |
| 189 | + <h5>{name}</h5> |
| 190 | + <p> |
| 191 | + <Link to={namespaceUrl}> |
| 192 | + {"@"} |
| 193 | + {namespace} |
| 194 | + </Link> |
| 195 | + </p> |
| 196 | + {description && <ClampedParagraph>{description}</ClampedParagraph>} |
| 197 | + <div |
| 198 | + className={cx( |
| 199 | + "align-items-center", |
| 200 | + "d-flex", |
| 201 | + "flex-wrap", |
| 202 | + "gap-2", |
| 203 | + "justify-content-between", |
| 204 | + "mt-auto" |
| 205 | + )} |
| 206 | + > |
| 207 | + <div> |
| 208 | + {visibility.toLowerCase() === "private" ? ( |
| 209 | + <> |
| 210 | + <Lock className={cx("bi", "me-1")} /> |
| 211 | + Private |
| 212 | + </> |
| 213 | + ) : ( |
| 214 | + <> |
| 215 | + <Globe2 className={cx("bi", "me-1")} /> |
| 216 | + Public |
| 217 | + </> |
| 218 | + )} |
| 219 | + </div> |
| 220 | + <TimeCaption |
| 221 | + datetime={creationDate} |
| 222 | + prefix="Created" |
| 223 | + enableTooltip |
| 224 | + /> |
| 225 | + </div> |
| 226 | + </CardBody> |
| 227 | + </Card> |
| 228 | + </Col> |
| 229 | + ); |
| 230 | +} |
0 commit comments