mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 16:11:12 -05:00
Reverted to commit 0091584677
This commit is contained in:
@@ -1,30 +1,4 @@
|
||||
/**
|
||||
* 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 { useQuery } from '@tanstack/react-query';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { queryKeys } from '@/lib/queryKeys';
|
||||
|
||||
@@ -44,35 +18,17 @@ interface RecentChange {
|
||||
changeReason?: string;
|
||||
}
|
||||
|
||||
export function useHomepageRecentChanges(
|
||||
enabled = true
|
||||
): UseQueryResult<RecentChange[]> {
|
||||
export function useHomepageRecentChanges(enabled = true) {
|
||||
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;
|
||||
|
||||
interface DatabaseRecentChange {
|
||||
entity_id: string;
|
||||
entity_name: string;
|
||||
entity_type: string;
|
||||
entity_slug: string;
|
||||
park_slug?: string;
|
||||
image_url?: string;
|
||||
change_type: string;
|
||||
changed_at: string;
|
||||
changed_by_username?: string;
|
||||
changed_by_avatar?: string;
|
||||
change_reason?: string;
|
||||
}
|
||||
|
||||
// Transform the database response to match our interface
|
||||
const result: RecentChange[] = (data as unknown as DatabaseRecentChange[] || []).map((item) => ({
|
||||
return (data || []).map((item: any) => ({
|
||||
id: item.entity_id,
|
||||
name: item.entity_name,
|
||||
type: item.entity_type as 'park' | 'ride' | 'company',
|
||||
@@ -86,17 +42,7 @@ export function useHomepageRecentChanges(
|
||||
avatarUrl: item.changed_by_avatar || undefined
|
||||
} : 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;
|
||||
})) as RecentChange[];
|
||||
},
|
||||
enabled,
|
||||
staleTime: 5 * 60 * 1000,
|
||||
|
||||
Reference in New Issue
Block a user