"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import Button from "@/components/shared/Button"; export default function ApplicationsPage() { const [applications, setApplications] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [deletingApps, setDeletingApps] = useState(new Set()); useEffect(() => { fetchApplications(); }, []); async function fetchApplications() { try { const response = await fetch( "http://localhost:8080/twirp/applications.ApplicationsService/ListApplications", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), }), }, ); if (!response.ok) throw new Error("Failed to fetch applications"); const data = await response.json(); setApplications(data.applications || []); } catch (error) { setError("Failed to load applications"); console.error("Error:", error); } finally { setIsLoading(false); } } async function handleDeleteApplication(appId) { if ( !confirm( "Are you sure you want to delete this application? This action cannot be undone and will delete all associated logs.", ) ) { return; } setDeletingApps((prev) => new Set([...prev, appId])); try { const response = await fetch( `http://localhost:8080/twirp/applications.ApplicationsService/DeleteApplication`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), application_id: appId, }), }, ); if (!response.ok) throw new Error("Failed to delete application"); await fetchApplications(); // Refresh the list } catch (error) { setError("Failed to delete application"); console.error("Error:", error); } finally { setDeletingApps((prev) => { const newSet = new Set(prev); newSet.delete(appId); return newSet; }); } } if (isLoading) { return (
Loading applications...
); } if (error) { return (
{error}
); } if (!applications.length) { return (

No applications

Create your first application to start sending logs to Argus.

); } return (
{applications.map((app) => ( handleDeleteApplication(app.id)} isDeleting={deletingApps.has(app.id)} /> ))}
); } function ApplicationItem({ application, onDelete, isDeleting }) { return (

{application.name}

{application.description}

Created on {new Date(application.created_at).toLocaleDateString()}
View Details
); }