Apply all API enhancements

This commit is contained in:
gpt-engineer-app[bot]
2025-10-30 23:55:18 +00:00
parent 8f4110d890
commit d40f0f13aa
10 changed files with 435 additions and 45 deletions

View File

@@ -1,4 +1,30 @@
import { useQuery } from '@tanstack/react-query';
/**
* Homepage Recent Changes Hook
*
* Fetches recent entity changes (parks, rides, companies) for homepage display.
* Uses optimized RPC function for single-query fetch of all data.
*
* Features:
* - Fetches up to 24 recent changes
* - Includes entity details, change metadata, and user info
* - Single database query via RPC
* - 5 minute cache for homepage performance
* - Performance monitoring
*
* @param enabled - Whether the query should run (default: true)
* @returns Array of recent changes with full entity context
*
* @example
* ```tsx
* const { data: changes, isLoading } = useHomepageRecentChanges();
*
* changes?.forEach(change => {
* console.log(`${change.name} was ${change.changeType} by ${change.changedBy?.username}`);
* });
* ```
*/
import { useQuery, UseQueryResult } from '@tanstack/react-query';
import { supabase } from '@/integrations/supabase/client';
import { queryKeys } from '@/lib/queryKeys';
@@ -18,17 +44,21 @@ interface RecentChange {
changeReason?: string;
}
export function useHomepageRecentChanges(enabled = true) {
export function useHomepageRecentChanges(
enabled = true
): UseQueryResult<RecentChange[]> {
return useQuery({
queryKey: queryKeys.homepage.recentChanges(),
queryFn: async () => {
const startTime = performance.now();
// Use the new database function to get all changes in a single query
const { data, error } = await supabase.rpc('get_recent_changes', { limit_count: 24 });
if (error) throw error;
// Transform the database response to match our interface
return (data || []).map((item: any): RecentChange => ({
const result: RecentChange[] = (data || []).map((item: any) => ({
id: item.entity_id,
name: item.entity_name,
type: item.entity_type as 'park' | 'ride' | 'company',
@@ -43,6 +73,16 @@ export function useHomepageRecentChanges(enabled = true) {
} : undefined,
changeReason: item.change_reason || undefined
}));
// Performance monitoring (dev only)
if (import.meta.env.DEV) {
const duration = performance.now() - startTime;
if (duration > 500) {
console.warn(`⚠️ Slow query: useHomepageRecentChanges took ${duration.toFixed(0)}ms`, { changeCount: result.length });
}
}
return result;
},
enabled,
staleTime: 5 * 60 * 1000,