import { Box, Button, Stack, ToggleButton, ToggleButtonGroup, Switch, FormControlLabel, FormGroup, } from '@mui/material'; import React, { useEffect, useState, ChangeEvent } from 'react'; import AccordionContainer from '../components/AccordionContainer'; import { Plugin } from '@typings/plugin'; import NovelItemCard from '../components/NovelItemCard'; import { Filters } from '@libs/filterInputs'; import usePlugin from '@hooks/usePlugin'; export default function PopularNovels() { const plugin = usePlugin(); const [novels, setNovels] = useState([]); const [filterValues, setFilterValues] = useState(); const [loading, setLoading] = useState(false); const [currentIndex, setCurrentIndex] = useState(0); const [maxIndex, setMaxIndex] = useState(0); const [isLatest, setIsLatest] = useState(true); const fetchNovelsByIndex = (index: number) => { if (plugin && index) { setLoading(true); plugin .popularNovels(index, { filters: filterValues, showLatestNovels: isLatest, }) .then(res => { if (res.length !== 0) { setCurrentIndex(index); if (index > maxIndex) { setMaxIndex(index); } setNovels(res); } }) .finally(() => setLoading(false)); } }; const handleSwitchLatestChange = (event: ChangeEvent) => { setIsLatest(event.target.checked); }; useEffect(() => { if (plugin) { setCurrentIndex(1); setMaxIndex(1); fetchNovelsByIndex(1); } }, [isLatest]); useEffect(() => { // Reset when changing plugins. setCurrentIndex(0); setMaxIndex(0); setNovels([]); if (plugin?.filters) { let filters = {}; for (const fKey in plugin.filters) { filters[fKey] = { type: plugin.filters[fKey].type, value: plugin.filters[fKey].value, }; } setFilterValues(filters); } }, [plugin]); return ( } label={{isLatest ? 'Latest' : 'Popular'}} /> fetchNovelsByIndex(value)} > {Array.from({ length: maxIndex }, (_, index) => index + 1).map( number => ( {number} ), )} {novels.map((novel, index) => ( ))} ); }