Source

atoms/story-elements/vidible-element/vidible-element.tsx

  1. // delivery\.vidible\.tv\/htmlembed\/pid=([^$]*)\/([^$]*).html\?vid=([^$]*)
  2. import React from "react";
  3. import { StoryElementProps } from "../types";
  4. import { O2PlayerTypes } from "../../o2-player/types";
  5. import { O2Player } from "../../o2-player";
  6. import { withStoryAndConfig } from "../../../context";
  7. import get from "lodash.get";
  8. type VidibleElementProps = StoryElementProps & Omit<O2PlayerTypes, "data-pid" | "data-bcid" | "data-vid">;
  9. interface VidibleIds {
  10. pid: string;
  11. bcid: string;
  12. vid: string;
  13. }
  14. export const getVidibleIDs = (url: string) => {
  15. const match = /delivery\.vidible\.tv\/htmlembed\/pid=([^$]*)\/([^$]*).html\?vid=([^$]*)/.exec(url);
  16. return match ? { pid: match[1], bcid: match[2], vid: match[3] } : null;
  17. };
  18. export const DefaultVidibleElement = ({
  19. element,
  20. layout = "responsive",
  21. width = "16",
  22. height = "9",
  23. "data-macros": dataMacros,
  24. ...props
  25. }: VidibleElementProps) => {
  26. const { metadata } = element;
  27. let vidibleIds: VidibleIds | null = null;
  28. if (metadata && metadata["player-url"]) {
  29. vidibleIds = getVidibleIDs(metadata["player-url"]);
  30. }
  31. return vidibleIds ? (
  32. <O2Player
  33. data-pid={vidibleIds.pid}
  34. data-bcid={vidibleIds.bcid}
  35. data-vid={vidibleIds.vid}
  36. data-macros={dataMacros ? dataMacros : "m.playback=click"}
  37. layout={layout}
  38. width={width}
  39. height={height}
  40. {...props}
  41. />
  42. ) : null;
  43. };
  44. export const VidibleElementBase = ({
  45. element,
  46. story,
  47. config,
  48. "data-macros": dataMacros
  49. }: StoryElementProps & { "data-macros"?: string }) => {
  50. const vidibleElementRender = get(config, ["opts", "render", "storyElementRender", "vidibleElementRender"], null);
  51. const title = element.subtype || element.title || "";
  52. return vidibleElementRender ? (
  53. vidibleElementRender({ story, config, element })
  54. ) : (
  55. <DefaultVidibleElement element={element} title={title} data-macros={dataMacros} />
  56. );
  57. };
  58. /**
  59. * VidibleElement is a story element.
  60. * The look of the VidibleElement can be changed using the render prop vidibleElementRender. In case vidibleElementRender is passed in the config, it is rendered. Otherwise a default VidibleElement is rendered.
  61. *
  62. * @param {Object} params object containing parameters passed to the render prop
  63. * @param {Object} params.story story object
  64. * @param {Object} params.config config object
  65. * @param {Object} params.element the story element. This is same as the story element found in the story API response
  66. * @param {Object} params.data-macros macros passed as data-macros to the amp component amp-o2-player
  67. *
  68. * ```javascript
  69. * ...
  70. * ampRoutes(app, {
  71. * render: {
  72. * storyElementRender: {
  73. * vidibleElementRender: ({ story, config, element }) => <MyCustomVidibleElement story={story} config={config} storyElement={element} />
  74. * }
  75. * }
  76. * })
  77. * ...
  78. * ```
  79. *
  80. * @category StoryElements
  81. * @module VidibleElement
  82. * @component
  83. */
  84. export const VidibleElement = withStoryAndConfig(VidibleElementBase);