-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patheditable-image-overlay.tsx
More file actions
171 lines (157 loc) · 4.81 KB
/
editable-image-overlay.tsx
File metadata and controls
171 lines (157 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"use client";
import { CameraIcon } from "lucide-react";
import React, { useRef, useState } from "react";
import type Webcam from "react-webcam";
import { toast } from "sonner";
import { cn } from "~/lib/utils";
import ImageCrop from "./file-uploader/image-crop";
import useFileUpload from "./file-uploader/use-file-upload";
import WebcamCapture from "./file-uploader/webcam-capture";
import { Loading } from "./loading";
import { Button } from "./ui/button";
type Step = "idle" | "crop" | "webcam";
interface EditableImageOverlayProps {
children: React.ReactNode;
canEdit: boolean;
folder: string;
aspectRatio?: number;
circularCrop?: boolean;
onImageSaved: (url: string) => void | Promise<void>;
isSaving?: boolean;
overlayPosition?: "top-right" | "center";
className?: string;
}
export function EditableImageOverlay({
children,
canEdit,
folder,
aspectRatio,
circularCrop,
onImageSaved,
isSaving,
overlayPosition = "top-right",
className,
}: EditableImageOverlayProps) {
const [step, setStep] = useState<Step>("idle");
const [image, setImage] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const webcamRef = useRef<Webcam>(null);
const { uploadFile } = useFileUpload();
if (!canEdit) {
return <>{children}</>;
}
const onSelectFile = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files && e.target.files.length > 0 && e.target.files[0]) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = () => {
setImage(reader.result as string);
setStep("crop");
};
reader.onerror = () => {
toast.error("Failed to read file.");
};
reader.readAsDataURL(file);
}
};
const handleCropComplete = async (blob: Blob) => {
if (!blob) {
toast.error("No cropped image to upload.");
return;
}
const file = new File([blob], "cropped_image.jpg", { type: "image/jpeg" });
try {
setLoading(true);
const url = await uploadFile(file, folder);
setStep("idle");
setLoading(false);
await onImageSaved(url);
} catch (error) {
toast.error("Failed to upload image.");
console.error(error);
setStep("idle");
setLoading(false);
}
};
const capturePhoto = () => {
if (!webcamRef.current) return;
const imageSrc = webcamRef.current.getScreenshot();
setImage(imageSrc);
setStep("crop");
};
const isLoading = loading || isSaving;
return (
<div className={cn("group/editable relative", className)}>
{children}
{/* Hidden file input */}
<input
type="file"
accept=".png, .jpg, .jpeg, .webp"
onChange={onSelectFile}
ref={fileInputRef}
className="hidden"
/>
{/* Edit overlay */}
{overlayPosition === "top-right" ? (
<Button
type="button"
variant="secondary"
size="sm"
onClick={() => fileInputRef.current?.click()}
disabled={isLoading}
aria-label="Edit image"
className="absolute top-3 right-3 z-20 gap-1.5 bg-black/50 hover:bg-black/70 text-white border-white/20 backdrop-blur-sm opacity-60 sm:opacity-0 sm:group-hover/editable:opacity-100 transition-opacity duration-200"
>
{isLoading ? (
<Loading />
) : (
<>
<CameraIcon className="size-4" />
<span className="text-xs hidden sm:inline">Edit</span>
</>
)}
</Button>
) : (
<div
onClick={() => fileInputRef.current?.click()}
role="button"
tabIndex={0}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
fileInputRef.current?.click();
}
}}
aria-label="Edit image"
className="absolute inset-0 z-10 flex items-center justify-center rounded-full bg-black/0 hover:bg-black/40 opacity-60 sm:opacity-0 sm:group-hover/editable:opacity-100 transition-all duration-200 cursor-pointer"
>
{isLoading ? (
<Loading />
) : (
<CameraIcon className="size-6 text-white drop-shadow-md" />
)}
</div>
)}
{/* Crop modal */}
{step === "crop" && image && (
<ImageCrop
image={image}
onComplete={handleCropComplete}
onCancel={() => setStep("idle")}
aspectRatio={aspectRatio}
circularCrop={circularCrop}
loading={loading}
/>
)}
{/* Webcam modal */}
{step === "webcam" && (
<WebcamCapture
ref={webcamRef}
capturePhoto={capturePhoto}
onCancel={() => setStep("idle")}
/>
)}
</div>
);
}