"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { AlertCircle, CheckCircle2, RefreshCw, Linkedin, Trash2, Plus } from "lucide-react"; import { SiFacebook, SiInstagram, SiYoutube } from "@icons-pack/react-simple-icons"; import Link from "next/link"; import { useSearchParams } from "next/navigation"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Badge } from "@/components/ui/badge"; import { useToast } from "@/hooks/use-toast"; interface ConnectedAccount { id: string; userId: string; provider: string; providerAccountId: string; name?: string | null; picture?: string | null; accessToken?: string | null; expiresAt?: Date | null; } interface SocialAutomation { id: string; userId: string; connectedAccountId: string; platform: string; triggerType: string; keywords: string[] | null; responseTemplate: string; actionType: string; createdAt: Date; } interface SocialSettingsProps { connectedAccounts: ConnectedAccount[]; automations?: SocialAutomation[]; } export function SocialSettings({ connectedAccounts, automations = [] }: SocialSettingsProps) { const searchParams = useSearchParams(); const error = searchParams.get("error"); const success = searchParams.get("success"); const { toast } = useToast(); const [autoReplies, setAutoReplies] = useState(automations); const [deleteId, setDeleteId] = useState(null); const fbAccount = connectedAccounts.find((a) => a.provider === "facebook"); const linkedinAccount = connectedAccounts.find((a) => a.provider === "linkedin"); const youtubeAccount = connectedAccounts.find((a) => a.provider === "youtube"); const handleDeleteAutomation = async () => { if (!deleteId) return; try { const res = await fetch(`/api/social/automations/${deleteId}`, { method: "DELETE", }); if (res.ok) { setAutoReplies((prev) => prev.filter((a) => a.id !== deleteId)); toast({ title: "Success", description: "Auto-reply rule deleted successfully", }); } else { toast({ title: "Error", description: "Failed to delete auto-reply rule", variant: "destructive", }); } } catch { toast({ title: "Error", description: "Failed to delete auto-reply rule", variant: "destructive", }); } finally { setDeleteId(null); } }; return (
{error && ( Error {error === "access_denied" ? "You denied access. Please try again and accept permissions." : error} )} {success && ( Success Account connected successfully! )}
{/* Facebook & Instagram */} Facebook & Instagram
{fbAccount ? (
{fbAccount.picture ? ( // eslint-disable-next-line @next/next/no-img-element {fbAccount.name ) : (
{fbAccount.name?.charAt(0)}
)}

{fbAccount.name}

Connected as {fbAccount.providerAccountId}

Connected & Active
) : (

Connect to manage Facebook Pages and Instagram Business accounts.

)}
{/* LinkedIn */} LinkedIn {linkedinAccount ? (
{linkedinAccount.picture ? ( // eslint-disable-next-line @next/next/no-img-element {linkedinAccount.name ) : (
{linkedinAccount.name?.charAt(0)}
)}

{linkedinAccount.name}

Connected as {linkedinAccount.providerAccountId}

Connected & Active
) : (

Connect LinkedIn to create posts and manage your profile.

)}
{/* YouTube */} YouTube {youtubeAccount ? (
{youtubeAccount.picture ? ( // eslint-disable-next-line @next/next/no-img-element {youtubeAccount.name ) : (
{youtubeAccount.name?.charAt(0)}
)}

{youtubeAccount.name}

Connected as {youtubeAccount.providerAccountId}

Connected & Active
) : (

Connect YouTube to upload videos and view analytics.

)}
{/* Auto-Reply Management Section */}
Auto-Reply Rules Manage automatic responses to comments and messages
{autoReplies.length === 0 ? (

No auto-reply rules configured yet

Create rules to automatically respond to comments and messages

) : (
{autoReplies.map((automation) => (
{automation.platform} {automation.triggerType}

{automation.keywords && automation.keywords.length > 0 ? `Keywords: ${automation.keywords.join(", ")}` : "All interactions"}

{automation.responseTemplate}

))}
)}
{/* Delete Confirmation Dialog */} setDeleteId(null)}> Delete Auto-Reply Rule? This will permanently delete this auto-reply rule. This action cannot be undone. Cancel Delete
); }