Fixed the automatic updating of all statuses.
This commit is contained in:
		@@ -304,12 +304,8 @@ const StatusList = ({headerImage, headerTitle}: StatusListProps) => {
 | 
				
			|||||||
    };
 | 
					    };
 | 
				
			||||||
  }, [fetchStatuses, isFocused, lastFetchTime]);
 | 
					  }, [fetchStatuses, isFocused, lastFetchTime]);
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  // Set up realtime subscription
 | 
					 | 
				
			||||||
  const setupRealtimeSubscription = useCallback(() => {
 | 
					  const setupRealtimeSubscription = useCallback(() => {
 | 
				
			||||||
    // Get only statuses from the last week to reduce payload
 | 
					    console.log('Setting up realtime subscription');
 | 
				
			||||||
    const oneWeekAgo = new Date();
 | 
					 | 
				
			||||||
    oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    const subscription = supabase
 | 
					    const subscription = supabase
 | 
				
			||||||
      .channel('status_changes')
 | 
					      .channel('status_changes')
 | 
				
			||||||
      .on('postgres_changes',
 | 
					      .on('postgres_changes',
 | 
				
			||||||
@@ -317,33 +313,135 @@ const StatusList = ({headerImage, headerTitle}: StatusListProps) => {
 | 
				
			|||||||
          event: 'INSERT',
 | 
					          event: 'INSERT',
 | 
				
			||||||
          schema: 'public',
 | 
					          schema: 'public',
 | 
				
			||||||
          table: 'statuses',
 | 
					          table: 'statuses',
 | 
				
			||||||
          filter: `created_at>gt.${oneWeekAgo.toISOString()}`
 | 
					 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        (payload) => {
 | 
					        (payload) => {
 | 
				
			||||||
          console.log('New status received:', payload);
 | 
					          console.log('New status received:', payload);
 | 
				
			||||||
          handleNewStatus(payload.new.id);
 | 
					          // Get the complete data immediately instead of just the ID
 | 
				
			||||||
 | 
					          const newStatus = payload.new;
 | 
				
			||||||
 | 
					          // Fetch the complete status with profile data
 | 
				
			||||||
 | 
					          fetchStatusWithProfile(newStatus.id);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      )
 | 
					      )
 | 
				
			||||||
      .subscribe((status) => {
 | 
					      .subscribe((status) => {
 | 
				
			||||||
        console.log('Realtime subscription status:', status);
 | 
					        console.log('Realtime subscription status:', status);
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    subscriptionRef.current = subscription;
 | 
					    subscriptionRef.current = subscription;
 | 
				
			||||||
    return subscription;
 | 
					    return subscription;
 | 
				
			||||||
  }, [handleNewStatus]);
 | 
					  }, []);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Add this new function to fetch a single status with profile data
 | 
				
			||||||
 | 
					  const fetchStatusWithProfile = async (statusId: string) => {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      console.log('Fetching status with profile:', statusId);
 | 
				
			||||||
 | 
					      const { data, error } = await supabase
 | 
				
			||||||
 | 
					        .from('statuses')
 | 
				
			||||||
 | 
					        .select(`
 | 
				
			||||||
 | 
					          id,
 | 
				
			||||||
 | 
					          user_id,
 | 
				
			||||||
 | 
					          status,
 | 
				
			||||||
 | 
					          created_at,
 | 
				
			||||||
 | 
					          profiles:profiles(full_name, avatar_url)
 | 
				
			||||||
 | 
					        `)
 | 
				
			||||||
 | 
					        .eq('id', statusId)
 | 
				
			||||||
 | 
					        .single();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (error) throw error;
 | 
				
			||||||
 | 
					      if (data) {
 | 
				
			||||||
 | 
					        // Transform the data
 | 
				
			||||||
 | 
					        const transformedStatus = {
 | 
				
			||||||
 | 
					          ...data,
 | 
				
			||||||
 | 
					          profiles: Array.isArray(data.profiles) ? data.profiles[0] : data.profiles
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        console.log('Received transformed status:', transformedStatus);
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        // Update statuses immediately without debouncing
 | 
				
			||||||
 | 
					        setStatuses(prevStatuses => {
 | 
				
			||||||
 | 
					          const newStatuses = [...prevStatuses];
 | 
				
			||||||
 | 
					          const existingIndex = newStatuses.findIndex(s => s.user_id === transformedStatus.user_id);
 | 
				
			||||||
 | 
					          
 | 
				
			||||||
 | 
					          if (existingIndex !== -1) {
 | 
				
			||||||
 | 
					            // If the new status is more recent, replace the existing one
 | 
				
			||||||
 | 
					            if (new Date(transformedStatus.created_at) > new Date(newStatuses[existingIndex].created_at)) {
 | 
				
			||||||
 | 
					              newStatuses[existingIndex] = transformedStatus;
 | 
				
			||||||
 | 
					              
 | 
				
			||||||
 | 
					              // Mark as recently updated
 | 
				
			||||||
 | 
					              setRecentlyUpdatedIds(prev => {
 | 
				
			||||||
 | 
					                const newSet = new Set(prev);
 | 
				
			||||||
 | 
					                newSet.add(transformedStatus.id);
 | 
				
			||||||
 | 
					                return newSet;
 | 
				
			||||||
 | 
					              });
 | 
				
			||||||
 | 
					              
 | 
				
			||||||
 | 
					              // Schedule removal of highlight
 | 
				
			||||||
 | 
					              setTimeout(() => {
 | 
				
			||||||
 | 
					                setRecentlyUpdatedIds(current => {
 | 
				
			||||||
 | 
					                  const updatedSet = new Set(current);
 | 
				
			||||||
 | 
					                  updatedSet.delete(transformedStatus.id);
 | 
				
			||||||
 | 
					                  return updatedSet;
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					              }, 3000);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					          } else {
 | 
				
			||||||
 | 
					            // If this is a new user, add to the array
 | 
				
			||||||
 | 
					            newStatuses.push(transformedStatus);
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            // Mark as recently updated
 | 
				
			||||||
 | 
					            setRecentlyUpdatedIds(prev => {
 | 
				
			||||||
 | 
					              const newSet = new Set(prev);
 | 
				
			||||||
 | 
					              newSet.add(transformedStatus.id);
 | 
				
			||||||
 | 
					              return newSet;
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            // Schedule removal of highlight
 | 
				
			||||||
 | 
					            setTimeout(() => {
 | 
				
			||||||
 | 
					              setRecentlyUpdatedIds(current => {
 | 
				
			||||||
 | 
					                const updatedSet = new Set(current);
 | 
				
			||||||
 | 
					                updatedSet.delete(transformedStatus.id);
 | 
				
			||||||
 | 
					                return updatedSet;
 | 
				
			||||||
 | 
					              });
 | 
				
			||||||
 | 
					            }, 3000);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					          
 | 
				
			||||||
 | 
					          // Sort by most recent
 | 
				
			||||||
 | 
					          newStatuses.sort((a, b) =>
 | 
				
			||||||
 | 
					            new Date(b.created_at).getTime() - new Date(a.created_at).getTime()
 | 
				
			||||||
 | 
					          );
 | 
				
			||||||
 | 
					          
 | 
				
			||||||
 | 
					          return newStatuses;
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        // Animate the fade-in
 | 
				
			||||||
 | 
					        Animated.sequence([
 | 
				
			||||||
 | 
					          Animated.timing(fadeAnimation, {
 | 
				
			||||||
 | 
					            toValue: 1,
 | 
				
			||||||
 | 
					            duration: 300,
 | 
				
			||||||
 | 
					            useNativeDriver: true,
 | 
				
			||||||
 | 
					          }),
 | 
				
			||||||
 | 
					          Animated.timing(fadeAnimation, {
 | 
				
			||||||
 | 
					            toValue: 0,
 | 
				
			||||||
 | 
					            duration: 300,
 | 
				
			||||||
 | 
					            delay: 2000,
 | 
				
			||||||
 | 
					            useNativeDriver: true,
 | 
				
			||||||
 | 
					          }),
 | 
				
			||||||
 | 
					        ]).start();
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
					      console.error('Error fetching status with profile:', error);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  // Set up real-time subscription when component is focused
 | 
					  // Set up real-time subscription when component is focused
 | 
				
			||||||
  useEffect(() => {
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    let subscription: RealtimeChannel | null = null;
 | 
				
			||||||
    if (isFocused && isConnected) {
 | 
					    if (isFocused && isConnected) {
 | 
				
			||||||
      // Initial fetch
 | 
					 | 
				
			||||||
      fetchStatuses();
 | 
					      fetchStatuses();
 | 
				
			||||||
      
 | 
					      subscription = setupRealtimeSubscription();
 | 
				
			||||||
      // Set up real-time subscription
 | 
					      console.log('Subscription established: ', subscription);
 | 
				
			||||||
      const subscription = setupRealtimeSubscription();
 | 
					 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
      // Clean up subscription when component unmounts or loses focus
 | 
					      // Clean up subscription when component unmounts or loses focus
 | 
				
			||||||
      return () => {
 | 
					      return () => {
 | 
				
			||||||
        if (subscription) {
 | 
					        if (subscription) {
 | 
				
			||||||
 | 
					          console.log('Removing subscription channel: ', subscription);
 | 
				
			||||||
          supabase.removeChannel(subscription);
 | 
					          supabase.removeChannel(subscription);
 | 
				
			||||||
          subscriptionRef.current = null;
 | 
					          subscriptionRef.current = null;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -540,14 +638,14 @@ const styles = StyleSheet.create({
 | 
				
			|||||||
    flexDirection: 'row',
 | 
					    flexDirection: 'row',
 | 
				
			||||||
    padding: 16,
 | 
					    padding: 16,
 | 
				
			||||||
    borderRadius: 12,
 | 
					    borderRadius: 12,
 | 
				
			||||||
    marginHorizontal: 16,
 | 
					    marginHorizontal: 8,
 | 
				
			||||||
    marginVertical: 8,
 | 
					    marginVertical: 8,
 | 
				
			||||||
    shadowColor: '#000',
 | 
					    shadowColor: '#000',
 | 
				
			||||||
    shadowOffset: { width: 0, height: 1 },
 | 
					    shadowOffset: { width: 0, height: 1 },
 | 
				
			||||||
    shadowOpacity: 0.1,
 | 
					    shadowOpacity: 0.1,
 | 
				
			||||||
    shadowRadius: 2,
 | 
					    shadowRadius: 2,
 | 
				
			||||||
    elevation: 2,
 | 
					    elevation: 2,
 | 
				
			||||||
    height: 104, // Fixed height for getItemLayout optimization
 | 
					    height: 120, // Fixed height for getItemLayout optimization
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  recentlyUpdated: {
 | 
					  recentlyUpdated: {
 | 
				
			||||||
    backgroundColor: 'rgba(100, 200, 255, 0.1)',
 | 
					    backgroundColor: 'rgba(100, 200, 255, 0.1)',
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user