import { router } from '@inertiajs/react'; import Box from '@mui/material/Box'; import Card from '@mui/material/Card'; import CardContent from '@mui/material/CardContent'; import CardMedia from '@mui/material/CardMedia'; import Chip from '@mui/material/Chip'; import Grid from '@mui/material/Grid'; import Typography from '@mui/material/Typography'; import { styled } from '@mui/material/styles'; import Headline from '../../components/Headline'; import { Pagination } from '@mui/material'; import { useState, useEffect, useRef } from "react"; import Author from './Author'; import { DotLottieReact } from '@lottiefiles/dotlottie-react'; import { Search } from './Search'; const StyledCard = styled(Card)(({ theme }) => ({ display: 'flex', flexDirection: 'column', padding: 0, height: '100%', backgroundColor: (theme.vars || theme).palette.background.paper, '&:hover': { backgroundColor: 'transparent', cursor: 'pointer', }, '&:focus-visible': { outline: '3px solid', outlineColor: 'hsla(210, 98%, 48%, 0.5)', outlineOffset: '2px', }, })); const StyledCardContent = styled(CardContent)({ display: 'flex', flexDirection: 'column', gap: 4, padding: 16, flexGrow: 1, '&:last-child': { paddingBottom: 16, }, }); const StyledTypography = styled(Typography)({ display: '-webkit-box', WebkitBoxOrient: 'vertical', WebkitLineClamp: 2, overflow: 'hidden', textOverflow: 'ellipsis', }); export default function MainContent({ pageTitle, pageDescription, categories, news, filters }) { const [selectedCategory, setSelectedCategory] = useState(filters?.category || 'all'); const [search, setSearch] = useState(filters?.search || ''); const [focusedCardIndex, setFocusedCardIndex] = useState(null); const searchDebounceRef = useRef(null); const handleFocus = (index) => { setFocusedCardIndex(index); }; useEffect(() => { if (search === (filters?.search || '')) { return; } if (searchDebounceRef.current) { clearTimeout(searchDebounceRef.current); } searchDebounceRef.current = setTimeout(() => { router.get(window.location.pathname, { category: selectedCategory, search: search }, { preserveState: true, preserveScroll: true, replace: true, }); }, 500); return () => { if (searchDebounceRef.current) { clearTimeout(searchDebounceRef.current); } }; }, [search]); const handleBlur = () => { setFocusedCardIndex(null); }; const handleCategory = (category) => { if (searchDebounceRef.current) { clearTimeout(searchDebounceRef.current); } setSelectedCategory(category); router.get(window.location.pathname, { category: category, search: search }, { preserveState: true, preserveScroll: true }); }; const handleSearch = () => { if (searchDebounceRef.current) { clearTimeout(searchDebounceRef.current); } router.get(window.location.pathname, { category: selectedCategory, search: search }, { preserveState: true, preserveScroll: true }); }; const handlePageChange = (event, value) => { router.get(window.location.pathname, { page: value, category: selectedCategory, search: search }, { preserveState: true, preserveScroll: true, }); }; return (
setSearch(e.target.value)} onSearch={handleSearch} /> handleCategory('all')} size="medium" label="Semua" sx={{ backgroundColor: selectedCategory === 'all' ? '' : 'transparent', }} /> {categories.map((category, index) => handleCategory(category.id)} size="medium" label={category.name} key={index} sx={{ backgroundColor: selectedCategory === category.id ? '' : 'transparent', }} /> )} setSearch(e.target.value)} onSearch={handleSearch} /> {news.data.map((news, index) => handleFocus(news.id)} onBlur={handleBlur} tabIndex={0} className={focusedCardIndex === news.id ? 'Mui-focused' : ''} onClick={() => router.get(`/news/${news.slug}`)} > {news.categories.map((category, index) => )} {news.title} )} {news.data.length > 0 ? ( ) : ( Tidak ada berita yang ditemukan ) }
); }