2023-12-31 14:31:33 +01:00
|
|
|
import { Button } from "@/components/ui/button";
|
2024-03-24 18:23:39 +01:00
|
|
|
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
|
2023-12-31 14:31:33 +01:00
|
|
|
import copy from "copy-to-clipboard";
|
2024-02-24 01:25:00 +01:00
|
|
|
import { useCallback, useMemo, useRef, useState } from "react";
|
2024-03-24 18:23:39 +01:00
|
|
|
import { LuCopy } from "react-icons/lu";
|
2023-12-31 14:31:33 +01:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
const logTypes = ["frigate", "go2rtc", "nginx"] as const;
|
|
|
|
type LogType = (typeof logTypes)[number];
|
2023-12-08 14:33:22 +01:00
|
|
|
|
|
|
|
function Logs() {
|
2023-12-31 14:31:33 +01:00
|
|
|
const [logService, setLogService] = useState<LogType>("frigate");
|
|
|
|
|
|
|
|
const { data: frigateLogs } = useSWR("logs/frigate", {
|
|
|
|
refreshInterval: 1000,
|
|
|
|
});
|
|
|
|
const { data: go2rtcLogs } = useSWR("logs/go2rtc", { refreshInterval: 1000 });
|
|
|
|
const { data: nginxLogs } = useSWR("logs/nginx", { refreshInterval: 1000 });
|
|
|
|
const logs = useMemo(() => {
|
|
|
|
if (logService == "frigate") {
|
|
|
|
return frigateLogs;
|
|
|
|
} else if (logService == "go2rtc") {
|
|
|
|
return go2rtcLogs;
|
|
|
|
} else if (logService == "nginx") {
|
|
|
|
return nginxLogs;
|
|
|
|
} else {
|
|
|
|
return "unknown logs";
|
|
|
|
}
|
|
|
|
}, [logService, frigateLogs, go2rtcLogs, nginxLogs]);
|
|
|
|
|
|
|
|
const handleCopyLogs = useCallback(() => {
|
|
|
|
copy(logs);
|
|
|
|
}, [logs]);
|
|
|
|
|
2024-02-24 01:25:00 +01:00
|
|
|
// scroll to bottom button
|
|
|
|
|
|
|
|
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
const [endVisible, setEndVisible] = useState(true);
|
|
|
|
const observer = useRef<IntersectionObserver | null>(null);
|
|
|
|
const endLogRef = useCallback(
|
|
|
|
(node: HTMLElement | null) => {
|
|
|
|
if (observer.current) observer.current.disconnect();
|
|
|
|
try {
|
|
|
|
observer.current = new IntersectionObserver((entries) => {
|
|
|
|
setEndVisible(entries[0].isIntersecting);
|
|
|
|
});
|
|
|
|
if (node) observer.current.observe(node);
|
|
|
|
} catch (e) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
},
|
2024-02-28 23:23:56 +01:00
|
|
|
[setEndVisible],
|
2024-02-24 01:25:00 +01:00
|
|
|
);
|
|
|
|
|
2023-12-08 14:33:22 +01:00
|
|
|
return (
|
2024-03-30 19:44:12 +01:00
|
|
|
<div className="size-full p-2 flex flex-col">
|
2023-12-31 14:31:33 +01:00
|
|
|
<div className="flex justify-between items-center">
|
2024-03-24 18:23:39 +01:00
|
|
|
<ToggleGroup
|
|
|
|
className="*:px-3 *:py-4 *:rounded-2xl"
|
|
|
|
type="single"
|
|
|
|
size="sm"
|
|
|
|
value={logService}
|
|
|
|
onValueChange={(value: LogType) =>
|
|
|
|
value ? setLogService(value) : null
|
|
|
|
} // don't allow the severity to be unselected
|
|
|
|
>
|
|
|
|
{Object.values(logTypes).map((item) => (
|
|
|
|
<ToggleGroupItem
|
|
|
|
key={item}
|
|
|
|
className={`flex items-center justify-between gap-2 ${logService == item ? "" : "text-gray-500"}`}
|
|
|
|
value={item}
|
|
|
|
aria-label={`Select ${item}`}
|
|
|
|
>
|
|
|
|
<div className="capitalize">{`${item} Logs`}</div>
|
|
|
|
</ToggleGroupItem>
|
|
|
|
))}
|
|
|
|
</ToggleGroup>
|
2023-12-31 14:31:33 +01:00
|
|
|
<div>
|
2024-03-24 18:23:39 +01:00
|
|
|
<Button
|
|
|
|
className="flex justify-between items-center gap-2"
|
|
|
|
size="sm"
|
|
|
|
onClick={handleCopyLogs}
|
|
|
|
>
|
|
|
|
<LuCopy />
|
|
|
|
<div className="hidden md:block">Copy to Clipboard</div>
|
|
|
|
</Button>
|
2023-12-31 14:31:33 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2024-02-24 01:25:00 +01:00
|
|
|
{!endVisible && (
|
2024-02-28 15:16:32 +01:00
|
|
|
<Button
|
|
|
|
className="absolute bottom-8 left-[50%] -translate-x-[50%] rounded-xl bg-accent-foreground text-white bg-gray-400 z-20 p-2"
|
|
|
|
variant="secondary"
|
2024-02-24 01:25:00 +01:00
|
|
|
onClick={() =>
|
|
|
|
contentRef.current?.scrollTo({
|
|
|
|
top: contentRef.current?.scrollHeight,
|
|
|
|
behavior: "smooth",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Jump to Bottom
|
2024-02-28 15:16:32 +01:00
|
|
|
</Button>
|
2024-02-24 01:25:00 +01:00
|
|
|
)}
|
|
|
|
|
|
|
|
<div
|
|
|
|
ref={contentRef}
|
2024-03-24 18:23:39 +01:00
|
|
|
className="w-full h-min my-2 font-mono text-sm bg-secondary rounded p-2 whitespace-pre-wrap overflow-auto"
|
2024-02-24 01:25:00 +01:00
|
|
|
>
|
2023-12-31 14:31:33 +01:00
|
|
|
{logs}
|
2024-02-24 01:25:00 +01:00
|
|
|
<div ref={endLogRef} />
|
2023-12-31 14:31:33 +01:00
|
|
|
</div>
|
2024-02-21 21:07:32 +01:00
|
|
|
</div>
|
2023-12-08 14:33:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Logs;
|