mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
refactor(not-found page: use dynamic discovery for not-found images
This commit is contained in:
@@ -4,14 +4,26 @@ import useSubplebbitsStore from '@plebbit/plebbit-react-hooks/dist/stores/subple
|
||||
import { HomeLogo } from '../home';
|
||||
import styles from './not-found.module.css';
|
||||
|
||||
const totalNotFoundImages = 2;
|
||||
// Dynamically import all not-found images at build time
|
||||
const notFoundModules = import.meta.glob('/public/assets/not-found/not-found-*.{jpg,jpeg,gif,png}', {
|
||||
eager: true,
|
||||
query: '?url',
|
||||
import: 'default',
|
||||
});
|
||||
const notFoundPaths = Object.values(notFoundModules) as string[];
|
||||
|
||||
const NotFoundImage = () => {
|
||||
const [imagePath] = useState(() => {
|
||||
const randomBannerIndex = Math.floor(Math.random() * totalNotFoundImages) + 1;
|
||||
return `assets/not-found/not-found-${randomBannerIndex}.jpg`;
|
||||
if (notFoundPaths.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return notFoundPaths[Math.floor(Math.random() * notFoundPaths.length)];
|
||||
});
|
||||
|
||||
if (!imagePath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return <img src={imagePath} alt='' />;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user