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"; "use client";
import { useActionState, useEffect, useRef, useState } from "react"; import { useActionState, useEffect, useState } from "react";
import { toast } from "sonner"; import { toast } from "sonner";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { import {
@@ -30,7 +30,6 @@ import { initialFormState } from "@/lib/form-action";
import { EYE_COLORS } from "@/lib/citizenship/types"; import { EYE_COLORS } from "@/lib/citizenship/types";
import { Spinner } from "@/components/ui/spinner"; import { Spinner } from "@/components/ui/spinner";
import { ImageUploader } from "@/components/image-uploader"; import { ImageUploader } from "@/components/image-uploader";
import { removeBackground } from "@imgly/background-removal";
type Props = { type Props = {
firstName: string; firstName: string;
@@ -51,9 +50,6 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
const [appType, setAppType] = useState<AppType | "">(""); const [appType, setAppType] = useState<AppType | "">("");
const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialFormState); const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialFormState);
const [isEuResident, setIsEuResident] = useState<boolean | null>(null); const [isEuResident, setIsEuResident] = useState<boolean | null>(null);
const [profileProcessing, setProfileProcessing] = useState(false);
const profileInputRef = useRef<HTMLInputElement>(null);
const signatureInputRef = useRef<HTMLInputElement>(null);
useEffect(() => { useEffect(() => {
if (state.error) { if (state.error) {
@@ -63,37 +59,6 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
const errors = state.fieldErrors ?? {}; 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 ( return (
<form action={formAction}> <form action={formAction}>
<FieldGroup> <FieldGroup>
@@ -322,26 +287,23 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
<FieldLegend>Images</FieldLegend> <FieldLegend>Images</FieldLegend>
<FieldDescription>Provide your profile image and your signature for your identity document.</FieldDescription> <FieldDescription>Provide your profile image and your signature for your identity document.</FieldDescription>
<FieldGroup> <FieldGroup>
<input ref={profileInputRef} type="hidden" name="profile" />
<input ref={signatureInputRef} type="hidden" name="signature" />
<ImageUploader <ImageUploader
id="profile" id="profile"
name="profile"
label="Profile" label="Profile"
description="Select a profile image to upload. The image should be taken on a neutral background." description="Select a profile image to upload. The image should be taken on a neutral background."
error={errors.profile?.[0]} error={errors.profile?.[0]}
previewClassName="aspect-4/5" previewClassName="aspect-4/5"
onFileSelect={handleProfileSelect} required
onRemove={handleProfileRemove}
isUploading={profileProcessing}
/> />
<ImageUploader <ImageUploader
id="signature" id="signature"
name="signature"
label="Signature" label="Signature"
description="Select a signature to upload." description="Select a signature to upload."
error={errors.signature?.[0]} error={errors.signature?.[0]}
previewClassName="aspect-5/2" previewClassName="aspect-5/2"
onFileSelect={handleSignatureSelect} required
onRemove={handleSignatureRemove}
/> />
</FieldGroup> </FieldGroup>
</FieldSet> </FieldSet>
+1 -1
View File
@@ -271,7 +271,7 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
}} }}
/> />
</div> </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"> <Button variant="ghost" onClick={() => moveBlockUp(index)} disabled={index === 0} size="icon-sm">
<ChevronUpIcon /> <ChevronUpIcon />
</Button> </Button>
+21 -4
View File
@@ -49,6 +49,12 @@ function validateFile(file: File, accept: string): string | null {
return 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) { 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 inputRef = useRef<HTMLInputElement>(null);
const [previewUrl, setPreviewUrl] = useState<string | null>(null); const [previewUrl, setPreviewUrl] = useState<string | null>(null);
@@ -99,7 +105,12 @@ export function ImageUploader({ id, name, label, description, error, variant = "
e.preventDefault(); e.preventDefault();
setIsDragOver(false); setIsDragOver(false);
const file = e.dataTransfer.files?.[0]; 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; const displayedError = clientError ?? error;
@@ -167,11 +178,17 @@ export function ImageUploader({ id, name, label, description, error, variant = "
<Field data-invalid={!!displayedError}> <Field data-invalid={!!displayedError}>
<FieldLabel htmlFor={id}>{label}</FieldLabel> <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 <div
className={cn( className={cn(
"w-28 rounded-md border border-dashed border-foreground/15 bg-muted flex items-center justify-center overflow-hidden shrink-0", "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" previewClassName ?? "aspect-square",
isDragOver && "bg-muted/60 border-foreground/30"
)} )}
> >
{isUploading ? ( {isUploading ? (
-1
View File
@@ -30,7 +30,6 @@ export async function login() {
state, 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}`); redirect(`${process.env.KOTOSOFT_ID_API_PUBLIC_URL}/oauth/authorize?${params}`);
} }