import React, { useEffect, useRef } from "react"; import { toast } from "sonner"; // @ts-ignore import { Rive } from "@rive-app/react-canvas"; // Use advanced WebGL2 renderer interface Props { nodeType: string; } export const RiveInput: React.FC = ({ nodeType }) => { const canvasRef = useRef(null); const riveInstanceRef = useRef(null); const viewModelRef = useRef(null); const dashboardRef = useRef(null); const rivePath = nodeType.includes("pose") ? "/rive/pose.riv" : nodeType.includes("lights") ? "/rive/lights.riv" : null; const labelName = nodeType.includes("pose") ? "Pose" : nodeType.includes("lights") ? "Light Controller" : ""; const artboard = nodeType.includes("lights") ? "Artboard" : "final for nover"; const isPose = nodeType.includes("pose"); const isLights = nodeType.includes("lights"); // Helper to update VM input value const updateVMInput = (key: string, value: any) => { const vm = viewModelRef.current; if (vm?.inputs?.[key]) { vm.inputs[key].value = value; } }; useEffect(() => { if (!canvasRef.current || !rivePath) return; // Clean up previous instance if (riveInstanceRef.current) { riveInstanceRef.current.cleanup(); } const rive = new Rive({ canvas: canvasRef.current, src: rivePath, autoplay: true, artboard, stateMachines: "State Machine 1", useOffscreenRenderer: false, // or true if you're sharing context onLoad: () => { rive.resizeDrawingSurfaceToCanvas(); console.log("Rive (advanced) loaded"); const vm = rive.viewModelByName('View Model 1'); const vmi = vm.defaultInstance(); rive.bindViewModelInstance(vmi); if(vm){ dashboardRef.current = vm; console.log('Dashboard View Model added: ', vm) }else{ console.log('NO VM') } }, onLoadError: (err: any) => { console.error("Failed to load .riv:", err); toast.error("Rive load error"); }, }); riveInstanceRef.current = rive; return () => { rive.cleanup(); }; }, [rivePath, artboard]); if (!rivePath) return null; return (
); }; export default RiveInput;