Skip to content

Commit 53ec41b

Browse files
committed
rename hook
1 parent 57216cc commit 53ec41b

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

web_ui/src/shared/components/media-items-list/media-items-list.component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from 'react-aria-components';
1717

1818
import { VIEW_MODE_SETTINGS, ViewModes } from '../media-view-modes/utils';
19-
import { useScrollToTargetItem } from './use-scroll-to-target-item.hook';
19+
import { useGetTargetPosition } from './use-get-target-position.hook';
2020

2121
import classes from './media-items-list.module.scss';
2222

@@ -69,7 +69,7 @@ export const MediaItemsList = <T extends object>({
6969

7070
const container = ref?.current?.firstElementChild;
7171

72-
useScrollToTargetItem({
72+
useGetTargetPosition({
7373
gap: config.gap,
7474
container,
7575
targetIndex: scrollToIndex,

web_ui/src/shared/components/media-items-list/use-scroll-to-target-item.hook.test.tsx renamed to web_ui/src/shared/components/media-items-list/use-get-target-position.hook.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
import { renderHook } from '@testing-library/react';
55

6-
import { useScrollToTargetItem } from './use-scroll-to-target-item.hook';
6+
import { useGetTargetPosition } from './use-get-target-position.hook';
77

8-
describe('useScrollToTargetItem', () => {
8+
describe('useGetTargetPosition', () => {
99
const mockCallback = jest.fn();
1010

1111
beforeEach(() => {
@@ -20,7 +20,7 @@ describe('useScrollToTargetItem', () => {
2020

2121
it('should not call callback when container is not provided', () => {
2222
renderHook(() =>
23-
useScrollToTargetItem({
23+
useGetTargetPosition({
2424
gap: 10,
2525
container: null,
2626
targetIndex: 5,
@@ -54,7 +54,7 @@ describe('useScrollToTargetItem', () => {
5454
const expectedScrollPos = (childHeight + gap) * targetRow; // 600
5555

5656
renderHook(() =>
57-
useScrollToTargetItem({
57+
useGetTargetPosition({
5858
gap,
5959
container,
6060
targetIndex,
@@ -72,7 +72,7 @@ describe('useScrollToTargetItem', () => {
7272
Object.defineProperty(container, 'clientWidth', { value: 1000 });
7373

7474
renderHook(() =>
75-
useScrollToTargetItem({
75+
useGetTargetPosition({
7676
gap: 10,
7777
container,
7878
targetIndex: 5,
@@ -88,7 +88,7 @@ describe('useScrollToTargetItem', () => {
8888
describe('should not call callback with invalid index', () => {
8989
it.each([undefined, null, -1, 1.5, NaN])('targetIndex: %p', (invalidIndex) => {
9090
renderHook(() =>
91-
useScrollToTargetItem({
91+
useGetTargetPosition({
9292
gap: 10,
9393
container: document.createElement('div'),
9494
targetIndex: invalidIndex as number,

web_ui/src/shared/components/media-items-list/use-scroll-to-target-item.hook.tsx renamed to web_ui/src/shared/components/media-items-list/use-get-target-position.hook.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@ import { DependencyList, useEffect } from 'react';
55

66
import { isNil } from 'lodash-es';
77

8-
interface useScrollToTargetItemProps {
8+
interface useGetTargetPositionProps {
99
gap: number;
10-
dependencies?: DependencyList;
10+
delay?: number;
1111
container?: Element | null;
1212
targetIndex?: number;
13+
dependencies?: DependencyList;
1314
callback: (scrollTo: number) => void;
1415
}
1516

1617
const isValidIndex = (index?: number): index is number => !isNil(index) && Number.isInteger(index) && index >= 0;
1718

18-
export const useScrollToTargetItem = ({
19+
export const useGetTargetPosition = ({
1920
gap,
21+
delay = 500,
2022
container,
2123
targetIndex,
2224
dependencies = [],
2325
callback,
24-
}: useScrollToTargetItemProps) => {
26+
}: useGetTargetPositionProps) => {
2527
useEffect(() => {
2628
const timeoutId = setTimeout(() => {
2729
if (!container || !isValidIndex(targetIndex)) {
@@ -31,14 +33,14 @@ export const useScrollToTargetItem = ({
3133
const containerWidth = container.clientWidth;
3234
const childrenWidth = container.firstElementChild?.clientWidth ?? 1;
3335
const childrenHeight = container.firstElementChild?.clientHeight ?? 1;
34-
const childrenPreRow = Math.floor(containerWidth / childrenWidth);
35-
const targetRow = Math.floor(targetIndex / childrenPreRow);
36+
const childrenPerRow = Math.floor(containerWidth / childrenWidth);
37+
const targetRow = Math.floor(targetIndex / childrenPerRow);
3638
const scrollTo = (childrenHeight + gap) * targetRow;
3739

3840
callback(scrollTo);
3941
// we don't want to scroll immediately
4042
// in case of changed view mode we have to scroll once view is rendered
41-
}, 500);
43+
}, delay);
4244

4345
return () => {
4446
timeoutId && clearTimeout(timeoutId);

0 commit comments

Comments
 (0)