From 43af4fcbc44c4b0ded5dfc26312257bcfaae4fe9 Mon Sep 17 00:00:00 2001 From: Manan Tank Date: Thu, 27 Mar 2025 22:34:04 +0530 Subject: [PATCH] [TOOL-3730] Dashboard: Fix forwardRef error with certain bundler setup --- .changeset/crazy-cups-jump.md | 5 + .../web/ui/MediaRenderer/MediaRenderer.tsx | 247 +++++++++--------- 2 files changed, 128 insertions(+), 124 deletions(-) create mode 100644 .changeset/crazy-cups-jump.md diff --git a/.changeset/crazy-cups-jump.md b/.changeset/crazy-cups-jump.md new file mode 100644 index 00000000000..735845d7fd1 --- /dev/null +++ b/.changeset/crazy-cups-jump.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Fix "Cannot read properties of undefined (reading 'forwardRef')" error in certain bundler setups diff --git a/packages/thirdweb/src/react/web/ui/MediaRenderer/MediaRenderer.tsx b/packages/thirdweb/src/react/web/ui/MediaRenderer/MediaRenderer.tsx index 0d368565c1b..17fd5b9110d 100644 --- a/packages/thirdweb/src/react/web/ui/MediaRenderer/MediaRenderer.tsx +++ b/packages/thirdweb/src/react/web/ui/MediaRenderer/MediaRenderer.tsx @@ -1,5 +1,6 @@ "use client"; -import React, { useState, useRef, useEffect } from "react"; +import type React from "react"; +import { forwardRef, useEffect, useRef, useState } from "react"; import { CarbonDocumentAudio, CarbonDocumentUnknown, @@ -49,134 +50,132 @@ import { useResolvedMediaType } from "./useResolvedMediaType.js"; * @param props - Refer to [`MediaRendererProps`](https://portal.thirdweb.com/references/typescript/v5/MediaRendererProps) to see the available props. */ export const MediaRenderer = /* @__PURE__ */ (() => - React.forwardRef( - function Media_Renderer( - { - src, - poster, - alt, - gatewayUrl, - requireInteraction = false, - width = "300px", - height = "300px", - style, - mimeType, - client, - controls, - className, - }, - ref, - ) { - const mergedStyle: React.CSSProperties = { - objectFit: "contain", - ...style, - }; + forwardRef(function Media_Renderer( + { + src, + poster, + alt, + gatewayUrl, + requireInteraction = false, + width = "300px", + height = "300px", + style, + mimeType, + client, + controls, + className, + }, + ref, + ) { + const mergedStyle: React.CSSProperties = { + objectFit: "contain", + ...style, + }; - const { mediaInfo, isFetched: mediaInfoIsFetched } = useResolvedMediaType( - client, - src ?? undefined, - mimeType, - gatewayUrl, - ); + const { mediaInfo, isFetched: mediaInfoIsFetched } = useResolvedMediaType( + client, + src ?? undefined, + mimeType, + gatewayUrl, + ); - const { mediaInfo: possiblePosterSrc } = useResolvedMediaType( - client, - poster ?? undefined, - undefined, - gatewayUrl, - ); + const { mediaInfo: possiblePosterSrc } = useResolvedMediaType( + client, + poster ?? undefined, + undefined, + gatewayUrl, + ); - if (!mediaInfoIsFetched || !src) { - return
; - } + if (!mediaInfoIsFetched || !src) { + return
; + } - if (mediaInfo.mimeType) { - // html content - if (mediaInfo.mimeType.startsWith("text/html")) { - return ( - } - requireInteraction={requireInteraction} - className={className} - alt={alt} - /> - ); - } + if (mediaInfo.mimeType) { + // html content + if (mediaInfo.mimeType.startsWith("text/html")) { + return ( + } + requireInteraction={requireInteraction} + className={className} + alt={alt} + /> + ); + } - // 3d model - if (mediaInfo.mimeType.startsWith("model")) { - console.error( - "Encountered an unsupported media type. 3D model support was removed in v5.92.0. To add a 3D model to your app, use @google/model-viewer and use the ModelViewer component.", - ); - } + // 3d model + if (mediaInfo.mimeType.startsWith("model")) { + console.error( + "Encountered an unsupported media type. 3D model support was removed in v5.92.0. To add a 3D model to your app, use @google/model-viewer and use the ModelViewer component.", + ); + } - // video - if (mediaInfo.mimeType.startsWith("video")) { - return ( - } - poster={ - possiblePosterSrc.mimeType?.startsWith("image/") - ? possiblePosterSrc.url - : undefined - } - requireInteraction={requireInteraction} - className={className} - controls={controls} - /> - ); - } + // video + if (mediaInfo.mimeType.startsWith("video")) { + return ( + } + poster={ + possiblePosterSrc.mimeType?.startsWith("image/") + ? possiblePosterSrc.url + : undefined + } + requireInteraction={requireInteraction} + className={className} + controls={controls} + /> + ); + } - // audio - if (mediaInfo.mimeType.startsWith("audio")) { - return ( - } - className={className} - height={height} - width={width} - controls={controls} - /> - ); - } + // audio + if (mediaInfo.mimeType.startsWith("audio")) { + return ( + } + className={className} + height={height} + width={width} + controls={controls} + /> + ); + } - // image - if (mediaInfo.mimeType.startsWith("image/")) { - return ( - } - className={className} - height={height} - width={width} - /> - ); - } + // image + if (mediaInfo.mimeType.startsWith("image/")) { + return ( + } + className={className} + height={height} + width={width} + /> + ); } + } - // unknown mime types or no mime type - return ( - } - className={className} - /> - ); - }, - ))(); + // unknown mime types or no mime type + return ( + } + className={className} + /> + ); + }))(); interface PlayButtonProps { onClick: () => void; @@ -227,7 +226,7 @@ const PlayButton: React.FC = ({ onClick, isPlaying }) => { }; const ImageRenderer = /* @__PURE__ */ (() => - React.forwardRef< + forwardRef< HTMLImageElement, Pick< MediaRendererProps, @@ -266,7 +265,7 @@ const ImageRenderer = /* @__PURE__ */ (() => }))(); const VideoPlayer = /* @__PURE__ */ (() => - React.forwardRef< + forwardRef< HTMLVideoElement, Pick< MediaRendererProps, @@ -393,7 +392,7 @@ const VideoPlayer = /* @__PURE__ */ (() => }))(); const AudioPlayer = /* @__PURE__ */ (() => - React.forwardRef< + forwardRef< HTMLAudioElement, Pick< MediaRendererProps, @@ -504,7 +503,7 @@ const AudioPlayer = /* @__PURE__ */ (() => * @internal Exported for tests */ export const IframePlayer = /* @__PURE__ */ (() => - React.forwardRef< + forwardRef< HTMLIFrameElement, Omit< MediaRendererProps, @@ -574,7 +573,7 @@ export const IframePlayer = /* @__PURE__ */ (() => * @internal Exported for tests */ export const LinkPlayer = /* @__PURE__ */ (() => - React.forwardRef< + forwardRef< HTMLAnchorElement, Pick >(function Link_Player({ src, alt, style, className }, ref) {