1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-20 14:37:43 +01:00

Add 'Overall Views' box

This commit is contained in:
Cooper Ransom 2024-03-15 13:41:15 -04:00
parent d9ded30a86
commit c1c7866751

View file

@ -8,6 +8,8 @@ import { Divider } from "@/components/utils/Divider";
import { Heading1, Paragraph } from "@/components/utils/Text"; import { Heading1, Paragraph } from "@/components/utils/Text";
import { SubPageLayout } from "./layouts/SubPageLayout"; import { SubPageLayout } from "./layouts/SubPageLayout";
// import { MediaGrid } from "@/components/media/MediaGrid"
// import { TopFlixCard } from "@/components/media/FlixCard";
function Button(props: { function Button(props: {
className: string; className: string;
@ -83,21 +85,48 @@ async function getRecentPlayedItems() {
throw new Error("RECENT_PLAYED_ITEMS not found"); throw new Error("RECENT_PLAYED_ITEMS not found");
} }
async function getTotalViews() {
const response = await fetch("https://backend.sudo-flix.lol/metrics");
const text = await response.text();
// Regex to match the view counts of all shows/movies with success="true"
const regex = /mw_media_watch_count{[^}]*,success="true"} (\d+)/g;
let totalViews = 0;
let match = regex.exec(text);
while (match !== null) {
totalViews += parseInt(match[1], 10);
match = regex.exec(text);
}
if (totalViews > 0) {
return totalViews.toString();
}
throw new Error("TOTAL_VIEWS not found");
}
export function TopFlix() { export function TopFlix() {
const [recentPlayedItems, setRecentPlayedItems] = useState<any[]>([]); const [recentPlayedItems, setRecentPlayedItems] = useState<any[]>([]);
const [totalViews, setTotalViews] = useState<string | null>(null);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [currentPage, setCurrentPage] = useState(1); const [currentPage, setCurrentPage] = useState(1);
const itemsPerPage = 10; const itemsPerPage = 10;
const maxItemsToShow = 100; // Maximum items to show
const maxPageCount = Math.ceil(maxItemsToShow / itemsPerPage); // Calculate max page count based on maxItemsToShow
useEffect(() => { useEffect(() => {
getRecentPlayedItems() getRecentPlayedItems()
.then((items) => { .then((items) => {
const uniqueItems = items.filter( // Limit the items to the first 100 to ensure we don't exceed the max page count
(item, index, self) => const limitedItems = items
index === self.findIndex((t2) => t2.tmdbFullId === item.tmdbFullId), .slice(0, maxItemsToShow)
); .filter(
(item, index, self) =>
index ===
self.findIndex((t2) => t2.tmdbFullId === item.tmdbFullId),
);
setRecentPlayedItems(uniqueItems); setRecentPlayedItems(limitedItems);
}) })
.catch((error) => { .catch((error) => {
console.error("Error fetching recent played items:", error); console.error("Error fetching recent played items:", error);
@ -105,6 +134,13 @@ export function TopFlix() {
.finally(() => { .finally(() => {
setLoading(false); setLoading(false);
}); });
getTotalViews()
.then((views) => {
setTotalViews(views);
})
.catch((error) => {
console.error("Error fetching total views:", error);
});
}, []); }, []);
function getItemsForCurrentPage() { function getItemsForCurrentPage() {
@ -122,12 +158,17 @@ export function TopFlix() {
return ( return (
<SubPageLayout> <SubPageLayout>
<ThiccContainer> <ThiccContainer classNames="px-4">
<Heading1>Top flix</Heading1> <Heading1>Top flix</Heading1>
<Paragraph className="mb-18"> <Paragraph className="mb-18">
The most popular movies on sudo-flix.lol, this data is fetched from The most popular movies on sudo-flix.lol, this data is fetched from
the current backend deployment. the current backend deployment.
</Paragraph> </Paragraph>
<div className="mt-8 w-auto">
<div className="bg-video-scraping-card rounded-xl py-5 px-7 inline-block">
<p className="font-bold">Overall Views: {totalViews}</p>
</div>
</div>
<div className="mt-8 w-full max-w-none"> <div className="mt-8 w-full max-w-none">
<Divider marginClass="my-3" /> <Divider marginClass="my-3" />
@ -137,11 +178,13 @@ export function TopFlix() {
<ConfigValue key={item.tmdbFullId} name={item.title}> <ConfigValue key={item.tmdbFullId} name={item.title}>
{`${item.providerId} - Views: `} {`${item.providerId} - Views: `}
<strong>{item.count}</strong> <strong>{item.count}</strong>
{/* <img src={coverUrl} alt={item.title} /> */}
</ConfigValue> </ConfigValue>
); );
})} })}
<div style={{ display: "flex", justifyContent: "space-between" }}> <div
style={{ display: "flex", justifyContent: "space-between" }}
className="mt-6"
>
<Button <Button
className="py-px box-content bg-buttons-secondary hover:bg-buttons-secondaryHover bg-opacity-90 text-buttons-secondaryText justify-center items-center" className="py-px box-content bg-buttons-secondary hover:bg-buttons-secondaryHover bg-opacity-90 text-buttons-secondaryText justify-center items-center"
onClick={() => setCurrentPage(currentPage - 1)} onClick={() => setCurrentPage(currentPage - 1)}
@ -150,16 +193,12 @@ export function TopFlix() {
Previous page Previous page
</Button> </Button>
<div> <div>
{currentPage} /{" "} {currentPage} / {maxPageCount}
{Math.ceil(recentPlayedItems.length / itemsPerPage)}
</div> </div>
<Button <Button
className="py-px box-content bg-buttons-secondary hover:bg-buttons-secondaryHover bg-opacity-90 text-buttons-secondaryText justify-center items-center" className="py-px box-content bg-buttons-secondary hover:bg-buttons-secondaryHover bg-opacity-90 text-buttons-secondaryText justify-center items-center"
onClick={() => setCurrentPage(currentPage + 1)} onClick={() => setCurrentPage(currentPage + 1)}
disabled={ disabled={currentPage === maxPageCount}
currentPage ===
Math.ceil(recentPlayedItems.length / itemsPerPage)
}
> >
Next page Next page
</Button> </Button>