import Nav from '../layouts/Nav';
import { useState, useMemo, useEffect, useCallback, memo, useRef, lazy, Suspense } from 'react';
import { Search, LayoutGrid, ChevronLeft, ChevronRight, Play } from 'lucide-react';
import { useNavigate } from 'react-router-dom';
import { useOptions } from '/src/utils/optionsContext';
import styles from '../styles/apps.module.css';
import theme from '../styles/theming.module.css';
import clsx from 'clsx';
const Pagination = lazy(() => import('@mui/material/Pagination'));
const AppCard = memo(({ app, onClick, fallbackMap, onImgError, itemTheme, itemStyles }) => {
const [loaded, setLoaded] = useState(false);
return (
onClick(app) : undefined}
>
{!loaded && !fallbackMap[app.appName] && (
)}
{fallbackMap[app.appName] ? (
) : (

setLoaded(true)}
onError={() => onImgError(app.appName)}
/>
)}
{app.appName.split('').join('\u200B')}
);
});
const CategoryRow = memo(({ category, games, onClick, onViewMore, fallback, onImgError, theme, styles }) => {
const ref = useRef(null);
const scroll = (dir) => {
if (ref.current) {
ref.current.scrollBy({
left: dir === 'left' ? -400 : 400,
behavior: 'smooth'
});
}
};
return (
{category}
{games.map((game) => (
))}
);
});
const Games = memo(() => {
const nav = useNavigate();
const { options } = useOptions();
const [data, setData] = useState({});
useEffect(() => {
let a = true;
import('../data/apps.json').then((m) => a && setData(m.default?.games || {}));
return () => {
a = false;
};
}, []);
const [q, setQ] = useState('');
const [page, setPage] = useState(1);
const [category, setCategory] = useState(null);
const [fallback, setFallback] = useState({});
const [dlCount, setDlCount] = useState(0);
const [showDl, setShowDl] = useState(false);
const [dlGames, setDlGames] = useState([]);
useEffect(() => {
import('../utils/localGmLoader').then(async (m) => {
const loader = new m.default();
await loader.cleanupOld();
const gms = await loader.getAllGms();
setDlCount(gms.length);
setDlGames(gms);
}).catch(() => {});
}, []);
const perPage = options.itemsPerPage || 20;
const all = useMemo(() => {
const games = [];
Object.values(data).forEach((cats) => {
games.push(...cats);
});
return games;
}, [data]);
const filtered = useMemo(() => {
let toFilter = all;
if (showDl) {
const dlNames = new Set(dlGames.map(g => g.name));
toFilter = all.filter(game => {
const firstUrl = Array.isArray(game.url) ? game.url[0] : game.url;
const gmName = firstUrl?.split('/').pop()?.replace('.zip', '');
return gmName && dlNames.has(gmName);
});
} else if (category) {
toFilter = data[category] || [];
}
if (q) {
const fq = q.toLowerCase().trim().replace(/\s/g, '');
toFilter = toFilter.filter((game) => {
const gameName = game.appName.toLowerCase().replace(/\s/g, '');
return gameName.includes(fq);
});
}
const total = Math.ceil(toFilter.length / perPage);
const paged = toFilter.slice((page - 1) * perPage, page * perPage);
return { filteredGames: toFilter, paged, totalPages: total };
}, [all, data, category, showDl, dlGames, q, page, perPage]);
useEffect(() => {
if (page > filtered.totalPages && filtered.totalPages > 0) setPage(1);
}, [page, filtered.totalPages]);
const navApp = useCallback(
(app) => {
if (!app) return;
nav('/docs/r/', { state: { app } });
},
[nav],
);
const handleSearch = useCallback((e) => {
setQ(e.target.value);
setCategory(null);
setPage(1);
}, []);
const handleViewMore = useCallback((cat) => {
setCategory(cat);
setQ('');
setPage(1);
}, []);
const handleBack = useCallback(() => {
setCategory(null);
setShowDl(false);
setQ('');
setPage(1);
}, []);
const handleViewDl = useCallback(() => {
setShowDl(true);
setCategory(null);
setQ('');
setPage(1);
}, []);
const handleImgError = useCallback(
(name) => setFallback((prev) => ({ ...prev, [name]: true })),
[],
);
const searchCls = useMemo(
() => clsx(theme.appsSearchColor, theme[`theme-${options.theme || 'default'}`]),
[options.theme],
);
const placeholder = useMemo(() => `Search ${all.length} games`, [all.length]);
return (
{(category || showDl) && (
)}
{showDl && (
Local games not played for 3+ days are automatically removed
)}
{!category && !showDl && dlCount > 0 && (
)}
{q || category || showDl ? (
<>
{filtered.paged.map((game) => (
))}
{filtered.filteredGames.length > perPage && (
setPage(v)}
shape="rounded"
variant="outlined"
sx={{
'& .MuiPaginationItem-root': {
color: options.paginationTextColor || '#9baec8',
borderColor: options.paginationBorderColor || '#ffffff1c',
backgroundColor: options.paginationBgColor || '#141d2b',
fontFamily: 'SFProText',
},
'& .Mui-selected': {
backgroundColor: `${options.paginationSelectedColor || '#75b3e8'} !important`,
color: '#fff !important',
},
}}
/>
)}
>
) : (
{Object.entries(data).map(([cat, games]) => (
))}
)}
);
});
Games.displayName = 'Games';
const GamesLayout = () => {
const { options } = useOptions();
const scrollCls = clsx(
'scrollbar scrollbar-thin scrollbar-track-transparent',
!options?.type || options.type === 'dark'
? 'scrollbar-thumb-gray-600'
: 'scrollbar-thumb-gray-500',
);
return (
);
};
export default GamesLayout;