@@ -29,9 +29,11 @@ export type GraphicsObjectClickEvent = {
2929export const InteractiveGraphics = ( {
3030 graphics,
3131 onObjectClicked,
32+ objectLimit,
3233} : {
3334 graphics : GraphicsObject
3435 onObjectClicked ?: ( event : GraphicsObjectClickEvent ) => void
36+ objectLimit ?: number
3537} ) => {
3638 const [ activeLayers , setActiveLayers ] = useState < string [ ] | null > ( null )
3739 const [ activeStep , setActiveStep ] = useState < number | null > ( null )
@@ -144,6 +146,41 @@ export const InteractiveGraphics = ({
144146 size ,
145147 )
146148
149+ const filterAndLimit = < T , > (
150+ objects : T [ ] | undefined ,
151+ filterFn : ( obj : T ) => boolean ,
152+ ) : ( T & { originalIndex : number } ) [ ] => {
153+ if ( ! objects ) return [ ]
154+ const filtered = objects
155+ . map ( ( obj , index ) => ( { ...obj , originalIndex : index } ) )
156+ . filter ( filterFn )
157+ return objectLimit ? filtered . slice ( - objectLimit ) : filtered
158+ }
159+
160+ const filteredLines = useMemo (
161+ ( ) => filterAndLimit ( graphics . lines , filterLines ) ,
162+ [ graphics . lines , filterLines , objectLimit ] ,
163+ )
164+ const filteredRects = useMemo (
165+ ( ) => filterAndLimit ( graphics . rects , filterRects ) ,
166+ [ graphics . rects , filterRects , objectLimit ] ,
167+ )
168+ const filteredPoints = useMemo (
169+ ( ) => filterAndLimit ( graphics . points , filterPoints ) ,
170+ [ graphics . points , filterPoints , objectLimit ] ,
171+ )
172+ const filteredCircles = useMemo (
173+ ( ) => filterAndLimit ( graphics . circles , filterCircles ) ,
174+ [ graphics . circles , filterCircles , objectLimit ] ,
175+ )
176+
177+ const totalFilteredObjects =
178+ filteredLines . length +
179+ filteredRects . length +
180+ filteredPoints . length +
181+ filteredCircles . length
182+ const isLimitReached = objectLimit && totalFilteredObjects > objectLimit
183+
147184 return (
148185 < div >
149186 { showToolbar && (
@@ -193,6 +230,12 @@ export const InteractiveGraphics = ({
193230 />
194231 Filter by step
195232 </ label >
233+ { isLimitReached && (
234+ < span style = { { color : "red" , fontSize : "12px" } } >
235+ Display limited to { objectLimit } objects. Received:{ " " }
236+ { totalFilteredObjects } .
237+ </ span >
238+ ) }
196239 </ div >
197240 ) }
198241 </ div >
@@ -207,46 +250,38 @@ export const InteractiveGraphics = ({
207250 } }
208251 >
209252 < DimensionOverlay transform = { realToScreen } >
210- { graphics . lines ?. map ( ( l , originalIndex ) =>
211- filterLines ( l ) ? (
212- < Line
213- key = { originalIndex }
214- line = { l }
215- index = { originalIndex }
216- interactiveState = { interactiveState }
217- />
218- ) : null ,
219- ) }
220- { graphics . rects ?. map ( ( r , originalIndex ) =>
221- filterRects ( r ) ? (
222- < Rect
223- key = { originalIndex }
224- rect = { r }
225- index = { originalIndex }
226- interactiveState = { interactiveState }
227- />
228- ) : null ,
229- ) }
230- { graphics . points ?. map ( ( p , originalIndex ) =>
231- filterPoints ( p ) ? (
232- < Point
233- key = { originalIndex }
234- point = { p }
235- index = { originalIndex }
236- interactiveState = { interactiveState }
237- />
238- ) : null ,
239- ) }
240- { graphics . circles ?. map ( ( c , originalIndex ) =>
241- filterCircles ( c ) ? (
242- < Circle
243- key = { originalIndex }
244- circle = { c }
245- index = { originalIndex }
246- interactiveState = { interactiveState }
247- />
248- ) : null ,
249- ) }
253+ { filteredLines . map ( ( line ) => (
254+ < Line
255+ key = { line . originalIndex }
256+ line = { line }
257+ index = { line . originalIndex }
258+ interactiveState = { interactiveState }
259+ />
260+ ) ) }
261+ { filteredRects . map ( ( rect ) => (
262+ < Rect
263+ key = { rect . originalIndex }
264+ rect = { rect }
265+ index = { rect . originalIndex }
266+ interactiveState = { interactiveState }
267+ />
268+ ) ) }
269+ { filteredPoints . map ( ( point ) => (
270+ < Point
271+ key = { point . originalIndex }
272+ point = { point }
273+ index = { point . originalIndex }
274+ interactiveState = { interactiveState }
275+ />
276+ ) ) }
277+ { filteredCircles . map ( ( circle ) => (
278+ < Circle
279+ key = { circle . originalIndex }
280+ circle = { circle }
281+ index = { circle . originalIndex }
282+ interactiveState = { interactiveState }
283+ />
284+ ) ) }
250285 < SuperGrid
251286 stringifyCoord = { ( x , y ) => `${ x . toFixed ( 2 ) } , ${ y . toFixed ( 2 ) } ` }
252287 width = { size . width }
0 commit comments