"use client"; import { useState } from "react"; import { useUser } from "@clerk/nextjs"; import log from "@/lib/logger"; import { toast } from "@/lib/toast"; import { useUsers, useRecommendations, useGenerateRecommendations, useApproveRecommendation, useUpdateRecommendation, type Recommendation, } from "@/hooks/use-api"; import { PageHeader } from "@/components/ui/PageHeader"; export default function RecommendationsPage() { const { user } = useUser(); const [useExternalModel, setUseExternalModel] = useState(false); const { data: users = [], isLoading: usersLoading } = useUsers(); const { data: allRecommendations = [], isLoading: recsLoading } = useRecommendations(); const pendingRecommendations = allRecommendations.filter( (r) => r.status === "pending", ); const generateRec = useGenerateRecommendations(); const approveRec = useApproveRecommendation(); const updateRec = useUpdateRecommendation(); const handleGenerate = async (userId: string) => { try { await generateRec.mutateAsync(userId); toast.success("Recommendation generated successfully!"); } catch (error) { log.error("Failed to generate recommendation", error); toast.error("Failed to generate recommendation."); } }; const handleApprove = async ( recommendationId: string, status: "approved" | "rejected", ) => { try { await approveRec.mutateAsync({ recommendationId, approved: status === "approved", }); toast.success("Recommendation status updated"); } catch (error) { log.error("Failed to approve recommendation", error); toast.error("Error processing request"); } }; const handleEdit = async (rec: Recommendation) => { const newContent = prompt("Edit Advice:", rec.content); const newActivityPlan = prompt("Edit Activity Plan:", rec.activityPlan); const newDietPlan = prompt("Edit Diet Plan:", rec.dietPlan); if ( newContent === null || newActivityPlan === null || newDietPlan === null ) { return; } try { await updateRec.mutateAsync({ id: rec.id, content: newContent, activityPlan: newActivityPlan, dietPlan: newDietPlan, }); toast.success("Recommendation updated successfully!"); } catch (error) { log.error("Failed to update recommendation", error); toast.error("Failed to update recommendation."); } }; if (usersLoading || recsLoading) { return (
Loading...
); } return (
{/* Generate Section */}

Generate Recommendations

Select a user to generate a new daily recommendation.

    {users.map((user) => (
  • {user.firstName} {user.lastName}

    {user.email}

  • ))} {users.length === 0 && (

    No users found.

    )}
{/* Pending Approvals Section */}

Pending Approvals

Review and approve AI-generated recommendations

{pendingRecommendations.length === 0 ? (

No pending recommendations.

) : (
    {pendingRecommendations.map((rec) => (
  • For: User {rec.userId}

    {new Date(rec.createdAt).toLocaleDateString()}
    Advice:{" "} {rec.recommendationText}
    Activity:{" "} {rec.activityPlan}
    Diet: {rec.dietPlan}
  • ))}
)}
); }