Files
startup/web/src/hooks/useLogs.js

28 lines
627 B
JavaScript

import { useEffect, useState } from "react";
export function useLogs(socket) {
const [logs, setLogs] = useState([]);
useEffect(() => {
const handleEntry = (entry) => setLogs((current) => [...current, entry]);
const handleSnapshot = (entries) => setLogs(entries ?? []);
socket.on("log:entry", handleEntry);
socket.on("log:snapshot", handleSnapshot);
return () => {
socket.off("log:entry", handleEntry);
socket.off("log:snapshot", handleSnapshot);
};
}, [socket]);
function clearLogs() {
socket.emit("logs:clear", {}, () => {});
}
return {
logs,
clearLogs
};
}