Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Prevent shifting of audio results when more results are added (#1527)

Dhruv Bhanushali303dfe641691
diff --git a/src/stores/media/index.ts b/src/stores/media/index.ts
index 52115ff75..ad8d8d288 100644
--- a/src/stores/media/index.ts
+++ b/src/stores/media/index.ts
@@ -152,7 +152,18 @@ export const useMediaStore = defineStore('media', {
}
},
- allMedia(): Media[] {
+ /**
+ * Returns a mixed bag of search results across media types.
+ *
+ * This does not contain all hits across all media types! It contains all
+ * the hits for the media type with the most hits and as many hits from
+ * other media types as can be sparsely spliced into the list. The
+ * leftover hits will appear in subsequent pages.
+ *
+ * TODO: Fix the algorithm.
+ * This implementation can hide hits from media types with fewer hits.
+ */
+ allMedia(state): Media[] {
const media = this.resultItems
// Seed the random number generator with the ID of
@@ -175,28 +186,32 @@ export const useMediaStore = defineStore('media', {
const rand = prng(seed)
const randomIntegerInRange = (min: number, max: number) =>
Math.floor(rand() * (max - min + 1)) + min
- /**
- * When navigating from All page to Audio page, VAllResultsGrid is displayed
- * for a short period of time. Then media['image'] is undefined, and it throws an error
- * `TypeError: can't convert undefined to object`. To fix it, we add `|| {}` to the media['image'].
- */
- /**
- * First, set the results to all images
- */
- const newResults = media.image
- // push other items into the list, using a random index.
+ // First, identify the media type with most hits
+ const [mostHits] = supportedMediaTypes
+ .map((type): [SupportedMediaType, number] => [
+ type,
+ state.results[type].count,
+ ])
+ .sort(([, a], [, b]) => b - a)[0]
+
+ // First, set the results to the type with most hits...
+ const newResults = media[mostHits]
+
+ // ...then push other items into the list, using a random index.
let nonImageIndex = 1
- for (const type of supportedMediaTypes.slice(1)) {
+ for (const type of supportedMediaTypes.filter(
+ (type) => type !== mostHits
+ )) {
for (const item of media[type]) {
newResults.splice(nonImageIndex, 0, item)
- // TODO: Fix the algorithm. Currently, when there is no images, the nonImageIndex can get higher
- // than general index, and items can get discarded.
- if (nonImageIndex > newResults.length + 1) break
nonImageIndex = randomIntegerInRange(
nonImageIndex + 1,
nonImageIndex + 6
)
+
+ // Prevent the bunching of audio results at the end.
+ if (nonImageIndex > newResults.length) break
}
}
diff --git a/test/unit/specs/stores/media-store.spec.js b/test/unit/specs/stores/media-store.spec.js
index d71c0774e..909cd9a8c 100644
--- a/test/unit/specs/stores/media-store.spec.js
+++ b/test/unit/specs/stores/media-store.spec.js
@@ -119,8 +119,6 @@ describe('Media Store', () => {
imageItems[1],
imageItems[2],
imageItems[3],
- audioItems[2],
- audioItems[3],
])
})
@@ -137,6 +135,7 @@ describe('Media Store', () => {
audioItems[0],
audioItems[1],
audioItems[2],
+ audioItems[3],
])
})
it('resultCountsPerMediaType returns correct items for %s', () => {