@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.e3316bb → 2.26.0-pr.feature-update-to-react-19.361.916e61e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wandelbots/wandelbots-js-react-components",
3
- "version": "2.26.0-pr.feature-update-to-react-19.361.e3316bb",
3
+ "version": "2.26.0-pr.feature-update-to-react-19.361.916e61e",
4
4
  "description": "React UI toolkit for building applications on top of the Wandelbots platform",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -2,9 +2,22 @@ import type {
2
2
  DHParameter,
3
3
  MotionGroupStateResponse,
4
4
  } from "@wandelbots/nova-api/v1"
5
- import { describe, expect, it } from "vitest"
5
+ import { describe, expect, it, vi } from "vitest"
6
6
  import RobotAnimator from "./RobotAnimator"
7
7
 
8
+ // Mock the dependencies
9
+ vi.mock("./robotModelLogic", () => ({
10
+ collectJoints: vi.fn(),
11
+ }))
12
+
13
+ vi.mock("@react-spring/three", () => ({
14
+ useSpring: vi.fn(),
15
+ }))
16
+
17
+ import { useSpring } from "@react-spring/three"
18
+
19
+ const mockUseSpring = useSpring as any
20
+
8
21
  describe("RobotAnimator", () => {
9
22
  it("should export the component correctly", () => {
10
23
  expect(RobotAnimator).toBeDefined()
@@ -68,66 +81,40 @@ describe("RobotAnimator", () => {
68
81
  }).not.toThrow()
69
82
  })
70
83
 
71
- it("should verify dynamic joint configuration logic", () => {
72
- // Test the logic that would create dynamic spring configs
73
- const testConfigs = [
74
- { jointCount: 3, expectedJoints: ["joint0", "joint1", "joint2"] },
75
- {
76
- jointCount: 6,
77
- expectedJoints: [
78
- "joint0",
79
- "joint1",
80
- "joint2",
81
- "joint3",
82
- "joint4",
83
- "joint5",
84
- ],
85
- },
86
- {
87
- jointCount: 8,
88
- expectedJoints: [
89
- "joint0",
90
- "joint1",
91
- "joint2",
92
- "joint3",
93
- "joint4",
94
- "joint5",
95
- "joint6",
96
- "joint7",
97
- ],
98
- },
99
- ]
84
+ it("should use React Spring for smooth interpolation", () => {
85
+ const mockSpringResult = {
86
+ joint0: { get: () => 0.6 },
87
+ joint1: { get: () => -1.2 },
88
+ }
100
89
 
101
- testConfigs.forEach(({ jointCount, expectedJoints }) => {
102
- // Simulate the logic from the component
103
- const dhParameters = Array(jointCount).fill({
104
- theta: 0,
105
- reverse_rotation_direction: false,
106
- })
107
- const jointValues = Array(jointCount)
108
- .fill(0)
109
- .map((_, i) => i * 0.1)
110
-
111
- const config: any = {
112
- config: { tension: 120, friction: 20 },
113
- onChange: () => {},
114
- }
90
+ // Mock useSpring to return spring values with get() methods
91
+ mockUseSpring.mockReturnValue(mockSpringResult)
115
92
 
116
- dhParameters.forEach((_, index) => {
117
- config[`joint${index}`] = jointValues[index] || 0
118
- })
119
-
120
- // Verify the configuration has the expected joint properties
121
- expectedJoints.forEach((jointKey) => {
122
- expect(config[jointKey]).toBeDefined()
123
- expect(typeof config[jointKey]).toBe("number")
124
- })
125
-
126
- // Verify no extra joints were added
127
- const actualJointKeys = Object.keys(config).filter((key) =>
128
- key.startsWith("joint"),
129
- )
130
- expect(actualJointKeys).toHaveLength(jointCount)
131
- })
93
+ // Verify useSpring would be called with proper configuration
94
+ expect(typeof mockUseSpring).toBe("function")
95
+
96
+ // Test the rotation calculation logic
97
+ const dhParam1 = { theta: 0.1, reverse_rotation_direction: false }
98
+ const dhParam2 = { theta: -0.2, reverse_rotation_direction: true }
99
+ const jointValue1 = 0.5
100
+ const jointValue2 = 1.0
101
+
102
+ // Calculate expected rotations
103
+ const expectedRotation1 = 1 * jointValue1 + 0.1 // 0.6
104
+ const expectedRotation2 = -1 * jointValue2 + -0.2 // -1.2
105
+
106
+ expect(expectedRotation1).toBe(0.6)
107
+ expect(expectedRotation2).toBe(-1.2)
108
+
109
+ // Test edge cases
110
+ const dhParamReverse = { theta: 0, reverse_rotation_direction: true }
111
+ const jointValueZero = 0
112
+ const expectedRotationReverse = -1 * jointValueZero + 0 // 0
113
+
114
+ expect(expectedRotationReverse).toBe(0)
115
+
116
+ // Verify the spring result structure
117
+ expect(mockSpringResult.joint0.get()).toBe(0.6)
118
+ expect(mockSpringResult.joint1.get()).toBe(-1.2)
132
119
  })
133
120
  })
@@ -1,10 +1,10 @@
1
- import { Globals, useSpring } from "@react-spring/three"
1
+ import { useSpring } from "@react-spring/three"
2
2
  import { useThree } from "@react-three/fiber"
3
3
  import type {
4
4
  DHParameter,
5
5
  MotionGroupStateResponse,
6
6
  } from "@wandelbots/nova-api/v1"
7
- import React, { useMemo, useRef } from "react"
7
+ import React, { useRef } from "react"
8
8
  import type { Group, Object3D } from "three"
9
9
  import { collectJoints } from "./robotModelLogic"
10
10
 
@@ -21,73 +21,55 @@ export default function RobotAnimator({
21
21
  onRotationChanged,
22
22
  children,
23
23
  }: RobotAnimatorProps) {
24
- Globals.assign({ frameLoop: "always" })
25
24
  const jointObjects = useRef<Object3D[]>([])
26
25
  const { invalidate } = useThree()
27
26
 
28
27
  function setGroupRef(group: Group | null) {
29
28
  if (!group) return
30
-
31
29
  jointObjects.current = collectJoints(group)
32
30
 
33
- // Set initial position
34
- setRotation()
35
- invalidate()
36
- }
37
-
38
- // Extract joint values
39
- const jointValues = useMemo(
40
- () =>
41
- rapidlyChangingMotionState.state.joint_position.joints.filter(
42
- (item) => item !== undefined,
43
- ),
44
- [rapidlyChangingMotionState.state.joint_position.joints],
45
- )
46
-
47
- // Create dynamic spring configuration based on actual number of joints
48
- const springConfig = useMemo(() => {
49
- const config: any = {
50
- config: {
51
- tension: 120,
52
- friction: 20,
53
- },
54
- onChange: () => {
55
- // This is critical: trigger setRotation when spring values change
56
- setRotation()
57
- invalidate()
58
- },
31
+ // Trigger callback if provided
32
+ if (onRotationChanged) {
33
+ const joints = rapidlyChangingMotionState.state.joint_position.joints
34
+ const filteredJoints = joints.filter((item) => item !== undefined)
35
+ onRotationChanged(jointObjects.current, filteredJoints)
59
36
  }
37
+ }
60
38
 
61
- // Add joint values dynamically based on dhParameters length
62
- dhParameters.forEach((_, index) => {
63
- config[`joint${index}`] = jointValues[index] || 0
64
- })
65
-
66
- return config
67
- }, [jointValues, dhParameters.length])
39
+ // Get current joint values
40
+ const joints = rapidlyChangingMotionState.state.joint_position.joints
41
+ const filteredJoints = joints.filter((item) => item !== undefined)
68
42
 
69
- const axisValues = useSpring(springConfig)
43
+ // Create spring values for each joint - declarative approach
44
+ const springValues: Record<string, number> = {}
45
+ for (
46
+ let index = 0;
47
+ index < Math.min(dhParameters.length, filteredJoints.length);
48
+ index++
49
+ ) {
50
+ const dhParam = dhParameters[index]
51
+ const jointValue = filteredJoints[index]!
52
+ const rotationOffset = dhParam.theta || 0
53
+ const rotationSign = dhParam.reverse_rotation_direction ? -1 : 1
70
54
 
71
- // Update setRotation to use the dynamic spring values
72
- function setRotation() {
73
- const updatedJointValues = dhParameters.map((_, index) => {
74
- const springValue = (axisValues as any)[`joint${index}`]
75
- return springValue ? springValue.get() : 0
76
- })
55
+ springValues[`joint${index}`] = rotationSign * jointValue + rotationOffset
56
+ }
77
57
 
78
- if (onRotationChanged) {
79
- onRotationChanged(jointObjects.current, updatedJointValues)
80
- } else {
58
+ // Declarative spring that automatically animates when target values change
59
+ const jointRotations = useSpring({
60
+ ...springValues,
61
+ config: { tension: 120, friction: 20 }, // Smooth but responsive
62
+ onChange: () => {
63
+ // Apply rotations to joint objects
81
64
  for (const [index, object] of jointObjects.current.entries()) {
82
- const dhParam = dhParameters[index]
83
- const rotationOffset = dhParam.theta || 0
84
- const rotationSign = dhParam.reverse_rotation_direction ? -1 : 1
85
-
86
- object.rotation.y =
87
- rotationSign * updatedJointValues[index]! + rotationOffset
65
+ const jointKey = `joint${index}`
66
+ if (jointKey in jointRotations) {
67
+ object.rotation.y = (jointRotations as any)[jointKey].get()
68
+ }
88
69
  }
89
- }
90
- }
70
+ invalidate()
71
+ },
72
+ })
91
73
 
92
74
  return <group ref={setGroupRef}>{children}</group>
93
75
  }