Skip to content

Commit

Permalink
[Fleet] Replace advanced usage of deprecated ref method with controll…
Browse files Browse the repository at this point in the history
…ed `selection.selected` API
  • Loading branch information
cee-chen committed Jan 26, 2024
1 parent 3213541 commit fc61638
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ interface Props {
sortField: keyof Agent;
sortOrder: 'asc' | 'desc';
onSelectionChange: (agents: Agent[]) => void;
tableRef?: React.Ref<any>;
selected: Agent[];
showUpgradeable: boolean;
totalAgents?: number;
pagination: Pagination;
Expand All @@ -77,9 +77,9 @@ export const AgentListTable: React.FC<Props> = (props: Props) => {
renderActions,
sortField,
sortOrder,
tableRef,
onTableChange,
onSelectionChange,
selected,
totalAgents = 0,
showUpgradeable,
pagination,
Expand Down Expand Up @@ -318,7 +318,6 @@ export const AgentListTable: React.FC<Props> = (props: Props) => {

return (
<EuiBasicTable<Agent>
ref={tableRef}
className="fleet__agentList__table"
data-test-subj="fleetAgentListTable"
loading={isLoading}
Expand All @@ -341,6 +340,7 @@ export const AgentListTable: React.FC<Props> = (props: Props) => {
}}
isSelectable={true}
selection={{
selected,
onSelectionChange,
selectable: isAgentSelectable,
selectableMessage: (selectable, agent) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
import React, { useState, useMemo, useCallback, useRef, useEffect } from 'react';
import { differenceBy, isEqual } from 'lodash';
import type { EuiBasicTable } from '@elastic/eui';
import { EuiSpacer, EuiPortal } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

Expand Down Expand Up @@ -72,7 +71,6 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
const [search, setSearch] = useState<string>(defaultKuery);
const [selectionMode, setSelectionMode] = useState<SelectionMode>('manual');
const [selectedAgents, setSelectedAgents] = useState<Agent[]>([]);
const tableRef = useRef<EuiBasicTable<Agent>>(null);
const { pagination, pageSizeOptions, setPagination } = usePagination();
const [sortField, setSortField] = useState<keyof Agent>('enrolled_at');
const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('desc');
Expand Down Expand Up @@ -413,21 +411,19 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
);

const onSelectionChange = (newAgents: Agent[]) => {
setSelectedAgents(newAgents);
if (selectionMode === 'query' && newAgents.length < selectedAgents.length) {
// differentiating between selection changed by agents dropping from current page or user action
const areSelectedAgentsStillVisible =
selectedAgents.length > 0 &&
differenceBy(selectedAgents, agentsOnCurrentPage, 'id').length === 0;
if (areSelectedAgentsStillVisible) {
setSelectionMode('manual');
} else {
if (!areSelectedAgentsStillVisible) {
// force selecting all agents on current page if staying in query mode
if (tableRef?.current) {
tableRef.current.setSelection(agentsOnCurrentPage);
}
return setSelectedAgents(agentsOnCurrentPage);
} else {
setSelectionMode('manual');
}
}
setSelectedAgents(newAgents);
};

const agentToUnenrollHasFleetServer = useMemo(() => {
Expand Down Expand Up @@ -634,10 +630,8 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
setSelectionMode={setSelectionMode}
selectedAgents={selectedAgents}
setSelectedAgents={(newAgents: Agent[]) => {
if (tableRef?.current) {
tableRef.current.setSelection(newAgents);
setSelectionMode('manual');
}
setSelectedAgents(newAgents);
setSelectionMode('manual');
}}
clearFilters={clearFilters}
isUsingFilter={isUsingFilter}
Expand All @@ -653,7 +647,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
agentPoliciesIndexedById={agentPoliciesIndexedById}
renderActions={renderActions}
onSelectionChange={onSelectionChange}
tableRef={tableRef}
selected={selectedAgents}
showUpgradeable={showUpgradeable}
onTableChange={onTableChange}
pagination={pagination}
Expand Down

0 comments on commit fc61638

Please sign in to comment.