Skip to content

Commit 9977796

Browse files
committed
Refactor thumbnail fetching logic
1 parent be5e333 commit 9977796

File tree

1 file changed

+135
-158
lines changed

1 file changed

+135
-158
lines changed

plugins/ThumbPreviews/ThumbPreviews.js

Lines changed: 135 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -119,112 +119,87 @@
119119
// If it's a default thumbnail, immediately fetch a random scene thumbnail and replace the image src
120120
if (defaultImageUrl) {
121121
// This fetching logic is similar to the mouseenter, but runs immediately for default thumbnails
122-
let randomQuery;
123-
124122
if (entityType === 'tags') {
125-
// For tags, use scene markers
126-
randomQuery = `
127-
query FindRandomSceneMarkerForThumbnail($entityId: ID!) {
128-
findSceneMarkers(
129-
scene_marker_filter: { tags: { value: [$entityId], modifier: INCLUDES_ALL } }
130-
) {
131-
scene_markers {
132-
id
123+
const randomThumbnailQuery = `
124+
query FindRandomScreenshotForTagDefaultThumbnail($entityId: ID!) {
125+
findSceneMarkers(scene_marker_filter: { tags: { value: [$entityId], modifier: INCLUDES_ALL } }) {
126+
scene_markers {
127+
id
128+
screenshot
129+
}
130+
}
131+
findScenes(scene_filter: { tags: { value: [$entityId], modifier: INCLUDES_ALL } }) {
132+
scenes {
133+
id
134+
paths {
133135
screenshot
134136
}
135137
}
136138
}
139+
}
137140
`;
141+
142+
csLib.callGQL({ query: randomThumbnailQuery, variables: { entityId } }).then(response => {
143+
let screenshotUrls = [];
144+
145+
const markerScreenshots = response?.findSceneMarkers?.scene_markers
146+
?.map(marker => marker?.screenshot)
147+
?.filter(url => url) || [];
148+
149+
const sceneScreenshots = response?.findScenes?.scenes
150+
?.map(scene => scene?.paths?.screenshot)
151+
?.filter(url => url) || [];
152+
153+
// Combine all screenshot URLs
154+
screenshotUrls = [...markerScreenshots, ...sceneScreenshots];
155+
156+
if (screenshotUrls.length > 0) {
157+
// Shuffle the combined array of screenshot URLs
158+
for (let i = screenshotUrls.length - 1; i > 0; i--) {
159+
const j = Math.floor(Math.random() * (i + 1));
160+
[screenshotUrls[i], screenshotUrls[j]] = [screenshotUrls[j], screenshotUrls[i]]; // Swap elements
161+
}
162+
163+
// Pick a random URL from the shuffled array
164+
randomSceneThumbnailUrl = screenshotUrls[0];
165+
166+
if (randomSceneThumbnailUrl) {
167+
// console.log("Found random screenshot URL for default thumbnail, replacing default:", randomSceneThumbnailUrl);
168+
// Replace the existing image thumbnail source
169+
existingImage.src = randomSceneThumbnailUrl;
170+
} else {
171+
// This case should ideally not be hit if screenshotUrls.length > 0, but as a safeguard
172+
// console.log(`First shuffled screenshot URL has no screenshot path.`, entityId);
173+
}
174+
} else {
175+
// console.log(`No random screenshot found for this ${entityType}.`, entityId);
176+
}
177+
}).catch(error => {
178+
// console.error("Error fetching random screenshot for default thumbnail:", error);
179+
randomSceneThumbnailUrl = null; // Reset URL on error
180+
}).finally(() => {
181+
isFetching = false; // Allow fetching again if needed
182+
});
138183
} else {
139-
// For other entity types, use scenes
140-
randomQuery = `
184+
// For other entity types, use scenes (existing logic)
185+
const randomQuery = `
141186
query FindRandomSceneForThumbnail($entityId: ID!) {
142187
findScenes(
143188
scene_filter: { ${currentConfig.graphqlFilter}: { value: [$entityId], modifier: INCLUDES_ALL } }
144189
) {
145-
scenes { # Fetch all scenes initially
190+
scenes {
146191
id
147192
paths {
148-
screenshot # Also get screenshot path here
193+
screenshot
149194
}
150195
}
151196
# Request total count to know if there are any scenes, though not strictly needed for this logic
152197
count
153198
}
154199
}
155200
`;
156-
}
157201

158-
csLib.callGQL({ query: randomQuery, variables: { entityId } }).then(response => {
159-
if (entityType === 'tags') {
160-
const markers = response?.findSceneMarkers?.scene_markers;
161-
162-
if (markers && markers.length > 0) {
163-
// Shuffle the array of markers
164-
for (let i = markers.length - 1; i > 0; i--) {
165-
const j = Math.floor(Math.random() * (i + 1));
166-
[markers[i], markers[j]] = [markers[j], markers[i]]; // Swap elements
167-
}
168-
// console.log(`Shuffled markers for default thumbnail:`, markers);
169-
170-
// Get the preview from the first marker in the shuffled array
171-
randomSceneThumbnailUrl = markers[0]?.screenshot;
172-
173-
if (randomSceneThumbnailUrl) {
174-
// console.log(`Found random marker screenshot URL on load, replacing default:`, randomSceneThumbnailUrl);
175-
// Replace the existing image thumbnail source
176-
existingImage.src = randomSceneThumbnailUrl;
177-
} else {
178-
// console.log(`First shuffled marker has no screenshot path.`, entityId);
179-
}
180-
} else {
181-
// console.log(`No random marker screenshot found on load for this ${entityType}, trying scenes as fallback.`, entityId);
182-
183-
// Fallback to scenes for tags
184-
const sceneFallbackQuery = `
185-
query FindRandomSceneForTagThumbnailFallback($entityId: ID!) {
186-
findScenes(
187-
scene_filter: { tags: { value: [$entityId], modifier: INCLUDES_ALL } }
188-
) {
189-
scenes {
190-
id
191-
paths {
192-
screenshot
193-
}
194-
}
195-
}
196-
}
197-
`;
198-
199-
csLib.callGQL({ query: sceneFallbackQuery, variables: { entityId } }).then(fallbackResponse => {
200-
const scenes = fallbackResponse?.findScenes?.scenes;
201-
202-
if (scenes && scenes.length > 0) {
203-
// Shuffle the array of scenes
204-
for (let i = scenes.length - 1; i > 0; i--) {
205-
const j = Math.floor(Math.random() * (i + 1));
206-
[scenes[i], scenes[j]] = [scenes[j], scenes[i]]; // Swap elements
207-
}
208-
// console.log(`Shuffled fallback scenes for default thumbnail:`, scenes);
209-
210-
// Get the screenshot from the first scene in the shuffled array
211-
randomSceneThumbnailUrl = scenes[0]?.paths?.screenshot;
212-
213-
if (randomSceneThumbnailUrl) {
214-
// console.log(`Found random scene screenshot URL on load (fallback), replacing default:`, randomSceneThumbnailUrl);
215-
// Replace the existing image thumbnail source
216-
existingImage.src = randomSceneThumbnailUrl;
217-
} else {
218-
// console.log(`First shuffled fallback scene has no screenshot path.`, entityId);
219-
}
220-
} else {
221-
// console.log(`No fallback scenes found for tag ${entityId}.`);
222-
}
223-
}).catch(error => {
224-
console.error("Error fetching fallback scene thumbnail on load:", error);
225-
});
226-
}
227-
} else {
202+
csLib.callGQL({ query: randomQuery, variables: { entityId } }).then(response => {
228203
const scenes = response?.findScenes?.scenes;
229204

230205
if (scenes && scenes.length > 0) {
@@ -248,10 +223,10 @@
248223
} else {
249224
// console.log(`No random scene thumbnail found on load for this ${entityType}.`, entityId);
250225
}
251-
}
252-
}).catch(error => {
253-
// console.error("Error fetching random scene thumbnail on load:", error);
254-
});
226+
}).catch(error => {
227+
// console.error("Error fetching random scene thumbnail on load:", error);
228+
});
229+
}
255230
}
256231

