| 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 }; |
| } |
|
|