turash/bugulma/frontend/hooks/map/useMapData.ts
Damir Mukimov 6347f42e20
Consolidate repositories: Remove nested frontend .git and merge into main repository
- Remove nested git repository from bugulma/frontend/.git
- Add all frontend files to main repository tracking
- Convert from separate frontend/backend repos to unified monorepo
- Preserve all frontend code and development history as tracked files
- Eliminate nested repository complexity for simpler development workflow

This creates a proper monorepo structure with frontend and backend
coexisting in the same repository for easier development and deployment.
2025-11-25 06:02:57 +01:00

91 lines
2.8 KiB
TypeScript

import { useEffect, useMemo, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { z } from 'zod';
import { historicalData } from '@/data/historicalData.ts';
import { historicalLandmarkSchema } from '@/schemas/historical.ts';
import { organizationsService } from '@/services/organizations-api.ts';
import { useOrganizationFilter } from '@/hooks/useOrganizationFilter.ts';
import { useMapFilter } from '@/contexts/MapFilterContext.tsx';
import { useDebouncedValue } from '@/hooks/useDebouncedValue.ts';
export const useMapData = () => {
const { organizations: allOrganizations } = useMapFilter();
const [searchParams] = useSearchParams();
const [searchResults, setSearchResults] = useState<typeof allOrganizations>([]);
const [isSearching, setIsSearching] = useState(false);
const searchQuery = searchParams.get('search');
const historicalLandmarks = useMemo(() => {
try {
return z.array(historicalLandmarkSchema).parse(historicalData);
} catch {
// Silently return empty array - historical landmarks are optional
return [];
}
}, []);
// Perform backend search when search query parameter is present
useEffect(() => {
if (searchQuery && searchQuery.trim()) {
setIsSearching(true);
// Add a small delay to show loading state for better UX
const searchPromise = organizationsService.search(searchQuery.trim(), 200);
// Set a minimum loading time for better UX perception
const minLoadingTime = new Promise(resolve => setTimeout(resolve, 300));
Promise.all([searchPromise, minLoadingTime])
.then(([results]) => {
setSearchResults(results);
})
.catch((error) => {
console.error('[useMapData] Search failed:', error);
setSearchResults([]);
})
.finally(() => {
setIsSearching(false);
});
} else {
setSearchResults([]);
setIsSearching(false);
}
}, [searchQuery]);
const {
searchTerm,
setSearchTerm,
selectedSectors,
sortOption,
setSortOption,
handleSectorChange,
} = useMapFilter();
// Debounce search term for filtering performance
const debouncedSearchTerm = useDebouncedValue(searchTerm, 300);
// Use search results if available, otherwise use all organizations
const organizationsToFilter = searchQuery && searchQuery.trim() ? searchResults : allOrganizations || [];
const filteredAndSortedOrgs = useOrganizationFilter(
organizationsToFilter,
debouncedSearchTerm,
selectedSectors,
sortOption
);
return {
organizations: organizationsToFilter,
historicalLandmarks,
filteredAndSortedOrgs,
searchTerm,
setSearchTerm,
debouncedSearchTerm,
selectedSectors,
sortOption,
setSortOption,
handleSectorChange,
isLoading: isSearching,
};
};