turash/bugulma/frontend/hooks/map/useOrganizationSites.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

71 lines
2.1 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';
import { BackendSite } from '@/schemas/backend/site';
import { getAllSites } from '@/services/sites-api';
import { Organization } from '@/types';
/**
* Hook to fetch sites for multiple organizations
* Returns a map of organization ID -> site (first site if multiple)
*/
export const useOrganizationSites = (organizations: Organization[]) => {
// Filter out organizations without valid IDs
const validOrgs = useMemo(
() => organizations.filter((org) => org.ID && org.ID.trim() !== ''),
[organizations]
);
const { data: allSites = [], isLoading, error } = useQuery({
queryKey: ['sites', 'all'],
queryFn: getAllSites,
staleTime: 5 * 60 * 1000, // 5 minutes
gcTime: 10 * 60 * 1000, // 10 minutes
retry: 1,
retryDelay: 1000,
});
// Debug logging for sites query
if (process.env.NODE_ENV === 'development') {
if (error) {
console.error('[useOrganizationSites] Sites query error:', error);
}
}
// Create a map of org ID -> first site (or null)
const orgSitesMap = useMemo(() => {
const map = new Map<string, BackendSite | null>();
validOrgs.forEach((org) => {
// Find sites that belong to this organization
const orgSites = allSites.filter((site) => site.OwnerOrganizationID === org.ID);
if (orgSites.length > 0) {
// Use first site if multiple exist
map.set(org.ID, orgSites[0]);
} else {
map.set(org.ID, null);
}
});
// Debug logging
if (process.env.NODE_ENV === 'development') {
console.log('[useOrganizationSites]', {
totalOrgs: validOrgs.length,
totalSites: allSites.length,
sitesWithOwners: allSites.filter(s => s.OwnerOrganizationID !== 'unknown-org').length,
orgSitesMapSize: map.size,
sampleMappings: Array.from(map.entries()).slice(0, 5).map(([orgId, site]) => ({
orgId,
hasSite: !!site,
siteCoords: site ? [site.Latitude, site.Longitude] : null,
})),
});
}
return map;
}, [validOrgs, allSites]);
return {
orgSitesMap,
isLoading,
};
};