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

564 lines
15 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';
2024-08-22 00:50:43 +03:00
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(
2024-08-22 00:50:43 +03:00
showFolderRuntimeInfo: boolean,
itemType: ItemKind,
itemMediaType: ItemMediaKind
): boolean {
return (
2024-08-22 00:50:43 +03:00
showFolderRuntimeInfo
&& (itemType === ItemKind.MusicAlbum
|| itemMediaType === ItemMediaKind.MusicArtist
|| itemType === ItemKind.Playlist
|| itemMediaType === ItemMediaKind.Playlist
|| itemMediaType === ItemMediaKind.MusicGenre)
);
}
function addTrackCountOrItemCount(
2024-08-22 00:50:43 +03:00
isFolderRuntimeEnabled: boolean,
showItemCountInfo: boolean,
itemSongCount: NullableNumber,
itemChildCount: NullableNumber,
itemRunTimeTicks: NullableNumber,
2024-08-10 04:09:59 +03:00
itemType: ItemKind,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-22 00:50:43 +03:00
if (isFolderRuntimeEnabled) {
const count = itemSongCount || itemChildCount;
if (count) {
addMiscInfo({ text: globalize.translate('TrackCount', count) });
}
if (itemRunTimeTicks) {
2024-08-22 00:50:43 +03:00
addMiscInfo({
text: datetime.getDisplayDuration(itemRunTimeTicks)
});
}
2024-08-22 00:50:43 +03:00
} else if (
showItemCountInfo
&& (itemType === ItemKind.PhotoAlbum || itemType === ItemKind.BoxSet)
) {
const count = itemChildCount;
if (count) {
addMiscInfo({ text: globalize.translate('ItemCount', count) });
}
}
}
function addOriginalAirDateInfo(
2024-08-22 00:50:43 +03:00
showOriginalAirDateInfo: boolean,
itemType: ItemKind,
itemMediaType: ItemMediaKind,
itemPremiereDate: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
2024-08-22 00:50:43 +03:00
showOriginalAirDateInfo
&& (itemType === ItemKind.Episode
|| itemMediaType === ItemMediaKind.Photo)
&& itemPremiereDate
) {
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 {
console.error('error parsing date:', itemPremiereDate);
}
}
}
function addSeriesTimerInfo(
2024-08-22 00:50:43 +03:00
showSeriesTimerInfo: boolean,
itemType: ItemKind,
itemRecordAnyTime: boolean | undefined,
itemStartDate: NullableString,
itemRecordAnyChannel: boolean | undefined,
itemChannelName: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-22 00:50:43 +03:00
if (showSeriesTimerInfo && 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({
2024-08-22 00:50:43 +03:00
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(
2024-08-10 04:09:59 +03:00
showYearInfo: boolean,
showEpisodeTitleInfo: boolean,
showOriginalAirDateInfo: boolean,
showProgramIndicatorInfo: boolean,
includeEpisodeTitleIndexNumber: boolean,
2024-08-22 00:50:43 +03:00
item: ItemDto,
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-22 00:50:43 +03:00
if (showProgramIndicatorInfo) {
addProgramIndicatorInfo(program, addMiscInfo);
}
addProgramTextInfo(
2024-08-10 04:09:59 +03:00
showEpisodeTitleInfo,
includeEpisodeTitleIndexNumber,
showOriginalAirDateInfo,
showYearInfo,
2024-08-22 00:50:43 +03:00
program,
addMiscInfo
);
}
}
function addProgramTextInfo(
2024-08-10 04:09:59 +03:00
showEpisodeTitleInfo: boolean,
includeEpisodeTitleIndexNumber: boolean,
showOriginalAirDateInfo: boolean,
showYearInfo: boolean,
2024-08-22 00:50:43 +03:00
program: ItemDto,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-22 00:50:43 +03:00
if (showEpisodeTitleInfo && (program.IsSeries || program.EpisodeTitle)) {
const text = itemHelper.getDisplayName(program, {
2024-08-10 04:09:59 +03:00
includeIndexNumber: includeEpisodeTitleIndexNumber
});
if (text) {
addMiscInfo({ text: text });
}
} else if (
2024-08-22 00:50:43 +03:00
((showOriginalAirDateInfo && program.IsMovie) || showYearInfo)
&& program.ProductionYear
) {
addMiscInfo({ text: program.ProductionYear });
2024-08-22 00:50:43 +03:00
} else if (showOriginalAirDateInfo && program.PremiereDate) {
try {
const date = datetime.parseISO8601Date(program.PremiereDate);
const text = globalize.translate(
'OriginalAirDateValue',
datetime.toLocaleDateString(date)
);
addMiscInfo({ text: text });
} catch {
console.error('error parsing date:', program.PremiereDate);
}
}
}
function addStartDateInfo(
2024-08-22 00:50:43 +03:00
showStartDateInfo: boolean,
itemStartDate: NullableString,
itemType: ItemKind,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
2024-08-22 00:50:43 +03:00
showStartDateInfo
&& 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 {
console.error('error parsing date:', itemStartDate);
}
}
}
function addSeriesProductionYearInfo(
2024-08-22 00:50:43 +03:00
showYearInfo: boolean,
itemProductionYear: NullableNumber,
itemType: ItemKind,
itemStatus: ItemStatus,
itemEndDate: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-22 00:50:43 +03:00
if (showYearInfo && itemProductionYear && itemType === ItemKind.Series) {
if (itemStatus === ItemStatus.Continuing) {
addMiscInfo({
text: globalize.translate(
'SeriesYearToPresent',
datetime.toLocaleString(itemProductionYear, {
useGrouping: false
})
)
});
} else {
2024-08-22 00:50:43 +03:00
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 {
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 {
console.error('error parsing date:', itemPremiereDate);
}
}
}
}
function addVideo3DFormat(
2024-08-22 00:50:43 +03:00
showVideo3DFormatInfo: boolean,
itemVideo3DFormat: NullableString,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-22 00:50:43 +03:00
if (showVideo3DFormatInfo && itemVideo3DFormat) {
addMiscInfo({ text: '3D' });
}
}
function addRunTimeInfo(
2024-08-22 00:50:43 +03:00
isFolderRuntimeEnabled: boolean,
showRuntimeInfo: boolean,
itemRunTimeTicks: NullableNumber,
itemType: ItemKind,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
2024-08-22 00:50:43 +03:00
!isFolderRuntimeEnabled
&& showRuntimeInfo
&& itemRunTimeTicks
&& itemType !== ItemKind.Series
&& itemType !== ItemKind.Program
&& itemType !== ItemKind.Timer
&& itemType !== ItemKind.Book
) {
if (itemType === ItemKind.Audio) {
2024-08-22 00:50:43 +03:00
addMiscInfo({
text: datetime.getDisplayRunningTime(itemRunTimeTicks)
});
} else {
2024-08-22 00:50:43 +03:00
addMiscInfo({
text: datetime.getDisplayDuration(itemRunTimeTicks)
});
}
}
}
function addOfficialRatingInfo(
2024-08-22 00:50:43 +03:00
showOfficialRatingInfo: boolean,
itemOfficialRating: NullableString,
itemType: ItemKind,
addMiscInfo: (val: MiscInfo) => void
): void {
if (
2024-08-22 00:50:43 +03:00
showOfficialRatingInfo
&& itemOfficialRating
&& itemType !== ItemKind.Season
&& itemType !== ItemKind.Episode
) {
addMiscInfo({
text: itemOfficialRating,
2024-08-10 04:09:59 +03:00
cssClass: 'mediaInfoText mediaInfoOfficialRating'
});
}
}
function addAudioContainer(
2024-08-10 04:09:59 +03:00
showAudioContainerInfo: boolean,
2024-08-22 00:50:43 +03:00
itemContainer: NullableString,
itemType: ItemKind,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-10 04:09:59 +03:00
if (
2024-08-22 00:50:43 +03:00
showAudioContainerInfo
&& itemContainer
2024-08-10 04:09:59 +03:00
&& itemType === ItemKind.Audio
) {
addMiscInfo({ text: itemContainer });
}
}
function addPhotoSize(
2024-08-22 00:50:43 +03:00
showPhotoSizeInfo: boolean,
itemMediaType: ItemMediaKind,
itemWidth: NullableNumber,
itemHeight: NullableNumber,
addMiscInfo: (val: MiscInfo) => void
): void {
2024-08-22 00:50:43 +03:00
if (
showPhotoSizeInfo
&& 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,
2024-08-22 00:50:43 +03:00
showFolderRuntimeInfo = false,
2024-08-10 04:09:59 +03:00
showRuntimeInfo = false,
2024-08-22 00:50:43 +03:00
showItemCountInfo = false,
showSeriesTimerInfo = false,
showStartDateInfo = false,
2024-08-10 04:09:59 +03:00
showProgramIndicatorInfo = false,
includeEpisodeTitleIndexNumber = false,
2024-08-22 00:50:43 +03:00
showOfficialRatingInfo = false,
showVideo3DFormatInfo = false,
showPhotoSizeInfo = 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);
}
};
2024-08-22 00:50:43 +03:00
const isFolderRuntimeEnabled = shouldShowFolderRuntime(
showFolderRuntimeInfo,
Type,
MediaType
);
addTrackCountOrItemCount(
2024-08-22 00:50:43 +03:00
isFolderRuntimeEnabled,
showItemCountInfo,
SongCount,
ChildCount,
RunTimeTicks,
Type,
addMiscInfo
);
addOriginalAirDateInfo(
2024-08-22 00:50:43 +03:00
showOriginalAirDateInfo,
Type,
MediaType,
PremiereDate,
addMiscInfo
);
addSeriesTimerInfo(
2024-08-22 00:50:43 +03:00
showSeriesTimerInfo,
Type,
RecordAnyTime,
StartDate,
RecordAnyChannel,
ChannelName,
addMiscInfo
);
2024-08-22 00:50:43 +03:00
addStartDateInfo(showStartDateInfo, StartDate, Type, addMiscInfo);
addSeriesProductionYearInfo(
2024-08-22 00:50:43 +03:00
showYearInfo,
ProductionYear,
Type,
Status,
EndDate,
addMiscInfo
);
addProgramIndicators(
2024-08-10 04:09:59 +03:00
showProgramIndicatorInfo,
showEpisodeTitleInfo,
includeEpisodeTitleIndexNumber,
showOriginalAirDateInfo,
showYearInfo,
2024-08-22 00:50:43 +03:00
item,
addMiscInfo
);
addYearInfo(
2024-08-10 04:09:59 +03:00
showYearInfo,
Type,
MediaType,
ProductionYear,
PremiereDate,
addMiscInfo
);
addRunTimeInfo(
2024-08-22 00:50:43 +03:00
isFolderRuntimeEnabled,
showRuntimeInfo,
RunTimeTicks,
Type,
addMiscInfo
);
addOfficialRatingInfo(
2024-08-22 00:50:43 +03:00
showOfficialRatingInfo,
OfficialRating,
Type,
addMiscInfo
);
2024-08-22 00:50:43 +03:00
addVideo3DFormat(showVideo3DFormatInfo, Video3DFormat, addMiscInfo);
2024-08-22 00:50:43 +03:00
addPhotoSize(showPhotoSizeInfo, MediaType, Width, Height, addMiscInfo);
2024-08-22 00:50:43 +03:00
addAudioContainer(showAudioContainerInfo, Container, Type, addMiscInfo);
return miscInfo;
}
export default usePrimaryMediaInfo;