257232
// Add mouse enter/leave listeners to the thumbnail section
@@ -333,25 +308,18 @@
333308
?.map(scene => scene?.paths?.preview)
334309
?.filter(url => url) || [];
335310

336-
// Shuffle markers and scenes separately
337-
// Shuffle marker URLs
338-
for (let i = markerUrls.length - 1; i > 0; i--) {
339-
const j = Math.floor(Math.random() * (i + 1));
340-
[markerUrls[i], markerUrls[j]] = [markerUrls[j], markerUrls[i]];
341-
}
342-
343-
// Shuffle scene URLs
344-
for (let i = sceneUrls.length - 1; i > 0; i--) {
311+
// Combine all URLs into a single array
312+
previewUrls = [...markerUrls, ...sceneUrls];
313+
314+
// Shuffle the combined array
315+
for (let i = previewUrls.length - 1; i > 0; i--) {
345316
const j = Math.floor(Math.random() * (i + 1));
346-
[sceneUrls[i], sceneUrls[j]] = [sceneUrls[j], sceneUrls[i]];
317+
[previewUrls[i], previewUrls[j]] = [previewUrls[j], previewUrls[i]]; // Swap elements
347318
}
348-
349-
// Combine shuffled marker URLs first, then shuffled scene URLs
350-
previewUrls = [...markerUrls, ...sceneUrls];
351-
319+
352320
// console.log(`Found ${markerUrls.length} marker streams and ${sceneUrls.length} scene previews for tag ${entityId}`);
353-
// console.log("Shuffled markers first, then scenes:", previewUrls);
354-
321+
// console.log("Combined and shuffled URLs:", previewUrls);
322+
355323
// Remove the fallback logic since we're now getting both in one query
356324
} else {
357325
// Extract preview URLs from the scenes for other entity types
@@ -511,32 +479,36 @@
511479
// If it's a default thumbnail and we haven't fetched a random scene thumbnail yet
512480
if (defaultImageUrl && !randomSceneThumbnailUrl && !isFetching) {
513481
isFetching = true;
514-
// console.log(`Fetching random scene thumbnail for default ${entityType} thumbnail:`, entityId);
515-
516-
let randomSceneQuery;
517-
482+
// console.log(`Fetching random scene or marker screenshot for default ${entityType} thumbnail:`, entityId);
483+
484+
let randomThumbnailQuery;
485+
518486
if (entityType === 'tags') {
519-
// For tags, use scene markers
520-
randomSceneQuery = `
521-
query FindRandomSceneMarkerForThumbnail($entityId: ID!) {
522-
findSceneMarkers(
523-
scene_marker_filter: { tags: { value: [$entityId], modifier: INCLUDES_ALL } }
524-
filter: { per_page: 1 }
525-
) {
487+
// For tags, get both scene markers and scenes
488+
randomThumbnailQuery = `
489+
query FindRandomScreenshotForTagDefaultThumbnail($entityId: ID!) {
490+
findSceneMarkers(scene_marker_filter: { tags: { value: [$entityId], modifier: INCLUDES_ALL } }) {
526491
scene_markers {
527492
id
528493
screenshot
529494
}
530495
}
496+
findScenes(scene_filter: { tags: { value: [$entityId], modifier: INCLUDES_ALL } }) {
497+
scenes {
498+
id
499+
paths {
500+
screenshot
501+
}
502+
}
503+
}
531504
}
532505
`;
533506
} else {
534-
// For other entity types, use scenes
535-
randomSceneQuery = `
507+
// For other entity types, use scenes (existing logic)
508+
randomThumbnailQuery = `
536509
query FindRandomSceneForThumbnail($entityId: ID!) {
537510
findScenes(
538511
scene_filter: { ${currentConfig.graphqlFilter}: { value: [$entityId], modifier: INCLUDES_ALL } }
539-
filter: { per_page: 1 }
540512
) {
541513
scenes {
542514
id
@@ -545,56 +517,61 @@
545517
}
546518
}
547519
}
520+
# Request total count to know if there are any scenes, though not strictly needed for this logic
521+
count
548522
}
549523
`;
550524
}
551525

552526
try {
553-
const response = await csLib.callGQL({ query: randomSceneQuery, variables: { entityId } });
554-
// console.log("GraphQL Response for random scene thumbnail:", response);
555-
527+
const response = await csLib.callGQL({ query: randomThumbnailQuery, variables: { entityId } });
528+
// console.log("GraphQL Response for random thumbnail:", response);
529+
530+
let screenshotUrls = [];
531+
556532
if (entityType === 'tags') {
557-
const marker = response?.findSceneMarkers?.scene_markers?.[0];
558-
randomSceneThumbnailUrl = marker?.screenshot;
559-
560-
// If no marker found, fallback to scenes
561-
if (!randomSceneThumbnailUrl) {
562-
// console.log(`No marker screenshot found for tag ${entityId}, trying scene fallback...`);
563-
const sceneFallbackQuery = `
564-
query FindRandomSceneForTagThumbnailFallback($entityId: ID!) {
565-
findScenes(
566-
scene_filter: { tags: { value: [$entityId], modifier: INCLUDES_ALL } }
567-
filter: { per_page: 1 }
568-
) {
569-
scenes {
570-
id
571-
paths {
572-
screenshot
573-
}
574-
}
575-
}
576-
}
577-
`;
578-
579-
const sceneFallbackResponse = await csLib.callGQL({ query: sceneFallbackQuery, variables: { entityId } });
580-
// console.log("GraphQL Scene Fallback Response for random thumbnail:", sceneFallbackResponse);
581-
582-
const scene = sceneFallbackResponse?.findScenes?.scenes?.[0];
583-
randomSceneThumbnailUrl = scene?.paths?.screenshot;
584-
}
533+
const markerScreenshots = response?.findSceneMarkers?.scene_markers
534+
?.map(marker => marker?.screenshot)
535+
?.filter(url => url) || [];
536+
537+
const sceneScreenshots = response?.findScenes?.scenes
538+
?.map(scene => scene?.paths?.screenshot)
539+
?.filter(url => url) || [];
540+
541+
// Combine all screenshot URLs
542+
screenshotUrls = [...markerScreenshots, ...sceneScreenshots];
543+
585544
} else {
586-
const scene = response?.findScenes?.scenes?.[0];
587-
randomSceneThumbnailUrl = scene?.paths?.screenshot;
545+
// For other entity types, use scene screenshots (existing logic)
546+
screenshotUrls = response?.findScenes?.scenes
547+
?.map(scene => scene?.paths?.screenshot)
548+
?.filter(url => url) || [];
588549
}
589550

590-
if (randomSceneThumbnailUrl) {
591-
// console.log("Found random scene thumbnail URL:", randomSceneThumbnailUrl);
592-
// TODO: Use this URL later for default thumbnails
551+
552+
if (screenshotUrls.length > 0) {
553+
// Shuffle the combined array of screenshot URLs
554+
for (let i = screenshotUrls.length - 1; i > 0; i--) {
555+
const j = Math.floor(Math.random() * (i + 1));
556+
[screenshotUrls[i], screenshotUrls[j]] = [screenshotUrls[j], screenshotUrls[i]]; // Swap elements
557+
}
558+
559+
// Pick a random URL from the shuffled array
560+
randomSceneThumbnailUrl = screenshotUrls[0];
561+
562+
if (randomSceneThumbnailUrl) {
563+
// console.log("Found random screenshot URL for default thumbnail, replacing default:", randomSceneThumbnailUrl);
564+
// Replace the existing image thumbnail source
565+
existingImage.src = randomSceneThumbnailUrl;
566+
} else {
567+
// This case should ideally not be hit if screenshotUrls.length > 0, but as a safeguard
568+
// console.log(`First shuffled screenshot URL has no screenshot path.`, entityId);
569+
}
593570
} else {
594-
// console.log(`No random scene thumbnail found for this ${entityType}.`, entityId);
571+
// console.log(`No random screenshot found for this ${entityType}.`, entityId);
595572
}
596573
} catch (error) {
597-
// console.error("Error fetching random scene thumbnail:", error);
574+
// console.error("Error fetching random screenshot for default thumbnail:", error);
598575
randomSceneThumbnailUrl = null; // Reset URL on error
599576
} finally {
600577
isFetching = false; // Allow fetching again if needed
@@ -762,4 +739,4 @@
762739
handleThumbLogic(containerElement, "groups");
763740
});
764741

765-
})();
742+
})();

0 commit comments

Comments
 (0)