import * as React from 'react'; import { EmptyStateBody } from '@patternfly/react-core'; import emptySnapshotImgUrl from '~/assets/Snapshots.svg'; import ColumnManagement from '~/components/ColumnManagement/ColumnManagement'; import { LEARN_MORE_SNAPSHOTS } from '~/consts/documentation'; import { PipelineRunEventType, PipelineRunLabel } from '~/consts/pipelinerun'; import { useK8sAndKarchResources } from '~/hooks/useK8sAndKarchResources'; import { SnapshotGroupVersionKind, SnapshotModel } from '~/models'; import { ExternalLink } from '~/shared'; import AppEmptyState from '~/shared/components/empty-state/AppEmptyState'; import FilteredEmptyState from '~/shared/components/empty-state/FilteredEmptyState'; import { useFilterState, useFilteredData, FilterToolbar } from '~/shared/components/Filter'; import ListLayout from '~/shared/components/list-layout/ListLayout'; import { Table, TableContainer } from '~/shared/components/TableV2'; import { useNamespace } from '~/shared/providers/Namespace'; import { getErrorState } from '~/shared/utils/error-utils'; import { Snapshot } from '~/types/coreBuildService'; import { FILTER_BY_VALUES, filterConfigs, filterOptions, SNAPSHOTS_LIST_COLUMN_STATE_KEY, SNAPSHOTS_LIST_COLUMNS, } from './snapshots-table-config'; export type SnapshotsListViewProps = { applicationName: string; groupName?: never } | { applicationName?: never; groupName: string }; const SnapshotsListView: React.FC> = ({ applicationName, groupName, }) => { const namespace = useNamespace(); const columnStateKey = groupName ? 'component-group-snapshots-list' : SNAPSHOTS_LIST_COLUMN_STATE_KEY; const { filterValues, clientFilterValues, clearAll, isFiltered } = useFilterState(filterConfigs); const { data: snapshots, getSource, isLoading, clusterError, archiveError, hasNextPage, isFetchingNextPage, fetchNextPage, } = useK8sAndKarchResources( applicationName || groupName ? { groupVersionKind: SnapshotGroupVersionKind, namespace, isList: true, selector: { matchLabels: { [groupName ? PipelineRunLabel.COMPONENT_GROUP : PipelineRunLabel.APPLICATION]: groupName || applicationName, }, }, } : undefined, SnapshotModel, undefined, undefined, { enableArchive: !filterValues.filterBy.includes(FILTER_BY_VALUES.RELEASABLE), }, ); const { filteredData: textFiltered } = useFilteredData( filterConfigs, snapshots ?? [], clientFilterValues, ); const finalFilteredSnapshots = React.useMemo(() => { return textFiltered.filter((s) => { if ( filterValues.filterBy.includes(FILTER_BY_VALUES.SHOW_MERGED_ONLY) && s.metadata.labels?.[PipelineRunLabel.TEST_COMMIT_EVENT_TYPE_LABEL] === PipelineRunEventType.PULL ) { return false; } return true; }); }, [textFiltered, filterValues.filterBy]); const meta = React.useMemo( () => ({ namespace, applicationName, groupName, getSource }), [namespace, applicationName, groupName, getSource], ); if (clusterError && archiveError) { // Don't display cluster error if the code is 404 as this error is expected if ( typeof clusterError === 'object' && clusterError !== null && 'code' in clusterError && clusterError.code !== 404 ) { return getErrorState(clusterError, !isLoading, 'snapshots'); } return getErrorState(archiveError, !isLoading, 'snapshots'); } return ( A snapshot is a point-in-time, immutable record of{' '} {groupName ? 'a component group' : 'an application'}'s container images.{' '} Learn more } > } noDataState={ Snapshots are created automatically by push events or pull request events. Snapshots can also be created manually if needed. Once created, Snapshots will be displayed on this page. } toolbar={ isFiltered || (snapshots ?? []).length > 0 ? ( columns={SNAPSHOTS_LIST_COLUMNS} columnStateKey={columnStateKey} showColumnManagement /> ) : undefined } > obj.metadata.uid ?? obj.metadata.name} aria-label="Snapshots List" enableSorting hasNextPage={hasNextPage} isFetchingNextPage={isFetchingNextPage} fetchNextPage={fetchNextPage} columnStateKey={columnStateKey} meta={meta} /> ); }; export default SnapshotsListView;