Video.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { useEffect, useRef, useState } from "react";
  2. import players from "video.js";
  3. import VideoPlayer from "./VideoPlayer";
  4. import type { IAttachmentResponse } from "../../api/Attachments";
  5. import { get } from "../../request";
  6. interface IWidget {
  7. fileName?: string;
  8. fileId?: string;
  9. src?: string;
  10. type?: string;
  11. }
  12. const VideoWidget = ({ _____fileName, fileId, src, type }: IWidget) => {
  13. const playerRef = useRef<players.Player>();
  14. const [url, setUrl] = useState<string>();
  15. useEffect(() => {
  16. if (fileId) {
  17. const url = `/v2/attachment/${fileId}`;
  18. console.info("VideoWidget api request", url);
  19. get<IAttachmentResponse>(url).then((json) => {
  20. console.debug("VideoWidget api response", json);
  21. if (json.ok) {
  22. setUrl(json.data.url);
  23. }
  24. });
  25. }
  26. }, [fileId]);
  27. useEffect(() => {
  28. if (src) {
  29. setUrl(src);
  30. }
  31. }, [src]);
  32. const handlePlayerReady = (player: players.Player) => {
  33. if (playerRef.current) {
  34. playerRef.current = player;
  35. player.on("waiting", () => {
  36. console.log("player is waiting");
  37. });
  38. player.on("dispose", () => {
  39. console.log("player will dispose");
  40. });
  41. }
  42. };
  43. return (
  44. <VideoPlayer
  45. options={{
  46. autoplay: false,
  47. controls: true,
  48. responsive: true,
  49. fluid: true,
  50. poster: "",
  51. sources: [
  52. {
  53. src: url ? url : "",
  54. type: type ? type : "video/mp4",
  55. },
  56. ],
  57. }}
  58. onReady={handlePlayerReady}
  59. />
  60. );
  61. };
  62. export default VideoWidget;