add random image loading on index page

This commit is contained in:
Kentai Radiquum 2025-01-30 02:18:51 +05:00
parent 0c3130e95f
commit 4d4b346ea2
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
6 changed files with 218 additions and 68 deletions

View file

@ -1,4 +1,7 @@
import { Log } from "./utils";
import { renderToString } from "react-dom/server";
import IndexPage from "./templates/index";
import fs from "fs";
const environment: "prod" | "dev" =
(process.env.ENVIRONMENT as "prod" | "dev") || "prod";
@ -43,9 +46,9 @@ async function listAllObjects(bucketName: string, prefix: string) {
if (item.Key != undefined) {
const ext = item.Key.split(".")[item.Key.split(".").length - 1];
if (["png", "jpg", "jpeg"].includes(ext.toLowerCase())) {
images.push(item.Key);
images.push(`${process.env.ENDPOINT}/${process.env.BUCKET}/${item.Key}`);
} else if (["mp4"].includes(ext.toLowerCase())) {
videos.push(item.Key);
videos.push(`${process.env.ENDPOINT}/${process.env.BUCKET}/${item.Key}`);
}
}
});
@ -87,32 +90,6 @@ if (!fs.existsSync("out/data/videos.json")) {
fs.writeFileSync("out/data/videos.json", JSON.stringify(videos));
}
import { renderToString } from "react-dom/server";
import IndexPage from "./templates/index";
import fs from "fs";
// function App(props: {}) {
// return (
// <html>
// <head>
// <link href="./static/tailwind.css" rel="stylesheet" />
// {environment == "dev" ? (
// <script src="./static/dev/hotreload.js"></script>
// ) : (
// ""
// )}
// <title>Bun Render Test</title>
// </head>
// <body>
// {[1, 2, 3, 4, 5, 6, 7, 8, 9].map((el) => (
// <div key={el} className="flex flex-wrap gap-8">
// <p className="text-6xl text-red-400">{el}</p>
// <p className="text-8xl text-green-400">{el * 2}</p>
// </div>
// ))}
// </body>
// </html>
// );
// }
let html = renderToString(
<IndexPage
title="Wah-Collection"
@ -126,7 +103,7 @@ let html = renderToString(
`${environment == "prod" ? process.env.WEB_URL : "."}/data/images.json`,
`${environment == "prod" ? process.env.WEB_URL : "."}/data/videos.json`,
],
dns: [process.env.ENDPOINT as string],
dns: [process.env.ENDPOINT as string, "https://wsrv.nl"],
}}
/>
);

View file

@ -1,37 +0,0 @@
async function get(url) {
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Failed to fetch ${url}`);
}
return await res.json();
}
function random_five(type) {
let _selection = [];
let result = [];
if (type == "images") {
_selection = images;
} else if (type == "videos") {
_selection = videos;
} else {
console.error(`WRONG TYPE (${type})! @ function random_five!`);
}
const max = _selection.length - 1;
for (let i = 0; i < 4; i++) {
result.push(_selection[Math.floor(Math.random() * max)]);
}
return result;
}
// async function FetchData() {
// let images = await get("./data/images.json");
// let videos = await get("./data/videos.json");
// console.log(images, videos);
// }
// window.onload = () => {
// FetchData();
// }

View file

@ -0,0 +1,80 @@
async function get(url) {
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Failed to fetch ${url}`);
}
return await res.json();
}
function randomElements(array) {
let result = [];
const max = array.length - 1;
for (let i = 0; i < 7; i++) {
result.push(array[Math.floor(Math.random() * max)]);
}
return result;
}
async function FetchData() {
let images = await get("/data/images.json");
let videos = await get("/data/videos.json");
const imagesContainer = document.getElementById("index_images");
randomElements(images).forEach((item) => {
const wrapper = document.createElement("div");
wrapper.className = "w-48 h-48 relative";
wrapper.innerHTML = `
<div role="status" class="absolute w-full h-full flex items-center justify-center z-[2]">
<svg
aria-hidden="true"
class="inline w-16 h-16 text-gray-200 animate-spin dark:text-gray-600 fill-yellow-400"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span class="sr-only">Loading...</span>
</div>
`
const blurImg = document.createElement("img");
const Img = document.createElement("img");
blurImg.src = `https://wsrv.nl/?url=${encodeURI(item)}&w=16&h=16`;
blurImg.className = "object-cover w-full h-full absolute inset-0 z-[1]";
Img.src = `https://wsrv.nl/?url=${encodeURI(item)}&w=256&h=256`;
Img.className = "invisible object-cover w-full h-full absolute inset-0 z-[3]";
wrapper.appendChild(blurImg);
wrapper.appendChild(Img);
Img.addEventListener("load", () => {
Img.classList.remove("invisible");
Img.removeEventListener("load", this);
});
// wrapper.style.setProperty("--blur-img", ``)
// wrapper.className = `[background-image:var(--blur-img)] bg-cover bg-center w-48 h-48`;
// wrapper.style.setProperty("--blur-img", `url()`)
// console.log(item, wrapper)
imagesContainer.appendChild(wrapper);
// const el = document.createElement("img");
// el.src = `//wsrv.nl/?url=${image}&w=16&h=16&blur&t=fit&precrop=true`
// async function _loadFull(src) {
// const res = await fetch(`//wsrv.nl/?url=${image}`)
// if (res.ok) {
// el.src = `//wsrv.nl/?url=${image}`
// }
// }
// el.className = "w-48 h-48 object-cover"
// _loadFull()
});
}
window.onload = () => {
FetchData();
};

