11import React , { useEffect , useState } from 'react' ;
22import { connect } from 'react-redux' ;
33import Plotly from 'plotly.js' ;
4- const PCA = require ( 'ml-pca' ) ;
4+ import * as PCA from 'ml-pca' ;
55import type { Binary } from 'mongodb' ;
66import type { Document } from 'bson' ;
77
88import type { VectorEmbeddingVisualizerState } from '../stores/reducer' ;
99import { loadDocuments } from '../stores/visualization' ;
1010import { ErrorSummary } from '@mongodb-js/compass-components' ;
11+ import { collectionModelLocator } from '@mongodb-js/compass-app-stores/provider' ;
1112
1213type HoverInfo = { x : number ; y : number ; text : string } | null ;
1314
1415export interface VectorVisualizerProps {
15- onFetchDocs : ( namespace : string ) => void ;
16+ onFetchDocs : ( ) => void ;
1617 docs : Document [ ] ;
1718 loadingDocumentsState : 'initial' | 'loading' | 'loaded' | 'error' ;
1819 loadingDocumentsError : Error | null ;
19- collection : { namespace : string } ;
2020}
2121
2222function normalizeTo2D ( vectors : Binary [ ] ) : { x : number ; y : number } [ ] {
2323 const raw = vectors . map ( ( v ) => Array . from ( v . toFloat32Array ( ) ) ) ;
24- const pca = new PCA ( raw ) ;
24+ const pca = new PCA . PCA ( raw ) ;
2525 const reduced = pca . predict ( raw , { nComponents : 2 } ) . to2DArray ( ) ;
26- return reduced . map ( ( [ x , y ] : [ number , number ] ) => ( { x, y } ) ) ;
26+ return reduced . map ( ( [ x , y ] ) => ( { x, y } ) ) ;
2727}
2828
2929const VectorVisualizer : React . FC < VectorVisualizerProps > = ( {
3030 onFetchDocs,
3131 docs,
3232 loadingDocumentsState,
3333 loadingDocumentsError,
34- collection,
3534} ) => {
3635 const [ hoverInfo , setHoverInfo ] = useState < HoverInfo > ( null ) ;
3736
3837 useEffect ( ( ) => {
3938 if ( loadingDocumentsState === 'initial' ) {
4039 // Fetch the documents when the component mounts when they aren't already loaded.
41- onFetchDocs ( collection . namespace ) ;
40+ onFetchDocs ( ) ;
4241 }
43- } , [ loadingDocumentsState , onFetchDocs , collection . namespace ] ) ;
42+ } , [ loadingDocumentsState , onFetchDocs ] ) ;
4443
4544 useEffect ( ( ) => {
4645 const container = document . getElementById ( 'vector-plot' ) ;
@@ -50,14 +49,11 @@ const VectorVisualizer: React.FC<VectorVisualizerProps> = ({
5049
5150 const plot = async ( ) => {
5251 try {
53- const ns = collection ?. namespace ;
54- if ( ! ns ) return ;
55-
5652 const vectors = docs . map ( ( doc ) => doc . review_vec ) . filter ( Boolean ) ;
5753
5854 if ( ! vectors . length ) return ;
5955
60- const points = normalizeTo2D ( vectors ) ;
56+ const points = normalizeTo2D ( vectors . slice ( 0 , 50 ) ) ;
6157
6258 await Plotly . newPlot (
6359 container ,
@@ -134,7 +130,7 @@ const VectorVisualizer: React.FC<VectorVisualizerProps> = ({
134130 return ( ) => {
135131 abortController . abort ( ) ;
136132 } ;
137- } , [ docs , collection . namespace ] ) ;
133+ } , [ docs ] ) ;
138134
139135 return (
140136 < div style = { { position : 'relative' , width : '100%' , height : '100%' } } >
0 commit comments