Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

17.4 KB
Raw Blame
import { itemHasAlpha } from '../../../lib/transparent-image'
import { useEffect, useRef, useState } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import { X, Sparkles, UserRoundPlus, FolderInput, ChevronLeft, ChevronRight } from 'lucide-react'
import { useCreateStore, type GalleryItem } from '../../../stores/createStore'
import { runCredits } from '../../../stores/cloudCatalogStore'
import { useCreateExp } from './CreateContext'
import { useComfyMedia } from './useComfyMedia'
import { PromptDetails } from './PromptDetails'
import { cn } from '../ui/cn'
import { LazyModelViewer } from '../../three/LazyModelViewer'
import { MoveToFolderModal, OnBody } from './GalleryFolders'
import { playsAsVideo } from './galleryUrl'
import { itemsInView } from '../../../lib/gallery-tags'

/** How far the mouse wheel zooms a picture in the large view, and the step per notch. */
const MAX_ZOOM = 5
const ZOOM_STEP = 1.2

/**
 * The entry before or after this one, in the order the gallery panel shows
 * them (the open folder, else all). Null at either end, and for an entry the
 * panel does not show.
 */
export function galleryNeighbor<T extends { id: string; folderId?: string; hidden?: boolean; tags?: string[] }>(
  all: readonly T[],
  folderOpen: string | null,
  id: string,
  step: -1 | 1,
  filter: { tag?: string | null; hidden?: boolean } = {},
): T | null {
  const shown = itemsInView(all, { folderId: folderOpen, tag: filter.tag ?? null, hidden: filter.hidden ?? false })
  const at = shown.findIndex((g) => g.id === id)
  if (at < 0) return null
  return shown[at + step] ?? null
}

/**
 * The entry to show once `id` has left the view (moved to another folder, one
 * or several at once): the next one still shown, at the end the one before,
 * null when none is left. `before` is the view while `id` was in it.
 */
export function viewSuccessor(before: readonly string[], now: readonly string[], id: string): string | null {
  const at = before.indexOf(id)
  if (at < 0) return null
  const still = new Set(now)
  return before.slice(at + 1).find((x) => still.has(x)) ?? before.slice(0, at).reverse().find((x) => still.has(x)) ?? null
}

