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

526 lines
14 KiB
TypeScript
Raw Normal View History

import * as userSettings from 'scripts/settings/userSettings';
import datetime from 'scripts/datetime';
2024-08-14 13:31:34 -04:00
import globalize from 'lib/globalize';
import itemHelper from '../itemHelper';
import { ItemKind } from 'types/base/models/item-kind';
import { ItemMediaKind } from 'types/base/models/item-media-kind';
import { ItemStatus } from 'types/base/models/item-status';
import type { NullableNumber, NullableString } from 'types/base/common/shared/types';
2024-03-03 01:31:35 +03:00
import type { ItemDto } from 'types/base/models/item-dto';
import type { MiscInfo } from 'types/mediaInfoItem';
2024-08-10 04:09:59 +03:00
import { PrimaryInfoOpts } from './type';
function shouldShowFolderRuntime(
itemType: ItemKind,
itemMediaType: ItemMediaKind
): boolean {
return (
itemType === ItemKind.MusicAlbum
|| itemMediaType === ItemMediaKind.MusicArtist
|| itemType === ItemKind.Playlist
|| itemMediaType === ItemMediaKind.Playlist
|| itemMediaType === ItemMediaKind.MusicGenre
);
}
function addTrackCountOrItemCount(
showFolderRuntime: boolean,
itemSongCount: NullableNumber,
itemChildCount: NullableNumber,
itemRunTimeTicks: NullableNumber,
2024-08-10 04:09:59 +03:00
itemType: ItemKind,
addMiscInfo: (val: MiscInfo) => void
): void {
if (showFolderRuntime) {
const count = itemSongCount ?? itemChildCount;
if (count) {
addMiscInfo({ text: globalize.translate('TrackCount', count) });
}
if (itemRunTimeTicks) {
addMiscInfo({ text: datetime.getDisplayDuration(itemRunTimeTicks) });
}
} else if (itemType === ItemKind.PhotoAlbum || itemType === ItemKind.BoxSet) {
const count = itemChildCount;
if (count) {
addMiscInfo({ text: globalize.translate('ItemCount', count) });
}
}
}
function addOriginalAirDateInfo(
itemType: ItemKind,
itemMediaType: ItemMediaKind,
2024-08-10 04:09:59 +03:00
showOriginalAirDateInfo: boolean,
itemPremiereDate: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
itemPremiereDate
&& (itemType === ItemKind.Episode || itemMediaType === ItemMediaKind.Photo)
2024-08-10 04:09:59 +03:00
&& showOriginalAirDateInfo
) {
try {
//don't modify date to locale if episode. Only Dates (not times) are stored, or editable in the edit metadata dialog
const date = datetime.parseISO8601Date(
itemPremiereDate,
itemType !== ItemKind.Episode
);
addMiscInfo({ text: datetime.toLocaleDateString(date) });
} catch (e) {
console.error('error parsing date:', itemPremiereDate);
}
}
}
function addSeriesTimerInfo(
itemType: ItemKind,
itemRecordAnyTime: boolean | undefined,
itemStartDate: NullableString,
itemRecordAnyChannel: boolean | undefined,
itemChannelName: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
if (itemType === ItemKind.SeriesTimer) {
if (itemRecordAnyTime) {
addMiscInfo({ text: globalize.translate('Anytime') });
} else {
addMiscInfo({ text: datetime.getDisplayTime(itemStartDate) });
}
if (itemRecordAnyChannel) {
addMiscInfo({ text: globalize.translate('AllChannels') });
} else {
addMiscInfo({
text: itemChannelName ?? globalize.translate('OneChannel')
});
}
}
}
function addProgramIndicatorInfo(
program: ItemDto | undefined,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
program?.IsLive
2024-08-10 04:09:59 +03:00
&& userSettings.get('guide-indicator-live') === 'true'
) {
addMiscInfo({
text: globalize.translate('Live'),
cssClass: 'mediaInfoProgramAttribute liveTvProgram'
});
} else if (
program?.IsPremiere
2024-08-10 04:09:59 +03:00
&& userSettings.get('guide-indicator-premiere') === 'true'
) {
addMiscInfo({
text: globalize.translate('Premiere'),
cssClass: 'mediaInfoProgramAttribute premiereTvProgram'
});
} else if (
program?.IsSeries
&& !program?.IsRepeat
2024-08-10 04:09:59 +03:00
&& userSettings.get('guide-indicator-new') === 'true'
) {
addMiscInfo({
text: globalize.translate('New'),
cssClass: 'mediaInfoProgramAttribute newTvProgram'
});
} else if (
program?.IsSeries
&& program?.IsRepeat
2024-08-10 04:09:59 +03:00
&& userSettings.get('guide-indicator-repeat') === 'true'
) {
addMiscInfo({
text: globalize.translate('Repeat'),
cssClass: 'mediaInfoProgramAttribute repeatTvProgram'
});
}
}
function addProgramIndicators(
item: ItemDto,
2024-08-10 04:09:59 +03:00
showYearInfo: boolean,
showEpisodeTitleInfo: boolean,
showOriginalAirDateInfo: boolean,
showProgramIndicatorInfo: boolean,
includeEpisodeTitleIndexNumber: boolean,
addMiscInfo: (val: MiscInfo) => void
): void {
if (item.Type === ItemKind.Program || item.Type === ItemKind.Timer) {
let program = item;
if (item.Type === ItemKind.Timer && item.ProgramInfo) {
program = item.ProgramInfo;
}
2024-08-10 04:09:59 +03:00
if (showProgramIndicatorInfo !== false) {
addProgramIndicatorInfo(program, addMiscInfo);
}
addProgramTextInfo(
program,
2024-08-10 04:09:59 +03:00
showEpisodeTitleInfo,
includeEpisodeTitleIndexNumber,
showOriginalAirDateInfo,
showYearInfo,
addMiscInfo
);
}
}
function addProgramTextInfo(
program: ItemDto,
2024-08-10 04:09:59 +03:00
showEpisodeTitleInfo: boolean,
includeEpisodeTitleIndexNumber: boolean,
showOriginalAirDateInfo: boolean,
showYearInfo: boolean,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-10 04:09:59 +03:00
if (
(program?.IsSeries || program?.EpisodeTitle)
&& showEpisodeTitleInfo !== false
) {
const text = itemHelper.getDisplayName(program, {
2024-08-10 04:09:59 +03:00
includeIndexNumber: includeEpisodeTitleIndexNumber
});
if (text) {
addMiscInfo({ text: text });
}
} else if (
program?.ProductionYear
2024-08-10 04:09:59 +03:00
&& ((program?.IsMovie && showOriginalAirDateInfo !== false)
|| showYearInfo !== false)
) {
addMiscInfo({ text: program.ProductionYear });
2024-08-10 04:09:59 +03:00
} else if (program?.PremiereDate && showOriginalAirDateInfo !== false) {
try {
const date = datetime.parseISO8601Date(program.PremiereDate);
const text = globalize.translate(
'OriginalAirDateValue',
datetime.toLocaleDateString(date)
);
addMiscInfo({ text: text });
} catch (e) {
console.error('error parsing date:', program.PremiereDate);
}
}
}
function addStartDateInfo(
itemStartDate: NullableString,
itemType: ItemKind,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
itemStartDate
&& itemType !== ItemKind.Program
&& itemType !== ItemKind.SeriesTimer
&& itemType !== ItemKind.Timer
) {
try {
const date = datetime.parseISO8601Date(itemStartDate);
addMiscInfo({ text: datetime.toLocaleDateString(date) });
if (itemType !== ItemKind.Recording) {
addMiscInfo({ text: datetime.getDisplayTime(date) });
}
} catch (e) {
console.error('error parsing date:', itemStartDate);
}
}
}
function addSeriesProductionYearInfo(
itemProductionYear: NullableNumber,
itemType: ItemKind,
2024-08-10 04:09:59 +03:00
showYearInfo: boolean,
itemStatus: ItemStatus,
itemEndDate: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-10 04:09:59 +03:00
if (itemProductionYear && showYearInfo && itemType === ItemKind.Series) {
if (itemStatus === ItemStatus.Continuing) {
addMiscInfo({
text: globalize.translate(
'SeriesYearToPresent',
datetime.toLocaleString(itemProductionYear, {
useGrouping: false
})
)
});
} else {
addproductionYearWithEndDate(itemProductionYear, itemEndDate, addMiscInfo);
}
}
}
function addproductionYearWithEndDate(
itemProductionYear: number,
itemEndDate: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
let productionYear = datetime.toLocaleString(itemProductionYear, {
useGrouping: false
});
if (itemEndDate) {
try {
const endYear = datetime.toLocaleString(
datetime.parseISO8601Date(itemEndDate).getFullYear(),
{ useGrouping: false }
);
/* At this point, text will contain only the start year */
if (endYear !== itemProductionYear) {
productionYear += `-${endYear}`;
}
} catch (e) {
console.error('error parsing date:', itemEndDate);
}
}
addMiscInfo({ text: productionYear });
}
function addYearInfo(
2024-08-10 04:09:59 +03:00
showYearInfo: boolean,
itemType: ItemKind,
itemMediaType: ItemMediaKind,
itemProductionYear: NullableNumber,
itemPremiereDate: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
2024-08-10 04:09:59 +03:00
showYearInfo
&& itemType !== ItemKind.Series
&& itemType !== ItemKind.Episode
&& itemType !== ItemKind.Person
&& itemMediaType !== ItemMediaKind.Photo
&& itemType !== ItemKind.Program
&& itemType !== ItemKind.Season
) {
if (itemProductionYear) {
addMiscInfo({ text: itemProductionYear });
} else if (itemPremiereDate) {
try {
const text = datetime.toLocaleString(
datetime.parseISO8601Date(itemPremiereDate).getFullYear(),
{ useGrouping: false }
);
addMiscInfo({ text: text });
} catch (e) {
console.error('error parsing date:', itemPremiereDate);
}
}
}
}
function addVideo3DFormat(
itemVideo3DFormat: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
if (itemVideo3DFormat) {
addMiscInfo({ text: '3D' });
}
}
function addRunTimeInfo(
itemRunTimeTicks: NullableNumber,
itemType: ItemKind,
showFolderRuntime: boolean,
2024-08-10 04:09:59 +03:00
showRuntimeInfo: boolean,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
itemRunTimeTicks
&& itemType !== ItemKind.Series
&& itemType !== ItemKind.Program
&& itemType !== ItemKind.Timer
&& itemType !== ItemKind.Book
&& !showFolderRuntime
2024-08-10 04:09:59 +03:00
&& showRuntimeInfo
) {
if (itemType === ItemKind.Audio) {
addMiscInfo({ text: datetime.getDisplayRunningTime(itemRunTimeTicks) });
} else {
addMiscInfo({ text: datetime.getDisplayDuration(itemRunTimeTicks) });
}
}
}
function addOfficialRatingInfo(
itemOfficialRating: NullableString,
itemType: ItemKind,
2024-08-10 04:09:59 +03:00
showOfficialRatingInfo: boolean,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
itemOfficialRating
2024-08-10 04:09:59 +03:00
&& showOfficialRatingInfo
&& itemType !== ItemKind.Season
&& itemType !== ItemKind.Episode
) {
addMiscInfo({
text: itemOfficialRating,
2024-08-10 04:09:59 +03:00
cssClass: 'mediaInfoText mediaInfoOfficialRating'
});
}
}
function addAudioContainer(
itemContainer: NullableString,
2024-08-10 04:09:59 +03:00
showAudioContainerInfo: boolean,
itemType: ItemKind,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-10 04:09:59 +03:00
if (
itemContainer
&& showAudioContainerInfo
&& itemType === ItemKind.Audio
) {
addMiscInfo({ text: itemContainer });
}
}
function addPhotoSize(
itemMediaType: ItemMediaKind,
itemWidth: NullableNumber,
itemHeight: NullableNumber,
addMiscInfo: (val: MiscInfo) => void
): void {
if (itemMediaType === ItemMediaKind.Photo && itemWidth && itemHeight) {
const size = `${itemWidth}x${itemHeight}`;
addMiscInfo({ text: size });
}
}
2024-08-10 04:09:59 +03:00
interface UsePrimaryMediaInfoProps extends PrimaryInfoOpts {
item: ItemDto;
}
function usePrimaryMediaInfo({
item,
2024-08-10 04:09:59 +03:00
showYearInfo = false,
showAudioContainerInfo = false,
showEpisodeTitleInfo = false,
showOriginalAirDateInfo = false,
showRuntimeInfo = false,
showProgramIndicatorInfo = false,
includeEpisodeTitleIndexNumber = false,
showOfficialRatingInfo = false
}: UsePrimaryMediaInfoProps) {
const {
EndDate,
Status,
StartDate,
ProductionYear,
Video3DFormat,
Type,
Width,
Height,
MediaType,
SongCount,
RecordAnyTime,
RecordAnyChannel,
ChannelName,
ChildCount,
RunTimeTicks,
PremiereDate,
OfficialRating,
Container
} = item;
const miscInfo: MiscInfo[] = [];
const addMiscInfo = (val: MiscInfo) => {
if (val) {
miscInfo.push(val);
}
};
const showFolderRuntime = shouldShowFolderRuntime(Type, MediaType);
addTrackCountOrItemCount(
showFolderRuntime,
SongCount,
ChildCount,
RunTimeTicks,
Type,
addMiscInfo
);
addOriginalAirDateInfo(
Type,
MediaType,
2024-08-10 04:09:59 +03:00
showOriginalAirDateInfo,
PremiereDate,
addMiscInfo
);
addSeriesTimerInfo(
Type,
RecordAnyTime,
StartDate,
RecordAnyChannel,
ChannelName,
addMiscInfo
);
addStartDateInfo(StartDate, Type, addMiscInfo);
addSeriesProductionYearInfo(
ProductionYear,
Type,
2024-08-10 04:09:59 +03:00
showYearInfo,
Status,
EndDate,
addMiscInfo
);
addProgramIndicators(
item,
2024-08-10 04:09:59 +03:00
showProgramIndicatorInfo,
showEpisodeTitleInfo,
includeEpisodeTitleIndexNumber,
showOriginalAirDateInfo,
showYearInfo,
addMiscInfo
);
addYearInfo(
2024-08-10 04:09:59 +03:00
showYearInfo,
Type,
MediaType,
ProductionYear,
PremiereDate,
addMiscInfo
);
addRunTimeInfo(
RunTimeTicks,
Type,
showFolderRuntime,
2024-08-10 04:09:59 +03:00
showRuntimeInfo,
addMiscInfo
);
addOfficialRatingInfo(
OfficialRating,
Type,
2024-08-10 04:09:59 +03:00
showOfficialRatingInfo,
addMiscInfo
);
addVideo3DFormat(Video3DFormat, addMiscInfo);
addPhotoSize(MediaType, Width, Height, addMiscInfo);
2024-08-10 04:09:59 +03:00
addAudioContainer(Container, showAudioContainerInfo, Type, addMiscInfo);
return miscInfo;
}
export default usePrimaryMediaInfo;