Skip to content

Commit 4f0ead2

Browse files
Copilotlstein
andauthored
Fix cluster badge not updating in metadata panel for both swiper and grid views (#168)
* Initial plan * Fix cluster display to use current image's cluster from UMAP points Co-authored-by: lstein <[email protected]> * Export updateClusterInfo and call it from grid-view to fix cluster badge updates Co-authored-by: lstein <[email protected]> * Fix cluster badge empty on initial load by listening for UMAP data loaded event Co-authored-by: lstein <[email protected]> * Fix grid-view test by adding updateClusterInfo to metadata-drawer mock Co-authored-by: lstein <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: lstein <[email protected]>
1 parent a0041c5 commit 4f0ead2

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

photomap/frontend/static/javascript/grid-view.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { toggleGridSwiperView } from "./events.js";
33
import {
44
replaceReferenceImagesWithLinks,
55
updateCurrentImageScore,
6+
updateClusterInfo,
67
} from "./metadata-drawer.js";
78
import { fetchImageByIndex } from "./search.js";
89
import { slideState } from "./slide-state.js";
@@ -703,6 +704,9 @@ class GridViewManager {
703704
document.getElementById("filepathText").textContent =
704705
data["filepath"] || "";
705706
document.getElementById("metadataLink").href = data["metadata_url"] || "#";
707+
708+
// Update cluster information display
709+
updateClusterInfo(data);
706710
}
707711

708712
// These functions act as a semaphore to prevent overlapping batch loads

photomap/frontend/static/javascript/metadata-drawer.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export function updateMetadataOverlay(slide) {
113113
}
114114

115115
// Update cluster information in the metadata window
116-
function updateClusterInfo(metadata) {
116+
export function updateClusterInfo(metadata) {
117117
const clusterInfoContainer = document.getElementById("clusterInfoContainer");
118118
const clusterInfoBadge = document.getElementById("clusterInfoBadge");
119119

@@ -208,10 +208,14 @@ export async function updateCurrentImageScore(metadata) {
208208
return;
209209
}
210210

211-
if (metadata.cluster !== null && metadata.cluster !== undefined) {
211+
// Get current cluster info from UMAP points, not from stale metadata
212+
const clusterInfo = getClusterInfoForImage(globalIndex, window.umapPoints);
213+
if (clusterInfo && clusterInfo.cluster !== null && clusterInfo.cluster !== undefined) {
214+
// Show "unclustered" text for cluster -1
215+
const clusterDisplay = clusterInfo.cluster === -1 ? "unclustered" : clusterInfo.cluster;
212216
scoreDisplay.showCluster(
213-
metadata.cluster || 0,
214-
metadata.color,
217+
clusterDisplay,
218+
clusterInfo.color,
215219
searchIndex,
216220
state.searchResults.length
217221
);
@@ -470,6 +474,24 @@ function setupOverlayButtons() {
470474
// Initialize metadata drawer - sets up all event listeners
471475
export function initializeMetadataDrawer() {
472476
setupOverlayButtons();
477+
478+
// Listen for UMAP data loaded event to refresh cluster info for the current slide
479+
window.addEventListener('umapDataLoaded', () => {
480+
const currentSlide = slideState.getCurrentSlide();
481+
if (currentSlide && currentSlide.globalIndex !== undefined) {
482+
// Get the slide data/metadata for the current slide
483+
// In swiper view, we need to get the actual slide element
484+
const swiperSlide = document.querySelector(`[data-global-index="${currentSlide.globalIndex}"]`);
485+
if (swiperSlide && swiperSlide.dataset) {
486+
updateClusterInfo(swiperSlide.dataset);
487+
updateCurrentImageScore(swiperSlide.dataset);
488+
} else {
489+
// In grid view or if slide element not found, construct minimal metadata
490+
const metadata = { globalIndex: currentSlide.globalIndex };
491+
updateClusterInfo(metadata);
492+
}
493+
}
494+
});
473495
}
474496

475497
// Position metadata drawer (called from events.js during initialization and on window resize)

photomap/frontend/static/javascript/umap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ export async function fetchUmapData() {
407407
window.umapPoints = points;
408408
state.dataChanged = false;
409409

410+
// Dispatch event to notify that UMAP data has been loaded
411+
window.dispatchEvent(new CustomEvent('umapDataLoaded'));
412+
410413
setUmapColorMode();
411414
} finally {
412415
hideUmapSpinner();

tests/frontend/grid-view.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ jest.unstable_mockModule('../../photomap/frontend/static/javascript/search.js',
6666
// Mock metadata-drawer.js
6767
jest.unstable_mockModule('../../photomap/frontend/static/javascript/metadata-drawer.js', () => ({
6868
replaceReferenceImagesWithLinks: jest.fn(() => ''),
69-
updateCurrentImageScore: jest.fn()
69+
updateCurrentImageScore: jest.fn(),
70+
updateClusterInfo: jest.fn()
7071
}));
7172

7273
// Mock slide-state.js

0 commit comments

Comments
 (0)