// src/pages/admin/ActivityLog.tsx import React from 'react'; import { ActivityLogItem } from '../../types'; import { UserProfile } from '../../types'; import { formatDistanceToNow } from 'date-fns'; import { useActivityLogQuery } from '../../hooks/queries/useActivityLogQuery'; import { logger } from '../../services/logger.client'; export type ActivityLogClickHandler = (log: ActivityLogItem) => void; interface ActivityLogProps { userProfile: UserProfile | null; onLogClick?: ActivityLogClickHandler; } const renderLogDetails = (log: ActivityLogItem, onLogClick?: ActivityLogClickHandler) => { // With discriminated unions, we can safely access properties based on the 'action' type. const userName = log.user_full_name || 'A user'; const isClickable = onLogClick !== undefined; switch (log.action) { case 'flyer_processed': return ( A new flyer for {log.details.store_name || 'a store'} was added. ); case 'recipe_created': return ( {userName} added a new recipe:{' '} onLogClick(log) : undefined} className={isClickable ? 'text-blue-500 hover:underline cursor-pointer' : ''} > {log.details.recipe_name || 'Untitled Recipe'} . ); case 'user_registered': return ( {log.details.full_name || 'A new user'} just joined! ); case 'recipe_favorited': return ( {userName} favorited the recipe:{' '} onLogClick(log) : undefined} className={isClickable ? 'text-blue-500 hover:underline cursor-pointer' : ''} > {log.details.recipe_name || 'a recipe'} . ); case 'list_shared': return ( {userName} shared the list " onLogClick(log) : undefined} className={isClickable ? 'text-blue-500 hover:underline cursor-pointer' : ''} > {log.details.list_name || 'a shopping list'} " with {log.details.shared_with_name || 'another user'}. ); default: return An unknown activity occurred.; } }; export const ActivityLog: React.FC = ({ userProfile, onLogClick }) => { // Use TanStack Query for data fetching (ADR-0005 Phase 5) const { data: logs = [], isLoading, error } = useActivityLogQuery(20, 0); if (!userProfile) { return null; // Don't render the component if the user is not logged in } return (

Recent Activity

{isLoading &&

Loading activity...

} {error &&

{error.message}

} {!isLoading && !error && logs.length === 0 && (

No recent activity to show.

)}
); };