Skip to content

Commit

Permalink
chore: Remove items dependency and react based on loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldowseza committed Oct 23, 2024
1 parent 9fcb634 commit fe18d37
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import {
import { renderHook } from '../../../../__tests__/render-hook';
import { mockPerformanceMetrics } from '../../../analytics/__tests__/mocks';

type RenderProps = Partial<UseTableInteractionMetricsProps<any>>;
type RenderProps = Partial<UseTableInteractionMetricsProps>;

const defaultProps = {
getComponentConfiguration: () => ({}),
getComponentIdentifier: () => 'My resources',
items: [],
itemCount: 10,
loading: undefined,
instanceIdentifier: undefined,
Expand Down
16 changes: 5 additions & 11 deletions src/internal/hooks/use-table-interaction-metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,25 @@ to be the cause of the current loading state.
*/
const USER_ACTION_TIME_LIMIT = 1_000;

export interface UseTableInteractionMetricsProps<T> {
export interface UseTableInteractionMetricsProps {
elementRef: React.RefObject<HTMLElement>;
instanceIdentifier: string | undefined;
loading: boolean | undefined;
items: readonly T[];
itemCount: number;
getComponentIdentifier: () => string | undefined;
getComponentConfiguration: () => JSONObject;
interactionMetadata: () => string;
}

export function useTableInteractionMetrics<T>({
export function useTableInteractionMetrics({
elementRef,
itemCount,
instanceIdentifier,
getComponentIdentifier,
getComponentConfiguration,
loading = false,
interactionMetadata,
items,
}: UseTableInteractionMetricsProps<T>) {
}: UseTableInteractionMetricsProps) {
const taskInteractionId = useRandomId();
const tableInteractionAttributes = useDOMAttribute(
elementRef,
Expand Down Expand Up @@ -82,19 +80,15 @@ export function useTableInteractionMetrics<T>({
instanceIdentifier,
noOfResourcesInTable: metadata.current.itemCount,
});
}
}, [instanceIdentifier, loading, taskInteractionId]);

useEffectOnUpdate(() => {
if (!loading) {
ComponentMetrics.componentUpdated({
taskInteractionId,
componentName: 'table',
actionType: (capturedUserAction.current || lastUserAction.current?.name) ?? '',
actionType: capturedUserAction.current ?? '',
componentConfiguration: metadata.current.getComponentConfiguration(),
});
}
}, [items, taskInteractionId, loading]);
}, [instanceIdentifier, loading, taskInteractionId]);

return {
tableInteractionAttributes,
Expand Down
4 changes: 1 addition & 3 deletions src/table/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,10 @@ const InternalTable = React.forwardRef(
};
};

const { setLastUserAction, tableInteractionAttributes } = useTableInteractionMetrics<T>({
const { setLastUserAction, tableInteractionAttributes } = useTableInteractionMetrics({
elementRef: tableRefObject,
loading,
instanceIdentifier: analyticsMetadata?.instanceIdentifier,
items,
itemCount: items.length,
getComponentIdentifier: getHeaderText,
getComponentConfiguration,
Expand Down Expand Up @@ -282,7 +281,6 @@ const InternalTable = React.forwardRef(
onSelectionChange,
ariaLabels,
loading,
setLastUserAction,
});
const isRowSelected = (row: TableRow<T>) => row.type === 'data' && isItemSelected(row.item);

Expand Down
7 changes: 1 addition & 6 deletions src/table/selection/use-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type SelectionOptions<T> = Pick<
| 'selectedItems'
| 'selectionType'
| 'trackBy'
> & { setLastUserAction?: (name: string) => void };
>;

export function useSelection<T>(options: SelectionOptions<T>): {
isItemSelected: (item: T) => boolean;
Expand All @@ -39,7 +39,6 @@ function useSingleSelection<T>({
selectedItems = [],
selectionType,
trackBy,
setLastUserAction,
}: SelectionOptions<T>) {
// The name assigned to all controls to combine them in a single group.
const selectionControlName = useUniqueId();
Expand All @@ -54,7 +53,6 @@ function useSingleSelection<T>({

const handleToggleItem = (item: T) => {
if (!isItemDisabled(item) && !isItemSelected(item)) {
setLastUserAction?.('selection');
fireNonCancelableEvent(onSelectionChange, { selectedItems: [item] });
}
};
Expand Down Expand Up @@ -84,7 +82,6 @@ function useMultiSelection<T>({
selectedItems = [],
selectionType,
trackBy,
setLastUserAction,
}: SelectionOptions<T>) {
// The name assigned to all controls to combine them in a single group.
const selectionControlName = useUniqueId();
Expand Down Expand Up @@ -154,7 +151,6 @@ function useMultiSelection<T>({
const handleToggleAll = () => {
const newSelectedItems = allEnabledItemsSelected ? deselectItems(items) : selectItems(items);
fireNonCancelableEvent(onSelectionChange, { selectedItems: newSelectedItems });
setLastUserAction?.('selection');
};

const handleToggleItem = (item: T) => {
Expand All @@ -163,7 +159,6 @@ function useMultiSelection<T>({
const selectedItems = isItemSelected(item) ? deselectItems(requestedItems) : selectItems(requestedItems);
fireNonCancelableEvent(onSelectionChange, { selectedItems });
setLastClickedItem(item);
setLastUserAction?.('selection');
}
};

Expand Down

0 comments on commit fe18d37

Please sign in to comment.