Brijesh's Git Server — argus-web @ 30ea74d756f91d64b8586dcecd13dc641fc8e90a

Web Ul for argus

src/app/dashboard/applications/page.js (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
"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 (
      <div className="flex items-center justify-center h-64">
        <div className="text-gray-500">Loading applications...</div>
      </div>
    );
  }

  if (error) {
    return (
      <div className="rounded-lg bg-white shadow p-6">
        <div className="text-red-600">{error}</div>
      </div>
    );
  }

  if (!applications.length) {
    return (
      <div className="rounded-lg bg-white shadow">
        <div className="px-4 py-12 my-0">
          <div className="text-center">
            <h3 className="mt-2 text-lg font-semibold text-gray-900">
              No applications
            </h3>
            <p className="mt-1 text-sm text-gray-500">
              Create your first application to start sending logs to Argus.
            </p>
            <div className="mt-6">
              <Link href="/dashboard/applications/new">
                <Button variant="secondary">Create Application</Button>
              </Link>
            </div>
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="bg-white shadow rounded-lg divide-y divide-gray-200">
      {applications.map((app) => (
        <ApplicationItem
          key={app.id}
          application={app}
          onDelete={() => handleDeleteApplication(app.id)}
          isDeleting={deletingApps.has(app.id)}
        />
      ))}
    </div>
  );
}

function ApplicationItem({ application, onDelete, isDeleting }) {
  return (
    <div className="px-6 py-4">
      <div className="flex items-center justify-between">
        <div className="space-y-1">
          <h3 className="text-lg font-medium text-gray-900">
            {application.name}
          </h3>
          <p className="text-sm text-gray-500">{application.description}</p>
          <div className="text-xs text-gray-400">
            Created on {new Date(application.created_at).toLocaleDateString()}
          </div>
        </div>
        <div className="flex items-center space-x-4">
          <Link
            href={`/dashboard/applications/${application.id}`}
            className="text-sm text-indigo-600 hover:text-indigo-900"
          >
            View Details
          </Link>
          <button
            onClick={onDelete}
            disabled={isDeleting}
            className={`text-sm text-red-600 hover:text-red-900 ${
              isDeleting ? "opacity-50 cursor-not-allowed" : ""
            }`}
          >
            {isDeleting ? "Deleting..." : "Delete"}
          </button>
        </div>
      </div>
    </div>
  );
}