"use client";
import { Fragment } from "react";
import {
Avatar,
Badge,
Box,
Divider,
Group,
Image,
ScrollArea,
SimpleGrid,
Stack,
Text,
Tooltip,
UnstyledButton,
} from "@mantine/core";
import { IconBook, IconCalendar, IconClock, IconStarFilled } from "@tabler/icons-react";
import type { RouterOutputs } from "@homarr/api";
import { clientApi } from "@homarr/api/client";
import { getMantineColor, toValidDate } from "@homarr/common";
import { getIconUrl } from "@homarr/definitions";
import type { MediaRelease } from "@homarr/integrations/types";
import { mediaTypeConfigurations } from "@homarr/integrations/types";
import type { ScopedTranslationFunction } from "@homarr/translation";
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";
import type { TablerIcon } from "@homarr/ui";
import { OverflowBadge } from "@homarr/ui";
import { WidgetEmptyState } from "../common/empty-state";
import type { WidgetComponentProps } from "../definition";
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "../common/application-url";
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
import { getUsableWidgetQueryData, isInitialWidgetQueryPending } from "../common/query-state";
import { WidgetQueryLoadingState } from "../common/query-state-indicator";
import classes from "./component.module.css";
export default function MediaReleasesWidget({
options,
integrationIds,
width,
displayMode,
}: WidgetComponentProps<"mediaReleases">) {
const releasesQuery = clientApi.widget.mediaRelease.getMediaReleases.useQuery({ integrationIds });
const response = getUsableWidgetQueryData(releasesQuery);
if (isInitialWidgetQueryPending(releasesQuery)) return ;
if (!response) return ;
const { releases, failedIntegrations } = response;
const isAdvanced = displayMode === "advanced";
return (
{releases.length === 0 ? (
) : (
{releases.map((item, index) => (
{!isAdvanced && index !== 0 && options.layout === "poster" && }
))}
)}
{failedIntegrations.length > 0 && (
)}
);
}
interface ItemProps {
item: RouterOutputs["widget"]["mediaRelease"]["getMediaReleases"]["releases"][number];
options: WidgetComponentProps<"mediaReleases">["options"];
isAdvanced: boolean;
}
const formatReleaseDate = (value: unknown, locale: string) => {
const date = toValidDate(value);
if (!date) return "—";
return Intl.DateTimeFormat(locale, {
month: "2-digit",
year: "numeric",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
hour12: false,
}).format(date);
};
const Item = ({ item, options, isAdvanced }: ItemProps) => {
const locale = useCurrentIntlLocale();
const t = useI18n("widget.mediaReleases");
const length = formatLength(item.length, item.type, t);
const showPoster = isAdvanced || options.layout === "poster";
const showSide = isAdvanced || options.showType || options.showSource;
const href = getSafeApplicationUrl(item.href);
return (
{!isAdvanced && options.layout === "backdrop" && (
)}
{showPoster && (
)}
{item.title}
{item.subtitle !== undefined && (
{item.subtitle}
)}
{length !== undefined && (
<>
>
)}
{item.producer !== undefined && (
<>
>
)}
{item.rating !== undefined && (
<>
>
)}
{item.price !== undefined && (
<>
>
)}
{item.tags.length > 0 && (
)}
{isAdvanced && item.description && (
{item.description}
)}
{showSide && (
{(isAdvanced || options.showType) && (
{item.type}
)}
{(isAdvanced || options.showSource) &&
(isAdvanced ? (
{item.integration.name}
) : (
))}
)}
);
};
interface IconAndLabelProps {
icon?: TablerIcon;
label: string;
}
const InfoDivider = () => (
•
);
const Info = ({ icon: Icon, label }: IconAndLabelProps) => {
return (
{Icon && }
{label}
);
};
const formatLength = (
length: number | undefined,
type: MediaRelease["type"],
t: ScopedTranslationFunction<"widget.mediaReleases">,
) => {
if (!length) return undefined;
if (type === "movie" || type === "tv" || type === "video" || type === "music" || type === "article") {
return {
type: "duration" as const,
label: t("length.duration", {
length: Math.round(length / 60).toString(),
}),
};
}
if (type === "book") {
return {
type: "page" as const,
label: length.toString(),
};
}
return undefined;
};