feat: improved deployment view scroll & style

This commit is contained in:
190km
2024-12-14 01:30:36 +01:00
parent 280be5c9df
commit 138650d561
2 changed files with 58 additions and 22 deletions

View File

@@ -20,9 +20,25 @@ interface Props {
export const ShowDeployment = ({ logPath, open, onClose, serverId }: Props) => { export const ShowDeployment = ({ logPath, open, onClose, serverId }: Props) => {
const [data, setData] = useState(""); const [data, setData] = useState("");
const [filteredLogs, setFilteredLogs] = useState<LogLine[]>([]); const [filteredLogs, setFilteredLogs] = useState<LogLine[]>([]);
const endOfLogsRef = useRef<HTMLDivElement>(null);
const wsRef = useRef<WebSocket | null>(null); // Ref to hold WebSocket instance const wsRef = useRef<WebSocket | null>(null); // Ref to hold WebSocket instance
const [autoScroll, setAutoScroll] = useState(true);
const scrollRef = useRef<HTMLDivElement>(null);
const scrollToBottom = () => {
if (autoScroll && scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
};
const handleScroll = () => {
if (!scrollRef.current) return;
const { scrollTop, scrollHeight, clientHeight } = scrollRef.current;
const isAtBottom = Math.abs(scrollHeight - scrollTop - clientHeight) < 10;
setAutoScroll(isAtBottom);
};
useEffect(() => { useEffect(() => {
if (!open || !logPath) return; if (!open || !logPath) return;
@@ -53,9 +69,6 @@ export const ShowDeployment = ({ logPath, open, onClose, serverId }: Props) => {
}; };
}, [logPath, open]); }, [logPath, open]);
const scrollToBottom = () => {
endOfLogsRef.current?.scrollIntoView({ behavior: "smooth" });
};
useEffect(() => { useEffect(() => {
const logs = parseLogs(data); const logs = parseLogs(data);
@@ -64,7 +77,12 @@ export const ShowDeployment = ({ logPath, open, onClose, serverId }: Props) => {
useEffect(() => { useEffect(() => {
scrollToBottom(); scrollToBottom();
}, [data]);
if (autoScroll && scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
}, [filteredLogs, autoScroll]);
return ( return (
<Dialog <Dialog
@@ -90,9 +108,11 @@ export const ShowDeployment = ({ logPath, open, onClose, serverId }: Props) => {
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
<div className="text-wrap rounded-lg border p-4 text-sm sm:max-w-[59rem]"> <div
<div> ref={scrollRef}
{ onScroll={handleScroll}
className="h-[720px] overflow-y-auto space-y-0 border p-4 bg-[#d4d4d4] dark:bg-[#050506] rounded custom-logs-scrollbar"
> {
filteredLogs.length > 0 ? filteredLogs.map((log: LogLine, index: number) => ( filteredLogs.length > 0 ? filteredLogs.map((log: LogLine, index: number) => (
<TerminalLine <TerminalLine
key={index} key={index}
@@ -105,8 +125,6 @@ export const ShowDeployment = ({ logPath, open, onClose, serverId }: Props) => {
<Loader2 className="h-6 w-6 animate-spin" /> <Loader2 className="h-6 w-6 animate-spin" />
</div> </div>
)} )}
<div ref={endOfLogsRef} />
</div>
</div> </div>
</DialogContent> </DialogContent>
</Dialog> </Dialog>

View File

@@ -25,9 +25,26 @@ export const ShowDeploymentCompose = ({
serverId, serverId,
}: Props) => { }: Props) => {
const [data, setData] = useState(""); const [data, setData] = useState("");
const endOfLogsRef = useRef<HTMLDivElement>(null);
const [filteredLogs, setFilteredLogs] = useState<LogLine[]>([]); const [filteredLogs, setFilteredLogs] = useState<LogLine[]>([]);
const wsRef = useRef<WebSocket | null>(null); // Ref to hold WebSocket instance const wsRef = useRef<WebSocket | null>(null); // Ref to hold WebSocket instance
const [autoScroll, setAutoScroll] = useState(true);
const scrollRef = useRef<HTMLDivElement>(null);
const scrollToBottom = () => {
if (autoScroll && scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
};
const handleScroll = () => {
if (!scrollRef.current) return;
const { scrollTop, scrollHeight, clientHeight } = scrollRef.current;
const isAtBottom = Math.abs(scrollHeight - scrollTop - clientHeight) < 10;
setAutoScroll(isAtBottom);
};
useEffect(() => { useEffect(() => {
if (!open || !logPath) return; if (!open || !logPath) return;
@@ -59,20 +76,19 @@ export const ShowDeploymentCompose = ({
}; };
}, [logPath, open]); }, [logPath, open]);
const scrollToBottom = () => {
endOfLogsRef.current?.scrollIntoView({ behavior: "smooth" });
};
useEffect(() => { useEffect(() => {
const logs = parseLogs(data); const logs = parseLogs(data);
console.log(data);
console.log(logs);
setFilteredLogs(logs); setFilteredLogs(logs);
}, [data]); }, [data]);
useEffect(() => { useEffect(() => {
scrollToBottom(); scrollToBottom();
}, [data]);
if (autoScroll && scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
}, [filteredLogs, autoScroll]);
return ( return (
<Dialog <Dialog
@@ -90,7 +106,7 @@ export const ShowDeploymentCompose = ({
} }
}} }}
> >
<DialogContent className={"sm:max-w-5xl overflow-y-auto max-h-screen"}> <DialogContent className={"sm:max-w-5xl max-h-screen"}>
<DialogHeader> <DialogHeader>
<DialogTitle>Deployment</DialogTitle> <DialogTitle>Deployment</DialogTitle>
<DialogDescription> <DialogDescription>
@@ -98,8 +114,12 @@ export const ShowDeploymentCompose = ({
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
<div className="text-wrap rounded-lg border p-4 text-sm sm:max-w-[59rem]"> <div
<div> ref={scrollRef}
onScroll={handleScroll}
className="h-[720px] overflow-y-auto space-y-0 border p-4 bg-[#d4d4d4] dark:bg-[#050506] rounded custom-logs-scrollbar"
>
{ {
filteredLogs.length > 0 ? filteredLogs.map((log: LogLine, index: number) => ( filteredLogs.length > 0 ? filteredLogs.map((log: LogLine, index: number) => (
@@ -115,8 +135,6 @@ export const ShowDeploymentCompose = ({
</div> </div>
) )
} }
<div ref={endOfLogsRef} />
</div>
</div> </div>
</DialogContent> </DialogContent>
</Dialog> </Dialog>