Refactor Sitemap component to improve context usage by replacing direct socket reference with context destructuring. This change enhances code clarity and maintains functionality for fetching category data.

This commit is contained in:
seb
2025-07-05 15:57:00 +02:00
parent f8f03b45b8
commit a6d7ed3e27

View File

@@ -36,7 +36,7 @@ const collectAllCategories = (categoryNode, categories = [], level = 0) => {
const Sitemap = () => { const Sitemap = () => {
const [categories, setCategories] = useState([]); const [categories, setCategories] = useState([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const {socket} = useContext(SocketContext); const context = useContext(SocketContext);
const sitemapLinks = [ const sitemapLinks = [
@@ -67,8 +67,8 @@ const Sitemap = () => {
} }
// Otherwise, fetch from socket if available // Otherwise, fetch from socket if available
if (socket) { if (context && context.socket && context.socket.connected && typeof window !== "undefined") {
socket.emit('categoryList', { categoryId: 209 }, (response) => { context.socket.emit('categoryList', { categoryId: 209 }, (response) => {
if (response && response.categoryTree) { if (response && response.categoryTree) {
// Store in cache // Store in cache
try { try {
@@ -95,7 +95,7 @@ const Sitemap = () => {
}; };
fetchCategories(); fetchCategories();
}, [socket]); }, [context]);
const content = ( const content = (
<> <>