@sqlrooms/cosmos 0.4.2 → 0.5.1

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.
Files changed (57) hide show
  1. package/dist/CosmosGraph.d.ts +3 -3
  2. package/dist/CosmosGraph.d.ts.map +1 -1
  3. package/dist/CosmosGraph.js +51 -13
  4. package/dist/CosmosGraph.js.map +1 -1
  5. package/dist/CosmosGraphControls.d.ts +1 -1
  6. package/dist/CosmosGraphControls.js +4 -4
  7. package/dist/CosmosGraphControls.js.map +1 -1
  8. package/dist/CosmosSimulationControls.d.ts +1 -5
  9. package/dist/CosmosSimulationControls.d.ts.map +1 -1
  10. package/dist/CosmosSimulationControls.js +7 -28
  11. package/dist/CosmosSimulationControls.js.map +1 -1
  12. package/dist/CosmosSlice.d.ts +75 -0
  13. package/dist/CosmosSlice.d.ts.map +1 -0
  14. package/dist/CosmosSlice.js +153 -0
  15. package/dist/CosmosSlice.js.map +1 -0
  16. package/dist/CosmosSliceConfig.d.ts +209 -0
  17. package/dist/CosmosSliceConfig.d.ts.map +1 -0
  18. package/dist/CosmosSliceConfig.js +187 -0
  19. package/dist/CosmosSliceConfig.js.map +1 -0
  20. package/dist/hooks/useHoverState.d.ts +11 -8
  21. package/dist/hooks/useHoverState.d.ts.map +1 -1
  22. package/dist/hooks/useHoverState.js +13 -9
  23. package/dist/hooks/useHoverState.js.map +1 -1
  24. package/dist/index.d.ts +61 -27
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +62 -26
  27. package/dist/index.js.map +1 -1
  28. package/dist/utils/coordinates.js.map +1 -1
  29. package/package.json +8 -4
  30. package/dist/CosmosGraphContext.d.ts +0 -76
  31. package/dist/CosmosGraphContext.d.ts.map +0 -1
  32. package/dist/CosmosGraphContext.js +0 -95
  33. package/dist/CosmosGraphContext.js.map +0 -1
  34. package/dist/components/CosmosGraph.d.ts +0 -14
  35. package/dist/components/CosmosGraph.d.ts.map +0 -1
  36. package/dist/components/CosmosGraph.js +0 -130
  37. package/dist/components/CosmosGraph.js.map +0 -1
  38. package/dist/config.d.ts +0 -24
  39. package/dist/config.d.ts.map +0 -1
  40. package/dist/config.js +0 -18
  41. package/dist/config.js.map +0 -1
  42. package/dist/hooks/index.d.ts +0 -29
  43. package/dist/hooks/index.d.ts.map +0 -1
  44. package/dist/hooks/index.js +0 -29
  45. package/dist/hooks/index.js.map +0 -1
  46. package/dist/hooks/useGraph.d.ts +0 -49
  47. package/dist/hooks/useGraph.d.ts.map +0 -1
  48. package/dist/hooks/useGraph.js +0 -93
  49. package/dist/hooks/useGraph.js.map +0 -1
  50. package/dist/hooks/useGraphConfig.d.ts +0 -101
  51. package/dist/hooks/useGraphConfig.d.ts.map +0 -1
  52. package/dist/hooks/useGraphConfig.js +0 -53
  53. package/dist/hooks/useGraphConfig.js.map +0 -1
  54. package/dist/hooks/useRelativeCoordinates.d.ts +0 -2
  55. package/dist/hooks/useRelativeCoordinates.d.ts.map +0 -1
  56. package/dist/hooks/useRelativeCoordinates.js +0 -10
  57. package/dist/hooks/useRelativeCoordinates.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CosmosSlice.js","sourceRoot":"","sources":["../src/CosmosSlice.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAC,KAAK,EAAuB,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EACL,WAAW,EACX,mBAAmB,GAEpB,MAAM,2BAA2B,CAAC;AAInC,OAAO,EAAC,OAAO,EAAC,MAAM,OAAO,CAAC;AAoD9B;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB;IAC/B,OAAO,WAAW,CAChB,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QACb,MAAM,EAAE;YACN,KAAK,EAAE,IAAI;YACX,mBAAmB,EAAE,IAAI;YAEzB,WAAW,EAAE,CAAC,SAAyB,EAAE,EAAE;gBACzC,kCAAkC;gBAClC,MAAM,QAAQ,GAAG,GAAG,E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AC;gBAC7B,IAAI,CAAC,KAAK;oBAAE,OAAO;gBACnB,KAAK,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC;YACtC,CAAC;YAED,YAAY,EAAE,CAAC,KAAK,EAAE,EAAE;gBACtB,MAAM,EAAC,KAAK,EAAC,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC;gBAC7B,IAAI,CAAC,KAAK;oBAAE,OAAO;gBACnB,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;YAC5B,CAAC;YAED,YAAY,EAAE,GAAG,EAAE;gBACjB,MAAM,EAAC,KAAK,EAAC,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC;gBAC7B,IAAI,CAAC,KAAK;oBAAE,OAAO;gBACnB,2CAA2C;gBAC3C,IAAK,KAAa,CAAC,KAAK,CAAC,GAAG,EAAE,UAAU,EAAE,CAAC;oBACxC,KAAa,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,GAAG,EAAE,CAAC;gBAC1C,CAAC;gBACD,KAAK,CAAC,KAAK,EAAE,CAAC;gBACd,KAAK,CAAC,OAAO,EAAE,CAAC;gBAChB,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACZ,OAAO,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE;oBACvB,KAAK,CAAC,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC;oBAC1B,KAAK,CAAC,MAAM,CAAC,mBAAmB,GAAG,KAAK,CAAC;gBAC3C,CAAC,CAAC,CACH,CAAC;YACJ,CAAC;SACF;KACF,CAAC,CACH,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,kBAAkB,CAChC,QAA8C;IAE9C,OAAO,mBAAmB,CAIxB,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,KAA+B,CAAC,CAAC,CAAC;AAC1D,CAAC","sourcesContent":["/**\n * @fileoverview Cosmos graph visualization state management using Zustand.\n * This module provides state management and control functions for the Cosmos graph visualization.\n */\n\nimport {Graph, GraphConfigInterface} from '@cosmograph/cosmos';\nimport {\n createSlice,\n useBaseProjectStore,\n type ProjectState,\n} from '@sqlrooms/project-builder';\nimport type {BaseProjectConfig} from '@sqlrooms/project-config';\nimport type {StateCreator} from 'zustand';\nimport {CosmosSliceConfig} from './CosmosSliceConfig';\nimport {produce} from 'immer';\n\n/**\n * Core state interface for the Cosmos graph visualization.\n * Contains the graph instance, simulation state, and all control functions.\n */\nexport type CosmosSliceState = {\n cosmos: {\n /** The current graph instance */\n graph: Graph | null;\n /** Whether the physics simulation is currently running */\n isSimulationRunning: boolean;\n /** Creates a new graph instance in the specified container */\n createGraph: (container: HTMLDivElement) => void;\n /** Toggles the physics simulation on/off */\n toggleSimulation: () => void;\n /** Adjusts the view to fit all nodes */\n fitView: () => void;\n /** Starts the simulation with initial energy */\n startWithEnergy: () => void;\n /** Cleans up and removes the current graph */\n destroyGraph: () => void;\n /** Updates the simulation configuration parameters */\n updateSimulationConfig: (\n config: Partial<CosmosSliceConfig['cosmos']>,\n ) => void;\n /** Updates the graph's visual configuration */\n updateGraphConfig: (config: Partial<GraphConfigInterface>) => void;\n /** Updates the graph's data (points, links, colors, etc.) */\n updateGraphData: (data: {\n pointPositions?: Float32Array;\n pointColors?: Float32Array;\n pointSizes?: Float32Array;\n linkIndexes?: Float32Array;\n linkColors?: Float32Array;\n }) => void;\n /** Sets the currently focused point by its index */\n setFocusedPoint: (index: number | undefined) => void;\n /** Sets the zoom level of the graph view */\n setZoomLevel: (level: number) => void;\n };\n};\n\n/**\n * Combined type representing the full project state including Cosmos functionality.\n * Merges the base project state with Cosmos-specific state and configuration.\n */\nexport type ProjectStateWithCosmos = ProjectState<\n BaseProjectConfig & CosmosSliceConfig\n> &\n CosmosSliceState;\n\n/**\n * Creates a Zustand slice for managing Cosmos graph state.\n * This slice handles graph creation, destruction, configuration, and data updates.\n *\n * @returns A state creator function for the Cosmos slice\n */\nexport function createCosmosSlice(): StateCreator<CosmosSliceState> {\n return createSlice<BaseProjectConfig & CosmosSliceConfig, CosmosSliceState>(\n (set, get) => ({\n cosmos: {\n graph: null,\n isSimulationRunning: true,\n\n createGraph: (container: HTMLDivElement) => {\n // Clean up old graph if it exists\n const oldGraph = get().cosmos.graph;\n if (oldGraph) {\n oldGraph.pause();\n oldGraph.destroy();\n }\n\n // Create and configure new graph\n const graph = new Graph(container);\n const config = get().project.config.cosmos;\n graph.setConfig(config);\n graph.start();\n\n set((state) =>\n produce(state, (draft) => {\n draft.cosmos.graph = graph;\n }),\n );\n },\n\n toggleSimulation: () => {\n const {graph} = get().cosmos;\n if (!graph) return;\n\n if (graph.isSimulationRunning) {\n graph.pause();\n set((state) =>\n produce(state, (draft) => {\n draft.cosmos.isSimulationRunning = false;\n }),\n );\n } else {\n graph.restart();\n set((state) =>\n produce(state, (draft) => {\n draft.cosmos.isSimulationRunning = true;\n }),\n );\n }\n },\n\n fitView: () => {\n const {graph} = get().cosmos;\n if (!graph) return;\n graph.fitView();\n },\n\n startWithEnergy: () => {\n const {graph} = get().cosmos;\n if (!graph) return;\n graph.start(1);\n set((state) =>\n produce(state, (draft) => {\n draft.cosmos.isSimulationRunning = true;\n }),\n );\n },\n\n updateSimulationConfig: (\n config: Partial<CosmosSliceConfig['cosmos']>,\n ) => {\n const {graph} = get().cosmos;\n\n set((state) =>\n produce(state, (draft) => {\n Object.assign(draft.project.config.cosmos, config);\n if (graph) {\n graph.setConfig(draft.project.config.cosmos);\n }\n }),\n );\n },\n\n updateGraphConfig: (config: Partial<GraphConfigInterface>) => {\n const {graph} = get().cosmos;\n\n set((state) =>\n produce(state, (draft) => {\n Object.assign(draft.project.config.cosmos, config);\n if (graph) {\n graph.setConfig(draft.project.config.cosmos);\n }\n }),\n );\n },\n\n updateGraphData: (data) => {\n const {graph} = get().cosmos;\n if (!graph) return;\n\n if (data.pointPositions) {\n graph.setPointPositions(data.pointPositions);\n }\n if (data.pointColors) {\n graph.setPointColors(data.pointColors);\n }\n if (data.pointSizes) {\n graph.setPointSizes(data.pointSizes);\n }\n if (data.linkIndexes) {\n graph.setLinks(data.linkIndexes);\n }\n if (data.linkColors) {\n graph.setLinkColors(data.linkColors);\n }\n\n graph.render();\n },\n\n setFocusedPoint: (index) => {\n const {graph} = get().cosmos;\n if (!graph) return;\n graph.setFocusedPointByIndex(index);\n },\n\n setZoomLevel: (level) => {\n const {graph} = get().cosmos;\n if (!graph) return;\n graph.setZoomLevel(level);\n },\n\n destroyGraph: () => {\n const {graph} = get().cosmos;\n if (!graph) return;\n // TODO: this should be happening in cosmos\n if ((graph as any).store.div?.firstChild) {\n (graph as any).store.div.innerHTML = '';\n }\n graph.pause();\n graph.destroy();\n set((state) =>\n produce(state, (draft) => {\n draft.cosmos.graph = null;\n draft.cosmos.isSimulationRunning = false;\n }),\n );\n },\n },\n }),\n );\n}\n\n/**\n * Hook to access the Cosmos store with proper typing.\n * Provides type-safe access to the combined project and Cosmos state.\n *\n * @template T The type of the selected state slice\n * @param selector A function that selects a portion of the state\n * @returns The selected state portion\n *\n * @example\n * ```typescript\n * const graph = useStoreWithCosmos(state => state.cosmos.graph);\n * const isRunning = useStoreWithCosmos(state => state.cosmos.isSimulationRunning);\n * ```\n */\nexport function useStoreWithCosmos<T>(\n selector: (state: ProjectStateWithCosmos) => T,\n): T {\n return useBaseProjectStore<\n BaseProjectConfig & CosmosSliceConfig,\n ProjectStateWithCosmos,\n T\n >((state) => selector(state as ProjectStateWithCosmos));\n}\n"]}
@@ -0,0 +1,209 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Zod schema for validating and configuring the Cosmos graph visualization.
4
+ * This schema defines all available configuration options and their types.
5
+ *
6
+ * The configuration is divided into several categories:
7
+ *
8
+ * Node Appearance:
9
+ * - `pointSizeScale`: Controls the size of nodes
10
+ * - `scalePointsOnZoom`: Enables dynamic node sizing based on zoom level
11
+ *
12
+ * Link Appearance:
13
+ * - `renderLinks`: Toggles link visibility
14
+ * - `linkWidthScale`: Controls link thickness
15
+ * - `linkArrows`: Toggles directional arrows
16
+ * - `linkArrowsSizeScale`: Controls arrow size
17
+ * - `curvedLinks`: Toggles curved/straight links
18
+ *
19
+ * Physics Simulation:
20
+ * - `simulationGravity`: Central gravitational force (0.25)
21
+ * - `simulationRepulsion`: Node repulsion force (1.0)
22
+ * - `simulationLinkSpring`: Link spring force (1.0)
23
+ * - `simulationLinkDistance`: Natural link length (10)
24
+ * - `simulationFriction`: Movement damping (0.85)
25
+ * - `simulationDecay`: Simulation cooling rate (1000)
26
+ *
27
+ * @example Basic configuration
28
+ * ```typescript
29
+ * const config: CosmosSliceConfig = {
30
+ * cosmos: {
31
+ * pointSizeScale: 1.2,
32
+ * scalePointsOnZoom: true,
33
+ * renderLinks: true,
34
+ * linkWidthScale: 1.5,
35
+ * simulationGravity: 0.25
36
+ * }
37
+ * };
38
+ * ```
39
+ *
40
+ * @example Directed graph with curved links
41
+ * ```typescript
42
+ * const directedGraphConfig: CosmosSliceConfig = {
43
+ * cosmos: {
44
+ * linkArrows: true,
45
+ * linkArrowsSizeScale: 1.2,
46
+ * curvedLinks: true,
47
+ * simulationLinkDistance: 15,
48
+ * simulationLinkSpring: 1.2
49
+ * }
50
+ * };
51
+ * ```
52
+ *
53
+ * @example High-performance configuration for large graphs
54
+ * ```typescript
55
+ * const largeGraphConfig: CosmosSliceConfig = {
56
+ * cosmos: {
57
+ * simulationGravity: 0.1,
58
+ * simulationRepulsion: 0.8,
59
+ * simulationFriction: 0.9,
60
+ * simulationDecay: 2000,
61
+ * scalePointsOnZoom: false
62
+ * }
63
+ * };
64
+ * ```
65
+ */
66
+ export declare const CosmosSliceConfig: z.ZodObject<{
67
+ cosmos: z.ZodObject<{
68
+ /**
69
+ * Scale factor for point (node) sizes in the graph.
70
+ * Values > 1 make nodes larger, values < 1 make them smaller.
71
+ * @default 1.1
72
+ */
73
+ pointSizeScale: z.ZodNumber;
74
+ /**
75
+ * When true, nodes will dynamically resize based on the current zoom level.
76
+ * This helps maintain visual clarity at different zoom levels.
77
+ * @default true
78
+ */
79
+ scalePointsOnZoom: z.ZodBoolean;
80
+ /**
81
+ * Controls whether links (edges) between nodes are displayed.
82
+ * @default true
83
+ */
84
+ renderLinks: z.ZodBoolean;
85
+ /**
86
+ * Scale factor for link (edge) width.
87
+ * Values > 1 make links thicker, values < 1 make them thinner.
88
+ * @default 1
89
+ */
90
+ linkWidthScale: z.ZodNumber;
91
+ /**
92
+ * Scale factor for the size of directional arrows on links.
93
+ * Only applies when linkArrows is true.
94
+ * @default 1
95
+ */
96
+ linkArrowsSizeScale: z.ZodNumber;
97
+ /**
98
+ * When true, displays arrows indicating link direction.
99
+ * Useful for directed graphs.
100
+ * @default false
101
+ */
102
+ linkArrows: z.ZodBoolean;
103
+ /**
104
+ * When true, links are rendered as curved Bezier paths.
105
+ * When false, links are straight lines.
106
+ * @default false
107
+ */
108
+ curvedLinks: z.ZodBoolean;
109
+ /**
110
+ * Controls the strength of the central gravitational force.
111
+ * Higher values pull nodes more strongly toward the center.
112
+ * @default 0.25
113
+ */
114
+ simulationGravity: z.ZodNumber;
115
+ /**
116
+ * Controls how strongly nodes repel each other.
117
+ * Higher values create more space between unconnected nodes.
118
+ * @default 1.0
119
+ */
120
+ simulationRepulsion: z.ZodNumber;
121
+ /**
122
+ * Controls the strength of the spring force between linked nodes.
123
+ * Higher values pull connected nodes more tightly together.
124
+ * @default 1.0
125
+ */
126
+ simulationLinkSpring: z.ZodNumber;
127
+ /**
128
+ * The natural or resting length of links between nodes.
129
+ * Higher values create more spacing between connected nodes.
130
+ * @default 10
131
+ */
132
+ simulationLinkDistance: z.ZodNumber;
133
+ /**
134
+ * Controls how quickly node movement decays.
135
+ * Higher values (closer to 1) create more damped movement.
136
+ * @default 0.85
137
+ */
138
+ simulationFriction: z.ZodNumber;
139
+ /**
140
+ * Controls how quickly the simulation stabilizes.
141
+ * Lower values result in longer, smoother transitions.
142
+ * @default 1000
143
+ */
144
+ simulationDecay: z.ZodNumber;
145
+ }, "strip", z.ZodTypeAny, {
146
+ pointSizeScale: number;
147
+ renderLinks: boolean;
148
+ linkWidthScale: number;
149
+ curvedLinks: boolean;
150
+ linkArrows: boolean;
151
+ linkArrowsSizeScale: number;
152
+ simulationDecay: number;
153
+ simulationGravity: number;
154
+ simulationRepulsion: number;
155
+ simulationLinkSpring: number;
156
+ simulationLinkDistance: number;
157
+ simulationFriction: number;
158
+ scalePointsOnZoom: boolean;
159
+ }, {
160
+ pointSizeScale: number;
161
+ renderLinks: boolean;
162
+ linkWidthScale: number;
163
+ curvedLinks: boolean;
164
+ linkArrows: boolean;
165
+ linkArrowsSizeScale: number;
166
+ simulationDecay: number;
167
+ simulationGravity: number;
168
+ simulationRepulsion: number;
169
+ simulationLinkSpring: number;
170
+ simulationLinkDistance: number;
171
+ simulationFriction: number;
172
+ scalePointsOnZoom: boolean;
173
+ }>;
174
+ }, "strip", z.ZodTypeAny, {
175
+ cosmos: {
176
+ pointSizeScale: number;
177
+ renderLinks: boolean;
178
+ linkWidthScale: number;
179
+ curvedLinks: boolean;
180
+ linkArrows: boolean;
181
+ linkArrowsSizeScale: number;
182
+ simulationDecay: number;
183
+ simulationGravity: number;
184
+ simulationRepulsion: number;
185
+ simulationLinkSpring: number;
186
+ simulationLinkDistance: number;
187
+ simulationFriction: number;
188
+ scalePointsOnZoom: boolean;
189
+ };
190
+ }, {
191
+ cosmos: {
192
+ pointSizeScale: number;
193
+ renderLinks: boolean;
194
+ linkWidthScale: number;
195
+ curvedLinks: boolean;
196
+ linkArrows: boolean;
197
+ linkArrowsSizeScale: number;
198
+ simulationDecay: number;
199
+ simulationGravity: number;
200
+ simulationRepulsion: number;
201
+ simulationLinkSpring: number;
202
+ simulationLinkDistance: number;
203
+ simulationFriction: number;
204
+ scalePointsOnZoom: boolean;
205
+ };
206
+ }>;
207
+ export type CosmosSliceConfig = z.infer<typeof CosmosSliceConfig>;
208
+ export declare function createDefaultCosmosConfig(): CosmosSliceConfig;
209
+ //# sourceMappingURL=CosmosSliceConfig.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CosmosSliceConfig.d.ts","sourceRoot":"","sources":["../src/CosmosSliceConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAuBtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,eAAO,MAAM,iBAAiB;;QAE1B;;;;WAIG;;QAGH;;;;WAIG;;QAKH;;;WAGG;;QAGH;;;;WAIG;;QAGH;;;;WAIG;;QAKH;;;;WAIG;;QAKH;;;;WAIG;;QAOH;;;;WAIG;;QAGH;;;;WAIG;;QAGH;;;;WAIG;;QAKH;;;;WAIG;;QAKH;;;;WAIG;;QAKH;;;;WAIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAOL,CAAC;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAElE,wBAAgB,yBAAyB,IAAI,iBAAiB,CAI7D"}
@@ -0,0 +1,187 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Default configuration values for the Cosmos graph visualization.
4
+ * These values provide a balanced starting point for most graph visualizations.
5
+ */
6
+ const DEFAULT_COSMOS_CONFIG = {
7
+ pointSizeScale: 1.1,
8
+ scalePointsOnZoom: true,
9
+ simulationGravity: 0.25,
10
+ simulationRepulsion: 1.0,
11
+ simulationLinkSpring: 1.0,
12
+ simulationLinkDistance: 10,
13
+ simulationFriction: 0.85,
14
+ simulationDecay: 1000,
15
+ renderLinks: true,
16
+ linkArrows: false,
17
+ curvedLinks: false,
18
+ linkWidthScale: 1,
19
+ linkArrowsSizeScale: 1,
20
+ };
21
+ /**
22
+ * Zod schema for validating and configuring the Cosmos graph visualization.
23
+ * This schema defines all available configuration options and their types.
24
+ *
25
+ * The configuration is divided into several categories:
26
+ *
27
+ * Node Appearance:
28
+ * - `pointSizeScale`: Controls the size of nodes
29
+ * - `scalePointsOnZoom`: Enables dynamic node sizing based on zoom level
30
+ *
31
+ * Link Appearance:
32
+ * - `renderLinks`: Toggles link visibility
33
+ * - `linkWidthScale`: Controls link thickness
34
+ * - `linkArrows`: Toggles directional arrows
35
+ * - `linkArrowsSizeScale`: Controls arrow size
36
+ * - `curvedLinks`: Toggles curved/straight links
37
+ *
38
+ * Physics Simulation:
39
+ * - `simulationGravity`: Central gravitational force (0.25)
40
+ * - `simulationRepulsion`: Node repulsion force (1.0)
41
+ * - `simulationLinkSpring`: Link spring force (1.0)
42
+ * - `simulationLinkDistance`: Natural link length (10)
43
+ * - `simulationFriction`: Movement damping (0.85)
44
+ * - `simulationDecay`: Simulation cooling rate (1000)
45
+ *
46
+ * @example Basic configuration
47
+ * ```typescript
48
+ * const config: CosmosSliceConfig = {
49
+ * cosmos: {
50
+ * pointSizeScale: 1.2,
51
+ * scalePointsOnZoom: true,
52
+ * renderLinks: true,
53
+ * linkWidthScale: 1.5,
54
+ * simulationGravity: 0.25
55
+ * }
56
+ * };
57
+ * ```
58
+ *
59
+ * @example Directed graph with curved links
60
+ * ```typescript
61
+ * const directedGraphConfig: CosmosSliceConfig = {
62
+ * cosmos: {
63
+ * linkArrows: true,
64
+ * linkArrowsSizeScale: 1.2,
65
+ * curvedLinks: true,
66
+ * simulationLinkDistance: 15,
67
+ * simulationLinkSpring: 1.2
68
+ * }
69
+ * };
70
+ * ```
71
+ *
72
+ * @example High-performance configuration for large graphs
73
+ * ```typescript
74
+ * const largeGraphConfig: CosmosSliceConfig = {
75
+ * cosmos: {
76
+ * simulationGravity: 0.1,
77
+ * simulationRepulsion: 0.8,
78
+ * simulationFriction: 0.9,
79
+ * simulationDecay: 2000,
80
+ * scalePointsOnZoom: false
81
+ * }
82
+ * };
83
+ * ```
84
+ */
85
+ export const CosmosSliceConfig = z.object({
86
+ cosmos: z.object({
87
+ /**
88
+ * Scale factor for point (node) sizes in the graph.
89
+ * Values > 1 make nodes larger, values < 1 make them smaller.
90
+ * @default 1.1
91
+ */
92
+ pointSizeScale: z.number().describe('Scale factor for point sizes'),
93
+ /**
94
+ * When true, nodes will dynamically resize based on the current zoom level.
95
+ * This helps maintain visual clarity at different zoom levels.
96
+ * @default true
97
+ */
98
+ scalePointsOnZoom: z
99
+ .boolean()
100
+ .describe('Dynamically resize points based on zoom level'),
101
+ /**
102
+ * Controls whether links (edges) between nodes are displayed.
103
+ * @default true
104
+ */
105
+ renderLinks: z.boolean().describe('Control links displaying'),
106
+ /**
107
+ * Scale factor for link (edge) width.
108
+ * Values > 1 make links thicker, values < 1 make them thinner.
109
+ * @default 1
110
+ */
111
+ linkWidthScale: z.number().describe('Scale factor for link width'),
112
+ /**
113
+ * Scale factor for the size of directional arrows on links.
114
+ * Only applies when linkArrows is true.
115
+ * @default 1
116
+ */
117
+ linkArrowsSizeScale: z
118
+ .number()
119
+ .describe('Scale factor for link arrows size'),
120
+ /**
121
+ * When true, displays arrows indicating link direction.
122
+ * Useful for directed graphs.
123
+ * @default false
124
+ */
125
+ linkArrows: z
126
+ .boolean()
127
+ .describe('Control displaying link direction arrows'),
128
+ /**
129
+ * When true, links are rendered as curved Bezier paths.
130
+ * When false, links are straight lines.
131
+ * @default false
132
+ */
133
+ curvedLinks: z
134
+ .boolean()
135
+ .describe('Render links as curved bezier paths instead of straight lines'),
136
+ /**
137
+ * Controls the strength of the central gravitational force.
138
+ * Higher values pull nodes more strongly toward the center.
139
+ * @default 0.25
140
+ */
141
+ simulationGravity: z.number().describe('Gravity force in the simulation'),
142
+ /**
143
+ * Controls how strongly nodes repel each other.
144
+ * Higher values create more space between unconnected nodes.
145
+ * @default 1.0
146
+ */
147
+ simulationRepulsion: z.number().describe('Repulsion force between nodes'),
148
+ /**
149
+ * Controls the strength of the spring force between linked nodes.
150
+ * Higher values pull connected nodes more tightly together.
151
+ * @default 1.0
152
+ */
153
+ simulationLinkSpring: z
154
+ .number()
155
+ .describe('Spring force for links between nodes'),
156
+ /**
157
+ * The natural or resting length of links between nodes.
158
+ * Higher values create more spacing between connected nodes.
159
+ * @default 10
160
+ */
161
+ simulationLinkDistance: z
162
+ .number()
163
+ .describe('Target distance between linked nodes'),
164
+ /**
165
+ * Controls how quickly node movement decays.
166
+ * Higher values (closer to 1) create more damped movement.
167
+ * @default 0.85
168
+ */
169
+ simulationFriction: z
170
+ .number()
171
+ .describe('Friction coefficient in the simulation'),
172
+ /**
173
+ * Controls how quickly the simulation stabilizes.
174
+ * Lower values result in longer, smoother transitions.
175
+ * @default 1000
176
+ */
177
+ simulationDecay: z
178
+ .number()
179
+ .describe('Decay coefficient in the simulation. Use smaller values if you want the simulation to "cool down" slower.'),
180
+ }),
181
+ });
182
+ export function createDefaultCosmosConfig() {
183
+ return {
184
+ cosmos: DEFAULT_COSMOS_CONFIG,
185
+ };
186
+ }
187
+ //# sourceMappingURL=CosmosSliceConfig.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CosmosSliceConfig.js","sourceRoot":"","sources":["../src/CosmosSliceConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAGtB;;;GAGG;AACH,MAAM,qBAAqB,GAAgC;IACzD,cAAc,EAAE,GAAG;IACnB,iBAAiB,EAAE,IAAI;IACvB,iBAAiB,EAAE,IAAI;IACvB,mBAAmB,EAAE,GAAG;IACxB,oBAAoB,EAAE,GAAG;IACzB,sBAAsB,EAAE,EAAE;IAC1B,kBAAkB,EAAE,IAAI;IACxB,eAAe,EAAE,IAAI;IACrB,WAAW,EAAE,IAAI;IACjB,UAAU,EAAE,KAAK;IACjB,WAAW,EAAE,KAAK;IAClB,cAAc,EAAE,CAAC;IACjB,mBAAmB,EAAE,CAAC;CAC0B,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,MAAM,CAAC,MAAM,iBAAiB,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Q,CACP,2GAA2G,CAC5G;KAC2D,CAAC;CAClE,CAAC,CAAC;AAGH,MAAM,UAAU,yBAAyB;IACvC,OAAO;QACL,MAAM,EAAE,qBAAqB;KAC9B,CAAC;AACJ,CAAC","sourcesContent":["import {z} from 'zod';\nimport {GraphConfigInterface} from '@cosmograph/cosmos';\n\n/**\n * Default configuration values for the Cosmos graph visualization.\n * These values provide a balanced starting point for most graph visualizations.\n */\nconst DEFAULT_COSMOS_CONFIG: CosmosSliceConfig['cosmos'] = {\n pointSizeScale: 1.1,\n scalePointsOnZoom: true,\n simulationGravity: 0.25,\n simulationRepulsion: 1.0,\n simulationLinkSpring: 1.0,\n simulationLinkDistance: 10,\n simulationFriction: 0.85,\n simulationDecay: 1000,\n renderLinks: true,\n linkArrows: false,\n curvedLinks: false,\n linkWidthScale: 1,\n linkArrowsSizeScale: 1,\n} as const satisfies Partial<GraphConfigInterface>;\n\n/**\n * Zod schema for validating and configuring the Cosmos graph visualization.\n * This schema defines all available configuration options and their types.\n *\n * The configuration is divided into several categories:\n *\n * Node Appearance:\n * - `pointSizeScale`: Controls the size of nodes\n * - `scalePointsOnZoom`: Enables dynamic node sizing based on zoom level\n *\n * Link Appearance:\n * - `renderLinks`: Toggles link visibility\n * - `linkWidthScale`: Controls link thickness\n * - `linkArrows`: Toggles directional arrows\n * - `linkArrowsSizeScale`: Controls arrow size\n * - `curvedLinks`: Toggles curved/straight links\n *\n * Physics Simulation:\n * - `simulationGravity`: Central gravitational force (0.25)\n * - `simulationRepulsion`: Node repulsion force (1.0)\n * - `simulationLinkSpring`: Link spring force (1.0)\n * - `simulationLinkDistance`: Natural link length (10)\n * - `simulationFriction`: Movement damping (0.85)\n * - `simulationDecay`: Simulation cooling rate (1000)\n *\n * @example Basic configuration\n * ```typescript\n * const config: CosmosSliceConfig = {\n * cosmos: {\n * pointSizeScale: 1.2,\n * scalePointsOnZoom: true,\n * renderLinks: true,\n * linkWidthScale: 1.5,\n * simulationGravity: 0.25\n * }\n * };\n * ```\n *\n * @example Directed graph with curved links\n * ```typescript\n * const directedGraphConfig: CosmosSliceConfig = {\n * cosmos: {\n * linkArrows: true,\n * linkArrowsSizeScale: 1.2,\n * curvedLinks: true,\n * simulationLinkDistance: 15,\n * simulationLinkSpring: 1.2\n * }\n * };\n * ```\n *\n * @example High-performance configuration for large graphs\n * ```typescript\n * const largeGraphConfig: CosmosSliceConfig = {\n * cosmos: {\n * simulationGravity: 0.1,\n * simulationRepulsion: 0.8,\n * simulationFriction: 0.9,\n * simulationDecay: 2000,\n * scalePointsOnZoom: false\n * }\n * };\n * ```\n */\nexport const CosmosSliceConfig = z.object({\n cosmos: z.object({\n /**\n * Scale factor for point (node) sizes in the graph.\n * Values > 1 make nodes larger, values < 1 make them smaller.\n * @default 1.1\n */\n pointSizeScale: z.number().describe('Scale factor for point sizes'),\n\n /**\n * When true, nodes will dynamically resize based on the current zoom level.\n * This helps maintain visual clarity at different zoom levels.\n * @default true\n */\n scalePointsOnZoom: z\n .boolean()\n .describe('Dynamically resize points based on zoom level'),\n\n /**\n * Controls whether links (edges) between nodes are displayed.\n * @default true\n */\n renderLinks: z.boolean().describe('Control links displaying'),\n\n /**\n * Scale factor for link (edge) width.\n * Values > 1 make links thicker, values < 1 make them thinner.\n * @default 1\n */\n linkWidthScale: z.number().describe('Scale factor for link width'),\n\n /**\n * Scale factor for the size of directional arrows on links.\n * Only applies when linkArrows is true.\n * @default 1\n */\n linkArrowsSizeScale: z\n .number()\n .describe('Scale factor for link arrows size'),\n\n /**\n * When true, displays arrows indicating link direction.\n * Useful for directed graphs.\n * @default false\n */\n linkArrows: z\n .boolean()\n .describe('Control displaying link direction arrows'),\n\n /**\n * When true, links are rendered as curved Bezier paths.\n * When false, links are straight lines.\n * @default false\n */\n curvedLinks: z\n .boolean()\n .describe(\n 'Render links as curved bezier paths instead of straight lines',\n ),\n\n /**\n * Controls the strength of the central gravitational force.\n * Higher values pull nodes more strongly toward the center.\n * @default 0.25\n */\n simulationGravity: z.number().describe('Gravity force in the simulation'),\n\n /**\n * Controls how strongly nodes repel each other.\n * Higher values create more space between unconnected nodes.\n * @default 1.0\n */\n simulationRepulsion: z.number().describe('Repulsion force between nodes'),\n\n /**\n * Controls the strength of the spring force between linked nodes.\n * Higher values pull connected nodes more tightly together.\n * @default 1.0\n */\n simulationLinkSpring: z\n .number()\n .describe('Spring force for links between nodes'),\n\n /**\n * The natural or resting length of links between nodes.\n * Higher values create more spacing between connected nodes.\n * @default 10\n */\n simulationLinkDistance: z\n .number()\n .describe('Target distance between linked nodes'),\n\n /**\n * Controls how quickly node movement decays.\n * Higher values (closer to 1) create more damped movement.\n * @default 0.85\n */\n simulationFriction: z\n .number()\n .describe('Friction coefficient in the simulation'),\n\n /**\n * Controls how quickly the simulation stabilizes.\n * Lower values result in longer, smoother transitions.\n * @default 1000\n */\n simulationDecay: z\n .number()\n .describe(\n 'Decay coefficient in the simulation. Use smaller values if you want the simulation to \"cool down\" slower.',\n ),\n } satisfies Partial<Record<keyof GraphConfigInterface, unknown>>),\n});\nexport type CosmosSliceConfig = z.infer<typeof CosmosSliceConfig>;\n\nexport function createDefaultCosmosConfig(): CosmosSliceConfig {\n return {\n cosmos: DEFAULT_COSMOS_CONFIG,\n };\n}\n"]}
@@ -17,18 +17,18 @@ export type HoverState = {
17
17
  * - Track which point is currently being hovered
18
18
  * - Store the hover position coordinates
19
19
  * - Clear the hover state
20
+ * - Provide event handlers for hover-related graph interactions
20
21
  *
21
22
  * @example
22
23
  * ```tsx
23
24
  * const Graph = () => {
24
25
  * const calcRelativeCoords = useRelativeCoordinates(containerRef);
25
- * const { hoveredPoint, onPointMouseOver, clearHoverState } = useHoverState(calcRelativeCoords);
26
+ * const { hoveredPoint, eventHandlers } = useHoverState(calcRelativeCoords);
26
27
  *
27
28
  * return (
28
29
  * <div ref={containerRef}>
29
30
  * <CosmosGraph
30
- * onPointMouseOver={onPointMouseOver}
31
- * onPointMouseOut={clearHoverState}
31
+ * config={eventHandlers}
32
32
  * />
33
33
  * {hoveredPoint && (
34
34
  * <Tooltip
@@ -42,14 +42,17 @@ export type HoverState = {
42
42
  * ```
43
43
  *
44
44
  * @param calcRelativeCoordinates - A function that converts client coordinates to container-relative coordinates
45
- * @returns An object containing the hover state and handlers
45
+ * @returns An object containing the hover state and event handlers for the graph
46
46
  */
47
47
  export declare const useHoverState: (calcRelativeCoordinates: ReturnType<typeof useRelativeCoordinates>) => {
48
48
  /** The current hover state, containing the index and position of the hovered point, or null if no point is hovered */
49
49
  hoveredPoint: HoverState;
50
- /** Handler to be called when a point is hovered. Updates the hover state with the point's index and position */
51
- onPointMouseOver: (index: number, pointPosition: [number, number], event: MouseEvent | D3DragEvent<HTMLCanvasElement, undefined, import("@cosmograph/cosmos/dist/modules/Store").Hovered> | D3ZoomEvent<HTMLCanvasElement, undefined> | undefined) => void;
52
- /** Handler to clear the hover state, typically called when the mouse leaves a point */
53
- clearHoverState: () => void;
50
+ /** Event handlers for hover-related graph interactions */
51
+ eventHandlers: {
52
+ onPointMouseOver: (index: number, pointPosition: [number, number], event: MouseEvent | D3DragEvent<HTMLCanvasElement, undefined, import("@cosmograph/cosmos/dist/modules/Store").Hovered> | D3ZoomEvent<HTMLCanvasElement, undefined> | undefined) => void;
53
+ onPointMouseOut: () => void;
54
+ onZoomStart: () => void;
55
+ onDragStart: () => void;
56
+ };
54
57
  };
55
58
  //# sourceMappingURL=useHoverState.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useHoverState.d.ts","sourceRoot":"","sources":["../../src/hooks/useHoverState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,sBAAsB,EAAC,MAAM,cAAc,CAAC;AAKpD;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,6DAA6D;IAC7D,KAAK,EAAE,MAAM,CAAC;IACd,4EAA4E;IAC5E,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC5B,GAAG,IAAI,CAAC;AAET;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,eAAO,MAAM,aAAa,4BACC,UAAU,CAAC,OAAO,sBAAsB,CAAC;IAwBhE,sHAAsH;;IAEtH,gHAAgH;;IAEhH,uFAAuF;;CAG1F,CAAC"}
1
+ {"version":3,"file":"useHoverState.d.ts","sourceRoot":"","sources":["../../src/hooks/useHoverState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,sBAAsB,EAAC,MAAM,cAAc,CAAC;AAKpD;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,6DAA6D;IAC7D,KAAK,EAAE,MAAM,CAAC;IACd,4EAA4E;IAC5E,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC5B,GAAG,IAAI,CAAC;AAET;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,eAAO,MAAM,aAAa,4BACC,UAAU,CAAC,OAAO,sBAAsB,CAAC;IAkChE,sHAAsH;;IAEtH,0DAA0D;;;;;;;CAG7D,CAAC"}
@@ -1,4 +1,4 @@
1
- import { useState, useCallback } from 'react';
1
+ import { useState, useCallback, useMemo } from 'react';
2
2
  import { hasClientCoordinates } from '../utils/coordinates';
3
3
  /**
4
4
  * A custom hook that manages hover state for graph points.
@@ -7,18 +7,18 @@ import { hasClientCoordinates } from '../utils/coordinates';
7
7
  * - Track which point is currently being hovered
8
8
  * - Store the hover position coordinates
9
9
  * - Clear the hover state
10
+ * - Provide event handlers for hover-related graph interactions
10
11
  *
11
12
  * @example
12
13
  * ```tsx
13
14
  * const Graph = () => {
14
15
  * const calcRelativeCoords = useRelativeCoordinates(containerRef);
15
- * const { hoveredPoint, onPointMouseOver, clearHoverState } = useHoverState(calcRelativeCoords);
16
+ * const { hoveredPoint, eventHandlers } = useHoverState(calcRelativeCoords);
16
17
  *
17
18
  * return (
18
19
  * <div ref={containerRef}>
19
20
  * <CosmosGraph
20
- * onPointMouseOver={onPointMouseOver}
21
- * onPointMouseOut={clearHoverState}
21
+ * config={eventHandlers}
22
22
  * />
23
23
  * {hoveredPoint && (
24
24
  * <Tooltip
@@ -32,7 +32,7 @@ import { hasClientCoordinates } from '../utils/coordinates';
32
32
  * ```
33
33
  *
34
34
  * @param calcRelativeCoordinates - A function that converts client coordinates to container-relative coordinates
35
- * @returns An object containing the hover state and handlers
35
+ * @returns An object containing the hover state and event handlers for the graph
36
36
  */
37
37
  export const useHoverState = (calcRelativeCoordinates) => {
38
38
  const [hoveredPoint, setHoveredPoint] = useState(null);
@@ -45,13 +45,17 @@ export const useHoverState = (calcRelativeCoordinates) => {
45
45
  }
46
46
  }, [calcRelativeCoordinates]);
47
47
  const clearHoverState = useCallback(() => setHoveredPoint(null), []);
48
+ const eventHandlers = useMemo(() => ({
49
+ onPointMouseOver,
50
+ onPointMouseOut: clearHoverState,
51
+ onZoomStart: clearHoverState,
52
+ onDragStart: clearHoverState,
53
+ }), [onPointMouseOver, clearHoverState]);
48
54
  return {
49
55
  /** The current hover state, containing the index and position of the hovered point, or null if no point is hovered */
50
56
  hoveredPoint,
51
- /** Handler to be called when a point is hovered. Updates the hover state with the point's index and position */
52
- onPointMouseOver,
53
- /** Handler to clear the hover state, typically called when the mouse leaves a point */
54
- clearHoverState,
57
+ /** Event handlers for hover-related graph interactions */
58
+ eventHandlers,
55
59
  };
56
60
  };
57
61
  //# sourceMappingURL=useHoverState.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useHoverState.js","sourceRoot":"","sources":["../../src/hooks/useHoverState.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAE,WAAW,EAAC,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAC,oBAAoB,EAAC,MAAM,sBAAsB,CAAC;AAe1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,uBAAkE,EAClE,EAAE;IACF,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,QAAQ,CAAa,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AAC;QACL,CAAC;IACH,CAAC,EACD,CAAC,uBAAuB,CAAC,CAC1B,CAAC;IAEJ,MAAM,eAAe,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;IAErE,MAAM,aAAa,GAAG,OAAO,CAC3B,GAAG,EAAE,CAAC,CAAC;QACL,gBAAgB;QAChB,eAAe,EAAE,eAAe;QAChC,WAAW,EAAE,eAAe;QAC5B,WAAW,EAAE,eAAe;KAC7B,CAAC,EACF,CAAC,gBAAgB,EAAE,eAAe,CAAC,CACpC,CAAC;IAEF,OAAO;QACL,sHAAsH;QACtH,YAAY;QACZ,0DAA0D;QAC1D,aAAa;KACd,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {useRelativeCoordinates} from '@sqlrooms/ui';\nimport {useState, useCallback, useMemo} from 'react';\nimport {hasClientCoordinates} from '../utils/coordinates';\nimport {GraphConfigInterface} from '@cosmograph/cosmos';\n\n/**\n * Represents the state of a hovered point in the graph.\n * When a point is hovered, contains its index and position coordinates.\n * When no point is hovered, the state is null.\n */\nexport type HoverState = {\n /** The index of the hovered point in the graph data array */\n index: number;\n /** The [x, y] coordinates of the hovered point relative to the container */\n position: [number, number];\n} | null;\n\n/**\n * A custom hook that manages hover state for graph points.\n *\n * This hook provides functionality to:\n * - Track which point is currently being hovered\n * - Store the hover position coordinates\n * - Clear the hover state\n * - Provide event handlers for hover-related graph interactions\n *\n * @example\n * ```tsx\n * const Graph = () => {\n * const calcRelativeCoords = useRelativeCoordinates(containerRef);\n * const { hoveredPoint, eventHandlers } = useHoverState(calcRelativeCoords);\n *\n * return (\n * <div ref={containerRef}>\n * <CosmosGraph\n * config={eventHandlers}\n * />\n * {hoveredPoint && (\n * <Tooltip\n * x={hoveredPoint.position[0]}\n * y={hoveredPoint.position[1]}\n * />\n * )}\n * </div>\n * );\n * };\n * ```\n *\n * @param calcRelativeCoordinates - A function that converts client coordinates to container-relative coordinates\n * @returns An object containing the hover state and event handlers for the graph\n */\nexport const useHoverState = (\n calcRelativeCoordinates: ReturnType<typeof useRelativeCoordinates>,\n) => {\n const [hoveredPoint, setHoveredPoint] = useState<HoverState>(null);\n\n const onPointMouseOver: GraphConfigInterface['onPointMouseOver'] =\n useCallback(\n (\n index: number,\n _pointPosition: [number, number],\n event: MouseEvent | TouchEvent,\n ) => {\n if (hasClientCoordinates(event)) {\n setHoveredPoint({\n index,\n position: calcRelativeCoordinates(event.clientX, event.clientY),\n });\n }\n },\n [calcRelativeCoordinates],\n );\n\n const clearHoverState = useCallback(() => setHoveredPoint(null), []);\n\n const eventHandlers = useMemo(\n () => ({\n onPointMouseOver,\n onPointMouseOut: clearHoverState,\n onZoomStart: clearHoverState,\n onDragStart: clearHoverState,\n }),\n [onPointMouseOver, clearHoverState],\n );\n\n return {\n /** The current hover state, containing the index and position of the hovered point, or null if no point is hovered */\n hoveredPoint,\n /** Event handlers for hover-related graph interactions */\n eventHandlers,\n };\n};\n"]}