"use client"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; import { Company } from "../../../lib/types"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { ShieldX, Settings, Save, Database, Bell } from "lucide-react"; export default function CompanySettingsPage() { const { data: session, status } = useSession(); // We store the full company object for future use and updates after save operations // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars const [company, setCompany] = useState(null); const [csvUrl, setCsvUrl] = useState(""); const [csvUsername, setCsvUsername] = useState(""); const [csvPassword, setCsvPassword] = useState(""); const [sentimentThreshold, setSentimentThreshold] = useState(""); const [message, setMessage] = useState(""); const [loading, setLoading] = useState(true); useEffect(() => { if (status === "authenticated") { const fetchCompany = async () => { setLoading(true); try { const res = await fetch("/api/dashboard/config"); const data = await res.json(); setCompany(data.company); setCsvUrl(data.company.csvUrl || ""); setCsvUsername(data.company.csvUsername || ""); setSentimentThreshold(data.company.sentimentAlert?.toString() || ""); if (data.company.csvPassword) { setCsvPassword(data.company.csvPassword); } } catch (error) { console.error("Failed to fetch company settings:", error); setMessage("Failed to load company settings."); } finally { setLoading(false); } }; fetchCompany(); } }, [status]); async function handleSave() { setMessage(""); try { const res = await fetch("/api/dashboard/settings", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ csvUrl, csvUsername, csvPassword, sentimentThreshold, }), }); if (res.ok) { setMessage("Settings saved successfully!"); // Update local state if needed const data = await res.json(); setCompany(data.company); } else { const error = await res.json(); setMessage( `Failed to save settings: ${error.message || "Unknown error"}` ); } } catch (error) { setMessage("Failed to save settings. Please try again."); console.error("Error saving settings:", error); } } // Loading state if (loading) { return (
Company Settings
Loading settings...
); } // Check for ADMIN access if (session?.user?.role !== "ADMIN") { return (
Access Denied

You don't have permission to view company settings.

); } return (
Company Settings
{message && ( {message} )}
{ e.preventDefault(); handleSave(); }} autoComplete="off" >
Data Source Configuration
setCsvUrl(e.target.value)} placeholder="https://example.com/data.csv" autoComplete="off" />
setCsvUsername(e.target.value)} placeholder="Username for CSV access (if needed)" autoComplete="off" />
setCsvPassword(e.target.value)} placeholder="Password will be updated only if provided" autoComplete="new-password" />

Leave blank to keep current password

Alert Configuration
setSentimentThreshold(e.target.value)} placeholder="Threshold value (0-100)" min="0" max="100" autoComplete="off" />

Percentage of negative sentiment sessions to trigger alert (0-100)

); }