mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
Some checks failed
CI/CD Pipeline / backend-lint (push) Failing after 31s
CI/CD Pipeline / backend-build (push) Has been skipped
CI/CD Pipeline / frontend-lint (push) Failing after 1m37s
CI/CD Pipeline / frontend-build (push) Has been skipped
CI/CD Pipeline / e2e-test (push) Has been skipped
- Replace all 'any' types with proper TypeScript interfaces - Fix React hooks setState in useEffect issues with lazy initialization - Remove unused variables and imports across all files - Fix React Compiler memoization dependency issues - Add comprehensive i18n translation keys for admin interfaces - Apply consistent prettier formatting throughout codebase - Clean up unused bulk editing functionality - Improve type safety and code quality across frontend Files changed: 39 - ImpactMetrics.tsx: Fixed any types and interfaces - AdminVerificationQueuePage.tsx: Added i18n keys, removed unused vars - LocalizationUIPage.tsx: Fixed memoization, added translations - LocalizationDataPage.tsx: Added type safety and translations - And 35+ other files with various lint fixes
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { useLayoutEffect, 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
|
|
useLayoutEffect(() => {
|
|
if (searchQuery && searchQuery.trim()) {
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
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,
|
|
};
|
|
};
|