Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

22.1 KB
Raw Blame
import { ChevronLeft, ChevronRight, InfoIcon, RotateCcw, X, ZoomIn, ZoomOut } from "lucide-react";
import React, { useEffect, useMemo, useRef, useState } from "react";
import MediaMetadataDetails from "@/components/MediaMetadataDetails";
import MotionPhotoPreview from "@/components/MotionPhotoPreview";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogTitle } from "@/components/ui/dialog";
import { VisuallyHidden } from "@/components/ui/visually-hidden";
import useMediaQuery from "@/hooks/useMediaQuery";
import { cn } from "@/lib/utils";
import { useTranslate } from "@/utils/i18n";
import type { PreviewMediaItem } from "@/utils/media-item";

interface Props {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  imgUrls?: string[];
  items?: PreviewMediaItem[];
  initialIndex?: number;
}

const MIN_ZOOM = 1;
const MAX_ZOOM = 4;
const ZOOM_STEP = 0.2;
const DOUBLE_TAP_ZOOM = 2;
const SWIPE_THRESHOLD = 60;
const SWIPE_DIRECTION_RATIO = 1.5;

const clampZoom = (scale: number) => Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, scale));

interface PanOffset {
  x: number;
  y: number;
}

const NO_PAN: PanOffset = { x: 0, y: 0 };

const clampPan = (offset: number, max: number) => Math.min(max, Math.max(-max, offset));

// The image is centered in the surface's content box, so each axis clamps to half of its overflow.
const clampPanOffset = (offset: PanOffset, scale: number, image: HTMLImageElement | null, surface: HTMLDivElement | null): PanOffset => {
  if (!image || !surface) {
    return offset;
  }
  const style = window.getComputedStyle(surface);
  const contentWidth = surface.clientWidth - (Number.parseFloat(style.paddingLeft) || 0) - (Number.parseFloat(style.paddingRight) || 0);
  const contentHeight = surface.clientHeight - (Number.parseFloat(style.paddingTop) || 0) - (Number.parseFloat(style.paddingBottom) || 0);
  const maxX = Math.max(0, (image.offsetWidth * scale - contentWidth) / 2);
  const maxY = Math.max(0, (image.offsetHeight * scale - contentHeight) / 2);
  return { x: clampPan(offset.x, maxX), y: clampPan(offset.y, maxY) };
};

interface PinchGesture {
  startCenterX: number;
  startCenterY: number;
  startDistance: number;
  startMidpointX: number;
  startMidpointY: number;
  startScale: number;
  startPan: PanOffset;
}

interface SwipeGesture {
  pointerId: number;
  startX: number;
  startY: number;
}

const distanceBetween = (first: { x: number; y: number }, second: { x: number; y: number }) =>
  Math.hypot(first.x - second.x, first.y - second.y);

