show a message about no matches found

This commit is contained in:
190km
2024-12-11 17:58:35 +01:00
parent 49b812e462
commit 9a51e0a00d

View File

@@ -1,253 +1,251 @@
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import React, { useEffect, useRef } from "react"; import React, { useEffect, useRef } from "react";
import { getLogType, LogLine, parseLogs } from "./utils"; import { getLogType, LogLine, parseLogs } from "./utils";
import { TerminalLine } from "./terminal-line"; import { TerminalLine } from "./terminal-line";
import { Download as DownloadIcon } from "lucide-react"; import { Download as DownloadIcon } from "lucide-react";
import { import {
Select, Select,
SelectContent, SelectContent,
SelectItem, SelectItem,
SelectTrigger, SelectTrigger,
SelectValue, SelectValue,
} from "@/components/ui/select"; } from "@/components/ui/select";
import { Badge } from "@/components/ui/badge"; import { Badge } from "@/components/ui/badge";
interface Props { interface Props {
containerId: string; containerId: string;
serverId?: string | null; serverId?: string | null;
} }
type TimeFilter = "all" | "1h" | "6h" | "24h" | "168h" | "720h"; type TimeFilter = "all" | "1h" | "6h" | "24h" | "168h" | "720h";
type TypeFilter = "all" | "error" | "warning" | "success" | "info"; type TypeFilter = "all" | "error" | "warning" | "success" | "info";
export const DockerLogsId: React.FC<Props> = ({ containerId, serverId }) => { export const DockerLogsId: React.FC<Props> = ({ containerId, serverId }) => {
const [rawLogs, setRawLogs] = React.useState(""); const [rawLogs, setRawLogs] = React.useState("");
const [filteredLogs, setFilteredLogs] = React.useState<LogLine[]>([]); const [filteredLogs, setFilteredLogs] = React.useState<LogLine[]>([]);
const [autoScroll, setAutoScroll] = React.useState(true); const [autoScroll, setAutoScroll] = React.useState(true);
const [lines, setLines] = React.useState<number>(100); const [lines, setLines] = React.useState<number>(100);
const [search, setSearch] = React.useState<string>(""); const [search, setSearch] = React.useState<string>("");
const [since, setSince] = React.useState<TimeFilter>("all"); const [since, setSince] = React.useState<TimeFilter>("all");
const [typeFilter, setTypeFilter] = React.useState<TypeFilter>("all"); const [typeFilter, setTypeFilter] = React.useState<TypeFilter>("all");
const scrollRef = useRef<HTMLDivElement>(null); const scrollRef = useRef<HTMLDivElement>(null);
const [errorMessage, setErrorMessage] = React.useState<string | null>(null);
const scrollToBottom = () => {
if (autoScroll && scrollRef.current) { const scrollToBottom = () => {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight; if (autoScroll && scrollRef.current) {
} scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}; }
};
const handleScroll = () => {
if (!scrollRef.current) return; const handleScroll = () => {
if (!scrollRef.current) return;
const { scrollTop, scrollHeight, clientHeight } = scrollRef.current;
const isAtBottom = Math.abs(scrollHeight - scrollTop - clientHeight) < 10; const { scrollTop, scrollHeight, clientHeight } = scrollRef.current;
setAutoScroll(isAtBottom); const isAtBottom = Math.abs(scrollHeight - scrollTop - clientHeight) < 10;
}; setAutoScroll(isAtBottom);
};
const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
setRawLogs(""); const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
setFilteredLogs([]); setRawLogs("");
setSearch(e.target.value || ""); setFilteredLogs([]);
}; setSearch(e.target.value || "");
};
const handleLines = (e: React.ChangeEvent<HTMLInputElement>) => {
setRawLogs(""); const handleLines = (e: React.ChangeEvent<HTMLInputElement>) => {
setFilteredLogs([]); setRawLogs("");
setLines(Number(e.target.value) || 1); setFilteredLogs([]);
}; setLines(Number(e.target.value) || 1);
};
const handleSince = (value: TimeFilter) => {
setRawLogs(""); const handleSince = (value: TimeFilter) => {
setFilteredLogs([]); setRawLogs("");
setSince(value); setFilteredLogs([]);
}; setSince(value);
};
const handleTypeFilter = (value: TypeFilter) => {
setTypeFilter(value); const handleTypeFilter = (value: TypeFilter) => {
}; setTypeFilter(value);
};
useEffect(() => {
setRawLogs(""); useEffect(() => {
setFilteredLogs([]); setRawLogs("");
}, [containerId]); setFilteredLogs([]);
}, [containerId]);
useEffect(() => {
if (!containerId) return; useEffect(() => {
if (!containerId) return;
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const params = new globalThis.URLSearchParams({ const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
containerId, const params = new globalThis.URLSearchParams({
tail: lines.toString(), containerId,
since, tail: lines.toString(),
search since,
}); search
});
if (serverId) {
params.append('serverId', serverId); if (serverId) {
} params.append('serverId', serverId);
}
const wsUrl = `${protocol}//${window.location.host}/docker-container-logs?${params.toString()}`;
console.log("Connecting to WebSocket:", wsUrl); const wsUrl = `${protocol}//${window.location.host}/docker-container-logs?${params.toString()}`;
const ws = new WebSocket(wsUrl); console.log("Connecting to WebSocket:", wsUrl);
const ws = new WebSocket(wsUrl);
ws.onopen = () => {
console.log("WebSocket connected"); ws.onopen = () => {
}; console.log("WebSocket connected");
};
ws.onmessage = (e) => {
// console.log("Received message:", e.data); ws.onmessage = (e) => {
setRawLogs((prev) => prev + e.data); // console.log("Received message:", e.data);
}; setRawLogs((prev) => prev + e.data);
};
ws.onerror = (error) => {
console.error("WebSocket error:", error); ws.onerror = (error) => {
}; console.error("WebSocket error:", error);
};
ws.onclose = (e) => {
console.log("WebSocket closed:", e.reason); ws.onclose = (e) => {
setRawLogs( console.log("WebSocket closed:", e.reason);
(prev) => setErrorMessage(`Connection closed!\nReason: ${e.reason || "WebSocket was closed try to refresh"}`);
`${prev}Connection closed!\nReason: ${ };
e.reason || "WebSocket was closed try to refresh"
}\n` return () => {
); if (ws.readyState === WebSocket.OPEN) {
}; ws.close();
}
return () => { };
if (ws.readyState === WebSocket.OPEN) { }, [containerId, serverId, lines, search, since]);
ws.close();
} const handleDownload = () => {
}; const logContent = filteredLogs
}, [containerId, serverId, lines, search, since]); .map(
({ timestamp, message }: { timestamp: Date | null; message: string }) =>
const handleDownload = () => { `${timestamp?.toISOString() || "No timestamp"} ${message}`
const logContent = filteredLogs )
.map( .join("\n");
({ timestamp, message }: { timestamp: Date | null; message: string }) =>
`${timestamp?.toISOString() || "No timestamp"} ${message}` const blob = new Blob([logContent], { type: "text/plain" });
) const url = URL.createObjectURL(blob);
.join("\n"); const a = document.createElement("a");
a.href = url;
const blob = new Blob([logContent], { type: "text/plain" }); a.download = `dokploy-logs-${new Date().toISOString()}.txt`;
const url = URL.createObjectURL(blob); document.body.appendChild(a);
const a = document.createElement("a"); a.click();
a.href = url; document.body.removeChild(a);
a.download = `dokploy-logs-${new Date().toISOString()}.txt`; URL.revokeObjectURL(url);
document.body.appendChild(a); };
a.click();
document.body.removeChild(a); const handleFilter = (logs: LogLine[]) => {
URL.revokeObjectURL(url); return logs.filter((log) => {
}; const logType = getLogType(log.message).type;
const handleFilter = (logs: LogLine[]) => { const matchesType = typeFilter === "all" || logType === typeFilter;
return logs.filter((log) => {
const logType = getLogType(log.message).type; return matchesType;
});
const matchesType = typeFilter === "all" || logType === typeFilter; };
return matchesType; useEffect(() => {
}); setRawLogs("");
}; setFilteredLogs([]);
}, [containerId]);
useEffect(() => {
setRawLogs(""); useEffect(() => {
setFilteredLogs([]); const logs = parseLogs(rawLogs);
}, [containerId]); const filtered = handleFilter(logs);
setFilteredLogs(filtered);
useEffect(() => { }, [rawLogs, search, lines, since, typeFilter]);
const logs = parseLogs(rawLogs);
const filtered = handleFilter(logs); useEffect(() => {
setFilteredLogs(filtered); scrollToBottom();
}, [rawLogs, search, lines, since, typeFilter]);
if (autoScroll && scrollRef.current) {
useEffect(() => { scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
scrollToBottom(); }
}, [filteredLogs, autoScroll]);
if (autoScroll && scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight; return (
} <div className="flex flex-col gap-4">
}, [filteredLogs, autoScroll]); <div className="shadow-md rounded-lg overflow-hidden">
<div className="space-y-4">
return ( <div className="flex flex-wrap justify-between items-start sm:items-center gap-4">
<div className="flex flex-col gap-4"> <div className="flex flex-wrap gap-4">
<div className="shadow-md rounded-lg overflow-hidden"> <Input
<div className="space-y-4"> type="text"
<div className="flex flex-wrap justify-between items-start sm:items-center gap-4"> placeholder="Number of lines to show"
<div className="flex flex-wrap gap-4"> value={lines}
<Input onChange={handleLines}
type="text" className="inline-flex h-9 text-sm text-white placeholder-gray-400 w-full sm:w-auto"
placeholder="Number of lines to show" />
value={lines} <Input
onChange={handleLines} type="search"
className="inline-flex h-9 text-sm text-white placeholder-gray-400 w-full sm:w-auto" placeholder="Search logs..."
/> value={search}
<Input onChange={handleSearch}
type="search" className="inline-flex h-9 text-sm text-white placeholder-gray-400 w-full sm:w-auto"
placeholder="Search logs..." />
value={search} <Select value={since} onValueChange={handleSince}>
onChange={handleSearch} <SelectTrigger className="w-[180px] h-9">
className="inline-flex h-9 text-sm text-white placeholder-gray-400 w-full sm:w-auto" <SelectValue placeholder="Time filter" />
/> </SelectTrigger>
<Select value={since} onValueChange={handleSince}> <SelectContent>
<SelectTrigger className="w-[180px] h-9"> <SelectItem value="1h">Last 1 hour</SelectItem>
<SelectValue placeholder="Time filter" /> <SelectItem value="6h">Last 6 hours</SelectItem>
</SelectTrigger> <SelectItem value="24h">Last 24 hours</SelectItem>
<SelectContent> <SelectItem value="168h">Last 7 days</SelectItem>
<SelectItem value="1h">Last 1 hour</SelectItem> <SelectItem value="720h">Last 30 days</SelectItem>
<SelectItem value="6h">Last 6 hours</SelectItem> <SelectItem value="all">All time</SelectItem>
<SelectItem value="24h">Last 24 hours</SelectItem> </SelectContent>
<SelectItem value="168h">Last 7 days</SelectItem> </Select>
<SelectItem value="720h">Last 30 days</SelectItem>
<SelectItem value="all">All time</SelectItem> <Select value={typeFilter} onValueChange={handleTypeFilter}>
</SelectContent> <SelectTrigger className="w-[180px] h-9">
</Select> <SelectValue placeholder="Type filter" />
</SelectTrigger>
<Select value={typeFilter} onValueChange={handleTypeFilter}> <SelectContent>
<SelectTrigger className="w-[180px] h-9"> <SelectItem value="all">
<SelectValue placeholder="Type filter" /> <Badge variant="blank">All</Badge>
</SelectTrigger> </SelectItem>
<SelectContent> <SelectItem value="error">
<SelectItem value="all"> <Badge variant="red">Error</Badge>
<Badge variant="blank">All</Badge> </SelectItem>
</SelectItem> <SelectItem value="warning">
<SelectItem value="error"> <Badge variant="yellow">Warning</Badge>
<Badge variant="red">Error</Badge> </SelectItem>
</SelectItem> <SelectItem value="success">
<SelectItem value="warning"> <Badge variant="green">Success</Badge>
<Badge variant="yellow">Warning</Badge> </SelectItem>
</SelectItem> <SelectItem value="info">
<SelectItem value="success"> <Badge variant="blue">Info</Badge>
<Badge variant="green">Success</Badge> </SelectItem>
</SelectItem> </SelectContent>
<SelectItem value="info"> </Select>
<Badge variant="blue">Info</Badge> </div>
</SelectItem>
</SelectContent> <Button
</Select> variant="outline"
</div> size="sm"
className="h-9"
<Button onClick={handleDownload}
variant="outline" >
size="sm" <DownloadIcon className="mr-2 h-4 w-4" />
className="h-9" Download logs
onClick={handleDownload} </Button>
> </div>
<DownloadIcon className="mr-2 h-4 w-4" /> <div
Download logs ref={scrollRef}
</Button> onScroll={handleScroll}
</div> className="h-[720px] overflow-y-auto space-y-0 border p-4 bg-[#d4d4d4] dark:bg-[#050506] rounded custom-logs-scrollbar"
<div >
ref={scrollRef} {
onScroll={handleScroll} filteredLogs.length > 0 ? filteredLogs.map((filteredLog: LogLine, index: number) => (
className="h-[720px] overflow-y-auto space-y-0 border p-4 bg-[#d4d4d4] dark:bg-[#050506] rounded custom-logs-scrollbar" <TerminalLine key={index} log={filteredLog} searchTerm={search} />
> )) : <div className="flex justify-center items-center h-full text-muted-foreground">No logs found</div>
{filteredLogs.map((filteredLog: LogLine, index: number) => ( }
<TerminalLine key={index} log={filteredLog} searchTerm={search} /> </div>
))} </div>
</div> </div>
</div> </div>
</div> );
</div> };
);
};