Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { createRef } from "react";
|
||||
import { render } from "@testing-library/react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import AudioOrVideoPlayer, { RefAudioOrVideo } from "./AudioOrVideoPlayer";
|
||||
|
||||
describe("AudioOrVideoPlayer", () => {
|
||||
it("renders a caption track for video media by default", () => {
|
||||
const { container } = render(<AudioOrVideoPlayer url="/demo.mp4" type="video" />);
|
||||
|
||||
const video = container.querySelector("video");
|
||||
const track = container.querySelector("track");
|
||||
|
||||
expect(video?.getAttribute("src")).toBe("/demo.mp4");
|
||||
expect(track?.getAttribute("kind")).toBe("captions");
|
||||
expect(track?.getAttribute("src")).toBe("data:text/vtt;charset=utf-8,WEBVTT%0A%0A");
|
||||
expect(track?.getAttribute("srclang")).toBe("en");
|
||||
expect(track?.getAttribute("label")).toBe("Captions");
|
||||
});
|
||||
|
||||
it("renders provided caption metadata and exposes the media ref", () => {
|
||||
const ref = createRef<RefAudioOrVideo>();
|
||||
const { container } = render(
|
||||
<AudioOrVideoPlayer
|
||||
ref={ref}
|
||||
url="/demo.mp3"
|
||||
type="audio"
|
||||
captionTrack={{
|
||||
src: "/captions/de.vtt",
|
||||
srcLang: "de",
|
||||
label: "Deutsch",
|
||||
default: true,
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
|
||||
const audio = container.querySelector("audio");
|
||||
const track = container.querySelector("track");
|
||||
|
||||
expect(ref.current?.media).toBe(audio);
|
||||
expect(track?.getAttribute("kind")).toBe("captions");
|
||||
expect(track?.getAttribute("src")).toBe("/captions/de.vtt");
|
||||
expect(track?.getAttribute("srclang")).toBe("de");
|
||||
expect(track?.getAttribute("label")).toBe("Deutsch");
|
||||
expect(track?.hasAttribute("default")).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
import { forwardRef, useRef, useImperativeHandle, Ref } from "react";
|
||||
import { t as i18nT } from "i18next";
|
||||
|
||||
export interface RefAudioOrVideo {
|
||||
media: HTMLAudioElement | HTMLVideoElement | null;
|
||||
}
|
||||
|
||||
export type MediaCaptionTrack = {
|
||||
src?: string;
|
||||
srcLang?: string;
|
||||
label?: string;
|
||||
default?: boolean;
|
||||
};
|
||||
|
||||
export type AudioOrVideoPlayerProps = {
|
||||
url: string;
|
||||
type: "audio" | "video";
|
||||
captionTrack?: MediaCaptionTrack;
|
||||
};
|
||||
|
||||
const EMPTY_CAPTIONS_VTT = "data:text/vtt;charset=utf-8,WEBVTT%0A%0A";
|
||||
|
||||
function getCaptionTrackProps(track?: MediaCaptionTrack) {
|
||||
return {
|
||||
src: track?.src ?? EMPTY_CAPTIONS_VTT,
|
||||
srcLang: track?.srcLang ?? "en",
|
||||
label: track?.label ?? i18nT("components.media.captions"),
|
||||
default: track?.default,
|
||||
};
|
||||
}
|
||||
|
||||
const AudioOrVideoPlayer = ({ url, type, captionTrack }: AudioOrVideoPlayerProps, ref: Ref<RefAudioOrVideo>) => {
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||
const videoRef = useRef<HTMLVideoElement | null>(null);
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
get media() {
|
||||
return type === "video" ? videoRef.current : audioRef.current;
|
||||
},
|
||||
}));
|
||||
|
||||
const trackProps = getCaptionTrackProps(captionTrack);
|
||||
|
||||
return (
|
||||
<div className="w-full">
|
||||
{type === "video" ? (
|
||||
<video ref={videoRef} src={url} controls className="border-border-default bg-surface-neutral-secondary/90 w-full rounded-md border shadow-2xs">
|
||||
<track kind="captions" {...trackProps} />
|
||||
</video>
|
||||
) : type === "audio" ? (
|
||||
<audio ref={audioRef} src={url} controls className="border-border-default bg-surface-neutral-secondary/90 w-full rounded-md border shadow-2xs">
|
||||
<track kind="captions" {...trackProps} />
|
||||
</audio>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default forwardRef(AudioOrVideoPlayer);
|
||||
@@ -0,0 +1,69 @@
|
||||
import { Dialog, DialogTitle } from "@headlessui/react";
|
||||
import { lazy, Suspense } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { MediaDto } from "~/application/dtos/entities/MediaDto";
|
||||
import ButtonSecondary from "../buttons/ButtonSecondary";
|
||||
import XIcon from "../icons/XIcon";
|
||||
import OpenModal from "../modals/OpenModal";
|
||||
import DownloadIcon from "../icons/DownloadIcon";
|
||||
|
||||
const PdfViewer = lazy(() => import("../pdf/PdfViewer"));
|
||||
|
||||
interface Props {
|
||||
item: MediaDto;
|
||||
onClose: () => void;
|
||||
onDownload: () => void;
|
||||
}
|
||||
|
||||
export default function PreviewMediaModal({ item, onClose, onDownload }: Props) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
function isPdf() {
|
||||
return item.type.endsWith("pdf");
|
||||
}
|
||||
|
||||
function isImage() {
|
||||
return item.type.includes("image");
|
||||
}
|
||||
|
||||
return (
|
||||
<OpenModal onClose={onClose}>
|
||||
<div>
|
||||
{!item ? (
|
||||
<div>{t("shared.notSet")}</div>
|
||||
) : (
|
||||
<div className="">
|
||||
<div className="flex items-center justify-between space-x-2">
|
||||
<DialogTitle as="h3" className="text-foreground truncate text-lg font-medium leading-6">
|
||||
{item.title}
|
||||
</DialogTitle>
|
||||
<div className="flex space-x-2">
|
||||
<ButtonSecondary type="button" onClick={onDownload}>
|
||||
<DownloadIcon className="h-4 w-4" />
|
||||
</ButtonSecondary>
|
||||
<ButtonSecondary type="button" onClick={onClose}>
|
||||
<XIcon className="h-4 w-4" />
|
||||
</ButtonSecondary>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-4 space-y-3">
|
||||
{isPdf() ? (
|
||||
<div>
|
||||
<Suspense fallback={<div className="h-96 animate-pulse rounded-md bg-gray-100" />}>
|
||||
<PdfViewer file={item.publicUrl ?? item.file} canDownload={false} />
|
||||
</Suspense>
|
||||
</div>
|
||||
) : isImage() ? (
|
||||
<div>
|
||||
<img alt={item.title} className="mx-auto h-96 object-contain" src={item.publicUrl ?? item.file} />
|
||||
</div>
|
||||
) : (
|
||||
<div>{t("components.media.notPdfOrImage")}</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</OpenModal>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user