import React, { useEffect, useState, useCallback } from 'react'; import NextImage from 'next/image'; import Link from 'next/link'; import { createSlug } from '@/core/utils/slug'; import { Swiper, SwiperSlide } from 'swiper/react'; import 'swiper/css'; import { fetchCategoryManagementSolr, fetchCategoryManagementVersion, } from '../api/categoryManagementApi'; const saveToLocalStorage = (key, data, version) => { const now = new Date(); const item = { value: data, version: version, lastFetchedTime: now.getTime(), }; localStorage.setItem(key, JSON.stringify(item)); }; const getFromLocalStorage = (key) => { const itemStr = localStorage.getItem(key); if (!itemStr) return null; const item = JSON.parse(itemStr); return item; }; const getElapsedTime = (lastFetchedTime) => { const now = new Date(); return now.getTime() - lastFetchedTime; }; const CategoryDynamicMobile = () => { const [selectedCategory, setSelectedCategory] = useState({}); const [categoryManagement, setCategoryManagement] = useState([]); const [isLoading, setIsLoading] = useState(false); useEffect(() => { const fetchCategoryData = async () => { setIsLoading(true); const res = await fetch('/api/category-management'); const { data } = await res.json(); if (data) { setCategoryManagement(data); } setIsLoading(false); }; fetchCategoryData(); }, []); useEffect(() => { if (categoryManagement?.length > 0) { const initialSelections = categoryManagement.reduce((acc, category) => { if (category.categories.length > 0) { acc[category.id] = category.categories[0].id_level_2; } return acc; }, {}); setSelectedCategory(initialSelections); } }, [categoryManagement]); const handleCategoryLevel2Click = (categoryId, idLevel2) => { setSelectedCategory((prev) => ({ ...prev, [categoryId]: idLevel2, })); }; return (