fix: images and content editor

This commit is contained in:
Ivan Rezchikov
2026-07-13 17:22:10 +03:00
parent fa0c2bf371
commit 8ee59bd310
4 changed files with 27 additions and 49 deletions
+5 -43
View File
@@ -1,6 +1,6 @@
"use client";
import { useActionState, useEffect, useRef, useState } from "react";
import { useActionState, useEffect, useState } from "react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import {
@@ -30,7 +30,6 @@ import { initialFormState } from "@/lib/form-action";
import { EYE_COLORS } from "@/lib/citizenship/types";
import { Spinner } from "@/components/ui/spinner";
import { ImageUploader } from "@/components/image-uploader";
import { removeBackground } from "@imgly/background-removal";
type Props = {
firstName: string;
@@ -51,9 +50,6 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
const [appType, setAppType] = useState<AppType | "">("");
const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialFormState);
const [isEuResident, setIsEuResident] = useState<boolean | null>(null);
const [profileProcessing, setProfileProcessing] = useState(false);
const profileInputRef = useRef<HTMLInputElement>(null);
const signatureInputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (state.error) {
@@ -63,37 +59,6 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
const errors = state.fieldErrors ?? {};
async function handleProfileSelect(file: File) {
setProfileProcessing(true);
try {
const resultBlob = await removeBackground(file);
const processedFile = new File([resultBlob], file.name, { type: "image/png" });
const dt = new DataTransfer();
dt.items.add(processedFile);
if (profileInputRef.current) profileInputRef.current.files = dt.files;
} catch {
const dt = new DataTransfer();
dt.items.add(file);
if (profileInputRef.current) profileInputRef.current.files = dt.files;
} finally {
setProfileProcessing(false);
}
}
function handleSignatureSelect(file: File) {
const dt = new DataTransfer();
dt.items.add(file);
if (signatureInputRef.current) signatureInputRef.current.files = dt.files;
}
function handleProfileRemove() {
if (profileInputRef.current) profileInputRef.current.value = "";
}
function handleSignatureRemove() {
if (signatureInputRef.current) signatureInputRef.current.value = "";
}
return (
<form action={formAction}>
<FieldGroup>
@@ -322,26 +287,23 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
<FieldLegend>Images</FieldLegend>
<FieldDescription>Provide your profile image and your signature for your identity document.</FieldDescription>
<FieldGroup>
<input ref={profileInputRef} type="hidden" name="profile" />
<input ref={signatureInputRef} type="hidden" name="signature" />
<ImageUploader
id="profile"
name="profile"
label="Profile"
description="Select a profile image to upload. The image should be taken on a neutral background."
error={errors.profile?.[0]}
previewClassName="aspect-4/5"
onFileSelect={handleProfileSelect}
onRemove={handleProfileRemove}
isUploading={profileProcessing}
required
/>
<ImageUploader
id="signature"
name="signature"
label="Signature"
description="Select a signature to upload."
error={errors.signature?.[0]}
previewClassName="aspect-5/2"
onFileSelect={handleSignatureSelect}
onRemove={handleSignatureRemove}
required
/>
</FieldGroup>
</FieldSet>
+1 -1
View File
@@ -271,7 +271,7 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
}}
/>
</div>
<div className="flex flex-col gap-1 items-center opacity-0 group-hover:opacity-100 transition-opacity shrink-0">
<div className="flex flex-col gap-1 items-center lg:opacity-0 group-hover:opacity-100 transition-opacity shrink-0">
<Button variant="ghost" onClick={() => moveBlockUp(index)} disabled={index === 0} size="icon-sm">
<ChevronUpIcon />
</Button>
+21 -4
View File
@@ -49,6 +49,12 @@ function validateFile(file: File, accept: string): string | null {
return null;
}
function fileToFileList(file: File): FileList {
const dt = new DataTransfer();
dt.items.add(file);
return dt.files;
}
export function ImageUploader({ id, name, label, description, error, variant = "default", previewClassName, accept = DEFAULT_ACCEPT, required, value, onFileSelect, onRemove, isUploading, children, caption, onCaptionChange }: Props) {
const inputRef = useRef<HTMLInputElement>(null);
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
@@ -99,7 +105,12 @@ export function ImageUploader({ id, name, label, description, error, variant = "
e.preventDefault();
setIsDragOver(false);
const file = e.dataTransfer.files?.[0];
if (file) applyFile(file);
if (!file) return;
if (inputRef.current) {
inputRef.current.files = fileToFileList(file);
}
applyFile(file);
}
const displayedError = clientError ?? error;
@@ -167,11 +178,17 @@ export function ImageUploader({ id, name, label, description, error, variant = "
<Field data-invalid={!!displayedError}>
<FieldLabel htmlFor={id}>{label}</FieldLabel>
<div className="flex flex-col md:flex-row items-start gap-4">
<div
onDragOver={(e) => { e.preventDefault(); setIsDragOver(true); }}
onDragLeave={() => setIsDragOver(false)}
onDrop={handleDrop}
className="flex flex-col md:flex-row items-start gap-4"
>
<div
className={cn(
"w-28 rounded-md border border-dashed border-foreground/15 bg-muted flex items-center justify-center overflow-hidden shrink-0",
previewClassName ?? "aspect-square"
"w-28 rounded-md border border-dashed border-foreground/15 bg-muted flex items-center justify-center overflow-hidden shrink-0 transition-colors",
previewClassName ?? "aspect-square",
isDragOver && "bg-muted/60 border-foreground/30"
)}
>
{isUploading ? (
-1
View File
@@ -30,7 +30,6 @@ export async function login() {
state,
});
//Frontend public url, not api. And wtf I need to dubplicate it all
redirect(`${process.env.KOTOSOFT_ID_API_PUBLIC_URL}/oauth/authorize?${params}`);
}