mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 23:31:12 -05:00
Implement cache management
This commit is contained in:
@@ -1,12 +1,20 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { useEffect } from 'react';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { queryKeys } from '@/lib/queryKeys';
|
||||
|
||||
/**
|
||||
* Hook to fetch reviews for a specific entity (park or ride)
|
||||
*/
|
||||
export function useEntityReviews(entityType: 'park' | 'ride', entityId: string | undefined, enabled = true) {
|
||||
return useQuery({
|
||||
export function useEntityReviews(
|
||||
entityType: 'park' | 'ride',
|
||||
entityId: string | undefined,
|
||||
enabled = true,
|
||||
enableRealtime = false // New parameter for opt-in real-time updates
|
||||
) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: queryKeys.reviews.entity(entityType, entityId || ''),
|
||||
queryFn: async () => {
|
||||
if (!entityId) return [];
|
||||
@@ -35,4 +43,34 @@ export function useEntityReviews(entityType: 'park' | 'ride', entityId: string |
|
||||
gcTime: 10 * 60 * 1000,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
|
||||
// Real-time subscription for new reviews (opt-in)
|
||||
useEffect(() => {
|
||||
if (!enableRealtime || !entityId || !enabled) return;
|
||||
|
||||
const channel = supabase
|
||||
.channel(`reviews-${entityType}-${entityId}`)
|
||||
.on(
|
||||
'postgres_changes',
|
||||
{
|
||||
event: 'INSERT',
|
||||
schema: 'public',
|
||||
table: 'reviews',
|
||||
filter: `${entityType}_id=eq.${entityId},moderation_status=eq.approved`,
|
||||
},
|
||||
(payload) => {
|
||||
console.log('⭐ New review posted:', payload.new);
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.reviews.entity(entityType, entityId)
|
||||
});
|
||||
}
|
||||
)
|
||||
.subscribe();
|
||||
|
||||
return () => {
|
||||
supabase.removeChannel(channel);
|
||||
};
|
||||
}, [enableRealtime, entityType, entityId, enabled, queryClient]);
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user