Spaces:
Running
Running
File size: 3,312 Bytes
72f0edb |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
import { useState } from "react";
import { supabase } from "@/lib/supabase";
import { AnimationRequest, RobotAnimationConfig } from "@/lib/types";
import { toast } from "sonner";
export const useCreateAnimation = () => {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [animation, setAnimation] = useState<RobotAnimationConfig | null>(null);
const createAnimation = async (
request: AnimationRequest
): Promise<RobotAnimationConfig | null> => {
const requestId = `anim-${Date.now()}`; // Generate unique ID for tracking
console.log(`[${requestId}] π Animation Generator: Starting request`);
console.log(`[${requestId}] π Description: "${request.description}"`);
setIsLoading(true);
setError(null);
const startTime = performance.now();
try {
console.log(
`[${requestId}] π‘ Calling Supabase edge function "create-animation"...`
);
const { data, error } = await supabase.functions.invoke(
"create-animation",
{
body: {
robotName: request.robotName,
urdfContent: request.urdfContent,
description: request.description,
},
}
);
if (error) {
console.error(`[${requestId}] β Supabase function error:`, error);
// Format the error message for display
const errorMessage = error.message || "Unknown error occurred";
setError(errorMessage);
toast.error("Animation Generation Failed", {
description: errorMessage.includes("non-2xx status code")
? "The animation generator encountered a server error."
: errorMessage,
duration: 5000,
});
throw new Error(errorMessage);
}
const endTime = performance.now();
console.log(
`[${requestId}] β
Edge function responded in ${(
endTime - startTime
).toFixed(2)}ms`
);
if (!data) {
throw new Error("No data returned from edge function");
}
// Quick validation of minimum required structure
if (
!data.joints ||
!Array.isArray(data.joints) ||
data.joints.length === 0
) {
console.error(`[${requestId}] β οΈ Invalid animation data:`, data);
throw new Error(
"Invalid animation configuration: Missing joint animations"
);
}
console.log(
`[${requestId}] π€ Animation generated with ${data.joints.length} joint(s)`
);
// Store the animation data
setAnimation(data);
return data;
} catch (err) {
const errorMessage =
err instanceof Error ? err.message : "Unknown error occurred";
const endTime = performance.now();
console.error(
`[${requestId}] β Error generating animation after ${(
endTime - startTime
).toFixed(2)}ms:`,
err
);
setError(errorMessage);
return null;
} finally {
setIsLoading(false);
console.log(`[${requestId}] π Animation request completed`);
}
};
return {
createAnimation,
animation,
isLoading,
error,
clearAnimation: () => setAnimation(null),
clearError: () => setError(null),
};
};
|