View file

@ -524,6 +524,23 @@
.collapse {
visibility: collapse;
}
.invisible {
visibility: hidden;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border-width: 0;
}
.absolute {
position: absolute;
}
.relative {
position: relative;
}
@ -533,18 +550,36 @@
.sticky {
position: sticky;
}
.inset-0 {
inset: calc(var(--spacing) * 0);
}
.top-0 {
top: calc(var(--spacing) * 0);
}
.top-24 {
top: calc(var(--spacing) * 24);
}
.right-0 {
right: calc(var(--spacing) * 0);
}
.left-0 {
left: calc(var(--spacing) * 0);
}
.left-24 {
left: calc(var(--spacing) * 24);
}
.z-10 {
z-index: 10;
}
.z-\[1\] {
z-index: 1;
}
.z-\[2\] {
z-index: 2;
}
.z-\[3\] {
z-index: 3;
}
.container {
width: 100%;
@media (width >= 40rem) {
@ -563,15 +598,27 @@
max-width: 96rem;
}
}
.m-auto {
margin: auto;
}
.mx-auto {
margin-inline: auto;
}
.my-auto {
margin-block: auto;
}
.block {
display: block;
}
.flex {
display: flex;
}
.hidden {
display: none;
}
.inline {
display: inline;
}
.inline-flex {
display: inline-flex;
}
@ -584,15 +631,60 @@
.h-6 {
height: calc(var(--spacing) * 6);
}
.h-8 {
height: calc(var(--spacing) * 8);
}
.h-16 {
height: calc(var(--spacing) * 16);
}
.h-24 {
height: calc(var(--spacing) * 24);
}
.h-32 {
height: calc(var(--spacing) * 32);
}
.h-48 {
height: calc(var(--spacing) * 48);
}
.h-64 {
height: calc(var(--spacing) * 64);
}
.h-full {
height: 100%;
}
.min-h-16 {
min-height: calc(var(--spacing) * 16);
}
.w-8 {
width: calc(var(--spacing) * 8);
}
.w-16 {
width: calc(var(--spacing) * 16);
}
.w-24 {
width: calc(var(--spacing) * 24);
}
.w-32 {
width: calc(var(--spacing) * 32);
}
.w-48 {
width: calc(var(--spacing) * 48);
}
.w-64 {
width: calc(var(--spacing) * 64);
}
.w-full {
width: 100%;
}
.border-collapse {
border-collapse: collapse;
}
.transform {
transform: var(--tw-rotate-x) var(--tw-rotate-y) var(--tw-rotate-z) var(--tw-skew-x) var(--tw-skew-y);
}
.animate-spin {
animation: var(--animate-spin);
}
.resize {
resize: both;
}
@ -605,6 +697,9 @@
.justify-between {
justify-content: space-between;
}
.justify-center {
justify-content: center;
}
.gap-4 {
gap: calc(var(--spacing) * 4);
}
@ -622,6 +717,24 @@
.bg-\[\#1d1f3d\] {
background-color: #1d1f3d;
}
.bg-\[var\(--blur-img\)\] {
background-color: var(--blur-img);
}
.\[background-image\:var\(--blur-img\)\] {
background-image: var(--blur-img);
}
.bg-cover {
background-size: cover;
}
.bg-center {
background-position: center;
}
.fill-yellow-400 {
fill: var(--color-yellow-400);
}
.object-cover {
object-fit: cover;
}
.px-8 {
padding-inline: calc(var(--spacing) * 8);
}
@ -639,6 +752,9 @@
.text-\[\#f9ebeb\] {
color: #f9ebeb;
}
.text-gray-200 {
color: var(--color-gray-200);
}
.text-green-400 {
color: var(--color-green-400);
}
@ -652,6 +768,10 @@
outline-style: var(--tw-outline-style);
outline-width: 1px;
}
.blur {
--tw-blur: blur(8px);
filter: var(--tw-blur,) var(--tw-brightness,) var(--tw-contrast,) var(--tw-grayscale,) var(--tw-hue-rotate,) var(--tw-invert,) var(--tw-saturate,) var(--tw-sepia,) var(--tw-drop-shadow,);
}
.filter {
filter: var(--tw-blur,) var(--tw-brightness,) var(--tw-contrast,) var(--tw-grayscale,) var(--tw-hue-rotate,) var(--tw-invert,) var(--tw-saturate,) var(--tw-sepia,) var(--tw-drop-shadow,);
}
@ -711,6 +831,11 @@
background-color: var(--color-slate-900);
}
}
.dark\:text-gray-600 {
@media (prefers-color-scheme: dark) {
color: var(--color-gray-600);
}
}
}
@keyframes spin {
to {

View file

@ -34,7 +34,6 @@ export default function head(props: {
) : (
""
)}
{/* <script src="./static/functions.js"></script> */}
</head>
);
}

View file

@ -24,7 +24,13 @@ export default function Index(props: {
/>
<body className="dark:bg-[#160606] text-[#f9ebeb]">
<Header path={props.path}/>
nothing yet . . .
<div className="container mx-auto">
<div>
<p>Images</p>
<div id="index_images" className="flex flex-wrap gap-4"></div>
</div>
</div>
<script src="./static/populate_index.js"></script>
</body>
</html>
);