Skip to content

Commit

Permalink
Merge pull request #5 from alfaruqii/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
alfaruqii authored Nov 11, 2024
2 parents c8034c0 + 1df8068 commit 514b53f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 27 deletions.
124 changes: 97 additions & 27 deletions app/components/media/Embeded.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getEnv } from "@/utils/getEnv";
"use client";
import React, { useState, useRef, useEffect } from "react";
import { AiOutlineSetting } from "react-icons/ai";
import { Provider } from "@/types/movies.type";

type EmbededProps = {
id: string;
Expand All @@ -7,37 +10,104 @@ type EmbededProps = {
ep?: string;
};

const sourcesMap: Provider[] = JSON.parse(
process.env.NEXT_PUBLIC_WATCHLO_SOURCE_EMBED || "[]"
);

function generateUrl(
base: string,
type: string,
id: string,
season: string,
ep: string
) {
return type?.toLowerCase() === "movie"
? `${base}/movie/${id}`
: `${base}/tv/${id}/${season}/${ep}`;
}

function Embeded({ id, type, season = "1", ep = "1" }: EmbededProps) {
const determinePathQuery = (): string => {
if (type.toLowerCase() === "tv") return `/tv/${id}/${season}/${ep}`;
return `/movie/${id}`;
const initialProvider =
sourcesMap.length > 0
? sourcesMap[0]
: {
name: "vidsrc.xyz",
label: "Initial Stream",
url: "https://vidsrc.to",
};
const [provider, setProvider] = useState(initialProvider);
console.log("provider: ", generateUrl(provider.url, type, id, season, ep));

const iframeRef = useRef<HTMLIFrameElement>(null);

const handleProviderChange = (providerName: string) => {
const selectedProvider = sourcesMap.find(
(source) => source.name === providerName
);
setProvider(selectedProvider || sourcesMap[0]);
};

const source = `${
getEnv("WATCHLO_SOURCE_EMBED") ??
process.env["NEXT_PUBLIC_WATCHLO_SOURCE_EMBED"]
}${determinePathQuery()}`;
return (
<>
<div
className={`mt-4 flex flex-col items-center overflow-hidden w-full ${
type.toLowerCase() === "tv" ? "col-span-3" : ""
}`}
>
{type.toLowerCase() === "movie" && (
<p className="text-center w-full text-xl mb-2 font-bold">Watch 🎬</p>
)}
<iframe
src={
source ??
"https://www.youtube.com/embed/xvFZjo5PgG0?si=ANsa_ekk0Jg9zbWg"
useEffect(() => {
const handleIframeLoad = () => {
if (iframeRef.current) {
try {
const iframeWindow = iframeRef.current.contentWindow;
if (iframeWindow) {
iframeWindow.onbeforeunload = (e: BeforeUnloadEvent) => {
e.preventDefault();
};
}
referrerPolicy="origin"
allowFullScreen
className="rounded drop-shadow-lg"
></iframe>
} catch (error) {
console.error("Error setting up iframe redirect prevention:", error);
}
}
};

if (iframeRef.current) {
iframeRef.current.addEventListener("load", handleIframeLoad);
}
}, [provider]);

return (
<div
className={`${
type.toLowerCase() === "tv" ? "col-span-3" : ""
} mt-4 flex w-full
flex-col`}
>
{/* Iframe at the top */}
{type.toLowerCase() === "movie" && (
<p className="mb-2 w-full text-center text-xl font-bold">Watch 🎬</p>
)}
<iframe
ref={iframeRef}
src={generateUrl(provider.url, type, id, season, ep)}
allowFullScreen
className="mb-4 aspect-video h-full w-full rounded drop-shadow-lg"
/>

{/* Provider changer below the iframe */}
<div className="flex items-center justify-between">
<div className="dropdown dropdown-top rounded">
<label tabIndex={0} className="btn btn-ghost">
<AiOutlineSetting className="mr-2 h-6 w-6" />
<span>{provider.label}</span>
</label>
<ul
tabIndex={0}
className="dropdown-content menu w-52 rounded-box bg-base-100 p-2 shadow"
>
{sourcesMap.map((source, index) => (
<li key={index}>
<a onClick={() => handleProviderChange(source.name)}>
{source.label}
</a>
</li>
))}
</ul>
</div>
</div>
</>
</div>
);
}

Expand Down
7 changes: 7 additions & 0 deletions app/types/movies.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export interface Provider {
name: string;
label: string;
ads: boolean;
url: string;
}

interface Genre {
id: number;
name: string;
Expand Down

0 comments on commit 514b53f

Please sign in to comment.