File size: 1,718 Bytes
cb5d9d0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import { useCallback, useState } from "react";
import { API_BASE } from "../api/client";
interface ApplyTextureResult {
output_url: string;
output_filename: string;
}
export function useApplyTexture() {
const [isApplying, setIsApplying] = useState(false);
const [resultUrl, setResultUrl] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const applyTexture = useCallback(
async (filename: string, maskIndices: number[], textureName: string, originalFilename?: string) => {
setIsApplying(true);
setError(null);
try {
const res = await fetch(`${API_BASE}/seg/apply_texture`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
filename,
mask_indices: maskIndices,
texture_name: textureName,
original_filename: originalFilename ?? filename,
direction_mode: "auto",
angle_degrees: 0,
}),
});
if (!res.ok) {
const text = await res.text();
throw new Error(text || `Error ${res.status}`);
}
const data: ApplyTextureResult = await res.json();
setResultUrl(`${API_BASE}${data.output_url}`);
return data;
} catch (err) {
const message = err instanceof Error ? err.message : "Error al aplicar textura";
setError(message);
throw new Error(message);
} finally {
setIsApplying(false);
}
},
[],
);
const resetResult = useCallback(() => {
setResultUrl(null);
setError(null);
}, []);
return { applyTexture, isApplying, resultUrl, error, resetResult };
}
|