Skip to content

Commit

Permalink
fix: add back logs component
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Guidée <[email protected]>
  • Loading branch information
quentinguidee committed Mar 3, 2024
1 parent 6c64c35 commit d8dd698
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
1 change: 0 additions & 1 deletion client/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
Expand Down
27 changes: 27 additions & 0 deletions client/src/components/Logs/Logs.module.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@use "../../styles/colors"
@use "../../styles/dimensions"
@use "../../styles/fonts"

.logs
line-height: 2
font-size: 13px
font-family: fonts.$font-family-code
border: 1px solid colors.$bg-tertiary
padding: 10px
border-radius: dimensions.$border-radius-lg
overflow: scroll
white-space: break-spaces

.line
display: flex
flex-direction: row
gap: 10px
padding: 0 10px
border-radius: 4px

&Error
color: colors.$red

.number
color: colors.$fg-primary
opacity: 0.4
66 changes: 66 additions & 0 deletions client/src/components/Logs/Logs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { HTMLProps, useEffect, useRef, useState } from "react";

import styles from "./Logs.module.sass";
import classNames from "classnames";
import useScrollPercentage from "react-scroll-percentage-hook";
import Downloads from "../Downloads/Downloads";

export type LogLine = {
kind: "out" | "err" | "downloads";
message: any;
};

function Line(props: LogLine) {
const { kind, message } = props;

if (kind === "downloads") {
return <Downloads downloads={message} />;
}

if (message.value === undefined) return null;
if (message.value === "") return null;

return (
<div
className={classNames({
[styles.line]: true,
[styles.lineError]: kind === "err",
})}
>
<div>{message.value}</div>
</div>
);
}

type Props = HTMLProps<HTMLDivElement> & {
lines: LogLine[];
};

export default function Logs(props: Readonly<Props>) {
const { lines } = props;

const { ref } = useScrollPercentage<HTMLDivElement>({
onProgress: (percentage) => {
setAutoScroll(percentage.vertical === 100);
},
});

const [autoScroll, setAutoScroll] = useState<boolean>(true);

const scroll = useRef();

useEffect(() => {
if (!autoScroll) return;
let s: any = scroll;
s.current.scrollIntoView({ behavior: "smooth" });
}, [autoScroll, lines]);

return (
<div className={styles.logs} ref={ref}>
{lines.map((line, i) => {
return <Line key={i} kind={line.kind} message={line.message} />;
})}
<div ref={scroll} />
</div>
);
}

0 comments on commit d8dd698

Please sign in to comment.