"use client"; import React, { useState } from "react"; import { useDocumentInfo, toast } from "@payloadcms/ui"; type Action = "upscale" | "recover"; interface ActionState { loading: boolean; resultId?: string | number; } export const AiMediaButtons: React.FC = () => { const { id } = useDocumentInfo(); const [upscale, setUpscale] = useState({ loading: false }); const [recover, setRecover] = useState({ loading: false }); if (!id) return null; // Only show on existing documents const runAction = async (action: Action) => { const setter = action === "upscale" ? setUpscale : setRecover; setter({ loading: true }); const label = action === "upscale" ? "AI Upscale" : "AI Recover"; toast.info( `${label} started – this can take 30–90 seconds, please wait…`, ); try { // The API path is hardcoded here and assuming that's where the host app registers the endpoint. const response = await fetch(`/api/media/${id}/ai-process`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action }), }); const result = await response.json(); if (!response.ok) { throw new Error(result.error || `${label} failed`); } setter({ loading: false, resultId: result.mediaId }); toast.success( `✅ ${label} erfolgreich! Neues Bild (ID: ${result.mediaId}) wurde gespeichert.`, ); } catch (err: any) { console.error(`[AiMediaButtons] ${action} error:`, err); toast.error( err instanceof Error ? err.message : `${label} fehlgeschlagen`, ); setter({ loading: false }); } }; const buttonStyle: React.CSSProperties = { background: "var(--theme-elevation-150)", border: "1px solid var(--theme-elevation-200)", color: "var(--theme-text)", padding: "8px 14px", borderRadius: "4px", fontSize: "13px", fontWeight: 500, display: "inline-flex", alignItems: "center", gap: "6px", transition: "opacity 0.15s ease", }; const disabledStyle: React.CSSProperties = { opacity: 0.55, cursor: "not-allowed", }; return (
{/* AI Upscale */}
{upscale.resultId && ( → Neues Bild öffnen (ID: {upscale.resultId}) )}
{/* AI Recover */}
{recover.resultId && ( → Neues Bild öffnen (ID: {recover.resultId}) )}

AI Upscale verbessert die Auflösung via{" "} google/upscaler. AI Recover restauriert alte/beschädigte Fotos via{" "} microsoft/bringing-old-photos-back-to-life. Das Ergebnis wird als neues Medium gespeichert.

); };