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; overflow: clip;
} }
.GalleryItem > img, video { .GalleryItem > img,
.GalleryItem > video {
object-fit: contain; object-fit: contain;
max-height: 80vh; max-height: 80vh;
} }
@ -62,7 +63,16 @@ body {
background-color: #0056b3; background-color: #0056b3;
} }
.PlaceholderItem > button,
.ParentDirectoryItem > button { .ParentDirectoryItem > button {
background-color: #4D4F5D; 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 './App.css';
import Gallery from './Gallery/index'; import Gallery from './Gallery/index';

View File

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

BIN
src/Gallery/placeholder.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB