Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

1.6 KB
Raw Blame
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
import { clamp } from 'es-toolkit/compat';
import { selectLastSelectedItem } from 'features/gallery/store/gallerySelectors';
import { imageSelected } from 'features/gallery/store/gallerySlice';
import { useCallback, useMemo } from 'react';

import { useGalleryImageNames } from './use-gallery-image-names';

export const useNextPrevItemNavigation = () => {
  const dispatch = useAppDispatch();
  const lastSelectedItem = useAppSelector(selectLastSelectedItem);
  const { imageNames, isFetching } = useGalleryImageNames();

  const currentIndex = useMemo(
    () => (lastSelectedItem ? imageNames.findIndex((n) => n === lastSelectedItem) : -1),
    [imageNames, lastSelectedItem]
  );
  const isOnFirstItem = currentIndex === 0;
  const isOnLastItem = currentIndex >= 0 && currentIndex === imageNames.length - 1;

  const navigateBy = useCallback(
    (delta: number) => {
      const maxIndex = imageNames.length - 1;
      if (maxIndex < 0) {
        return;
      }

      const targetIndex = currentIndex >= 0 ? clamp(currentIndex + delta, 0, maxIndex) : 0;
      const imageName = imageNames[targetIndex];
      if (!imageName) {
        return;
      }
      dispatch(imageSelected(imageName));
    },
    [currentIndex, dispatch, imageNames]
  );

  const goToPreviousImage = useCallback(() => {
    navigateBy(-1);
  }, [navigateBy]);

  const goToNextImage = useCallback(() => {
    navigateBy(1);
  }, [navigateBy]);

  return { goToPreviousImage, goToNextImage, isOnFirstItem, isOnLastItem, isFetching };
};