This document outlines the real-time data update features implemented in the GlobeTrotter application.
The application now supports real-time data updates across all major features, ensuring that users see immediate changes in the UI when data is added, updated, or deleted.
- Location:
/app/itinerary/[id]/ - Components:
AddActivityForm.tsx- Handles activity creation with immediate UI feedbackAddActivitySection.tsx- Manages local state for instant updatesClientItineraryWrapper.tsx- Provides real-time subscription to activity changes
How it works:
- When adding activities, the form immediately updates the local state
- The API returns the created activity data for instant UI updates
- Supabase real-time subscriptions keep data in sync across browser tabs
- Cost calculations update in real-time as activities are added
- Features:
- Daily cost calculations update immediately
- Total trip cost displays with live indicator
- Quick navigation shows costs per day
- Dashboard stats update in real-time
- Location:
/app/itinerary/ - Components:
ClientItineraryList.tsx- Real-time subscription to itinerary changesItineraryForm.tsx- Automatic page refresh after creation
How it works:
- New itineraries appear immediately in the list
- Itinerary count updates live on dashboard
- Real-time subscriptions keep data synchronized
- Location:
/app/dashboard/ - Component:
ClientDashboard.tsx - Features:
- Live trip counters
- Real-time activity counts
- Dynamic spending totals
- Visual indicators for live updates
- Location:
/app/calendar/ - Feature: Automatic data refresh every 30 seconds
- Benefit: Ensures calendar data stays current without manual refresh
Uses Supabase's real-time functionality:
const channel = supabase
.channel('activities_changes')
.on('postgres_changes', {
event: '*',
schema: 'public',
table: 'activities',
filter: `itinerary_id=eq.${itineraryId}`
}, (payload) => {
// Handle real-time updates
})
.subscribe();Components use local state for immediate UI feedback:
const [localActivitiesByDay, setLocalActivitiesByDay] =
useState<Record<string, Activity[]>>(activitiesByDay);
const handleActivityAdded = (newActivity?: Activity) => {
if (newActivity) {
setLocalActivitiesByDay(prev => ({
...prev,
[newActivity.date]: [...(prev[newActivity.date] || []), newActivity]
}));
}
};API routes now return created data for immediate updates:
const { data: newActivity, error } = await supabase
.from("activities")
.insert([{ itinerary_id, date, activity_name, cost }])
.select()
.single();
return NextResponse.json({ success: true, activity: newActivity });- Pulsing green dots indicate live data
- "Live updates enabled" text confirms real-time functionality
- Success notifications provide immediate feedback
- Loading states show when operations are in progress
- Form submissions show instant success/error messages
- Data appears in UI immediately after creation
- Costs and counters update without page refresh
- Navigation between days shows updated data instantly
- Changes made in one browser tab appear in other open tabs
- Consistent data state across multiple sessions
- Real-time notifications for data changes
- Better User Experience: No need to refresh pages to see updates
- Instant Feedback: Users immediately see the result of their actions
- Collaborative Features: Multiple users can see each other's changes
- Reduced Server Load: Less manual refreshing by users
- Modern Web App Feel: Behaves like a native application
lib/hooks/useRealTimeData.ts- Custom hook for real-time data managementapp/itinerary/[id]/ClientItineraryWrapper.tsx- Client wrapper for itinerary detailsapp/itinerary/ClientItineraryList.tsx- Client wrapper for itinerary listapp/dashboard/ClientDashboard.tsx- Client wrapper for dashboard
app/itinerary/[id]/AddActivityForm.tsx- Enhanced form with callback supportapp/itinerary/[id]/AddActivitySection.tsx- Local state management for real-time updatesapp/itinerary/[id]/page.tsx- Uses client wrapper for real-time featuresapp/itinerary/ItineraryForm.tsx- Auto-refresh after creationapp/itinerary/page.tsx- Uses client wrapper for real-time list updatesapp/dashboard/page.tsx- Uses client wrapper for live dashboardapp/calendar/page.tsx- Auto-refresh functionalityapp/api/add-activity/route.ts- Returns created activity data
- Push Notifications: Notify users of changes via browser notifications
- Conflict Resolution: Handle simultaneous edits by multiple users
- Offline Support: Cache changes locally when offline
- Real-time Chat: Add collaborative features for trip planning
- Activity Deletion: Add real-time deletion of activities
- Bulk Operations: Support bulk actions with real-time updates
- Open the application in two browser tabs
- Add an activity in one tab
- Observe the activity appear immediately in both tabs
- Check that costs update in real-time
- Create a new itinerary and see it appear in the list immediately
- Verify dashboard stats update as activities are added