@mottosports/motto-video-player 1.0.1-rc.51 → 1.0.1-rc.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1115,7 +1115,7 @@ html[dir=rtl] .shaka-overflow-menu .shaka-overflow-button .material-svg-icon:fir
1115
1115
  `);
1116
1116
 
1117
1117
  // src/Player.tsx
1118
- import { forwardRef, useEffect as useEffect5, useRef as useRef8, useImperativeHandle, useCallback as useCallback8 } from "react";
1118
+ import { forwardRef, useEffect as useEffect5, useRef as useRef8, useImperativeHandle, useCallback as useCallback8, useState as useState4 } from "react";
1119
1119
  import shaka3 from "shaka-player/dist/shaka-player.ui";
1120
1120
 
1121
1121
  // src/hooks/useShakaPlayer.ts
@@ -1170,7 +1170,7 @@ var supportsWidevinePersistentLicenses = () => {
1170
1170
  import initShakaPlayerMux from "@mux/mux-data-shakaplayer";
1171
1171
 
1172
1172
  // package.json
1173
- var version = "1.0.1-rc.51";
1173
+ var version = "1.0.1-rc.53";
1174
1174
 
1175
1175
  // src/utils/licenseCache.ts
1176
1176
  var PERSISTENT_LICENSE_PREFIX = "motto_lic_";
@@ -3557,6 +3557,81 @@ html[dir=rtl] .shaka-overflow-menu .shaka-overflow-button .material-svg-icon:fir
3557
3557
 
3558
3558
  // src/Player.tsx
3559
3559
  import { twMerge as twMerge2 } from "tailwind-merge";
3560
+
3561
+ // src/utils/scriptLoader.ts
3562
+ var SCRIPT_CONFIGS = {
3563
+ ima: {
3564
+ src: "https://imasdk.googleapis.com/js/sdkloader/ima3.js",
3565
+ id: "ima-sdk",
3566
+ type: "text/javascript"
3567
+ },
3568
+ system73: {
3569
+ src: "//cdn.s73cloud.com/4/s73-sdk-shakaplayer.js",
3570
+ id: "system73-sdk",
3571
+ type: "application/javascript"
3572
+ }
3573
+ };
3574
+ function loadScript(config) {
3575
+ return new Promise((resolve, reject) => {
3576
+ if (document.getElementById(config.id)) {
3577
+ resolve();
3578
+ return;
3579
+ }
3580
+ const script = document.createElement("script");
3581
+ script.id = config.id;
3582
+ script.src = config.src;
3583
+ script.type = config.type || "text/javascript";
3584
+ script.async = true;
3585
+ script.onload = () => resolve();
3586
+ script.onerror = () => reject(new Error(`Failed to load script: ${config.src}`));
3587
+ document.head.appendChild(script);
3588
+ });
3589
+ }
3590
+ function getRequiredScriptLoaders(needsIma, needsSystem73) {
3591
+ const loaders = [];
3592
+ if (needsIma) {
3593
+ loaders.push(loadScript(SCRIPT_CONFIGS.ima));
3594
+ }
3595
+ if (needsSystem73) {
3596
+ loaders.push(loadScript(SCRIPT_CONFIGS.system73));
3597
+ }
3598
+ return loaders;
3599
+ }
3600
+ async function loadScripts(scriptLoaders) {
3601
+ if (scriptLoaders.length === 0) {
3602
+ return;
3603
+ }
3604
+ try {
3605
+ await Promise.all(scriptLoaders);
3606
+ } catch (error) {
3607
+ console.error("Error loading scripts:", error);
3608
+ throw error;
3609
+ }
3610
+ }
3611
+ function waitForGlobal(globalName, timeout = 5e3) {
3612
+ return new Promise((resolve, reject) => {
3613
+ const startTime = Date.now();
3614
+ function checkGlobal() {
3615
+ const global = window[globalName];
3616
+ if (global) {
3617
+ resolve(global);
3618
+ return;
3619
+ }
3620
+ if (Date.now() - startTime > timeout) {
3621
+ reject(new Error(`Timeout waiting for global: ${globalName}`));
3622
+ return;
3623
+ }
3624
+ setTimeout(checkGlobal, 100);
3625
+ }
3626
+ checkGlobal();
3627
+ });
3628
+ }
3629
+ async function waitForGlobals(globalNames, timeout = 5e3) {
3630
+ const promises = globalNames.map((name) => waitForGlobal(name, timeout));
3631
+ await Promise.all(promises);
3632
+ }
3633
+
3634
+ // src/Player.tsx
3560
3635
  import { jsx as jsx8, jsxs as jsxs6 } from "react/jsx-runtime";
3561
3636
  var Player = forwardRef(
3562
3637
  ({
@@ -3584,6 +3659,7 @@ var Player = forwardRef(
3584
3659
  }, ref) => {
3585
3660
  const videoRef = useRef8(null);
3586
3661
  const containerRef = useRef8(null);
3662
+ const [isScriptsLoaded, setIsScriptsLoaded] = useState4(false);
3587
3663
  useImperativeHandle(ref, () => videoRef.current, []);
3588
3664
  const { playerRef, initializePlayer, destroyPlayer, isRetrying } = useShakaPlayer({
3589
3665
  src,
@@ -3674,7 +3750,11 @@ var Player = forwardRef(
3674
3750
  if (!imaConfig?.adTagUrl || !playerRef.current || !videoRef.current || !uiRef.current) {
3675
3751
  return;
3676
3752
  }
3677
- console.log("Initializing ads...");
3753
+ if (!window.google?.ima) {
3754
+ console.error("Google IMA SDK not available when trying to initialize ads");
3755
+ return;
3756
+ }
3757
+ console.log("Initializing ads with autoplay:", autoPlay);
3678
3758
  try {
3679
3759
  const player = playerRef.current;
3680
3760
  const video = videoRef.current;
@@ -3686,23 +3766,53 @@ var Player = forwardRef(
3686
3766
  console.error("Ad manager not available");
3687
3767
  return;
3688
3768
  }
3689
- adManager.initClientSide(container, video);
3690
3769
  const google = window.google;
3770
+ let adsRenderingSettings = null;
3771
+ if (imaConfig.adsRenderingSettings) {
3772
+ adsRenderingSettings = imaConfig.adsRenderingSettings;
3773
+ } else if (autoPlay) {
3774
+ adsRenderingSettings = new google.ima.AdsRenderingSettings();
3775
+ adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
3776
+ }
3777
+ adManager.initClientSide(container, video, adsRenderingSettings);
3691
3778
  const adsRequest = new google.ima.AdsRequest();
3692
3779
  adsRequest.adTagUrl = imaConfig.adTagUrl;
3693
3780
  adManager.requestClientSideAds(adsRequest);
3694
- console.log("Ads requested with tag:", imaConfig.adTagUrl);
3781
+ console.log("Ads requested successfully with tag:", imaConfig.adTagUrl, "autoplay:", autoPlay);
3695
3782
  } catch (error) {
3696
3783
  console.error("Error initializing ads:", error);
3697
3784
  }
3698
- }, [imaConfig]);
3785
+ }, [imaConfig, autoPlay]);
3786
+ useEffect5(() => {
3787
+ const loadRequiredScripts = async () => {
3788
+ try {
3789
+ const scriptLoaders = getRequiredScriptLoaders(!!imaConfig?.adTagUrl, !!system73Config?.apiKey);
3790
+ await loadScripts(scriptLoaders);
3791
+ const globalsToWait = [];
3792
+ if (imaConfig?.adTagUrl) {
3793
+ globalsToWait.push("google");
3794
+ }
3795
+ if (system73Config?.apiKey) {
3796
+ globalsToWait.push("S73ShakaPlayerWrapper");
3797
+ }
3798
+ if (globalsToWait.length > 0) {
3799
+ await waitForGlobals(globalsToWait);
3800
+ }
3801
+ setIsScriptsLoaded(true);
3802
+ } catch (error) {
3803
+ console.error("Error loading required scripts:", error);
3804
+ setIsScriptsLoaded(true);
3805
+ }
3806
+ };
3807
+ loadRequiredScripts();
3808
+ }, [imaConfig?.adTagUrl, system73Config?.apiKey]);
3699
3809
  useEffect5(() => {
3700
3810
  const video = videoRef.current;
3701
- if (!video) return;
3811
+ if (!video || !isScriptsLoaded) return;
3702
3812
  const initialize = async () => {
3703
3813
  try {
3704
3814
  let system73Wrapper = null;
3705
- if (system73Config?.apiKey) {
3815
+ if (system73Config?.apiKey && window.S73ShakaPlayerWrapper) {
3706
3816
  const playerConfig = { ...shakaConfig };
3707
3817
  system73Wrapper = initializeSystem73(playerConfig);
3708
3818
  if (system73Wrapper) {
@@ -3718,7 +3828,8 @@ var Player = forwardRef(
3718
3828
  const cleanupQuality = setupQualityTracking();
3719
3829
  configureQuality();
3720
3830
  await initializeUI();
3721
- if (imaConfig) {
3831
+ if (imaConfig?.adTagUrl && window.google?.ima) {
3832
+ console.log("Initializing ads immediately after UI setup");
3722
3833
  initializeAds();
3723
3834
  }
3724
3835
  } catch (error) {
@@ -3733,16 +3844,15 @@ var Player = forwardRef(
3733
3844
  destroyMux();
3734
3845
  destroyPlayer();
3735
3846
  };
3736
- }, [src]);
3847
+ }, [src, isScriptsLoaded]);
3737
3848
  useEffect5(() => {
3738
3849
  const video = videoRef.current;
3739
3850
  if (!video) return;
3740
3851
  video.autoplay = autoPlay;
3741
3852
  video.loop = loop;
3742
- video.muted = muted;
3743
3853
  video.controls = false;
3744
3854
  if (poster) video.poster = poster;
3745
- }, [autoPlay, loop, muted, poster]);
3855
+ }, [autoPlay, loop, muted, poster, imaConfig?.adTagUrl]);
3746
3856
  useEffect5(() => {
3747
3857
  const video = videoRef.current;
3748
3858
  if (!video) return;
@@ -3960,7 +4070,7 @@ var getErrorType = (error, video) => {
3960
4070
  };
3961
4071
 
3962
4072
  // src/messages/useMessages.tsx
3963
- import { useState as useState4, useEffect as useEffect6 } from "react";
4073
+ import { useState as useState5, useEffect as useEffect6 } from "react";
3964
4074
 
3965
4075
  // src/messages/en.json
3966
4076
  var en_default = {
@@ -4262,8 +4372,8 @@ var getBrowserLanguage = () => {
4262
4372
  return availableLanguages[language] ? language : "en";
4263
4373
  };
4264
4374
  var useMessages = (locale) => {
4265
- const [language, setLanguage] = useState4("en");
4266
- const [translations, setTranslations] = useState4(availableLanguages.en);
4375
+ const [language, setLanguage] = useState5("en");
4376
+ const [translations, setTranslations] = useState5(availableLanguages.en);
4267
4377
  useEffect6(() => {
4268
4378
  const lang = !!availableLanguages?.[locale] ? locale : getBrowserLanguage();
4269
4379
  ;
@@ -4378,7 +4488,7 @@ var Video = ({
4378
4488
  };
4379
4489
 
4380
4490
  // src/Event.tsx
4381
- import { useCallback as useCallback9, useEffect as useEffect8, useState as useState5 } from "react";
4491
+ import { useCallback as useCallback9, useEffect as useEffect8, useState as useState6 } from "react";
4382
4492
  import { twMerge as twMerge4 } from "tailwind-merge";
4383
4493
  import { useQuery as useQuery2 } from "@tanstack/react-query";
4384
4494
  import { Fragment, jsx as jsx10, jsxs as jsxs8 } from "react/jsx-runtime";
@@ -4411,8 +4521,8 @@ var Event = ({
4411
4521
  retry: queryOptions.retry ?? 3,
4412
4522
  retryDelay: queryOptions.retryDelay ?? ((attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 3e4))
4413
4523
  });
4414
- const [activePlaylist, setActivePlaylist] = useState5();
4415
- const [activeVideoId, setActiveVideoId] = useState5();
4524
+ const [activePlaylist, setActivePlaylist] = useState6();
4525
+ const [activeVideoId, setActiveVideoId] = useState6();
4416
4526
  const videoIds = eventData?.videoIds ?? [];
4417
4527
  const {
4418
4528
  data: videosData,
@@ -4428,7 +4538,7 @@ var Event = ({
4428
4538
  retry: queryOptions.retry ?? 3,
4429
4539
  retryDelay: queryOptions.retryDelay ?? ((attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 3e4))
4430
4540
  });
4431
- const [loadingApisState, setLoadingApisState] = useState5(true);
4541
+ const [loadingApisState, setLoadingApisState] = useState6(true);
4432
4542
  useEffect8(() => {
4433
4543
  if (videosData !== void 0) {
4434
4544
  setLoadingApisState(false);
@@ -4476,8 +4586,8 @@ var Event = ({
4476
4586
  }
4477
4587
  }
4478
4588
  }, [activeVideoId, videosData, events]);
4479
- const [error, setError] = useState5(null);
4480
- const [loadingPlaylist, setLoadingPlaylist] = useState5(true);
4589
+ const [error, setError] = useState6(null);
4590
+ const [loadingPlaylist, setLoadingPlaylist] = useState6(true);
4481
4591
  const videosDataError = videosData?.some((video) => !!video.error);
4482
4592
  useEffect8(() => {
4483
4593
  if (isEventLoading || videosIsLoading || loadingApisState) {
@@ -4576,7 +4686,7 @@ function PreEvent({
4576
4686
  }) {
4577
4687
  const date = new Date(event.startTime);
4578
4688
  const now = /* @__PURE__ */ new Date();
4579
- const [remainingTime, setRemainingTime] = useState5(
4689
+ const [remainingTime, setRemainingTime] = useState6(
4580
4690
  date.getTime() - now.getTime()
4581
4691
  );
4582
4692
  const shouldBeStarted = remainingTime < 0;
@@ -4720,7 +4830,7 @@ var TitleAndDescription = ({
4720
4830
  };
4721
4831
 
4722
4832
  // src/CreativeWork.tsx
4723
- import { useEffect as useEffect9, useState as useState6 } from "react";
4833
+ import { useEffect as useEffect9, useState as useState7 } from "react";
4724
4834
  import { twMerge as twMerge5 } from "tailwind-merge";
4725
4835
  import { useQuery as useQuery3 } from "@tanstack/react-query";
4726
4836
  import { Fragment as Fragment2, jsx as jsx11, jsxs as jsxs9 } from "react/jsx-runtime";
@@ -4753,9 +4863,9 @@ var CreativeWork = ({
4753
4863
  retry: queryOptions.retry ?? 3,
4754
4864
  retryDelay: queryOptions.retryDelay ?? ((attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 3e4))
4755
4865
  });
4756
- const [activePlaylist, setActivePlaylist] = useState6();
4757
- const [activeVideoId, setActiveVideoId] = useState6();
4758
- const [showCountDown, setShowCountDown] = useState6(false);
4866
+ const [activePlaylist, setActivePlaylist] = useState7();
4867
+ const [activeVideoId, setActiveVideoId] = useState7();
4868
+ const [showCountDown, setShowCountDown] = useState7(false);
4759
4869
  const videoIds = creativeWorkData?.videoIds ?? [];
4760
4870
  const {
4761
4871
  data: videosData,
@@ -4771,7 +4881,7 @@ var CreativeWork = ({
4771
4881
  retry: queryOptions.retry ?? 3,
4772
4882
  retryDelay: queryOptions.retryDelay ?? ((attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 3e4))
4773
4883
  });
4774
- const [loadingApisState, setLoadingApisState] = useState6(true);
4884
+ const [loadingApisState, setLoadingApisState] = useState7(true);
4775
4885
  useEffect9(() => {
4776
4886
  if (videosData !== void 0) {
4777
4887
  setLoadingApisState(false);
@@ -4822,7 +4932,7 @@ var CreativeWork = ({
4822
4932
  }
4823
4933
  }
4824
4934
  }, [activeVideoId, videosData, events]);
4825
- const [error, setError] = useState6(null);
4935
+ const [error, setError] = useState7(null);
4826
4936
  const videosDataError = videosData?.some((video) => !!video.error);
4827
4937
  useEffect9(() => {
4828
4938
  if (creativeWorkError || videosError || videosDataError) {
@@ -4846,7 +4956,7 @@ var CreativeWork = ({
4846
4956
  }
4847
4957
  ) }) });
4848
4958
  }
4849
- const [loadingPlaylist, setLoadingPlaylist] = useState6(true);
4959
+ const [loadingPlaylist, setLoadingPlaylist] = useState7(true);
4850
4960
  useEffect9(() => {
4851
4961
  const creativeWorkLoadedWithNoVideos = !isCreativeWorkLoading && creativeWorkData && creativeWorkData.videoIds && creativeWorkData.videoIds.length === 0;
4852
4962
  const creativeWorkLoadedWithNoData = !isCreativeWorkLoading && creativeWorkData && !creativeWorkData.videoIds;
@@ -4923,7 +5033,7 @@ function PreCreativeWork({
4923
5033
  }) {
4924
5034
  const date = new Date(creativeWork.releaseTime);
4925
5035
  const now = /* @__PURE__ */ new Date();
4926
- const [remainingTime, setRemainingTime] = useState6(
5036
+ const [remainingTime, setRemainingTime] = useState7(
4927
5037
  date.getTime() - now.getTime()
4928
5038
  );
4929
5039
  const shouldBeStarted = remainingTime < 0;