Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

4.1 KB
Raw Blame
"use client";

import { Button } from "@rallly/ui/button";
import {
  Dialog,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@rallly/ui/dialog";
import React from "react";
import type { Crop, PercentCrop } from "react-image-crop";
import ReactCrop, { centerCrop, makeAspectCrop } from "react-image-crop";
import "react-image-crop/dist/ReactCrop.css";
import { Trans, useTranslation } from "@/i18n/client";
import { getCroppedImg } from "@/lib/image-processing";

export interface ImageCropDialogProps {
  /** Whether the dialog is open */
  open: boolean;
  /** Image URL to crop */
  imageSrc: string | null;
  /** Original file being cropped */
  originalFile: File | null;
  /** Loading state during upload */
  isUploading?: boolean;
  /** Callback when dialog should close */
  onClose: () => void;
  /** Callback when crop is complete with the cropped file */
  onCropComplete: (croppedFile: File) => Promise<void>;
}

export function ImageCropDialog({
  open,
  imageSrc,
  originalFile,
  isUploading = false,
  onClose,
  onCropComplete,
}: ImageCropDialogProps) {
  return (
    <Dialog open={open} onOpenChange={onClose}>
      <DialogContent className="w-fit">
        <DialogHeader>
          <DialogTitle>
            <Trans i18nKey="imageCropDialogTitle" defaults="Crop image" />
          </DialogTitle>
        </DialogHeader>
        {/* Keyed by the image's preview URL, so every image starts from a
        fresh crop, even one picked while the last dialog animates out */}
        <ImageCropper
          key={imageSrc}
          imageSrc={imageSrc}
          originalFile={originalFile}
          isUploading={isUploading}
          onCropComplete={onCropComplete}
        />
      </DialogContent>
    </Dialog>
  );
}

function ImageCropper({
  imageSrc,
  originalFile,
  isUploading,
  onCropComplete,
}: {
  imageSrc: string | null;
  originalFile: File | null;
  isUploading: boolean;
  onCropComplete: (croppedFile: File) => Promise<void>;
}) {
  const { t } = useTranslation();
  const [crop, setCrop] = React.useState<Crop>();
  const [completedCrop, setCompletedCrop] = React.useState<PercentCrop>();
  const imgRef = React.useRef<HTMLImageElement>(null);

  const onImageLoad = (e: React.SyntheticEvent<HTMLImageElement>) => {
    const { width, height } = e.currentTarget;
    const crop = centerCrop(
      makeAspectCrop(
        {
          unit: "%",
          width: 100,
        },
        1, // 1:1 aspect ratio
        width,
        height,
      ),
      width,
      height,
    );
    setCrop(crop);
  };

  const handleCropComplete = async () => {
    if (!completedCrop || !imgRef.current || !originalFile) return;

    try {
      const croppedFile = await getCroppedImg(
        imgRef.current,
        completedCrop,
        originalFile.name,
        originalFile.type,
      );

      await onCropComplete(croppedFile);
    } catch (error) {
      console.error("Failed to crop image:", error);
      throw error; // Re-throw to let parent handle the error
    }
  };

  return (
    <>
      {imageSrc && (
        <div className="flex justify-center">
          <ReactCrop
            crop={crop}
            onChange={(_, percentCrop) => setCrop(percentCrop)}
            onComplete={(_, percentCrop) => {
              setCompletedCrop(percentCrop);
            }}
            aspect={1}
            minWidth={100}
            minHeight={100}
            className="max-h-96"
          >
            {/* biome-ignore lint/performance/noImgElement: required for react-image-crop */}
            <img
              ref={imgRef}
              alt={t("cropPreview", {
                defaultValue: "Crop preview",
              })}
              src={imageSrc}
              onLoad={onImageLoad}
              className="max-h-96 max-w-full"
            />
          </ReactCrop>
        </div>
      )}

      <DialogFooter>
        <Button
          variant="primary"
          className="w-full"
          loading={isUploading}
          onClick={handleCropComplete}
          disabled={!completedCrop}
        >
          <Trans i18nKey="uploadImage" defaults="Upload" />
        </Button>
      </DialogFooter>
    </>
  );
}