-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstance.tsx
More file actions
90 lines (78 loc) · 3.19 KB
/
Copy pathinstance.tsx
File metadata and controls
90 lines (78 loc) · 3.19 KB
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
import { useEffect, useState } from "react";
import { useParams } from "react-router";
import { Instance, InstancesResponse } from "@/types/InstancesResponse.ts";
import { Card, CardContent } from "@/components/ui/card.tsx";
import { LoadingSpinner } from "@/components/ui/Spinner.tsx";
import { apiGet } from "@/lib/api.ts";
import {findInstanceByName, getName} from "@/lib/instance-utils.tsx";
import { InstanceDetails } from "@/components/instances/instance-details.tsx";
export const InstanceDetail = () => {
const { name } = useParams<{ name: string }>();
const [instance, setInstance] = useState<Instance>();
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string>();
useEffect(() => {
const previousTitle = document.title;
if (instance) {
document.title = `${getName(instance)} - Etherpad Scanner`;
}
return () => {
document.title = previousTitle;
};
}, [instance]);
useEffect(() => {
const fetchInstance = async () => {
try {
// Fetch all instances and find the one matching the name parameter
const response = await apiGet<InstancesResponse>("/instances");
const decodedName = decodeURIComponent(name || "");
const found = findInstanceByName(response.instances, decodedName);
if (found) {
setInstance(found);
} else {
setError(`Instance "${decodedName}" not found`);
}
} catch (err) {
setError(`Failed to fetch instance: ${err instanceof Error ? err.message : "Unknown error"}`);
} finally {
setLoading(false);
}
};
void fetchInstance();
}, [name]);
if (loading) return <LoadingSpinner />;
if (error) {
return (
<div className="flex flex-col h-full overflow-y-auto p-5">
<h1 className="text-4xl font-bold mb-5">Instance Details</h1>
<Card>
<CardContent className="pt-6">
<p className="text-red-700">{error}</p>
</CardContent>
</Card>
</div>
);
}
if (!instance) {
return (
<div className="flex flex-col h-full overflow-y-auto p-5">
<h1 className="text-4xl font-bold mb-5">Instance Details</h1>
<Card>
<CardContent className="pt-6">
<p className="text-red-700">Instance not found</p>
</CardContent>
</Card>
</div>
);
}
return (
<div className="flex flex-col h-full overflow-y-auto p-5">
<h1 className="text-4xl font-bold mb-5">Instance Details</h1>
<h2 className="text-2xl mb-5">{getName(instance)}</h2>
<p className="mb-5">
Scanned on: {new Date(instance.scan.scan_time).toLocaleString()}
</p>
<InstanceDetails instance={instance} />
</div>
);
};