"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 (
Select a user to generate a new daily recommendation.
{user.firstName} {user.lastName}
{user.email}
No users found.
)}Review and approve AI-generated recommendations
No pending recommendations.
) : (