import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
import { capitalize, upperCase, omit } from 'lodash';
import { translate as __ } from 'foremanReact/common/I18n';
import { STATUS } from 'foremanReact/constants';
import {
Button,
Checkbox,
Drawer,
DrawerActions,
DrawerCloseButton,
DrawerContent,
DrawerContentBody,
DrawerHead,
DrawerPanelContent,
DrawerPanelBody,
Dropdown,
DropdownItem,
KebabToggle,
Text,
TextContent,
TextList,
TextListItem,
TextListItemVariants,
TextListVariants,
TextVariants,
} from '@patternfly/react-core';
import { TableVariant, Tbody, Td, Th, Thead, Tr, ActionsColumn } from '@patternfly/react-table';
import TableWrapper from '../../../components/Table/TableWrapper';
import {
selectAlternateContentSources,
selectAlternateContentSourcesError,
selectAlternateContentSourcesStatus,
} from '../ACSSelectors';
import { useSelectionSet, useTableSort } from '../../../components/Table/TableHooks';
import getAlternateContentSources, { deleteACS, bulkDeleteACS, getACSDetails, refreshACS, bulkRefreshACS } from '../ACSActions';
import ACSCreateWizard from '../Create/ACSCreateWizard';
import LastSync from '../../ContentViews/Details/Repositories/LastSync';
import ACSExpandableDetails from '../Details/ACSExpandableDetails';
import './ACSTable.scss';
import Loading from '../../../components/Loading';
import EmptyStateMessage from '../../../components/Table/EmptyStateMessage';
import { hasPermission } from '../../ContentViews/helpers';
const ACSTable = () => {
const response = useSelector(selectAlternateContentSources);
const status = useSelector(selectAlternateContentSourcesStatus);
const error = useSelector(selectAlternateContentSourcesError);
const resolved = status === STATUS.RESOLVED;
const [searchQuery, updateSearchQuery] = useState('');
const [isCreateWizardOpen, setIsCreateWizardOpen] = useState(false);
const dispatch = useDispatch();
const metadata = omit(response, ['results']);
const {
can_create: canCreate = false,
can_edit: canEdit = false,
can_delete: canDelete = false,
can_view: canView = false,
results,
} = response;
const { pathname } = useLocation();
const { push } = useHistory();
const [acsId, setAcsId] = useState(pathname.split('/')[2]);
const [expandedId, setExpandedId] = useState(acsId);
const [isExpanded, setIsExpanded] = useState(false);
const drawerRef = useRef(null);
const [kebabOpen, setKebabOpen] = useState(false);
const [detailsKebabOpen, setDetailsKebabOpen] = useState(false);
const [deleting, setDeleting] = useState(false);
const renderActionButtons = status === STATUS.RESOLVED && !!results?.length;
const {
selectOne, isSelected, isSelectable: _isSelectable,
selectedCount, selectionSet, ...selectionSetVars
} = useSelectionSet({
results,
metadata,
});
useEffect(() => {
if (acsId) {
dispatch(getACSDetails(acsId));
setExpandedId(acsId);
setIsExpanded(true);
}
}, [dispatch, acsId]);
const onExpand = () => drawerRef.current && drawerRef.current.focus();
const onDelete = (id) => {
setDeleting(true);
dispatch(deleteACS(id, () => {
setDeleting(false);
if (id?.toString() === acsId?.toString()) {
push('/alternate_content_sources');
} else {
dispatch(getAlternateContentSources());
}
}));
};
const onRefresh = (id) => {
dispatch(refreshACS(id, () =>
dispatch(getAlternateContentSources())));
};
const onBulkDelete = (ids) => {
setDeleting(true);
dispatch(bulkDeleteACS({ ids }, () => {
setDeleting(false);
if (acsId && ids.has(Number(acsId))) {
push('/alternate_content_sources');
} else {
dispatch(getAlternateContentSources());
}
}));
};
const onBulkRefresh = (ids) => {
dispatch(bulkRefreshACS({ ids }, () =>
dispatch(getAlternateContentSources())));
};
const createButtonOnclick = () => {
setIsCreateWizardOpen(true);
};
const onCloseClick = () => {
setExpandedId(null);
setAcsId(null);
window.history.replaceState(null, '', '/alternate_content_sources');
setIsExpanded(false);
};
const onClick = (id) => {
if (Number(id) === Number(expandedId)) {
onCloseClick();
} else {
setExpandedId(id);
setAcsId(id);
window.history.replaceState(null, '', `/alternate_content_sources/${id}/details`);
setIsExpanded(true);
}
};
const isSingleSelected = rowId => (Number(rowId) === Number(acsId) ||
Number(rowId) === Number(expandedId));
const customStyle = {
borderLeft: '5px solid var(--pf-global--primary-color--100)',
};
const PanelContent = () => {
if (!resolved) return <>>;
const acs = results?.find(source => source?.id === Number(expandedId));
if (!acs && isExpanded) {
setExpandedId(null);
setIsExpanded(false);
}
const { last_refresh: lastTask, permissions } = acs ?? {};
const { last_refresh_words: lastRefreshWords, started_at: startedAt } = lastTask ?? {};
return (