function PreviewImageDialog({ open, onOpenChange, imgUrls = [], items, initialIndex = 0 }: Props) {
  const t = useTranslate();
  const sm = useMediaQuery("sm");
  const [currentIndex, setCurrentIndex] = useState(initialIndex);
  const [zoomScale, setZoomScale] = useState(MIN_ZOOM);
  const [showDetails, setShowDetails] = useState(false);
  const [panOffset, setPanOffset] = useState<PanOffset>(NO_PAN);
  const [swipeOffset, setSwipeOffset] = useState(0);
  const [isInteracting, setIsInteracting] = useState(false);
  const imageRef = useRef<HTMLImageElement>(null);
  const surfaceRef = useRef<HTMLDivElement>(null);
  const pointersRef = useRef(new Map<number, { x: number; y: number }>());
  const panDragRef = useRef<{ pointerId: number; startX: number; startY: number; originX: number; originY: number } | null>(null);
  const pinchRef = useRef<PinchGesture | null>(null);
  const swipeRef = useRef<SwipeGesture | null>(null);
  const previewItems = useMemo(
    () => items ?? imgUrls.map((url) => ({ id: url, kind: "image" as const, sourceUrl: url, posterUrl: url, filename: "Image" })),
    [imgUrls, items],
  );

  useEffect(() => {
    if (open) {
      setCurrentIndex(initialIndex);
      setShowDetails(false);
    }
  }, [initialIndex, open]);

  const itemCount = previewItems.length;
  const safeIndex = Math.max(0, Math.min(currentIndex, itemCount - 1));
  const currentItem = previewItems[safeIndex];
  const hasMultiple = itemCount > 1;
  const isImagePreview = currentItem?.kind === "image";
  const canGoPrevious = safeIndex > 0;
  const canGoNext = safeIndex < itemCount - 1;
  const zoomPercent = Math.round(zoomScale * 100);
  const isZoomed = zoomScale > MIN_ZOOM;

  useEffect(() => {
    const drag = panDragRef.current;
    if (drag && imageRef.current?.hasPointerCapture(drag.pointerId)) {
      imageRef.current.releasePointerCapture(drag.pointerId);
    }
    panDragRef.current = null;
    pointersRef.current.clear();
    pinchRef.current = null;
    swipeRef.current = null;
    setZoomScale(MIN_ZOOM);
    setPanOffset(NO_PAN);
    setSwipeOffset(0);
    setIsInteracting(false);
  }, [currentItem?.id, open]);

  useEffect(() => {
    if (zoomScale === MIN_ZOOM) {
      panDragRef.current = null;
      if (!pinchRef.current) {
        setIsInteracting(false);
      }
    }
    setPanOffset((current) => {
      if (zoomScale === MIN_ZOOM) {
        return NO_PAN;
      }
      return clampPanOffset(current, zoomScale, imageRef.current, surfaceRef.current);
    });
  }, [zoomScale]);

  const handleClose = () => onOpenChange(false);
  const handlePrevious = () => {
    setCurrentIndex((prev) => Math.max(prev - 1, 0));
  };
  const handleNext = () => {
    setCurrentIndex((prev) => Math.min(prev + 1, itemCount - 1));
  };
  // The dialog popup stops arrow keys from reaching the document, so navigation listens on the popup itself.
  const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
    if (event.altKey || event.ctrlKey || event.metaKey) {
      return;
    }
    if (event.key === "ArrowLeft") {
      event.preventDefault();
      handlePrevious();
    } else if (event.key === "ArrowRight") {
      event.preventDefault();
      handleNext();
    }
  };

  const updateZoom = (nextScale: number) => {
    setZoomScale(clampZoom(nextScale));
  };
  const resetZoom = () => setZoomScale(MIN_ZOOM);
  const handleZoomIn = () => updateZoom(zoomScale + ZOOM_STEP);
  const handleZoomOut = () => updateZoom(zoomScale - ZOOM_STEP);
  const handleWheel = (event: React.WheelEvent<HTMLDivElement>) => {
    if (isImagePreview) {
      event.preventDefault();
      updateZoom(zoomScale + (event.deltaY < 0 ? ZOOM_STEP : -ZOOM_STEP));
    }
  };
  const handleDoubleClick = () => setZoomScale((scale) => (scale === MIN_ZOOM ? DOUBLE_TAP_ZOOM : MIN_ZOOM));

  const startPan = (pointerId: number, point: { x: number; y: number }) => {
    panDragRef.current = { pointerId, startX: point.x, startY: point.y, originX: panOffset.x, originY: panOffset.y };
    setIsInteracting(true);
  };

  const startPinch = () => {
    const [first, second] = [...pointersRef.current.values()];
    // The untransformed wrapper shares the image's layout center, which the pan offset is measured from.
    const rect = imageRef.current?.parentElement?.getBoundingClientRect();
    const startCenterX = rect ? rect.left + rect.width / 2 : 0;
    const startCenterY = rect ? rect.top + rect.height / 2 : 0;
    pinchRef.current = {
      startCenterX,
      startCenterY,
      startDistance: distanceBetween(first, second),
      startMidpointX: (first.x + second.x) / 2 - startCenterX,
      startMidpointY: (first.y + second.y) / 2 - startCenterY,
      startScale: zoomScale,
      startPan: panOffset,
    };
    panDragRef.current = null;
    swipeRef.current = null;
    setSwipeOffset(0);
    setIsInteracting(true);
  };

  const updatePinch = () => {
    const pinch = pinchRef.current;
    const [first, second] = [...pointersRef.current.values()];
    if (!pinch || !first || !second) {
      return;
    }
    const nextScale = clampZoom(pinch.startScale * (distanceBetween(first, second) / pinch.startDistance));
    if (nextScale === MIN_ZOOM) {
      setZoomScale(MIN_ZOOM);
      setPanOffset(NO_PAN);
      return;
    }
    // Anchor the image point under the moving midpoint as the fingers scale it.
    const ratio = nextScale / pinch.startScale;
    const midpointX = (first.x + second.x) / 2 - pinch.startCenterX;
    const midpointY = (first.y + second.y) / 2 - pinch.startCenterY;
    const next = {
      x: midpointX - (pinch.startMidpointX - pinch.startPan.x) * ratio,
      y: midpointY - (pinch.startMidpointY - pinch.startPan.y) * ratio,
    };
    setPanOffset(clampPanOffset(next, nextScale, imageRef.current, surfaceRef.current));
    setZoomScale(nextScale);
  };

  const handlePointerDown = (event: React.PointerEvent<HTMLImageElement>) => {
    if (event.button !== 0) {
      return;
    }
    event.preventDefault();
    event.currentTarget.setPointerCapture(event.pointerId);
    pointersRef.current.set(event.pointerId, { x: event.clientX, y: event.clientY });
    if (pointersRef.current.size >= 2) {
      startPinch();
    } else if (isZoomed) {
      startPan(event.pointerId, { x: event.clientX, y: event.clientY });
    } else if (hasMultiple) {
      swipeRef.current = { pointerId: event.pointerId, startX: event.clientX, startY: event.clientY };
    }
  };

  const handlePointerMove = (event: React.PointerEvent<HTMLImageElement>) => {
    const pointers = pointersRef.current;
    if (!pointers.has(event.pointerId)) {
      return;
    }
    pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
    if (pointers.size >= 2 && pinchRef.current) {
      updatePinch();
      return;
    }
    const swipe = swipeRef.current;
    if (swipe && swipe.pointerId === event.pointerId) {
      setSwipeOffset(event.clientX - swipe.startX);
      setIsInteracting(true);
      return;
    }
    const drag = panDragRef.current;
    if (!drag || drag.pointerId !== event.pointerId) {
      return;
    }
    const next = { x: drag.originX + event.clientX - drag.startX, y: drag.originY + event.clientY - drag.startY };
    setPanOffset(clampPanOffset(next, zoomScale, imageRef.current, surfaceRef.current));
  };

  const handlePointerEnd = (event: React.PointerEvent<HTMLImageElement>, cancelled = false) => {
    const pointers = pointersRef.current;
    if (!pointers.has(event.pointerId)) {
      return;
    }
    if (event.currentTarget.hasPointerCapture(event.pointerId)) {
      event.currentTarget.releasePointerCapture(event.pointerId);
    }
    pointers.delete(event.pointerId);
    if (pointers.size < 2) {
      pinchRef.current = null;
    }
    if (pointers.size === 1) {
      // Re-anchor the drag on the remaining finger so a pinch lift does not jump.
      const [remainingId, remaining] = [...pointers.entries()][0];
      if (isZoomed) {
        panDragRef.current = {
          pointerId: remainingId,
          startX: remaining.x,
          startY: remaining.y,
          originX: panOffset.x,
          originY: panOffset.y,
        };
      } else {
        panDragRef.current = null;
        setIsInteracting(false);
      }
      return;
    }
    if (pointers.size === 0) {
      const swipe = swipeRef.current;
      if (!cancelled && swipe && swipe.pointerId === event.pointerId) {
        const deltaX = event.clientX - swipe.startX;
        const deltaY = event.clientY - swipe.startY;
        if (Math.abs(deltaX) > SWIPE_THRESHOLD && Math.abs(deltaX) > Math.abs(deltaY) * SWIPE_DIRECTION_RATIO) {
          if (deltaX < 0) {
            handleNext();
          } else {
            handlePrevious();
          }
        }
      }
      swipeRef.current = null;
      setSwipeOffset(0);
      panDragRef.current = null;
      setIsInteracting(false);
    }
  };

  if (!itemCount || !currentItem) {
    return null;
  }

  return (
    <Dialog
      open={open}
      onOpenChange={(nextOpen, eventDetails) => {
        if (!nextOpen && showDetails && eventDetails.reason === "escape-key") {
          eventDetails.cancel();
          setShowDetails(false);
          return;
        }
        onOpenChange(nextOpen);
      }}
    >
      <DialogContent
        showCloseButton={false}
        className="!h-dvh !w-[100vw] !max-h-dvh !max-w-[100vw] overflow-hidden rounded-none border-0 bg-black/92 p-0 shadow-none"
        onKeyDown={handleKeyDown}
      >
        <VisuallyHidden>
          <DialogTitle>{currentItem.filename || "Attachment preview"}</DialogTitle>
          <DialogDescription>
            Attachment preview dialog. Press Escape to close, use left or right arrow keys to switch items, and zoom images with the
            controls, mouse wheel, double tap, or a pinch gesture.
          </DialogDescription>
        </VisuallyHidden>

        <div
          className={cn(
            "absolute inset-x-0 top-0 z-50 bg-linear-to-b from-black/70 via-black/35 to-transparent px-3 pb-6 pt-3 sm:px-5 sm:pt-4",
            showDetails && "lg:pr-[23rem]",
          )}
        >
          <div className="flex items-start justify-between gap-3">
            <div className="min-w-0 text-white">
              <div className="truncate text-sm font-medium">{currentItem.filename || "Attachment"}</div>
              {hasMultiple && (
                <div className="mt-1 text-xs text-white/70">
                  {safeIndex + 1} / {itemCount}
                </div>
              )}
            </div>

            <div className="flex shrink-0 items-center gap-1.5">
              <Button
                type="button"
                onClick={() => setShowDetails((visible) => !visible)}
                variant="ghost"
                size="icon"
                className={cn("rounded-full text-white hover:bg-white/16 hover:text-white", showDetails ? "bg-white/18" : "bg-white/10")}
                aria-label={showDetails ? t("attachment-details.actions.hide") : t("attachment-details.actions.show")}
                aria-expanded={showDetails}
                aria-controls="attachment-media-details"
              >
                <InfoIcon className="h-4 w-4" />
              </Button>
              <Button
                type="button"
                onClick={handleClose}
                variant="ghost"
                size="icon"
                className="rounded-full bg-white/10 text-white hover:bg-white/16 hover:text-white"
                aria-label="Close preview"
              >
                <X className="h-4 w-4" />
              </Button>
            </div>
          </div>
        </div>

        <div
          ref={surfaceRef}
          data-testid={isImagePreview ? "preview-zoom-surface" : undefined}
          className={cn(
            "flex h-full w-full items-center justify-center px-3 pb-20 pt-16 sm:px-16 sm:pb-8 sm:pt-20",
            isImagePreview && !isZoomed && "cursor-zoom-in",
            showDetails && "lg:pr-[26rem]",
          )}
          onWheel={handleWheel}
          onClick={(event) => {
            if (event.target === event.currentTarget && !isZoomed) {
              if (showDetails) {
                setShowDetails(false);
              } else {
                handleClose();
              }
            }
          }}
        >
          <div className="flex max-h-full max-w-full items-center justify-center" onClick={(event) => event.stopPropagation()}>
            {currentItem.kind === "video" ? (
              <video
                key={currentItem.id}
                src={currentItem.sourceUrl}
                poster={currentItem.posterUrl}
                className={cn(
                  "max-h-[calc(100dvh-8rem)] max-w-[calc(100vw-1.5rem)] rounded-md object-contain sm:max-h-[calc(100dvh-7rem)] sm:max-w-[calc(100vw-8rem)]",
                  showDetails && "lg:max-w-[calc(100vw-30rem)]",
                )}
                controls
                autoPlay
                playsInline
              />
            ) : currentItem.kind === "motion" ? (
              <MotionPhotoPreview
                key={currentItem.id}
                posterUrl={currentItem.posterUrl}
                motionUrl={currentItem.motionUrl}
                alt={`Preview live photo ${safeIndex + 1} of ${itemCount}`}
                presentationTimestampUs={currentItem.presentationTimestampUs}
                badgeClassName="left-3 top-3 sm:left-4 sm:top-4"
                mediaClassName={cn(
                  "max-h-[calc(100dvh-8rem)] max-w-[calc(100vw-1.5rem)] rounded-md object-contain sm:max-h-[calc(100dvh-7rem)] sm:max-w-[calc(100vw-8rem)]",
                  showDetails && "lg:max-w-[calc(100vw-30rem)]",
                )}
              />
            ) : (
              <img
                ref={imageRef}
                src={currentItem.sourceUrl}
                alt={`Preview image ${safeIndex + 1} of ${itemCount}`}
                className={cn(
                  "max-h-[calc(100dvh-8rem)] max-w-[calc(100vw-1.5rem)] rounded-md object-contain select-none sm:max-h-[calc(100dvh-7rem)] sm:max-w-[calc(100vw-8rem)]",
                  showDetails && "lg:max-w-[calc(100vw-30rem)]",
                  (isZoomed || hasMultiple) && (isInteracting ? "cursor-grabbing" : "cursor-grab"),
                )}
                style={{
                  transform: `translate3d(${panOffset.x + swipeOffset}px, ${panOffset.y}px, 0) scale(${zoomScale})`,
                  transition: isInteracting ? "none" : "transform 120ms ease-out",
                  transformOrigin: "center center",
                  touchAction: "none",
                }}
                onDoubleClick={handleDoubleClick}
                onPointerDown={handlePointerDown}
                onPointerMove={handlePointerMove}
                onPointerUp={handlePointerEnd}
                onPointerCancel={(event) => handlePointerEnd(event, true)}
                draggable={false}
                loading="eager"
                decoding="async"
              />
            )}
          </div>
        </div>

        {isImagePreview && (
          <div className={cn("absolute inset-x-0 bottom-0 z-30 px-3 pb-3 pt-6", showDetails && "hidden lg:block lg:pr-[22rem]")}>
            <div className="mx-auto flex w-fit items-center gap-1 rounded-full bg-black/60 px-2 py-2 text-white shadow-lg backdrop-blur-sm">
              {hasMultiple && !sm && (
                <>
                  <ZoomButton label="Previous item" onClick={handlePrevious} disabled={!canGoPrevious}>
                    <ChevronLeft className="h-4 w-4" />
                  </ZoomButton>
                  <div className="min-w-9 px-1 text-center text-xs font-medium tabular-nums text-white/75">
                    {safeIndex + 1}/{itemCount}
                  </div>
                  <ZoomButton label="Next item" onClick={handleNext} disabled={!canGoNext}>
                    <ChevronRight className="h-4 w-4" />
                  </ZoomButton>
                  <div className="mx-1 h-5 w-px bg-white/18" />
                </>
              )}
              <ZoomButton label="Zoom out" onClick={handleZoomOut} disabled={zoomScale === MIN_ZOOM}>
                <ZoomOut className="h-4 w-4" />
              </ZoomButton>
              <div className="min-w-12 px-2 text-center text-xs font-medium tabular-nums text-white/80">{zoomPercent}%</div>
              <ZoomButton label="Zoom in" onClick={handleZoomIn} disabled={zoomScale === MAX_ZOOM}>
                <ZoomIn className="h-4 w-4" />
              </ZoomButton>
              <div className="mx-1 h-5 w-px bg-white/18" />
              <ZoomButton label="Reset zoom" onClick={resetZoom} disabled={!isZoomed}>
                <RotateCcw className="h-4 w-4" />
              </ZoomButton>
            </div>
          </div>
        )}

        {hasMultiple && sm && (
          <>
            <NavButton
              side="left"
              disabled={!canGoPrevious}
              label="Previous item"
              onClick={handlePrevious}
              icon={<ChevronLeft className="h-5 w-5" />}
            />
            <NavButton
              side="right"
              disabled={!canGoNext}
              label="Next item"
              onClick={handleNext}
              icon={<ChevronRight className="h-5 w-5" />}
              detailsOpen={showDetails}
            />
          </>
        )}

        {hasMultiple && !sm && !isImagePreview && !showDetails && (
          <div className="absolute inset-x-0 bottom-0 z-20 px-3 pb-3 pt-6">
            <div className="mx-auto flex max-w-xs items-center justify-between rounded-full bg-black/55 px-2 py-2 backdrop-blur-sm">
              <Button
                type="button"
                variant="ghost"
                size="sm"
                onClick={handlePrevious}
                disabled={!canGoPrevious}
                className="rounded-full px-3 text-white hover:bg-white/10 hover:text-white disabled:text-white/35"
              >
                Prev
              </Button>
              <div className="px-3 text-xs text-white/75">
                {safeIndex + 1} / {itemCount}
              </div>
              <Button
                type="button"
                variant="ghost"
                size="sm"
                onClick={handleNext}
                disabled={!canGoNext}
                className="rounded-full px-3 text-white hover:bg-white/10 hover:text-white disabled:text-white/35"
              >
                Next
              </Button>
            </div>
          </div>
        )}

        {showDetails && <MediaMetadataDetails id="attachment-media-details" item={currentItem} onClose={() => setShowDetails(false)} />}
      </DialogContent>
    </Dialog>
  );
}

