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.

21.1 KB
Raw Blame
import { combine } from '@atlaskit/pragmatic-drag-and-drop/combine';
import { draggable } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
import { Box, Button, Flex, IconButton } from '@invoke-ai/ui-library';
import { useStore } from '@nanostores/react';
import { useAppSelector, useAppStore } from 'app/store/storeHooks';
import { useClipboard } from 'common/hooks/useClipboard';
import { useDownloadItem } from 'common/hooks/useDownloadImage';
import { openMediaInNewTab } from 'common/util/openMediaInNewTab';
import { isMediaCookieSelfHealPending } from 'features/auth/hooks/useMediaCookieRefresh';
import { useMediaUrl } from 'features/auth/store/mediaCookieRefresh';
import { useDeleteVideoModalApi } from 'features/deleteVideoModal/store/state';
import { multipleVideoDndSource, singleVideoDndSource } from 'features/dnd/dnd';
import { dndInputFix } from 'features/dnd/util';
import VideoMetadataViewer from 'features/gallery/components/ImageMetadataViewer/VideoMetadataViewer';
import NextPrevItemButtons from 'features/gallery/components/NextPrevItemButtons';
import { useNextPrevItemNavigation } from 'features/gallery/components/useNextPrevItemNavigation';
import { selectSelectedBoardId, selectSelection } from 'features/gallery/store/gallerySelectors';
import { isVideoName } from 'features/gallery/store/types';
import { useRegisteredHotkeys } from 'features/system/components/HotkeysModal/useHotkeyData';
import { toast } from 'features/toast/toast';
import { navigationApi } from 'features/ui/layouts/navigation-api';
import {
  selectActiveTab,
  selectShouldShowItemDetails,
  selectShouldShowProgressInViewer,
} from 'features/ui/store/uiSelectors';
import type { AnimationProps } from 'framer-motion';
import { AnimatePresence, motion } from 'framer-motion';
import { memo, useCallback, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { PiArrowSquareOutBold, PiCopyBold, PiDownloadSimpleBold, PiTrashSimpleBold, PiXBold } from 'react-icons/pi';
import type { VideoDTO } from 'services/api/types';

import { useImageViewerContext } from './context';
import { NoContentForViewer } from './NoContentForViewer';
import { ProgressImage } from './ProgressImage2';
import { ProgressImageTiles } from './ProgressImageTiles';
import { ProgressIndicator } from './ProgressIndicator2';
import { usePaintedItemName, useSelectedItemReveal } from './useSelectedItemReveal';
import { VideoPlayButtonOverlay } from './VideoPlayButtonOverlay';

type Props = {
  videoDTO: VideoDTO | null;
};

/**
 * Counterpart to CurrentImagePreview for videos. A single <video> element spans both states:
 *
 *  - **idle**: muted, no controls. Without a `poster` attribute the browser decodes and
 *    displays the video's actual first frame at full resolution (much sharper than the
 *    small WebP gallery thumbnail upscaled to fit the viewer). A centered play button
 *    overlay sits on top.
 *  - **playing**: native HTML5 controls + audio. The element is the same DOM node, so the
 *    decoded buffer carries over — no reload when the user hits play.
 *
 * Changing the selected video swaps the element via `key={videoName}`, which discards the
 * old playback state cleanly.
 *
 * Mirrors CurrentImagePreview's progress overlay so denoise previews from a new render
 * appear on top of the previously-loaded video. Without this, a freshly generated render's
 * progress images had nowhere to display whenever a video was the last-selected gallery
 * item (and the user only saw the static first-frame still until the new video finished).
 * Also mirrors its temporary reveal: clicking a gallery thumbnail mid-render lifts the
 * overlay briefly so the click visibly lands, then the live preview returns.
 */
export const CurrentVideoPreview = memo(({ videoDTO }: Props) => {
  const videoUrl = useMediaUrl(videoDTO?.video_url);
  const { t } = useTranslation();
  const store = useAppStore();
  const videoName = videoDTO?.video_name ?? null;
  const videoRef = useRef<HTMLVideoElement | null>(null);
  const [isPlaying, setIsPlaying] = useState(false);
  const { isMediaReady, onPainted } = usePaintedItemName(videoName);
  const shouldShowProgressInViewer = useAppSelector(selectShouldShowProgressInViewer);
  const shouldShowItemDetails = useAppSelector(selectShouldShowItemDetails);
  const activeTab = useAppSelector(selectActiveTab);
  const deleteVideoModal = useDeleteVideoModalApi();
  const { downloadItem } = useDownloadItem();
  const clipboard = useClipboard();
  const {
    $progressEvent,
    $progressImage,
    $activeProgressData,
    $isProgressImageResolving,
    $isTemporarilyShowingSelectedImage,
    revealMachine,
    onLoadImage,
  } = useImageViewerContext();
  const progressEvent = useStore($progressEvent);
  const progressImage = useStore($progressImage);
  const activeProgressData = useStore($activeProgressData);
  const isProgressImageResolving = useStore($isProgressImageResolving);
  const isTemporarilyShowingSelectedImage = useStore($isTemporarilyShowingSelectedImage);
  const hasProgressImage = progressImage !== null;
  // `!isPlaying`: a reveal exposes the play button, and an explicit play is a stronger signal than
  // the click that triggered the reveal — never re-cover an actively-playing video with the opaque
  // overlay (its audio would keep running underneath, with the controls unreachable). The overlay
  // returns when playback ends — whether the user closes the player or the video runs out.
  const withProgress =
    shouldShowProgressInViewer && hasProgressImage && !isTemporarilyShowingSelectedImage && !isPlaying;
  // When more than one session is generating concurrently (multi-GPU), tile their previews instead
  // of letting the sessions overwrite each other's full-size preview. Mirrors CurrentImagePreview.
  const withTiledProgress = withProgress && activeProgressData.length > 1;
  const { goToPreviousImage, goToNextImage, isFetching } = useNextPrevItemNavigation();
  // One controller per mounted preview component; the previous-item ref inside it is the shared
  // one from the viewer context, so image <-> video clicks read as selection changes on both ends.

  // Whenever the selected video changes, drop back to the idle still + play overlay.
  useEffect(() => {
    setIsPlaying(false);
  }, [videoName]);

  // Mid-generation gallery clicks: mirror CurrentImagePreview's temporary reveal. Without this,
  // the opaque progress overlay swallows every video-thumbnail click for the whole render — the
  // selection changes underneath, but nothing visibly happens. The sequencing lives in the
  // controller (selectedItemReveal.ts); the effect wiring around it lives in the hook, where it is
  // mounted and tested with real lifecycles. preload="metadata" plus the near-zero seek does not
  // prove a frame exists, so readiness comes from usePaintedItemName fed by onLoadedData.
  useSelectedItemReveal({
    revealMachine,
    renderedItemName: videoName,
    isMediaReady: isMediaReady,
    shouldShowProgressInViewer,
    hasProgressImage,
    isProgressImageResolving,
  });

  // Register the viewer's <video> as a drag source so users can drag the currently-displayed
  // video onto node fields (e.g. a Video Primitive's "Starting Video" input) directly from
  // the viewer, just like they can from the gallery thumbnail. Mirrors GalleryVideoItem's
  // setup. Without this, the bare <video> element has no drag handler and the drop target
  // sees nothing it can accept.
  useEffect(() => {
    const element = videoRef.current;
    if (!element || !videoDTO) {
      return;
    }
    return combine(
      dndInputFix(element),
      draggable({
        element,
        getInitialData: () => {
          // Honor any active gallery multi-selection so dropping onto a board moves the whole
          // batch, matching the gallery thumbnail's behavior.
          const state = store.getState();
          const selection = selectSelection(state);
          const boardId = selectSelectedBoardId(state);
          if (selection.length > 1 && selection.includes(videoDTO.video_name)) {
            const video_names = selection.filter(isVideoName);
            const image_names = selection.filter((n) => !isVideoName(n));
            return multipleVideoDndSource.getData({
              video_names,
              image_names,
              board_id: boardId,
            });
          }
          return singleVideoDndSource.getData({ videoDTO }, videoDTO.video_name);
        },
      })
    );
  }, [videoDTO, store]);

  // Drop back to the idle still and tell the user, without touching the progress overlay. Used on
  // its own for a rejected play(), where the media itself is fine.
  const reportPlaybackFailure = useCallback(() => {
    setIsPlaying(false);
    // A restored session's <video> request can 401 before the media-cookie self-heal
    // completes; the URL version bumps and the element reloads once the cookie lands.
    // Surfacing an error toast for that transient window would be a false alarm.
    if (isMediaCookieSelfHealPending()) {
      return false;
    }
    toast({
      id: 'VIDEO_PLAYBACK_FAILED',
      status: 'error',
      title: t('toast.videoPlaybackFailed'),
      description: t('toast.videoPlaybackFailedDesc'),
    });
    return true;
  }, [t]);

  const handleVideoError = useCallback(() => {
    // The self-heal case reloads the element, which then fires onLoadedMetadata and ends any
    // pending resolve on its own — so only a real element error takes the shortcut below.
    if (!reportPlaybackFailure()) {
      return;
    }
    // A genuinely errored element will never fire onLoadedMetadata, which is what normally ends
    // this session's post-render "resolve" illusion — end it here instead of letting the overlay
    // sit over the viewer for the whole resolve timeout. Attributed to this video's session, so
    // the lifecycle ignores it if some other session's illusion is the one pending, and it is a
    // no-op when none is.
    onLoadImage(videoDTO?.session_id ?? null);
  }, [onLoadImage, reportPlaybackFailure, videoDTO?.session_id]);

  const handlePlay = useCallback(() => {
    setIsPlaying(true);
    // The ref points at the same element we'll re-render with controls/audio; calling
    // play() here keeps the user gesture wired to playback without waiting for React.
    // A rejected play() (blocked autoplay, codec error, missing media cookie) must roll
    // the state back — otherwise the overlay stays hidden over a dead element and the
    // rejection surfaces as an unhandled promise. An AbortError is benign — pause(),
    // close, or navigating away interrupted a pending play() — so roll back silently.
    videoRef.current?.play().catch((error: unknown) => {
      if (error instanceof DOMException && error.name === 'AbortError') {
        setIsPlaying(false);
        return;
      }
      // Not a load failure: onLoadedMetadata has usually already fired, and the element is intact.
      // Routing this through the error path would end whichever session's resolve illusion happens
      // to be pending, for a user gesture unrelated to any render.
      reportPlaybackFailure();
    });
  }, [reportPlaybackFailure]);

  // Close: stop playback and drop back to the first-frame preview + play overlay. We
  // explicitly pause() because toggling React's `controls` prop hides the chrome but does
  // not stop playback. Seeking back to ~0 nudges the decoder to re-paint the first frame
  // (mirroring handleLoadedMetadata's near-zero seek trick).
  const handleClose = useCallback(() => {
    const el = videoRef.current;
    if (el) {
      el.pause();
      try {
        el.currentTime = 0.0001;
      } catch {
        // Some browsers throw if metadata isn't fully ready yet; harmless.
      }
    }
    setIsPlaying(false);
  }, []);

  const handleDelete = useCallback(async () => {
    if (!videoDTO) {
      return;
    }
    try {
      await deleteVideoModal.delete([videoDTO.video_name]);
    } catch {
      // user canceled the confirmation dialog
    }
  }, [deleteVideoModal, videoDTO]);

  const handleDownload = useCallback(() => {
    if (!videoDTO) {
      return;
    }
    void downloadItem(videoDTO.video_url, videoDTO.video_name);
  }, [downloadItem, videoDTO]);

  const handleOpenInNewTab = useCallback(() => {
    if (!videoDTO) {
      return;
    }
    openMediaInNewTab(videoDTO.video_url);
  }, [videoDTO]);

  // Cross-browser clipboard support for raw `video/*` MIME types doesn't really exist — Chrome
  // and Firefox both reject anything outside a small allow-list (image/png, image/jpeg, text).
  // So instead we grab the currently-displayed frame off the <video> element via a canvas and
  // hand the resulting PNG to the standard image-clipboard path. The video is same-origin so
  // the canvas doesn't taint.
  const handleCopyFrame = useCallback(async () => {
    const el = videoRef.current;
    if (!el || !el.videoWidth || !el.videoHeight) {
      return;
    }
    try {
      const canvas = document.createElement('canvas');
      canvas.width = el.videoWidth;
      canvas.height = el.videoHeight;
      const ctx = canvas.getContext('2d');
      if (!ctx) {
        throw new Error('Unable to acquire 2D canvas context');
      }
      ctx.drawImage(el, 0, 0);
      const blob = await new Promise<Blob | null>((resolve) => {
        canvas.toBlob((b) => resolve(b), 'image/png');
      });
      if (!blob) {
        throw new Error('Unable to encode frame as PNG');
      }
      clipboard.writeImage(blob, () => {
        toast({
          id: 'IMAGE_COPIED',
          title: t('toast.imageCopied'),
          status: 'success',
        });
      });
    } catch (err) {
      toast({
        id: 'PROBLEM_COPYING_IMAGE',
        title: t('toast.problemCopyingImage'),
        description: String(err),
        status: 'error',
      });
    }
  }, [clipboard, t]);

  // Mirror CurrentImagePreview's hover-driven next/prev gating so the arrows only intrude
  // while the user is interacting with the viewer.
  const [shouldShowNextPrevButtons, setShouldShowNextPrevButtons] = useState<boolean>(false);
  const timeoutId = useRef(0);
  const onMouseOver = useCallback(() => {
    setShouldShowNextPrevButtons(true);
    window.clearTimeout(timeoutId.current);
  }, []);
  const onMouseOut = useCallback(() => {
    timeoutId.current = window.setTimeout(() => {
      setShouldShowNextPrevButtons(false);
    }, 500);
  }, []);

  const handleViewerArrowNavigation = useCallback(
    (event: KeyboardEvent, navigate: () => void) => {
      if (!navigationApi.isViewerArrowNavigationMode(activeTab) || !videoDTO || isFetching) {
        return;
      }
      if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement) {
        return;
      }
      event.preventDefault();
      navigate();
    },
    [activeTab, videoDTO, isFetching]
  );

  const onHotkeyPrevImage = useCallback(
    (event: KeyboardEvent) => {
      handleViewerArrowNavigation(event, goToPreviousImage);
    },
    [goToPreviousImage, handleViewerArrowNavigation]
  );

  const onHotkeyNextImage = useCallback(
    (event: KeyboardEvent) => {
      handleViewerArrowNavigation(event, goToNextImage);
    },
    [goToNextImage, handleViewerArrowNavigation]
  );

  useRegisteredHotkeys({
    id: 'galleryNavLeft',
    category: 'gallery',
    callback: onHotkeyPrevImage,
    options: { preventDefault: true },
    dependencies: [onHotkeyPrevImage],
  });

  useRegisteredHotkeys({
    id: 'galleryNavRight',
    category: 'gallery',
    callback: onHotkeyNextImage,
    options: { preventDefault: true },
    dependencies: [onHotkeyNextImage],
  });

  // Analogous to <DndImage onLoad={onLoadImage}> in the image viewer: clear any stale
  // denoise progress overlay once the new video's metadata is in. Without this, the
  // ImageViewerContext atom stays set after a video render (there's no image load to
  // trigger its clear), so the overlay sticks over the freshly-selected video forever.
  //
  // Also force a first-frame paint via a near-zero seek. With preload="metadata" some
  // browsers populate dimensions/duration but don't actually decode and display the first
  // video frame until playback or a seek — the element just shows its black background.
  // Setting currentTime to 0.0001 nudges the decoder to paint without measurably advancing.
  const handleLoadedMetadata = useCallback(() => {
    onLoadImage(videoDTO?.session_id ?? null);
    const el = videoRef.current;
    if (el && !isPlaying && el.currentTime === 0) {
      try {
        el.currentTime = 0.0001;
      } catch {
        // Some browsers throw if metadata isn't fully ready yet; harmless.
      }
    }
  }, [isPlaying, onLoadImage, videoDTO?.session_id]);

  if (!videoDTO) {
    return <NoContentForViewer />;
  }

  return (
    <Flex
      onMouseOver={onMouseOver}
      onMouseOut={onMouseOut}
      width="full"
      height="full"
      alignItems="center"
      justifyContent="center"
      position="relative"
    >
      <video
        key={videoName ?? undefined}
        ref={videoRef}
        // Resolves to /api/v1/videos/i/{name}/full, which supports HTTP Range — used both
        // for first-frame decode (preload=metadata) and for scrub during playback.
        src={videoUrl}
        preload="metadata"
        muted={!isPlaying}
        playsInline
        controls={isPlaying}
        onLoadedMetadata={handleLoadedMetadata}
        onLoadedData={onPainted}
        onEnded={handleClose}
        onError={handleVideoError}
        style={{
          maxWidth: '100%',
          maxHeight: '100%',
          borderRadius: 4,
          outline: 'none',
          background: 'black',
        }}
      />
      {!isPlaying && !withProgress && <VideoPlayButtonOverlay onClick={handlePlay} />}
      {/* Gated on the states themselves, not on !withProgress: playing and revealing both turn
          withProgress off, and the full-screen metadata panel would land exactly on top of the
          playback controls / the just-revealed video, swallowing the interaction they exist for. */}
      {shouldShowItemDetails && !isPlaying && !isTemporarilyShowingSelectedImage && !withProgress && (
        <Box position="absolute" opacity={0.8} top={0} width="full" height="full" borderRadius="base">
          <VideoMetadataViewer video={videoDTO} />
        </Box>
      )}
      {withProgress && (
        <Flex w="full" h="full" position="absolute" alignItems="center" justifyContent="center" bg="base.900">
          {withTiledProgress ? (
            <ProgressImageTiles data={activeProgressData} />
          ) : (
            <>
              <ProgressImage progressImage={progressImage} />
              {progressEvent && (
                <ProgressIndicator progressEvent={progressEvent} position="absolute" top={6} right={6} size={8} />
              )}
            </>
          )}
        </Flex>
      )}
      {/* Top action bar, right-aligned. Auto-sized Flex anchored to insetInlineEnd leaves the
          rest of the viewer click-through so clicking the video still pauses native playback.
          Order: open in new tab, copy frame, download, delete, then the labelled close button
          farthest right (only while the player is active). */}
      <Flex position="absolute" top={2} insetInlineEnd={2} zIndex={2} gap={1}>
        <IconButton
          aria-label={t('common.openInNewTab')}
          tooltip={t('common.openInNewTab')}
          icon={<PiArrowSquareOutBold />}
          onClick={handleOpenInNewTab}
          variant="solid"
          size="sm"
        />
        <IconButton
          aria-label={t('gallery.copyVideoFrame')}
          tooltip={t('gallery.copyVideoFrame')}
          icon={<PiCopyBold />}
          onClick={handleCopyFrame}
          variant="solid"
          size="sm"
        />
        <IconButton
          aria-label={t('gallery.download')}
          tooltip={t('gallery.download')}
          icon={<PiDownloadSimpleBold />}
          onClick={handleDownload}
          variant="solid"
          size="sm"
        />
        <IconButton
          aria-label={t('gallery.deleteVideo', { count: 1 })}
          tooltip={t('gallery.deleteVideo', { count: 1 })}
          icon={<PiTrashSimpleBold />}
          onClick={handleDelete}
          colorScheme="error"
          variant="solid"
          size="sm"
        />
        {isPlaying && (
          <Button leftIcon={<PiXBold />} onClick={handleClose} variant="solid" size="sm">
            {t('gallery.closeVideoPlayer')}
          </Button>
        )}
      </Flex>
      <AnimatePresence>
        {shouldShowNextPrevButtons && (
          <Box
            as={motion.div}
            key="nextPrevButtons"
            initial={initial}
            animate={animateArrows}
            exit={exit}
            position="absolute"
            top={0}
            right={0}
            bottom={0}
            left={0}
            pointerEvents="none"
          >
            <NextPrevItemButtons />
          </Box>
        )}
      </AnimatePresence>
    </Flex>
  );
});

const initial: AnimationProps['initial'] = {
  opacity: 0,
};
const animateArrows: AnimationProps['animate'] = {
  opacity: 1,
  transition: { duration: 0.07 },
};
const exit: AnimationProps['exit'] = {
  opacity: 0,
  transition: { duration: 0.07 },
};

CurrentVideoPreview.displayName = 'CurrentVideoPreview';