1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

use random backdrop image on item details pages

This commit is contained in:
TheMelmacian 2023-07-07 22:51:19 +02:00
parent b735379a78
commit aec90cbc96
3 changed files with 60 additions and 25 deletions

View file

@ -1,3 +1,7 @@
import { BaseItemDto } from '@jellyfin/sdk/lib/generated-client';
import { randomInt } from './number';
import { ApiClient } from 'jellyfin-apiclient';
/**
* Gets the url search string.
* This function should be used instead of location.search alone, because the app router
@ -33,3 +37,54 @@ export const getParameterByName = (name: string, url?: string | null | undefined
// eslint-disable-next-line compat/compat
return new URLSearchParams(url).get(name) || '';
};
export interface ScaleImageOptions {
maxWidth?: number;
width?: number;
maxHeight?: number;
height?: number;
fillWidth?: number;
fillHeight?: number;
quality?: number;
}
/**
* Returns the url of a random backdrop image of an item.
* If the item has no backdrop image, the url of a random backdrop image of the parent item is returned.
* Falls back to the primary image (cover) of the item, if neither the item nor it's parent have at least one backdrop image.
* Returns undefined if no usable image was found.
* @param apiClient The ApiClient to generate the url.
* @param item The item for which the backdrop image is requested.
* @param options Optional; allows to scale the backdrop image.
* @returns The url of a random backdrop image of the provided item.
*/
export const getRandomItemBackdropImageUrl = (apiClient: ApiClient, item: BaseItemDto, options: ScaleImageOptions = {}): string | undefined => {
let imgUrl;
if (item.BackdropImageTags && item.BackdropImageTags.length) {
const backdropImgIndex = randomInt(0, item.BackdropImageTags.length - 1);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
imgUrl = apiClient.getScaledImageUrl(item.Id!, {
type: 'Backdrop',
index: backdropImgIndex,
tag: item.BackdropImageTags[backdropImgIndex],
...options
});
} else if (item.ParentBackdropItemId && item.ParentBackdropImageTags && item.ParentBackdropImageTags.length) {
const backdropImgIndex = randomInt(0, item.ParentBackdropImageTags.length - 1);
imgUrl = apiClient.getScaledImageUrl(item.ParentBackdropItemId, {
type: 'Backdrop',
index: backdropImgIndex,
tag: item.ParentBackdropImageTags[backdropImgIndex],
...options
});
} else if (item.ImageTags && item.ImageTags.Primary) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
imgUrl = apiClient.getScaledImageUrl(item.Id!, {
type: 'Primary',
tag: item.ImageTags.Primary,
...options
});
}
return imgUrl;
};