react-native-vimeo-bridge
Version:
🎥 Easy-to-use Vimeo player for React Native with cross-platform support
58 lines (57 loc) • 1.55 kB
JavaScript
;
import { useEffect, useState } from 'react';
import { createVimeoOEmbedUrl } from "../utils/index.js";
/**
* Hook to fetch the oEmbed data for a Vimeo video.
* @param url - The URL of the Vimeo video.
* @returns The oEmbed data, loading state, and error.
*/
const useVimeoOEmbed = (url, params) => {
const [oEmbed, setOEmbed] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const oEmbedUrl = createVimeoOEmbedUrl(url, params);
useEffect(() => {
if (!oEmbedUrl) {
return;
}
const controller = new AbortController();
setError(null);
setOEmbed(null);
const fetchOEmbed = async () => {
setIsLoading(true);
try {
const response = await fetch(oEmbedUrl, {
signal: controller.signal
});
if (!response.ok) {
throw new Error('Failed to fetch oEmbed');
}
const data = await response.json();
setOEmbed(data);
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
return;
}
if (error instanceof Error) {
setError(error);
return;
}
setError(new Error('Failed to fetch oEmbed'));
} finally {
setIsLoading(false);
}
};
fetchOEmbed();
return () => {
controller.abort();
};
}, [oEmbedUrl]);
return {
oEmbed,
isLoading,
error
};
};
export default useVimeoOEmbed;
//# sourceMappingURL=useVimeoOEmbed.js.map