@react-native-youtube-bridge/react
Version:
React implementation for react-native-youtube-bridge
111 lines (107 loc) • 3.85 kB
JavaScript
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
const __react_native_youtube_bridge_core = __toESM(require("@react-native-youtube-bridge/core"));
const react = __toESM(require("react"));
//#region src/hooks/useYoutubeVideoId.ts
const useYouTubeVideoId = (source, onError) => {
const sourceValue = (0, react.useMemo)(() => {
if (!source) return;
if (typeof source === "string") return source;
if ("videoId" in source) return source.videoId;
if ("url" in source) return source.url;
return null;
}, [typeof source === "string" ? source : source && "videoId" in source ? source.videoId : source && "url" in source ? source.url : null]);
const videoId = (0, react.useMemo)(() => {
if (sourceValue === null) {
console.error("Invalid YouTube source: ", sourceValue);
onError?.({
code: 1002,
message: __react_native_youtube_bridge_core.ERROR_CODES[1002]
});
return null;
}
if (sourceValue === void 0) return void 0;
if ((0, __react_native_youtube_bridge_core.validateVideoId)(sourceValue)) return sourceValue;
const extractedId = (0, __react_native_youtube_bridge_core.extractVideoIdFromUrl)(sourceValue);
if (!extractedId) {
console.error("Invalid YouTube source: ", sourceValue);
onError?.({
code: 1002,
message: __react_native_youtube_bridge_core.ERROR_CODES[1002]
});
return null;
}
return extractedId;
}, [sourceValue, onError]);
return videoId;
};
var useYoutubeVideoId_default = useYouTubeVideoId;
//#endregion
//#region src/hooks/useYoutubeOEmbed.ts
/**
* Hook to fetch the oEmbed data for a YouTube video.
* @param url - The URL of the YouTube video.
* @returns The oEmbed data, loading state, and error.
*/
const useYoutubeOEmbed = (url) => {
const [oEmbed, setOEmbed] = (0, react.useState)();
const [isLoading, setIsLoading] = (0, react.useState)(false);
const [error, setError] = (0, react.useState)(null);
(0, react.useEffect)(() => {
if (!url) return;
const controller = new AbortController();
setError(null);
setOEmbed(void 0);
const fetchOEmbed = async () => {
setIsLoading(true);
try {
const response = await fetch(`https://www.youtube.com/oembed?format=json&url=${encodeURIComponent(url)}`, { signal: controller.signal });
if (!response.ok) throw new Error("Failed to fetch oEmbed");
const data = await response.json();
setOEmbed(data);
} catch (error$1) {
if (error$1 instanceof Error && error$1.name === "AbortError") return;
if (error$1 instanceof Error) {
setError(error$1);
return;
}
setError(new Error("Failed to fetch oEmbed"));
} finally {
setIsLoading(false);
}
};
fetchOEmbed();
return () => {
controller.abort();
};
}, [url]);
return {
oEmbed,
isLoading,
error
};
};
var useYoutubeOEmbed_default = useYoutubeOEmbed;
//#endregion
exports.useYouTubeVideoId = useYoutubeVideoId_default;
exports.useYoutubeOEmbed = useYoutubeOEmbed_default;