@geekapps/silo-elements-nextjs 0.2.48 → 0.2.50

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.
@@ -9,7 +9,13 @@ type SourceProps = {
9
9
  default?: boolean;
10
10
  };
11
11
  type CaptionsProps = {
12
- src: string;
12
+ src?: string;
13
+ data?: Array<{
14
+ src: string;
15
+ srclang: string;
16
+ label: string;
17
+ default?: boolean;
18
+ }>;
13
19
  };
14
20
  type ThumbnailProps = {
15
21
  src: string;
@@ -1,4 +1,4 @@
1
- import React, { useMemo, useState, useEffect, useRef, useCallback } from 'react';
1
+ import React, { useMemo, useState, useRef, useEffect, useCallback } from 'react';
2
2
  import gsap from 'gsap';
3
3
  import { Pause, Play, VolumeX, Volume2, Captions as Captions$1, Settings, Minimize, Maximize } from 'lucide-react';
4
4
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
@@ -42,29 +42,39 @@ function Video({
42
42
  const parsed = useMemo(() => parseVideoChildren(children), [children]);
43
43
  const [captions, setCaptions] = useState([]);
44
44
  const [poster, setPoster] = useState(void 0);
45
+ const captionsSourceRef = useRef("");
45
46
  useEffect(() => {
46
- if (!parsed.captionsSrc) {
47
- setCaptions([]);
48
- return;
49
- }
50
- let cancelled = false;
51
- fetch(parsed.captionsSrc, { cache: "no-store" }).then((r) => r.json()).then((data) => {
52
- if (!cancelled && Array.isArray(data)) {
53
- const resolved = data.map((c, i) => ({ ...c, default: i === 0 }));
54
- setCaptions(resolved);
47
+ const sourceKey = parsed.captionsSrc ?? parsed.captionsData?.[0]?.srclang ?? "";
48
+ function applyResolved(data) {
49
+ const resolved = data.map((c, i) => ({ ...c, default: c.default ?? i === 0 }));
50
+ setCaptions(resolved);
51
+ if (captionsSourceRef.current !== sourceKey) {
52
+ captionsSourceRef.current = sourceKey;
55
53
  const defaultTrack = resolved.find((c) => c.default);
56
54
  if (defaultTrack) {
57
55
  setSubtitleMode(defaultTrack.srclang);
58
56
  setSubtitleStyle((st) => ({ ...st, track: defaultTrack.srclang }));
59
57
  }
60
58
  }
59
+ }
60
+ if (parsed.captionsData) {
61
+ applyResolved(parsed.captionsData);
62
+ return;
63
+ }
64
+ if (!parsed.captionsSrc) {
65
+ setCaptions([]);
66
+ return;
67
+ }
68
+ let cancelled = false;
69
+ fetch(parsed.captionsSrc, { cache: "no-store" }).then((r) => r.json()).then((data) => {
70
+ if (!cancelled && Array.isArray(data)) applyResolved(data);
61
71
  }).catch(() => {
62
72
  if (!cancelled) setCaptions([]);
63
73
  });
64
74
  return () => {
65
75
  cancelled = true;
66
76
  };
67
- }, [parsed.captionsSrc]);
77
+ }, [parsed.captionsSrc, parsed.captionsData]);
68
78
  useEffect(() => {
69
79
  if (!parsed.thumbnailSrc) {
70
80
  setPoster(void 0);
@@ -1630,7 +1640,11 @@ function parseVideoChildren(children) {
1630
1640
  }
1631
1641
  if (child.type === Captions || name === "SiloCaptions") {
1632
1642
  const element = child;
1633
- parsed.captionsSrc = element.props.src;
1643
+ if (element.props.data) {
1644
+ parsed.captionsData = element.props.data;
1645
+ } else if (element.props.src) {
1646
+ parsed.captionsSrc = element.props.src;
1647
+ }
1634
1648
  }
1635
1649
  if (child.type === VideoThumbnail || name === "SiloVideoThumbnail") {
1636
1650
  const element = child;