import { useEffect, useState } from "react";
import ResourcesView from "../components/resources_view.tsx";
import { network } from "../network/network.ts";
import { app } from "../app.ts";
import { Resource, RSort, Statistics } from "../network/models.ts";
import { useTranslation } from "../utils/i18n";
import { useAppContext } from "../components/AppContext.tsx";
import Select from "../components/select.tsx";
import { useNavigate } from "react-router";
import { useNavigator } from "../components/navigator.tsx";
import {
MdOutlineAccessTime,
MdOutlineArchive,
MdOutlineClass,
} from "react-icons/md";
export default function HomePage() {
useEffect(() => {
document.title = app.appName;
}, []);
const { t } = useTranslation();
const appContext = useAppContext();
const [order, setOrder] = useState(() => {
if (appContext && appContext.get("home_page_order") !== undefined) {
return appContext.get("home_page_order");
}
return RSort.TimeDesc;
});
useEffect(() => {
if (appContext && order !== RSort.TimeDesc) {
appContext.set("home_page_order", order);
}
}, [appContext, order]);
return (
<>
network.getResources(page, order)}
/>
>
);
}
function HomeHeader() {
const [pinnedResources, setPinnedResources] = useState([]);
const [statistic, setStatistic] = useState(null);
const [currentIndex, setCurrentIndex] = useState(0);
const navigator = useNavigator();
const appContext = useAppContext();
useEffect(() => {
const pinned = appContext.get("pinned_resources");
const stats = appContext.get("site_statistics");
if (pinned) {
setPinnedResources(pinned);
}
if (stats) {
setStatistic(stats);
}
if (pinned && stats) {
return;
}
const prefetchData = app.getPreFetchData();
if (prefetchData && prefetchData.background) {
navigator.setBackground(
network.getResampledImageUrl(prefetchData.background),
);
}
let ok1 = false;
let ok2 = false;
if (prefetchData && prefetchData.statistics) {
setStatistic(prefetchData.statistics);
appContext.set("site_statistics", prefetchData.statistics);
ok1 = true;
}
if (prefetchData && prefetchData.pinned) {
const r = prefetchData.pinned;
appContext.set("pinned_resources", r);
setPinnedResources(r!);
ok2 = true;
}
if (ok1 && ok2) {
return;
}
const fetchPinnedResources = async () => {
const res = await network.getPinnedResources();
if (res.success) {
appContext.set("pinned_resources", res.data);
setPinnedResources(res.data ?? []);
}
};
const fetchStatistics = async () => {
const res = await network.getStatistic();
if (res.success) {
appContext.set("site_statistics", res.data);
setStatistic(res.data!);
}
};
fetchPinnedResources();
fetchStatistics();
}, [appContext, navigator]);
// Auto-scroll carousel every 5 seconds
useEffect(() => {
if (pinnedResources.length <= 1) {
return;
}
const interval = setInterval(() => {
setCurrentIndex((prevIndex) => (prevIndex + 1) % pinnedResources.length);
}, 5000);
return () => clearInterval(interval);
}, [pinnedResources.length, currentIndex]);
if (pinnedResources.length == 0 || statistic == null) {
return <>>;
}
return (
{app.appName}
{app.siteDescription}
);
}
function PinnedResourcesCarousel({
resources,
currentIndex,
onIndexChange,
}: {
resources: Resource[];
currentIndex: number;
onIndexChange: (index: number) => void;
}) {
return (
{resources.map((resource) => (
))}
{resources.length > 1 && (
{resources.map((_, index) => (
)}
);
}
function PinnedResourceItem({ resource }: { resource: Resource }) {
const navigate = useNavigate();
return (
{
e.preventDefault();
navigate(`/resources/${resource.id}`);
}}
>
{resource.image != null && (
)}
{resource.title}
);
}
function StatisticCard({ statistic }: { statistic: Statistics }) {
const { t } = useTranslation();
const now = new Date();
const createdAt = new Date(statistic.start_time * 1000);
const diffTime = Math.abs(now.getTime() - createdAt.getTime());
const survivalTime = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
return (
{t("Resources")}
{statistic.total_resources}
{t("Files")}
{statistic.total_files}
{t("Survival time")}
{survivalTime}
);
}