@mottosports/motto-video-player 1.0.1-rc.52 → 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.52";
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
  ({
@@ -3582,9 +3657,9 @@ var Player = forwardRef(
3582
3657
  containerClassName,
3583
3658
  ...videoProps
3584
3659
  }, ref) => {
3585
- console.log("system73Config", system73Config);
3586
3660
  const videoRef = useRef8(null);
3587
3661
  const containerRef = useRef8(null);
3662
+ const [isScriptsLoaded, setIsScriptsLoaded] = useState4(false);
3588
3663
  useImperativeHandle(ref, () => videoRef.current, []);
3589
3664
  const { playerRef, initializePlayer, destroyPlayer, isRetrying } = useShakaPlayer({
3590
3665
  src,
@@ -3675,7 +3750,11 @@ var Player = forwardRef(
3675
3750
  if (!imaConfig?.adTagUrl || !playerRef.current || !videoRef.current || !uiRef.current) {
3676
3751
  return;
3677
3752
  }
3678
- 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);
3679
3758
  try {
3680
3759
  const player = playerRef.current;
3681
3760
  const video = videoRef.current;
@@ -3687,23 +3766,53 @@ var Player = forwardRef(
3687
3766
  console.error("Ad manager not available");
3688
3767
  return;
3689
3768
  }
3690
- adManager.initClientSide(container, video);
3691
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);
3692
3778
  const adsRequest = new google.ima.AdsRequest();
3693
3779
  adsRequest.adTagUrl = imaConfig.adTagUrl;
3694
3780
  adManager.requestClientSideAds(adsRequest);
3695
- console.log("Ads requested with tag:", imaConfig.adTagUrl);
3781
+ console.log("Ads requested successfully with tag:", imaConfig.adTagUrl, "autoplay:", autoPlay);
3696
3782
  } catch (error) {
3697
3783
  console.error("Error initializing ads:", error);
3698
3784
  }
3699
- }, [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]);
3700
3809
  useEffect5(() => {
3701
3810
  const video = videoRef.current;
3702
- if (!video) return;
3811
+ if (!video || !isScriptsLoaded) return;
3703
3812
  const initialize = async () => {
3704
3813
  try {
3705
3814
  let system73Wrapper = null;
3706
- if (system73Config?.apiKey) {
3815
+ if (system73Config?.apiKey && window.S73ShakaPlayerWrapper) {
3707
3816
  const playerConfig = { ...shakaConfig };
3708
3817
  system73Wrapper = initializeSystem73(playerConfig);
3709
3818
  if (system73Wrapper) {
@@ -3719,7 +3828,8 @@ var Player = forwardRef(
3719
3828
  const cleanupQuality = setupQualityTracking();
3720
3829
  configureQuality();
3721
3830
  await initializeUI();
3722
- if (imaConfig) {
3831
+ if (imaConfig?.adTagUrl && window.google?.ima) {
3832
+ console.log("Initializing ads immediately after UI setup");
3723
3833
  initializeAds();
3724
3834
  }
3725
3835
  } catch (error) {
@@ -3734,16 +3844,15 @@ var Player = forwardRef(
3734
3844
  destroyMux();
3735
3845
  destroyPlayer();
3736
3846
  };
3737
- }, [src]);
3847
+ }, [src, isScriptsLoaded]);
3738
3848
  useEffect5(() => {
3739
3849
  const video = videoRef.current;
3740
3850
  if (!video) return;
3741
3851
  video.autoplay = autoPlay;
3742
3852
  video.loop = loop;
3743
- video.muted = muted;
3744
3853
  video.controls = false;
3745
3854
  if (poster) video.poster = poster;
3746
- }, [autoPlay, loop, muted, poster]);
3855
+ }, [autoPlay, loop, muted, poster, imaConfig?.adTagUrl]);
3747
3856
  useEffect5(() => {
3748
3857
  const video = videoRef.current;
3749
3858
  if (!video) return;
@@ -3961,7 +4070,7 @@ var getErrorType = (error, video) => {
3961
4070
  };
3962
4071
 
3963
4072
  // src/messages/useMessages.tsx
3964
- import { useState as useState4, useEffect as useEffect6 } from "react";
4073
+ import { useState as useState5, useEffect as useEffect6 } from "react";
3965
4074
 
3966
4075
  // src/messages/en.json
3967
4076
  var en_default = {
@@ -4263,8 +4372,8 @@ var getBrowserLanguage = () => {
4263
4372
  return availableLanguages[language] ? language : "en";
4264
4373
  };
4265
4374
  var useMessages = (locale) => {
4266
- const [language, setLanguage] = useState4("en");
4267
- const [translations, setTranslations] = useState4(availableLanguages.en);
4375
+ const [language, setLanguage] = useState5("en");
4376
+ const [translations, setTranslations] = useState5(availableLanguages.en);
4268
4377
  useEffect6(() => {
4269
4378
  const lang = !!availableLanguages?.[locale] ? locale : getBrowserLanguage();
4270
4379
  ;
@@ -4379,7 +4488,7 @@ var Video = ({
4379
4488
  };
4380
4489
 
4381
4490
  // src/Event.tsx
4382
- 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";
4383
4492
  import { twMerge as twMerge4 } from "tailwind-merge";
4384
4493
  import { useQuery as useQuery2 } from "@tanstack/react-query";
4385
4494
  import { Fragment, jsx as jsx10, jsxs as jsxs8 } from "react/jsx-runtime";
@@ -4412,8 +4521,8 @@ var Event = ({
4412
4521
  retry: queryOptions.retry ?? 3,
4413
4522
  retryDelay: queryOptions.retryDelay ?? ((attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 3e4))
4414
4523
  });
4415
- const [activePlaylist, setActivePlaylist] = useState5();
4416
- const [activeVideoId, setActiveVideoId] = useState5();
4524
+ const [activePlaylist, setActivePlaylist] = useState6();
4525
+ const [activeVideoId, setActiveVideoId] = useState6();
4417
4526
  const videoIds = eventData?.videoIds ?? [];
4418
4527
  const {
4419
4528
  data: videosData,
@@ -4429,7 +4538,7 @@ var Event = ({
4429
4538
  retry: queryOptions.retry ?? 3,
4430
4539
  retryDelay: queryOptions.retryDelay ?? ((attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 3e4))
4431
4540
  });
4432
- const [loadingApisState, setLoadingApisState] = useState5(true);
4541
+ const [loadingApisState, setLoadingApisState] = useState6(true);
4433
4542
  useEffect8(() => {
4434
4543
  if (videosData !== void 0) {
4435
4544
  setLoadingApisState(false);
@@ -4477,8 +4586,8 @@ var Event = ({
4477
4586
  }
4478
4587
  }
4479
4588
  }, [activeVideoId, videosData, events]);
4480
- const [error, setError] = useState5(null);
4481
- const [loadingPlaylist, setLoadingPlaylist] = useState5(true);
4589
+ const [error, setError] = useState6(null);
4590
+ const [loadingPlaylist, setLoadingPlaylist] = useState6(true);
4482
4591
  const videosDataError = videosData?.some((video) => !!video.error);
4483
4592
  useEffect8(() => {
4484
4593
  if (isEventLoading || videosIsLoading || loadingApisState) {
@@ -4577,7 +4686,7 @@ function PreEvent({
4577
4686
  }) {
4578
4687
  const date = new Date(event.startTime);
4579
4688
  const now = /* @__PURE__ */ new Date();
4580
- const [remainingTime, setRemainingTime] = useState5(
4689
+ const [remainingTime, setRemainingTime] = useState6(
4581
4690
  date.getTime() - now.getTime()
4582
4691
  );
4583
4692
  const shouldBeStarted = remainingTime < 0;
@@ -4721,7 +4830,7 @@ var TitleAndDescription = ({
4721
4830
  };
4722
4831
 
4723
4832
  // src/CreativeWork.tsx
4724
- import { useEffect as useEffect9, useState as useState6 } from "react";
4833
+ import { useEffect as useEffect9, useState as useState7 } from "react";
4725
4834
  import { twMerge as twMerge5 } from "tailwind-merge";
4726
4835
  import { useQuery as useQuery3 } from "@tanstack/react-query";
4727
4836
  import { Fragment as Fragment2, jsx as jsx11, jsxs as jsxs9 } from "react/jsx-runtime";
@@ -4754,9 +4863,9 @@ var CreativeWork = ({
4754
4863
  retry: queryOptions.retry ?? 3,
4755
4864
  retryDelay: queryOptions.retryDelay ?? ((attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 3e4))
4756
4865
  });
4757
- const [activePlaylist, setActivePlaylist] = useState6();
4758
- const [activeVideoId, setActiveVideoId] = useState6();
4759
- const [showCountDown, setShowCountDown] = useState6(false);
4866
+ const [activePlaylist, setActivePlaylist] = useState7();
4867
+ const [activeVideoId, setActiveVideoId] = useState7();
4868
+ const [showCountDown, setShowCountDown] = useState7(false);
4760
4869
  const videoIds = creativeWorkData?.videoIds ?? [];
4761
4870
  const {
4762
4871
  data: videosData,
@@ -4772,7 +4881,7 @@ var CreativeWork = ({
4772
4881
  retry: queryOptions.retry ?? 3,
4773
4882
  retryDelay: queryOptions.retryDelay ?? ((attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 3e4))
4774
4883
  });
4775
- const [loadingApisState, setLoadingApisState] = useState6(true);
4884
+ const [loadingApisState, setLoadingApisState] = useState7(true);
4776
4885
  useEffect9(() => {
4777
4886
  if (videosData !== void 0) {
4778
4887
  setLoadingApisState(false);
@@ -4823,7 +4932,7 @@ var CreativeWork = ({
4823
4932
  }
4824
4933
  }
4825
4934
  }, [activeVideoId, videosData, events]);
4826
- const [error, setError] = useState6(null);
4935
+ const [error, setError] = useState7(null);
4827
4936
  const videosDataError = videosData?.some((video) => !!video.error);
4828
4937
  useEffect9(() => {
4829
4938
  if (creativeWorkError || videosError || videosDataError) {
@@ -4847,7 +4956,7 @@ var CreativeWork = ({
4847
4956
  }
4848
4957
  ) }) });
4849
4958
  }
4850
- const [loadingPlaylist, setLoadingPlaylist] = useState6(true);
4959
+ const [loadingPlaylist, setLoadingPlaylist] = useState7(true);
4851
4960
  useEffect9(() => {
4852
4961
  const creativeWorkLoadedWithNoVideos = !isCreativeWorkLoading && creativeWorkData && creativeWorkData.videoIds && creativeWorkData.videoIds.length === 0;
4853
4962
  const creativeWorkLoadedWithNoData = !isCreativeWorkLoading && creativeWorkData && !creativeWorkData.videoIds;
@@ -4924,7 +5033,7 @@ function PreCreativeWork({
4924
5033
  }) {
4925
5034
  const date = new Date(creativeWork.releaseTime);
4926
5035
  const now = /* @__PURE__ */ new Date();
4927
- const [remainingTime, setRemainingTime] = useState6(
5036
+ const [remainingTime, setRemainingTime] = useState7(
4928
5037
  date.getTime() - now.getTime()
4929
5038
  );
4930
5039
  const shouldBeStarted = remainingTime < 0;