export function Lightbox({ item, onClose, onSaveCharacter, onNavigate, promptOpen = false }: {
  item: GalleryItem | null
  onClose: () => void
  /** The prompt unfolded from the start: asked for by the Prompt word on the
   *  stage. Opened any other way the large view folds it. */
  promptOpen?: boolean
  /** Show another entry of the gallery: the arrows on screen and the arrow
   *  keys step through the entries in the order of the gallery panel. */
  onNavigate?: (item: GalleryItem) => void
  /** Take frames of this video for a character: a training set or a saved character. */
  onSaveCharacter?: (item: GalleryItem) => void
}) {
  const backend = useCreateStore((s) => s.backend)
  const isGenerating = useCreateStore((s) => s.isGenerating)
  const { enhanceVideo, quota } = useCreateExp()
  const { src: mediaUrl, onError: onMediaError } = useComfyMedia(item)
  // D3: "Move to folder" from the large view. Keyed by the entry, so the
  // dialog never comes back up for another one.
  const [movingId, setMovingId] = useState<string | null>(null)
  // Discord (20.10.): step through the gallery with the arrows
  // and zoom a picture with the mouse wheel, as a photo viewer does.
  const all = useCreateStore((s) => s.gallery)
  const folderOpen = useCreateStore((s) => s.galleryFolderOpen)
  // 3.1.0 (D27): the tag filter and the Hidden view are part of the view too.
  const tagOpen = useCreateStore((s) => s.galleryTagOpen)
  const showHidden = useCreateStore((s) => s.galleryShowHidden)
  const filter = { tag: tagOpen, hidden: showHidden }
  const prev = item && onNavigate ? galleryNeighbor(all, folderOpen, item.id, -1, filter) : null
  const next = item && onNavigate ? galleryNeighbor(all, folderOpen, item.id, 1, filter) : null
  // D3 (Discord 01.11.2026): an entry moved out of the open folder is no
  // longer in the view the arrows step through, and they were gone. The view
  // goes on with the next entry of the folder, or closes when none is left.
  // An entry opened while not in the view (from the stage) is left alone.
  const viewKey = itemsInView(all, { folderId: folderOpen, ...filter }).map((g) => g.id).join('\n')
  const lastView = useRef<{ id: string; ids: string[] } | null>(null)
  useEffect(() => {
    if (!item) { lastView.current = null; return }
    const ids = viewKey ? viewKey.split('\n') : []
    if (ids.includes(item.id)) { lastView.current = { id: item.id, ids }; return }
    const before = lastView.current
    lastView.current = null
    if (!before || before.id !== item.id) return
    const to = viewSuccessor(before.ids, ids, item.id)
    const target = to ? useCreateStore.getState().gallery.find((g) => g.id === to) : undefined
    if (target && onNavigate) onNavigate(target)
    else onClose()
  }, [item, viewKey, onNavigate, onClose])
  const [zoom, setZoom] = useState({ id: '', scale: 1, x: 50, y: 50, panX: 0, panY: 0 })
  const scale = item && zoom.id === item.id ? zoom.scale : 1
  const panX = scale > 1 ? zoom.panX : 0
  const panY = scale > 1 ? zoom.panY : 0
  // B47 (3.0.9, Discord 30.10.2026): a zoomed picture is moved
  // with the mouse button held, as in a photo viewer. The drag goes on outside
  // the picture, and letting go there does not close the view.
  const [dragging, setDragging] = useState(false)
  const dragMoved = useRef(false)
  // Discord 01.11.2026 (3.0.8, Windows): zoomed in and back out, the picture
  // stayed shifted with its last pixel column smeared in stripes beside it,
  // although the zoom itself was back at 1 with no pan. WebView2 kept the
  // composited layer of the zoom. Back at the fitted size the picture gets a
  // fresh element (this key), without the opening animation.
  const [layer, setLayer] = useState(0)

  useEffect(() => {
    if (!item) return
    const onKey = (e: KeyboardEvent) => {
      if (e.key === 'Escape') onClose()
      // The folder dialog and a playing video keep their own keys.
      if (movingId || (e.target instanceof HTMLElement && e.target.closest('input, textarea, select, video, [contenteditable="true"]'))) return
      if (e.key === 'ArrowLeft' && prev && onNavigate) { e.preventDefault(); onNavigate(prev) }
      if (e.key === 'ArrowRight' && next && onNavigate) { e.preventDefault(); onNavigate(next) }
    }
    window.addEventListener('keydown', onKey)
    return () => window.removeEventListener('keydown', onKey)
  }, [item, onClose, prev, next, onNavigate, movingId])

  const onWheel = (e: React.WheelEvent<HTMLImageElement>) => {
    if (!item) return
    const box = e.currentTarget.getBoundingClientRect()
    // Zoom toward the point under the pointer, as long as the picture is at
    // its fitted size; once zoomed in, that point stays where it is.
    const x = box.width > 0 ? ((e.clientX - box.left) / box.width) * 100 : 50
    const y = box.height > 0 ? ((e.clientY - box.top) / box.height) * 100 : 50
    const factor = e.deltaY < 0 ? ZOOM_STEP : 1 / ZOOM_STEP
    const nextScale = Math.min(MAX_ZOOM, Math.max(1, scale * factor))
    // Back at the fitted size the picture is centred again.
    const keep = nextScale > 1 ? { panX, panY } : { panX: 0, panY: 0 }
    setZoom({ id: item.id, scale: nextScale, x: scale === 1 ? x : zoom.x, y: scale === 1 ? y : zoom.y, ...keep })
    if (scale > 1 && nextScale === 1) setLayer((n) => n + 1)
  }

  const onPanStart = (e: React.MouseEvent<HTMLImageElement>) => {
    if (!item || scale <= 1 || e.button !== 0) return
    // No ghost image of the browser's own drag.
    e.preventDefault()
    const box = e.currentTarget.getBoundingClientRect()
    // How far the picture may go: its own zoomed size beyond the fitted one,
    // so a part of it always stays in view.
    const limitX = (box.width / scale) * (scale - 1)
    const limitY = (box.height / scale) * (scale - 1)
    const start = { x: e.clientX, y: e.clientY, panX, panY }
    const id = item.id
    const zoomed = { scale, x: zoom.x, y: zoom.y }
    dragMoved.current = false
    setDragging(true)
    const clamp = (v: number, limit: number) => Math.max(-limit, Math.min(limit, v))
    const onMove = (m: MouseEvent) => {
      const dx = m.clientX - start.x
      const dy = m.clientY - start.y
      if (Math.abs(dx) + Math.abs(dy) > 2) dragMoved.current = true
      setZoom({ id, ...zoomed, panX: clamp(start.panX + dx, limitX), panY: clamp(start.panY + dy, limitY) })
    }
    const onUp = () => {
      setDragging(false)
      window.removeEventListener('mousemove', onMove)
      window.removeEventListener('mouseup', onUp)
    }
    window.addEventListener('mousemove', onMove)
    window.addEventListener('mouseup', onUp)
  }

  // Video super-resolution: only for finished CLOUD renders (jobId = the
  // clip lives in the user's render storage, which the enhance op requires).
  const canEnhance = item?.type === 'video' && !!item.jobId && backend === 'cloud' && !isGenerating
  // Gate on the run's cost like the Composer does — Enhance is a paid action
  // too, and without the gate the only feedback was a server 429 after the
  // progress bar had already started. Clip length is unknown client-side, so
  // runCredits mirrors the server's 8 s default.
  const enhanceCredits =
    canEnhance && quota != null
      ? runCredits('video', 'upscale', item.model || '', undefined, quota.costs.video)
      : null
  const enhanceOk = enhanceCredits !== null && quota != null && quota.remaining.credits >= enhanceCredits

  return (
    <AnimatePresence>
      {item && (
        <motion.div
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          exit={{ opacity: 0 }}
          onClick={() => {
            // A drag of the zoomed picture that ended outside it is no click.
            if (dragMoved.current) { dragMoved.current = false; return }
            onClose()
          }}
          // Opaque: a stale layer of a zoom must never let the app show through.
          className="lu-on-dark fixed inset-0 z-[80] bg-black flex flex-col items-center justify-center gap-3 p-8"
        >
          <button onClick={onClose} className="absolute top-4 right-4 w-9 h-9 flex items-center justify-center rounded-lg bg-white/10 text-gray-200 hover:bg-white/20">
            <X size={18} />
          </button>
          {canEnhance && (
            <button
              onClick={(e) => {
                e.stopPropagation()
                if (!enhanceOk) return
                onClose()
                void enhanceVideo(item)
              }}
              disabled={!enhanceOk}
              title={
                enhanceOk
                  ? 'Upscale this clip to 1080p on the cloud fleet'
                  : 'Not enough credits left this billing period for a video enhance'
              }
              className={cn(
                'absolute top-4 right-16 h-9 px-3 flex items-center gap-1.5 rounded-lg text-[0.7rem] font-medium',
                enhanceOk ? 'bg-white/10 text-gray-200 hover:bg-white/20' : 'bg-white/5 text-gray-500 cursor-not-allowed',
              )}
            >
              <Sparkles size={13} /> Enhance
            </button>
          )}
          {onSaveCharacter && item.type === 'video' && !item.unavailable && (
            <button
              onClick={(e) => {
                e.stopPropagation()
                onClose()
                onSaveCharacter(item)
              }}
              title="Take frames of this video for a training set or a saved character"
              className={cn(
                'absolute top-4 h-9 px-3 flex items-center gap-1.5 rounded-lg t-control bg-white/10 text-gray-200 hover:bg-white/20',
                canEnhance ? 'right-[10.5rem]' : 'right-16',
              )}
            >
              <UserRoundPlus size={13} /> Use frames for a character
            </button>
          )}
          <button
            onClick={(e) => { e.stopPropagation(); setMovingId(item.id) }}
            title="Move to folder"
            className="absolute top-4 left-4 h-9 px-3 flex items-center gap-1.5 rounded-lg t-control bg-white/10 text-gray-200 hover:bg-white/20"
          >
            <FolderInput size={13} /> Move to folder
          </button>
          {prev && onNavigate && (
            <button
              onClick={(e) => { e.stopPropagation(); onNavigate(prev) }}
              title="Previous" aria-label="Previous"
              data-testid="lightbox-prev"
              className="absolute left-4 top-1/2 -translate-y-1/2 z-10 w-9 h-9 flex items-center justify-center rounded-lg bg-white/10 text-gray-200 hover:bg-white/20"
            >
              <ChevronLeft size={18} />
            </button>
          )}
          {next && onNavigate && (
            <button
              onClick={(e) => { e.stopPropagation(); onNavigate(next) }}
              title="Next" aria-label="Next"
              data-testid="lightbox-next"
              className="absolute right-4 top-1/2 -translate-y-1/2 z-10 w-9 h-9 flex items-center justify-center rounded-lg bg-white/10 text-gray-200 hover:bg-white/20"
            >
              <ChevronRight size={18} />
            </button>
          )}
          {/* The dialog goes onto the page, out of this dark surface: its
              `lu-on-dark` turned the field text white on the white dialog of
              the light theme (Discord 01.11.2026). The layer keeps it above
              the large view (z-80). A click in it still bubbles through the
              React tree, so it must not reach the backdrop that closes the view. */}
          {movingId === item.id && (
            <div className="contents" onClick={(e) => e.stopPropagation()}>
              <OnBody>
                <div className="fixed inset-0 z-[90]">
                  <MoveToFolderModal ids={[item.id]} onClose={() => setMovingId(null)} />
                </div>
              </OnBody>
            </div>
          )}
          {item.type === '3d' ? (
            // The model takes the whole room; its own bar carries the
            // download menu and the looks.
            <div className="min-h-0 flex-1 w-full flex items-center justify-center overflow-hidden" onClick={(e) => e.stopPropagation()} data-testid="lightbox-3d">
              <div className="w-full h-full max-w-[1400px]">
                <LazyModelViewer key={item.id} item={item} />
              </div>
            </div>
          ) : item.type === 'audio' ? (
            <motion.div
              initial={{ scale: 0.95 }}
              animate={{ scale: 1 }}
              exit={{ scale: 0.95 }}
              onClick={(e) => e.stopPropagation()}
              className="w-[480px] max-w-[90vw] flex flex-col items-center gap-4 p-8 rounded-lg bg-white/[0.04] border border-white/[0.08]"
            >
              <audio src={mediaUrl} controls autoPlay onError={onMediaError} className="w-full" />
            </motion.div>
          ) : (
            // The media takes the room the prompt leaves: the box below never
            // lies on the picture.
            <div className="min-h-0 flex-1 w-full flex items-center justify-center overflow-hidden">
              {playsAsVideo(item) ? (
                <motion.video
                  initial={{ scale: 0.95 }}
                  animate={{ scale: 1 }}
                  exit={{ scale: 0.95 }}
                  src={mediaUrl}
                  controls
                  autoPlay
                  loop
                  onError={onMediaError}
                  onClick={(e) => e.stopPropagation()}
                  className="max-w-full max-h-full object-contain rounded-lg"
                />
              ) : (
                <motion.img
                  key={layer}
                  initial={layer === 0 ? { scale: 0.95 } : false}
                  // The wheel zoom rides on the same scale the opening
                  // animation uses: a style scale next to it would be
                  // animated back to 1.
                  animate={{ scale, x: panX, y: panY }}
                  // While the mouse moves the picture it follows at once.
                  transition={dragging ? { duration: 0 } : undefined}
                  exit={{ scale: 0.95 }}
                  src={mediaUrl}
                  alt={item.prompt}
                  draggable={false}
                  onError={onMediaError}
                  onClick={(e) => { e.stopPropagation(); dragMoved.current = false }}
                  onWheel={onWheel}
                  onMouseDown={onPanStart}
                  onDoubleClick={(e) => {
                    e.stopPropagation()
                    if (scale > 1) setLayer((n) => n + 1)
                    setZoom({ id: item.id, scale: 1, x: 50, y: 50, panX: 0, panY: 0 })
                  }}
                  data-testid="lightbox-image"
                  data-zoom={scale}
                  data-pan={`${Math.round(panX)},${Math.round(panY)}`}
                  style={{ originX: (scale > 1 ? zoom.x : 50) / 100, originY: (scale > 1 ? zoom.y : 50) / 100 }}
                  className={cn(
                    'max-w-full max-h-full object-contain rounded-lg',
                    scale > 1 && (dragging ? 'cursor-grabbing' : 'cursor-grab'),
                    itemHasAlpha(item) && 'lu-checker',
                  )}
                />
              )}
            </div>
          )}
          {/* Folded, as under the stage of Create (Discord, 20.10.): the
              picture is what the large view is opened for. */}
          <PromptDetails item={item} open={promptOpen} className="shrink-0 max-h-[40vh] overflow-y-auto scrollbar-thin bg-black/70" />
        </motion.div>
      )}
    </AnimatePresence>
  )
}