@aiready/components 0.14.2 → 0.14.3

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.
@@ -37,13 +37,14 @@ interface UseForceSimulationReturn {
37
37
  alpha: number;
38
38
  }
39
39
 
40
- declare function useForceSimulation(initialNodes: SimulationNode[], initialLinks: SimulationLink[], options: ForceSimulationOptions): UseForceSimulationReturn & {
40
+ interface UseForceSimulationReturnExt extends UseForceSimulationReturn {
41
41
  setForcesEnabled: (enabled: boolean) => void;
42
- };
42
+ }
43
+ declare function useForceSimulation(initialNodes: SimulationNode[], initialLinks: SimulationLink[], options: ForceSimulationOptions): UseForceSimulationReturnExt;
43
44
  declare function useDrag(simulation: d3.Simulation<SimulationNode, any> | null | undefined): {
44
45
  onDragStart: (event: any, node: SimulationNode) => void;
45
46
  onDrag: (event: any, node: SimulationNode) => void;
46
47
  onDragEnd: (event: any, node: SimulationNode) => void;
47
48
  };
48
49
 
49
- export { type ForceSimulationOptions as F, type SimulationLink as S, type UseForceSimulationReturn as U, type SimulationNode as a, useForceSimulation as b, useDrag as u };
50
+ export { type ForceSimulationOptions as F, type SimulationLink as S, type UseForceSimulationReturn as U, type SimulationNode as a, useForceSimulation as b, type UseForceSimulationReturnExt as c, useDrag as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiready/components",
3
- "version": "0.14.2",
3
+ "version": "0.14.3",
4
4
  "description": "Unified shared components library (UI, charts, hooks, utilities) for AIReady",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -128,7 +128,7 @@
128
128
  "framer-motion": "^12.35.0",
129
129
  "lucide-react": "^0.577.0",
130
130
  "tailwind-merge": "^3.0.0",
131
- "@aiready/core": "0.24.3"
131
+ "@aiready/core": "0.24.4"
132
132
  },
133
133
  "devDependencies": {
134
134
  "@testing-library/jest-dom": "^6.6.5",
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Constants for force simulation defaults and configuration
3
+ */
4
+ export const SIMULATION_DEFAULTS = {
5
+ CHARGE_STRENGTH: -300,
6
+ LINK_DISTANCE: 100,
7
+ LINK_STRENGTH: 1,
8
+ COLLISION_STRENGTH: 1,
9
+ COLLISION_RADIUS: 10,
10
+ CENTER_STRENGTH: 0.1,
11
+ ALPHA_DECAY: 0.0228,
12
+ VELOCITY_DECAY: 0.4,
13
+ ALPHA_TARGET: 0,
14
+ WARM_ALPHA: 0.3,
15
+ ALPHA_MIN: 0.01,
16
+ TICK_THROTTLE_MS: 33, // ~30 fps
17
+ MAX_SIMULATION_TIME_MS: 3000,
18
+ STABILIZE_ON_STOP: true,
19
+ } as const;
20
+
21
+ /**
22
+ * Force names used in d3-force
23
+ */
24
+ export const FORCE_NAMES = {
25
+ LINK: 'link',
26
+ CHARGE: 'charge',
27
+ CENTER: 'center',
28
+ COLLISION: 'collision',
29
+ X: 'x',
30
+ Y: 'y',
31
+ } as const;
32
+
33
+ /**
34
+ * Event names used in d3-force
35
+ */
36
+ export const EVENT_NAMES = {
37
+ TICK: 'tick',
38
+ END: 'end',
39
+ } as const;
@@ -6,13 +6,33 @@ import { SimulationNode } from './simulation-types';
6
6
  */
7
7
  export function stabilizeNodes(nodes: SimulationNode[]): void {
8
8
  nodes.forEach((n) => {
9
- (n as any).vx = 0;
10
- (n as any).vy = 0;
9
+ n.vx = 0;
10
+ n.vy = 0;
11
11
  if (typeof n.x === 'number') n.x = Number(n.x.toFixed(3));
12
12
  if (typeof n.y === 'number') n.y = Number(n.y.toFixed(3));
13
13
  });
14
14
  }
15
15
 
16
+ /**
17
+ * Seeds nodes with initial positions in a spread circle.
18
+ * This ensures nodes don't stack at origin and have some initial velocity.
19
+ */
20
+ export function seedCircularPositions(
21
+ nodes: SimulationNode[],
22
+ width: number,
23
+ height: number
24
+ ): void {
25
+ const radius = Math.min(width, height) * 0.45;
26
+ nodes.forEach((n, i) => {
27
+ const angle = (i * 2 * Math.PI) / nodes.length;
28
+ n.x = width / 2 + radius * Math.cos(angle);
29
+ n.y = height / 2 + radius * Math.sin(angle);
30
+ // Add very small random velocity to avoid large initial motion
31
+ n.vx = (Math.random() - 0.5) * 2;
32
+ n.vy = (Math.random() - 0.5) * 2;
33
+ });
34
+ }
35
+
16
36
  /**
17
37
  * Seeds nodes with random positions within bounds.
18
38
  * Used as fallback when initial positioning fails.
@@ -25,7 +45,25 @@ export function seedRandomPositions(
25
45
  nodes.forEach((n) => {
26
46
  n.x = Math.random() * width;
27
47
  n.y = Math.random() * height;
28
- (n as any).vx = (Math.random() - 0.5) * 10;
29
- (n as any).vy = (Math.random() - 0.5) * 10;
48
+ n.vx = (Math.random() - 0.5) * 10;
49
+ n.vy = (Math.random() - 0.5) * 10;
30
50
  });
31
51
  }
52
+
53
+ /**
54
+ * Safely stops a d3 simulation and performs cleanup tasks.
55
+ */
56
+ export function safelyStopSimulation(
57
+ simulation: d3.Simulation<SimulationNode, any>,
58
+ nodes: SimulationNode[],
59
+ options: { stabilize?: boolean } = {}
60
+ ): void {
61
+ try {
62
+ if (options.stabilize) {
63
+ stabilizeNodes(nodes);
64
+ }
65
+ simulation.stop();
66
+ } catch (error) {
67
+ console.warn('AIReady: Failed to stop simulation safely:', error);
68
+ }
69
+ }