import { Image, ImageUploader } from '@components/admin/ImageUploader.js';
import { InputField } from '@components/common/form/InputField.js';
import { _ } from '@evershop/evershop/lib/locale/translate/_';
import React, { useEffect, useState } from 'react';
import { useFormContext } from 'react-hook-form';
import uniqid from 'uniqid';
type PreviewType = 'logo' | 'favicon' | 'social';
/**
* Build a URL for the on-the-fly image endpoint (`GET /images`), which resizes
* + re-encodes the asset and serves it with an immutable cache header. Passing
* the stored (relative) `/assets/...` path lets the processor read the file
* locally instead of re-fetching it over HTTP.
*/
function imageEndpoint(
src: string,
params: { w: number; h?: number; q?: number; f?: 'webp' | 'jpeg' | 'png' }
): string {
const query = new URLSearchParams({
src,
w: String(params.w),
q: String(params.q ?? 80),
f: params.f ?? 'webp'
});
if (params.h) {
query.set('h', String(params.h));
}
return `/images?${query.toString()}`;
}
function AssetPreview({ url, type }: { url: string; type: PreviewType }) {
if (type === 'favicon') {
// Render at the exact sizes the storefront emits so contrast / legibility
// problems at 16px are obvious before saving.
return (
{[16, 32, 48].map((size) => (
{size}px
))}
);
}
if (type === 'social') {
// Mirrors the actual og:image render (width-only, jpeg) so the admin sees
// the true crop/ratio a crawler will get.
return (
);
}
// logo — show it on light and dark so a logo that disappears on one theme is
// caught immediately.
return (
);
}
export interface BrandAssetUploaderProps {
/** Form field / setting key, e.g. `logo`, `favicon`, `socialSharingImage`. */
name: string;
/** Current saved value (a `/assets/...` URL) or empty string. */
defaultValue?: string;
/** Media subfolder the file lands in; a unique token is appended per mount. */
folder?: string;
/** Which contextual preview to render under the uploader. */
previewType?: PreviewType;
/**
* When set, the asset's real intrinsic pixel size is measured on upload and
* written to these form fields. Used for the logo — whose aspect ratio is
* arbitrary — so the storefront can size it without distortion or layout
* shift. Omit for fixed-ratio assets (favicon, social image).
*/
widthName?: string;
heightName?: string;
defaultWidth?: string;
defaultHeight?: string;
}
/**
* Single-asset image upload for store branding (logo / favicon / social image).
* Wraps the shared {@link ImageUploader} in single mode, mirrors the uploaded
* URL into a hidden form field so it saves with the rest of the store settings,
* and renders a contextual preview built from the `/images` endpoint.
*/
export function BrandAssetUploader({
name,
defaultValue,
folder = 'branding',
previewType,
widthName,
heightName,
defaultWidth,
defaultHeight
}: BrandAssetUploaderProps) {
const [image, setImage] = useState(
defaultValue ? { uuid: name, url: defaultValue } : undefined
);
const { setValue } = useFormContext();
// A fresh folder per mount so replacing an asset never overwrites the old
// file: the image endpoint caches by source path with an immutable header, so
// a reused filename would keep serving the previous render.
const [targetPath] = useState(() => `${folder}/${uniqid()}`);
useEffect(() => {
setValue(name, image?.url || '');
}, [image, name, setValue]);
// Measure the real intrinsic size of the asset (logo only) so the storefront
// sizes it correctly. `window.Image` — the bare `Image` is the type imported
// above, not the DOM constructor.
useEffect(() => {
if (!widthName || !heightName) {
return;
}
if (!image?.url) {
setValue(widthName, '');
setValue(heightName, '');
return;
}
const probe = new window.Image();
probe.onload = () => {
setValue(widthName, String(probe.naturalWidth));
setValue(heightName, String(probe.naturalHeight));
};
probe.src = image.url;
}, [image, widthName, heightName, setValue]);
return (
{/* The shared uploader is a 100%-width square; in this wide settings
column that balloons, so cap it to a compact thumbnail. */}
{
if (images.length > 0) {
setImage(images[0]);
}
}}
onDelete={() => setImage(undefined)}
targetPath={targetPath}
/>
{widthName && (
)}
{heightName && (
)}
{previewType && image?.url ? (
) : null}
);
}