|
1 | | -import {useCallback, useEffect, useMemo, useState} from 'react' |
| 1 | +import {useCallback, useEffect, useMemo, useState, useRef} from 'react' |
2 | 2 |
|
3 | 3 | import { |
4 | 4 | VirtualizedFlexGrid, |
@@ -177,24 +177,35 @@ export default function RepoView() { |
177 | 177 | setViewerOpen(true) |
178 | 178 | }, []) |
179 | 179 |
|
180 | | - // Create stable click handlers for each item to avoid recreating functions |
181 | | - const createItemClickHandler = useCallback( |
182 | | - (index: number) => () => handleImageClick(index), |
| 180 | + // Create stable click handlers map to avoid recreating functions |
| 181 | + const clickHandlersRef = useRef<Map<number, () => void>>(new Map()) |
| 182 | + |
| 183 | + const getClickHandler = useCallback( |
| 184 | + (index: number) => { |
| 185 | + if (!clickHandlersRef.current.has(index)) { |
| 186 | + clickHandlersRef.current.set(index, () => handleImageClick(index)) |
| 187 | + } |
| 188 | + return clickHandlersRef.current.get(index)! |
| 189 | + }, |
183 | 190 | [handleImageClick], |
184 | 191 | ) |
185 | 192 |
|
| 193 | + // Memoize repo to ensure stable reference |
| 194 | + // useTargetRepository already memoizes, but we ensure it's stable here too |
| 195 | + const stableRepo = useMemo(() => repo, [repo]) |
| 196 | + |
186 | 197 | const itemRenderer = useCallback( |
187 | 198 | ({index, item}: RenderData<string>) => ( |
188 | 199 | <ImageCell |
189 | 200 | key={index} |
190 | | - repo={repo} |
| 201 | + repo={stableRepo} |
191 | 202 | path={item} |
192 | | - onClick={createItemClickHandler(index)} |
| 203 | + onClick={getClickHandler(index)} |
193 | 204 | mcmetaPaths={mcmetaPaths} |
194 | 205 | animationEnabled={animationEnabled} |
195 | 206 | /> |
196 | 207 | ), |
197 | | - [repo, createItemClickHandler, mcmetaPaths, animationEnabled], |
| 208 | + [stableRepo, getClickHandler, mcmetaPaths, animationEnabled], |
198 | 209 | ) |
199 | 210 | const downloadFilteredImages = useCallback(async () => { |
200 | 211 | const paths = filteredImageFiles || [] |
|
0 commit comments