@wandelbots/wandelbots-js-react-components 4.1.0 → 4.1.1-pr.feat-RB-3107-motion-group-visualizer.510.3add2c2

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.
@@ -0,0 +1,73 @@
1
+ import React, { useCallback, useEffect, useMemo, useState } from "react"
2
+ import { NovaClient } from "@wandelbots/nova-js/v2"
3
+ import type { KinematicModel } from "@wandelbots/nova-api/v2"
4
+
5
+ import { externalizeComponent } from "../../externalizeComponent"
6
+ import { SupportedRobot, type SupportedRobotProps } from "./SupportedRobot"
7
+ import { SupportedLinearAxis, type SupportedLinearAxisProps } from "./SupportedLinearAxis"
8
+
9
+ export type MotionGroupVisualizerProps = {
10
+ inverseSolver?: string | null
11
+ } & (SupportedRobotProps | SupportedLinearAxisProps)
12
+
13
+ export const MotionGroupVisualizer: React.FC<MotionGroupVisualizerProps> = externalizeComponent((props: MotionGroupVisualizerProps) => {
14
+ const { inverseSolver: inverseSolverProp, modelFromController, ...rest } = props
15
+
16
+ const [inverseSolver, setInverseSolver] = useState<string | null | undefined>(inverseSolverProp)
17
+
18
+ const instanceUrl = import.meta.env.WANDELAPI_BASE_URL
19
+
20
+ /**
21
+ * Fetches the kinematic model from the API and saved the inverse_solver property, which defined
22
+ * whether the motion group should be displayed as a robot or linear axis
23
+ */
24
+ const fetchKinematicModel = useCallback(async () => {
25
+ const nova = new NovaClient({ instanceUrl })
26
+ try {
27
+ const kinematicModel: KinematicModel = await nova.api.motionGroupModels.getMotionGroupKinematicModel(
28
+ modelFromController,
29
+ )
30
+
31
+ setInverseSolver(kinematicModel.inverse_solver)
32
+ } catch (err) {
33
+ console.warn(`Failed to fetch kinematic model from API for ${modelFromController}, falling back to local config`)
34
+ }
35
+ }, [modelFromController, instanceUrl])
36
+
37
+ /**
38
+ * Checks the delivered inverseSolver property from the consumer
39
+ * Defined, Null - carry on with the delivered value - the inverseSolver was fetched via consumer app
40
+ * Undefined - carry out a request to fetch the inverseSolver value
41
+ */
42
+ useEffect(() => {
43
+ if (inverseSolverProp === undefined && modelFromController && instanceUrl) {
44
+ fetchKinematicModel()
45
+ }
46
+ }, [inverseSolverProp, modelFromController, fetchKinematicModel, instanceUrl])
47
+
48
+ /**
49
+ * The turntable models return inverseSolver = null - however these models
50
+ * should be rendered with SupportedRobot instead of SupportedLinearAxis
51
+ *
52
+ * In the future - this hard coded check would be replaced with a new "type" property
53
+ * from dh parameters
54
+ */
55
+ const isTurnTable = useMemo(() => {
56
+ return ["KUKA_DK250", "KUKA_DK500", "YASKAWA_TURN1", "YASKAWA_TURN2", "YASKAWA_TURN3"]
57
+ .includes(modelFromController)
58
+ }, [modelFromController])
59
+
60
+ if (inverseSolver || isTurnTable) {
61
+ return (
62
+ <SupportedRobot modelFromController={modelFromController} {...rest} />
63
+ )
64
+ }
65
+
66
+ if (inverseSolver === null) {
67
+ return (
68
+ <SupportedLinearAxis modelFromController={modelFromController} {...rest} />
69
+ )
70
+ }
71
+
72
+ return null
73
+ })