-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
773 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,52 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import axios from 'axios'; | ||
import Image from 'next/image'; | ||
import { getTmdbKey } from '@/utils/freekeys'; | ||
|
||
const TMDBPoster = ({ imdbId }: Record<string, string>) => { | ||
const Poster = ({ imdbId }: Record<string, string>) => { | ||
const [posterUrl, setPosterUrl] = useState(''); | ||
const [imgLoaded, setImgLoaded] = useState(false); | ||
const imgRef = useRef<HTMLDivElement | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
const response = await axios.get(`https://api.themoviedb.org/3/find/${imdbId}?api_key=${getTmdbKey()}&external_source=imdb_id`); | ||
const baseUrl = 'https://image.tmdb.org/t/p/w200'; | ||
if (response.data.movie_results.length > 0 && response.data.movie_results[0].poster_path) { | ||
setPosterUrl(baseUrl + response.data.movie_results[0].poster_path); | ||
} if (response.data.tv_results.length > 0 && response.data.tv_results[0].poster_path) { | ||
setPosterUrl(baseUrl + response.data.tv_results[0].poster_path); | ||
} else { | ||
// If no poster_path, set a placeholder image URL | ||
setPosterUrl(`https://picsum.photos/seed/${imdbId}/200/300`); | ||
} | ||
const posterPath = response.data.movie_results[0]?.poster_path || response.data.tv_results[0]?.poster_path; | ||
if (!posterPath) setPosterUrl(`https://picsum.photos/seed/${imdbId}/200/300`); | ||
else setPosterUrl(`${baseUrl}${posterPath}`); | ||
}; | ||
|
||
const imgObserver = new IntersectionObserver((entries, observer) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
fetchData(); | ||
setImgLoaded(true); | ||
observer.disconnect(); | ||
} | ||
}); | ||
}); | ||
|
||
try { | ||
if (imdbId) fetchData(); | ||
else setPosterUrl(`https://picsum.photos/seed/${imdbId}/200/300`); | ||
if (imdbId && imgRef.current) { | ||
imgObserver.observe(imgRef.current); | ||
} else { | ||
setPosterUrl(`https://picsum.photos/seed/${imdbId}/200/300`); | ||
} | ||
} catch (error: any) { | ||
setPosterUrl(`https://picsum.photos/seed/${imdbId}/200/300`); | ||
} | ||
|
||
return () => { | ||
imgObserver.disconnect(); | ||
}; | ||
}, [imdbId]); | ||
|
||
return ( | ||
<div> | ||
{posterUrl && <Image width={200} height={300} src={posterUrl} alt="Movie poster" />} | ||
<div ref={imgRef}> | ||
{imgLoaded && posterUrl && <Image width={200} height={300} src={posterUrl} alt="Movie poster" />} | ||
</div> | ||
); | ||
}; | ||
|
||
export default TMDBPoster; | ||
export default Poster; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.