|
| 1 | +import React, { useState, useContext } from "react"; |
| 2 | +import { |
| 3 | + EuiBasicTable, |
| 4 | + EuiTableFieldDataColumnType, |
| 5 | + EuiTableComputedColumnType, |
| 6 | + EuiFieldSearch, |
| 7 | + EuiPageTemplate, |
| 8 | + CriteriaWithPagination, |
| 9 | + Pagination, |
| 10 | +} from "@elastic/eui"; |
| 11 | +import EuiCustomLink from "../../components/EuiCustomLink"; |
| 12 | +import { useParams } from "react-router-dom"; |
| 13 | +import useLoadRegistry from "../../queries/useLoadRegistry"; |
| 14 | +import RegistryPathContext from "../../contexts/RegistryPathContext"; |
| 15 | + |
| 16 | +interface Feature { |
| 17 | + name: string; |
| 18 | + featureView: string; |
| 19 | + type: string; |
| 20 | +} |
| 21 | + |
| 22 | +type FeatureColumn = |
| 23 | + | EuiTableFieldDataColumnType<Feature> |
| 24 | + | EuiTableComputedColumnType<Feature>; |
| 25 | + |
| 26 | +const FeatureListPage = () => { |
| 27 | + const { projectName } = useParams(); |
| 28 | + const registryUrl = useContext(RegistryPathContext); |
| 29 | + const { data, isLoading, isError } = useLoadRegistry(registryUrl); |
| 30 | + const [searchText, setSearchText] = useState(""); |
| 31 | + |
| 32 | + const [sortField, setSortField] = useState<keyof Feature>("name"); |
| 33 | + const [sortDirection, setSortDirection] = useState<"asc" | "desc">("asc"); |
| 34 | + |
| 35 | + const [pageIndex, setPageIndex] = useState(0); |
| 36 | + const [pageSize, setPageSize] = useState(100); |
| 37 | + |
| 38 | + if (isLoading) return <p>Loading...</p>; |
| 39 | + if (isError) return <p>Error loading features.</p>; |
| 40 | + |
| 41 | + const features: Feature[] = data?.allFeatures || []; |
| 42 | + |
| 43 | + const filteredFeatures = features.filter((feature) => |
| 44 | + feature.name.toLowerCase().includes(searchText.toLowerCase()), |
| 45 | + ); |
| 46 | + |
| 47 | + const sortedFeatures = [...filteredFeatures].sort((a, b) => { |
| 48 | + const valueA = a[sortField].toLowerCase(); |
| 49 | + const valueB = b[sortField].toLowerCase(); |
| 50 | + return sortDirection === "asc" |
| 51 | + ? valueA.localeCompare(valueB) |
| 52 | + : valueB.localeCompare(valueA); |
| 53 | + }); |
| 54 | + |
| 55 | + const paginatedFeatures = sortedFeatures.slice( |
| 56 | + pageIndex * pageSize, |
| 57 | + (pageIndex + 1) * pageSize, |
| 58 | + ); |
| 59 | + |
| 60 | + const columns: FeatureColumn[] = [ |
| 61 | + { |
| 62 | + name: "Feature Name", |
| 63 | + field: "name", |
| 64 | + sortable: true, |
| 65 | + render: (name: string, feature: Feature) => ( |
| 66 | + <EuiCustomLink |
| 67 | + to={`/p/${projectName}/feature-view/${feature.featureView}/feature/${name}`} |
| 68 | + > |
| 69 | + {name} |
| 70 | + </EuiCustomLink> |
| 71 | + ), |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "Feature View", |
| 75 | + field: "featureView", |
| 76 | + sortable: true, |
| 77 | + render: (featureView: string) => ( |
| 78 | + <EuiCustomLink to={`/p/${projectName}/feature-view/${featureView}`}> |
| 79 | + {featureView} |
| 80 | + </EuiCustomLink> |
| 81 | + ), |
| 82 | + }, |
| 83 | + { name: "Type", field: "type", sortable: true }, |
| 84 | + ]; |
| 85 | + |
| 86 | + const onTableChange = ({ page, sort }: CriteriaWithPagination<Feature>) => { |
| 87 | + if (sort) { |
| 88 | + setSortField(sort.field as keyof Feature); |
| 89 | + setSortDirection(sort.direction); |
| 90 | + } |
| 91 | + if (page) { |
| 92 | + setPageIndex(page.index); |
| 93 | + setPageSize(page.size); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + const getRowProps = (feature: Feature) => ({ |
| 98 | + "data-test-subj": `row-${feature.name}`, |
| 99 | + }); |
| 100 | + |
| 101 | + const pagination: Pagination = { |
| 102 | + pageIndex, |
| 103 | + pageSize, |
| 104 | + totalItemCount: sortedFeatures.length, |
| 105 | + pageSizeOptions: [20, 50, 100], |
| 106 | + }; |
| 107 | + |
| 108 | + return ( |
| 109 | + <EuiPageTemplate panelled> |
| 110 | + <EuiPageTemplate.Header pageTitle="Feature List" /> |
| 111 | + <EuiPageTemplate.Section> |
| 112 | + <EuiFieldSearch |
| 113 | + placeholder="Search features" |
| 114 | + value={searchText} |
| 115 | + onChange={(e) => setSearchText(e.target.value)} |
| 116 | + fullWidth |
| 117 | + /> |
| 118 | + <EuiBasicTable |
| 119 | + columns={columns} |
| 120 | + items={paginatedFeatures} |
| 121 | + rowProps={getRowProps} |
| 122 | + sorting={{ |
| 123 | + sort: { field: sortField, direction: sortDirection }, |
| 124 | + }} |
| 125 | + onChange={onTableChange} |
| 126 | + pagination={pagination} |
| 127 | + /> |
| 128 | + </EuiPageTemplate.Section> |
| 129 | + </EuiPageTemplate> |
| 130 | + ); |
| 131 | +}; |
| 132 | + |
| 133 | +export default FeatureListPage; |
0 commit comments