import type { FieldImageSlot } from "@cloudhal/ui"; import { Button, Field, ImagePreview, InsetPanel, SkeletonLine, Text } from "@cloudhal/ui"; import { Images, Trash } from "@phosphor-icons/react"; import { useQuery } from "@tanstack/react-query"; import { useState } from "react"; import { useI18n } from "../../i18n/index.tsx"; import { api } from "../../lib/api.ts"; import { assetMeta, assetPreviewUrl, assetTitle } from "../../lib/media.ts"; import { AssetPickerDialog } from "../MediaPicker.tsx"; /** * An image field (RFC-0041 ยง2): the chosen media-library image as a thumbnail with its title, and * "Choose image" / "Change" / "Remove"; the value is the bare asset id (the alt text is the * asset's own, per language). The picker is the editor's (`AssetPickerDialog`). */ export function FieldImage({ siteId, defaultLocale, slot, }: { siteId: string; defaultLocale: string; slot: FieldImageSlot; }) { const { t, locale } = useI18n("fields", "media"); const [open, setOpen] = useState(false); const id = slot.value; const detail = useQuery({ queryKey: ["asset", siteId, id], queryFn: () => api.assets.get(siteId, id as string), enabled: Boolean(id), }); const asset = detail.data?.asset; return ( {!id ? ( {t("fields.image.empty")} ) : asset ? ( <> {assetTitle(asset, locale, defaultLocale)} {assetMeta(asset, locale)} ) : detail.isError ? ( {t("media.field.missing")} ) : ( )} {!slot.readOnly && ( {id && ( )} )} { slot.onChange(item.asset.id); setOpen(false); }} /> ); }