turash/bugulma/frontend/hooks/pages/useOrganizationData.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

47 lines
2.0 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';
import { historicalData } from '@/data/historicalData';
import { getSimilarOrganizations } from '@/services/organizations-api';
import { useOrganization as useOrganizationAPI } from '@/hooks/api';
import { useOrganizations } from '@/hooks/useOrganizations';
export const useOrganizationData = (organizationId: string | undefined) => {
const { data: organization, isLoading, error } = useOrganizationAPI(organizationId);
const { organizations } = useOrganizations();
const relatedLandmark = useMemo(() => {
if (!organization) return null;
return historicalData.find((landmark) => landmark.relatedOrgId === organization.ID) || null;
}, [organization]);
// Use backend API for similar organizations
// Returns empty structure immediately to prevent blocking render
const { data: similarOrgsData, isLoading: isLoadingSimilar } = useQuery({
queryKey: ['similarOrganizations', organizationId],
queryFn: () => getSimilarOrganizations(organizationId!, 5),
enabled: !!organizationId,
placeholderData: { organizations: [] }, // Render immediately with empty array
});
// Map backend response to Organization objects - always return an array
const similarOrgs = useMemo(() => {
if (!similarOrgsData?.organizations || !Array.isArray(similarOrgsData.organizations)) return [];
if (!organizations || !Array.isArray(organizations)) return [];
return similarOrgsData.organizations
.map((similar) => {
if (!similar?.id) return null;
return organizations.find((org) => org?.ID === similar.id);
})
.filter((org): org is NonNullable<typeof org> => org !== undefined && org !== null);
}, [similarOrgsData, organizations]);
return {
organization: organization || undefined,
relatedLandmark: relatedLandmark || null,
similarOrgs: Array.isArray(similarOrgs) ? similarOrgs : [],
isLoading: isLoading || isLoadingSimilar,
error: error instanceof Error ? error : error ? new Error(String(error)) : null,
};
};