|
| 1 | +import React, { useCallback, useState } from "react"; |
| 2 | + |
| 3 | +import { Pagination, Spinner } from "@canonical/react-components"; |
| 4 | + |
| 5 | +import Asset from "./Asset"; |
| 6 | + |
| 7 | +import { useWebpageAssets } from "@/services/api/hooks/assets"; |
| 8 | +import type { IPage } from "@/services/api/types/pages"; |
| 9 | + |
| 10 | +const WebpageAssets: React.FC<{ page: IPage }> = ({ page }) => { |
| 11 | + const [currentPage, setCurrentPage] = useState(1); |
| 12 | + const { data: assetsData, isLoading } = useWebpageAssets(page.url ?? "", page.project?.name ?? "", currentPage, 12); |
| 13 | + |
| 14 | + const paginate = useCallback((pageNumber: number) => { |
| 15 | + setCurrentPage(pageNumber); |
| 16 | + document.querySelector("#webpage-assets")?.scrollIntoView({ |
| 17 | + block: "start", |
| 18 | + behavior: "smooth", |
| 19 | + }); |
| 20 | + }, []); |
| 21 | + |
| 22 | + if (isLoading) return <Spinner text="Loading assets ..." />; |
| 23 | + if (!assetsData?.assets?.length) return null; |
| 24 | + |
| 25 | + return ( |
| 26 | + <div id="webpage-assets"> |
| 27 | + <section className="p-section"> |
| 28 | + <p className="p-text--small-caps u-sv1">Assets used: {assetsData.total}</p> |
| 29 | + <div className="grid-row--25-25-25-25"> |
| 30 | + {assetsData.assets?.map((asset) => { |
| 31 | + return ( |
| 32 | + <div className="grid-col" key={asset.id}> |
| 33 | + <div className="p-section--shallow"> |
| 34 | + <Asset asset={asset} /> |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + ); |
| 38 | + })} |
| 39 | + </div> |
| 40 | + <Pagination |
| 41 | + centered |
| 42 | + currentPage={assetsData.page} |
| 43 | + itemsPerPage={assetsData.page_size} |
| 44 | + paginate={paginate} |
| 45 | + totalItems={assetsData.total} |
| 46 | + /> |
| 47 | + </section> |
| 48 | + </div> |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +export default WebpageAssets; |
0 commit comments