furina.id/src/pages/home/page.tsx

232 lines
5.9 KiB
TypeScript
Raw Normal View History

2024-01-17 01:55:25 +00:00
import titleImg from "@/assets/images/title-img.svg";
2024-01-06 12:35:42 +07:00
import PageMetadata from "@/components/containers/PageMetadata";
2024-01-17 01:55:25 +00:00
import { cn } from "@/utility/utils";
import dayjs from "dayjs";
2024-01-17 02:45:00 +00:00
import { ComponentProps, useEffect, useMemo, useRef, useState } from "react";
2024-01-17 01:55:25 +00:00
import { Link } from "react-router-dom";
import { icons } from "./icons";
import { useQuery } from "react-query";
import pb from "@/utility/api";
2024-01-06 03:35:50 +07:00
2024-01-05 23:28:25 +07:00
const HomePage = () => {
2024-01-17 01:55:25 +00:00
return (
<div className="h-screen w-full bg-slate-900 overflow-hidden relative">
<PageMetadata title="" />
<BackgroundSlideshow />
2024-01-06 03:35:50 +07:00
2024-01-17 01:55:25 +00:00
<DateTime />
</div>
);
};
2024-01-06 03:35:50 +07:00
2024-01-17 01:55:25 +00:00
const BackgroundSlideshow = () => {
2024-01-17 02:45:00 +00:00
const slideshowTimer = 10000;
2024-01-17 01:55:25 +00:00
const { data: wallpapers } = useQuery({
queryKey: ["wallpapers"],
queryFn: async () => {
const items = await pb.collection("wallpapers").getFullList({
sort: "@random",
expand: "artwork",
});
return items.map((item) => {
const artwork = item.expand?.artwork;
2024-01-17 03:10:25 +00:00
const img = pb.files.getUrl(artwork, artwork?.image);
return { ...artwork, src: img };
2024-01-17 01:55:25 +00:00
});
},
2024-01-17 02:02:50 +00:00
refetchOnWindowFocus: false,
2024-01-17 01:55:25 +00:00
});
2024-01-06 03:35:50 +07:00
2024-01-17 02:45:00 +00:00
const [currentIdx, setCurrentIdx] = useState(0);
2024-01-17 03:10:25 +00:00
const curArtwork = wallpapers?.[currentIdx];
2024-01-17 02:45:00 +00:00
useEffect(() => {
if (!wallpapers) {
return;
}
const intv = setInterval(() => {
setCurrentIdx((cur) => (cur + 1) % wallpapers.length);
}, slideshowTimer);
return () => {
clearInterval(intv);
};
}, [wallpapers, slideshowTimer]);
2024-01-17 01:55:25 +00:00
return (
<>
<img
src={titleImg}
alt="title"
className="h-4 md:h-8 absolute top-6 left-6 md:top-8 md:left-8 lg:left-[5%] lg:top-[5%] z-[5]"
/>
2024-01-06 03:35:50 +07:00
2024-01-17 03:10:25 +00:00
{curArtwork ? (
<BackgroundImage src={curArtwork?.src} artwork={curArtwork} />
) : null}
2024-01-17 01:55:25 +00:00
</>
);
};
2024-01-06 03:35:50 +07:00
2024-01-17 01:55:25 +00:00
type BackgroundImageProps = {
src: string;
2024-01-17 03:10:25 +00:00
artwork: any;
2024-01-17 01:55:25 +00:00
};
2024-01-06 03:35:50 +07:00
2024-01-17 03:10:25 +00:00
const BackgroundImage = ({ src, artwork }: BackgroundImageProps) => {
2024-01-17 02:45:00 +00:00
const lastChangeRef = useRef<Date | null>(null);
2024-01-17 01:55:25 +00:00
const [isLoaded, setLoaded] = useState(false);
2024-01-17 02:45:00 +00:00
const [imgSrc, setImgSrc] = useState("");
useEffect(() => {
setLoaded(false);
lastChangeRef.current = new Date();
}, [src]);
const onLoaded = () => {
setLoaded(true);
setImgSrc(src);
};
2024-01-06 03:35:50 +07:00
return (
2024-01-17 01:55:25 +00:00
<>
<img
src={src}
alt="img"
className="hidden"
2024-01-17 02:45:00 +00:00
onLoad={() => {
let timer = lastChangeRef.current
? new Date().getMilliseconds() -
lastChangeRef.current.getMilliseconds()
: 100;
timer = Math.max(500, Math.min(timer, 1000));
setTimeout(onLoaded, timer);
}}
2024-01-06 12:35:42 +07:00
/>
2024-01-06 03:35:50 +07:00
<div
className={cn(
2024-01-17 02:45:00 +00:00
"absolute w-[100vw] bg-center bg-cover inset-0 transition-opacity duration-500",
isLoaded ? "opacity-100" : "opacity-0"
2024-01-06 03:35:50 +07:00
)}
2024-01-17 02:45:00 +00:00
style={{ backgroundImage: `url('${imgSrc}')` }}
2024-01-17 03:10:25 +00:00
>
<a
href={artwork.srcUrl}
className="text-white absolute bottom-4 left-4 md:left-8 text-sm opacity-80 hover:opacity-100 [text-shadow:_0_1px_5px_rgb(0_0_0_/_60%)]"
>
{`Illustration by ${artwork.artistName}`}
</a>
</div>
2024-01-17 01:55:25 +00:00
</>
);
};
const DateTime = () => {
const [time, setTime] = useState(new Date());
2024-01-06 03:35:50 +07:00
2024-01-17 01:55:25 +00:00
useEffect(() => {
const intv = setInterval(() => {
setTime(new Date());
}, 1000);
return () => {
clearInterval(intv);
};
}, [setTime]);
const message = useMemo(() => {
const hours = time.getHours();
let msg = "Day";
if (hours >= 18 && hours <= 2) {
msg = "Night";
} else if (hours > 2 && hours <= 9) {
msg = "Morning";
} else if (hours > 9 && hours <= 15) {
msg = "Day";
} else if (hours > 15 && hours < 18) {
msg = "Evening";
}
return `Good ${msg}~ 💧✨`;
}, [time]);
return (
<div className="absolute left-1/2 -translate-x-1/2 lg:left-[5%] lg:translate-x-0 bottom-1/2 translate-y-1/2 w-full md:w-auto px-4 md:px-0 text-white [text-shadow:_0_1px_5px_rgb(0_0_0_/_60%)] text-center lg:text-left z-10">
<p className="text-md md:text-2xl">{message}</p>
<p className="text-7xl md:text-8xl font-light mt-0.5">
{dayjs(time).format("HH:mm")}
</p>
<p className="text-md md:text-lg font-light mt-2">
{dayjs(time).format("dddd, DD MMM YYYY")}
</p>
<AppNav className="mt-8" />
2024-01-06 03:35:50 +07:00
</div>
);
};
2024-01-17 01:55:25 +00:00
const AppNav = ({ className }: ComponentProps<"div">) => {
2024-01-06 03:35:50 +07:00
return (
2024-01-17 01:55:25 +00:00
<div
className={cn(
2024-01-19 11:20:29 +00:00
"flex items-start justify-center lg:justify-start flex-wrap gap-3 gap-x-5 lg:gap-x-6",
2024-01-17 01:55:25 +00:00
className
)}
>
<AppNavItem title="Pat Furina" icon={icons.patFurina} path="/pat-pat" />
<AppNavItem title="Album" icon={icons.treasures} path="/treasures" />
<AppNavItem
title="Facebook"
icon={icons.facebook}
path="https://www.facebook.com/"
iconClassName="p-1"
/>
<AppNavItem
title="X"
icon={icons.twitter}
path="https://twitter.com/"
iconClassName="bg-black"
/>
<AppNavItem
title="Furinamains Discord"
icon={icons.furinamains}
path="https://discord.com/invite/ew8yz3h5at"
iconClassName="bg-[#6b473a]"
/>
2024-01-06 03:35:50 +07:00
</div>
);
2024-01-05 23:28:25 +07:00
};
2024-01-17 01:55:25 +00:00
type AppNavItemProps = {
title: string;
icon: string;
path?: string;
iconClassName?: string;
};
const AppNavItem = ({ title, icon, path, iconClassName }: AppNavItemProps) => {
return (
<Link to={path || "#"} className="flex flex-col items-center w-12 group">
<div
className={cn(
"bg bg-white rounded-lg w-12 h-12 overflow-hidden group-hover:scale-110 transition-all",
iconClassName
)}
>
<img
src={icon}
alt={title}
className="w-full h-full object-contain rounded-lg"
/>
</div>
<p className={cn("text-white text-center mt-2 text-sm")}>{title}</p>
</Link>
);
};
2024-01-05 23:28:25 +07:00
export default HomePage;