File size: 7,512 Bytes
9b2dc95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a34cccb
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { useState } from 'react';
import { StatusCard } from './components/StatusCard';
import { MetricsChart } from './components/MetricsChart';
import { AddRepoModal } from './components/AddRepoModal';
import { usePolling } from './hooks/usePolling';
import type { Repo, RepoStatus, Metrics } from './types';

const API_BASE = '/api';

export function App() {
  const [selectedRepoId, setSelectedRepoId] = useState<string | null>(null);
  const [selectedRange, setSelectedRange] = useState<'hour' | 'day' | 'week' | 'month'>('hour');
  const [autoRefresh, setAutoRefresh] = useState(true);
  const [refreshInterval, setRefreshInterval] = useState(5000);
  const [showAddModal, setShowAddModal] = useState(false);

  const { data: repos, loading: reposLoading, refetch: refetchRepos } = usePolling<Repo[]>(
    () => fetch(`${API_BASE}/repos`).then(r => r.json()),
    refreshInterval,
    autoRefresh
  );

  const { data: statusData, loading: statusLoading } = usePolling<RepoStatus | null>(
    () => selectedRepoId ? fetch(`${API_BASE}/status?repoId=${selectedRepoId}`).then(r => r.json()) : Promise.resolve(null),
    refreshInterval,
    autoRefresh && !!selectedRepoId
  );

  const { data: metricsData, loading: metricsLoading } = usePolling<Metrics | null>(
    () => selectedRepoId ? fetch(`${API_BASE}/metrics?repoId=${selectedRepoId}&range=${selectedRange}`).then(r => r.json()) : Promise.resolve(null),
    refreshInterval,
    autoRefresh && !!selectedRepoId
  );

  const handleAddRepo = async (namespace: string, repo: string) => {
    await fetch(`${API_BASE}/repos`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ namespace, repo }),
    });
    refetchRepos();
  };

  const handleDeleteRepo = async (repoId: string) => {
    await fetch(`${API_BASE}/repos/${repoId}`, { method: 'DELETE' });
    if (selectedRepoId === repoId) setSelectedRepoId(null);
    refetchRepos();
  };

  const selectedRepo = repos?.find(r => r.id === selectedRepoId);

  return (
    <div className="min-h-screen bg-gray-900 text-white">
      <header className="bg-gray-800 border-b border-gray-700 px-6 py-4">
        <div className="flex items-center justify-between">
          <h1 className="text-2xl font-bold bg-gradient-to-r from-blue-400 to-purple-500 bg-clip-text text-transparent">
            SpaceProbe
          </h1>
          <button
            onClick={() => setShowAddModal(true)}
            className="px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-lg transition"
          >
            + Add Repo
          </button>
        </div>
      </header>

      <main className="p-6">
        <div className="grid grid-cols-1 lg:grid-cols-4 gap-6">
          <div className="lg:col-span-1 space-y-4">
            <div className="bg-gray-800 rounded-lg p-4 border border-gray-700">
              <h2 className="text-lg font-semibold mb-3">Repositories</h2>
              {reposLoading ? (
                <div className="space-y-2">
                  {[1, 2, 3].map(i => (
                    <div key={i} className="h-12 bg-gray-700 rounded animate-pulse"></div>
                  ))}
                </div>
              ) : repos?.length === 0 ? (
                <p className="text-gray-500 text-sm">No repositories added</p>
              ) : (
                <div className="space-y-2">
                  {repos?.map(repo => (
                    <div
                      key={repo.id}
                      onClick={() => setSelectedRepoId(repo.id)}
                      className={`p-3 rounded-lg cursor-pointer transition ${
                        selectedRepoId === repo.id
                          ? 'bg-blue-600/20 border border-blue-500'
                          : 'bg-gray-700 hover:bg-gray-600'
                      }`}
                    >
                      <div className="flex items-center justify-between">
                        <span className="text-sm font-medium">{repo.namespace}/{repo.repo}</span>
                        <button
                          onClick={(e) => {
                            e.stopPropagation();
                            handleDeleteRepo(repo.id);
                          }}
                          className="text-gray-500 hover:text-red-400"
                        >
                          ×
                        </button>
                      </div>
                    </div>
                  ))}
                </div>
              )}
            </div>

            <div className="bg-gray-800 rounded-lg p-4 border border-gray-700">
              <h2 className="text-lg font-semibold mb-3">Settings</h2>
              <div className="space-y-3">
                <label className="flex items-center justify-between">
                  <span className="text-gray-400">Auto-refresh</span>
                  <input
                    type="checkbox"
                    checked={autoRefresh}
                    onChange={(e) => setAutoRefresh(e.target.checked)}
                    className="w-4 h-4 accent-blue-500"
                  />
                </label>
                <div>
                  <span className="text-gray-400 text-sm">Interval (ms)</span>
                  <select
                    value={refreshInterval}
                    onChange={(e) => setRefreshInterval(Number(e.target.value))}
                    className="w-full mt-1 bg-gray-700 text-white rounded px-3 py-2"
                  >
                    <option value={1000}>1s</option>
                    <option value={5000}>5s</option>
                    <option value={10000}>10s</option>
                    <option value={30000}>30s</option>
                  </select>
                </div>
              </div>
            </div>
          </div>

          <div className="lg:col-span-3 space-y-6">
            {!selectedRepoId ? (
              <div className="flex items-center justify-center h-64 text-gray-500">
                Select a repository to view metrics
              </div>
            ) : (
              <>
                <StatusCard
                  status={statusData}
                  namespace={selectedRepo?.namespace || ''}
                  repo={selectedRepo?.repo || ''}
                  loading={statusLoading}
                />

                <div className="flex gap-2">
                  {(['hour', 'day', 'week', 'month'] as const).map(range => (
                    <button
                      key={range}
                      onClick={() => setSelectedRange(range)}
                      className={`px-4 py-2 rounded-lg transition ${
                        selectedRange === range
                          ? 'bg-blue-600 text-white'
                          : 'bg-gray-700 text-gray-400 hover:bg-gray-600'
                      }`}
                    >
                      {range.charAt(0).toUpperCase() + range.slice(1)}
                    </button>
                  ))}
                </div>

                <div className="grid grid-cols-1 gap-6">
                  <MetricsChart data={metricsData} metric="cpu" loading={metricsLoading} />
                  <MetricsChart data={metricsData} metric="memory" loading={metricsLoading} />
                </div>
              </>
            )}
          </div>
        </div>
      </main>

      <AddRepoModal
        isOpen={showAddModal}
        onClose={() => setShowAddModal(false)}
        onSubmit={handleAddRepo}
      />
    </div>
  );
}