interface NavButtonProps {
  side: "left" | "right";
  disabled: boolean;
  label: string;
  onClick: () => void;
  icon: React.ReactNode;
  detailsOpen?: boolean;
}

const NavButton = ({ side, disabled, label, onClick, icon, detailsOpen = false }: NavButtonProps) => (
  <Button
    type="button"
    variant="ghost"
    size="icon"
    disabled={disabled}
    onClick={onClick}
    aria-label={label}
    className={cn(
      "absolute top-1/2 z-20 hidden h-11 w-11 -translate-y-1/2 rounded-full bg-white/10 text-white backdrop-blur-sm hover:bg-white/16 hover:text-white disabled:opacity-25 sm:flex",
      side === "left" ? "left-4" : detailsOpen ? "right-4 lg:right-[23rem]" : "right-4",
    )}
  >
    {icon}
  </Button>
);

const ZoomButton = ({
  disabled,
  label,
  onClick,
  children,
}: {
  disabled?: boolean;
  label: string;
  onClick: () => void;
  children: React.ReactNode;
}) => (
  <Button
    type="button"
    variant="ghost"
    size="icon"
    disabled={disabled}
    onClick={onClick}
    aria-label={label}
    className="h-9 w-9 rounded-full text-white hover:bg-white/12 hover:text-white disabled:text-white/35"
  >
    {children}
  </Button>
);

export default PreviewImageDialog;