Compare commits

..

3 Commits

Author SHA1 Message Date
f3d7c7abf2 Implement basic placeholder skeleton when loading 2023-11-24 19:24:40 +08:00
07dd094d6b Fix css selectors for img and video as direct child 2023-11-24 19:10:15 +08:00
1555cf37cd Refactor post() and deal with fetched data as a typed interface.
the post() function now uses await syntax rather than .then()
2023-11-23 19:17:45 +08:00
4 changed files with 96 additions and 57 deletions

View File

@ -38,7 +38,8 @@ body {
overflow: clip;
}
.GalleryItem > img, video {
.GalleryItem > img,
.GalleryItem > video {
object-fit: contain;
max-height: 80vh;
}
@ -62,7 +63,16 @@ body {
background-color: #0056b3;
}
.PlaceholderItem > button,
.ParentDirectoryItem > button {
background-color: #4D4F5D;
}
.PlaceholderItem > a {
height: 1em;
}
.PlaceholderItem > button {
height: calc(16px + 10px * 2);
width: 6em;
}

View File

@ -1,4 +1,3 @@
import React from 'react';
import './App.css';
import Gallery from './Gallery/index';

View File

@ -1,24 +1,30 @@
import React from 'react';
import '../App.css';
import placeholderPng from './placeholder.png';
import { useState, useEffect } from 'react';
function Gallery() {
const [data, setData] = useState('');
const [data, setData] = useState<galleryItemInfo[]>([]);
const [category, setCategory] = useState('');
useEffect(() => {
post(category, (json: string) => {
setData(json);
});
post(category, (data: galleryItemInfo[]) => {
setData(data);
});
}, [category]);
const updateCategory = (category: string) => {
setData(getPlaceholderData);
setCategory(category);
};
return (
<div className="Gallery">
<div className="Header">
{getContents(data, true, category, setCategory)}
{Contents(data, true, category, updateCategory)}
</div>
<div className="Contents">
{getContents(data, false, category, setCategory)}
{Contents(data, false, category, updateCategory)}
</div>
</div>
);
@ -28,32 +34,25 @@ interface galleryItemInfo {
is_dir: boolean;
url: string;
thumbnail_url: string;
isPlaceholder: boolean;
}
function getContents(
data: string,
function Contents(
data: galleryItemInfo[],
getDir: boolean,
category: string,
setCategory: (category: string) => void
) {
if (data === '') return;
if (data === null) return;
let obj = null;
try {
obj = JSON.parse(data);
} catch (error: unknown) {
if (!(error instanceof SyntaxError)) {
throw new Error(error as unknown as undefined);
}
return data;
}
return obj
.sort((a: galleryItemInfo, b: galleryItemInfo) => {
return data
.sort((a: galleryItemInfo, b: galleryItemInfo): number => {
if (a.is_dir && b.is_dir) return 0;
if (a.is_dir && !b.is_dir) return -1;
if (!a.is_dir && b.is_dir) return 1;
if (!a.is_dir && !b.is_dir) return 0;
return 0;
})
.filter((item: galleryItemInfo) => {
return getDir == item.is_dir;
@ -65,6 +64,7 @@ function getContents(
thumbnail_url:
item.thumbnail_url === null ? item.url : item.thumbnail_url,
onClick: () => {},
isPlaceholder: item.isPlaceholder,
};
if (item.is_dir) {
@ -91,11 +91,13 @@ interface galleryItem {
url: string;
thumbnail_url: string;
onClick: () => void;
isPlaceholder: boolean;
}
function ImageItem({ thumbnail_url, url }: galleryItem) {
function ImageItem({ thumbnail_url, url, isPlaceholder }: galleryItem) {
const placeholderClass = isPlaceholder ? 'PlaceholderItem' : '';
return (
<div className="GalleryItem">
<div className={`GalleryItem ${placeholderClass}`}>
<a href={url} target="_blank">
{getFileName(url)}
</a>
@ -117,16 +119,15 @@ function VideoItem({ url }: galleryItem) {
);
}
function DirectoryItem({ url, onClick }: galleryItem) {
function DirectoryItem({ url, onClick, isPlaceholder }: galleryItem) {
let buttonText = getFileName(url);
const isBackButton = buttonText === '..';
buttonText = isBackButton ? 'Back' : `Category: ${buttonText}`;
const placeholderClass = isPlaceholder ? 'PlaceholderItem' : '';
buttonText = isBackButton ? 'Back' : `${buttonText}`;
const backButtonClass = isBackButton ? 'ParentDirectoryItem' : '';
return (
<div className={`DirectoryItem ${backButtonClass}`}>
<button className="DirectoryItem" onClick={onClick}>
{buttonText}
</button>
<div className={`DirectoryItem ${backButtonClass} ${placeholderClass}`}>
<button onClick={onClick}>{buttonText}</button>
</div>
);
}
@ -135,10 +136,40 @@ function getFileName(string: string): string {
return string.split('/').at(-1) as unknown as string;
}
function post(category: string, callback: (text: string) => void) {
fetch(
'http://localhost/photo-viewer-backend/php/get.php',
//'https://dundun.ddns.net/photo-viewer/photo-viewer-backend/php/get.php',
function getPlaceholderData(): galleryItemInfo[] {
const placeholderDir = (): galleryItemInfo => {
return {
is_dir: true,
url: '',
thumbnail_url: '',
isPlaceholder: true,
};
};
const placeholderImg = (): galleryItemInfo => {
return {
is_dir: false,
url: '',
thumbnail_url: `${placeholderPng}`,
isPlaceholder: true,
};
};
return [
placeholderDir(),
placeholderDir(),
placeholderImg(),
placeholderImg(),
placeholderImg(),
];
}
async function post(
category: string,
setDataCallback: (data: galleryItemInfo[]) => void
) {
try {
const response = await fetch(
//'http://localhost/photo-viewer-backend/php/get.php',
'https://dundun.ddns.net/photo-viewer/photo-viewer-backend/php/get.php',
{
method: 'POST',
headers: {
@ -148,20 +179,19 @@ function post(category: string, callback: (text: string) => void) {
category: category,
}),
}
)
.then((response) => {
if (!response.ok) {
throw new Error('Network response was not ok');
);
setDataCallback(await response.json());
} catch (error) {
console.error('Error fetching data: ', error);
setDataCallback([
{
is_dir: false,
url: 'error_fetching',
thumbnail_url: '',
isPlaceholder: false,
},
]);
}
return response;
})
.then((response) => response.text())
.then((data) => {
callback(data as unknown as string);
})
.catch((error) => {
console.error('There was a problem with the fetch operation:', error);
});
}
export default Gallery;

BIN
src/Gallery/placeholder.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB