"use client"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Credenza, CredenzaBody, CredenzaClose, CredenzaContent, CredenzaDescription, CredenzaFooter, CredenzaHeader, CredenzaTitle } from "@app/components/Credenza"; import { useTranslations } from "next-intl"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { toast } from "@app/hooks/useToast"; import { formatAxiosError } from "@app/lib/api"; import { ListUserOlmsResponse } from "@server/routers/olm"; import { ResponseT } from "@server/types/Response"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@app/components/ui/table"; import { Tabs, TabsList, TabsTrigger, TabsContent } from "@app/components/ui/tabs"; import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog"; import { Loader2, RefreshCw } from "lucide-react"; import moment from "moment"; import { useUserContext } from "@app/hooks/useUserContext"; type ViewDevicesDialogProps = { open: boolean; setOpen: (val: boolean) => void; }; type Device = { olmId: string; dateCreated: string; version: string | null; name: string | null; clientId: number | null; userId: string | null; archived: boolean; }; export default function ViewDevicesDialog({ open, setOpen }: ViewDevicesDialogProps) { const t = useTranslations(); const { env } = useEnvContext(); const api = createApiClient({ env }); const { user } = useUserContext(); const [devices, setDevices] = useState([]); const [loading, setLoading] = useState(false); const [isArchiveModalOpen, setIsArchiveModalOpen] = useState(false); const [selectedDevice, setSelectedDevice] = useState(null); const [activeTab, setActiveTab] = useState<"available" | "archived">("available"); const fetchDevices = async () => { setLoading(true); try { const res = await api.get>( `/user/${user?.userId}/olms` ); if (res.data.success && res.data.data) { setDevices(res.data.data.olms); } } catch (error: any) { console.error("Error fetching devices:", error); toast({ variant: "destructive", title: t("errorLoadingDevices") || "Error loading devices", description: formatAxiosError( error, t("failedToLoadDevices") || "Failed to load devices" ) }); } finally { setLoading(false); } }; useEffect(() => { if (open) { fetchDevices(); } }, [open]); const archiveDevice = async (olmId: string) => { try { await api.post(`/user/${user?.userId}/olm/${olmId}/archive`); toast({ title: t("deviceArchived") || "Device archived", description: t("deviceArchivedDescription") || "The device has been successfully archived." }); // Update the device's archived status in the local state setDevices( devices.map((d) => d.olmId === olmId ? { ...d, archived: true } : d ) ); setIsArchiveModalOpen(false); setSelectedDevice(null); } catch (error: any) { console.error("Error archiving device:", error); toast({ variant: "destructive", title: t("errorArchivingDevice"), description: formatAxiosError( error, t("failedToArchiveDevice") ) }); } }; function reset() { setDevices([]); setSelectedDevice(null); setIsArchiveModalOpen(false); } return ( <> { setOpen(val); if (!val) { reset(); } }} > {t("viewDevices") || "View Devices"} {t("viewDevicesDescription") || "Manage your connected devices"} {loading ? (
) : ( setActiveTab(value as "available" | "archived") } className="w-full" > {t("available") || "Available"} ( { devices.filter( (d) => !d.archived ).length } ) {t("archived") || "Archived"} ( { devices.filter( (d) => d.archived ).length } ) {devices.filter((d) => !d.archived) .length === 0 ? (
{t("noDevices") || "No devices found"}
) : (
{t("name") || "Name"} {t("dateCreated") || "Date Created"} {t("actions") || "Actions"} {devices .filter( (d) => !d.archived ) .map((device) => ( {device.name || t( "unnamedDevice" ) || "Unnamed Device"} {moment( device.dateCreated ).format( "lll" )} ))}
)}
{devices.filter((d) => d.archived) .length === 0 ? (
{t("noArchivedDevices") || "No archived devices found"}
) : (
{t("name") || "Name"} {t("dateCreated") || "Date Created"} {devices .filter( (d) => d.archived ) .map((device) => ( {device.name || t( "unnamedDevice" ) || "Unnamed Device"} {moment( device.dateCreated ).format( "lll" )} ))}
)}
)}
{selectedDevice && ( { setIsArchiveModalOpen(val); if (!val) { setSelectedDevice(null); } }} dialog={

{t("deviceQuestionArchive") || "Are you sure you want to archive this device?"}

{t("deviceMessageArchive") || "The device will be archived and removed from your active devices list."}

} buttonText={t("deviceArchiveConfirm") || "Archive Device"} onConfirm={async () => archiveDevice(selectedDevice.olmId)} string={selectedDevice.name || selectedDevice.olmId} title={t("archiveDevice") || "Archive Device"} /> )} ); }