import { useEffect } from "react";
import { useRoutineLogs } from "../../api/hooks";
import { RefreshFreshness, refreshMs, useRefreshToken } from "../../components/RefreshControl";
import { LogViewer } from "./LogViewer";
export interface RoutineLogsProps {
id: string;
title: string;
onBack: () => void;
}
/** Tail of the routine's most recent run's log, with optional auto-refresh (#357: the daemon
* periodically reaps finished workbenches, so a stale tail should be able to clear itself). */
export function RoutineLogs({ id, title, onBack }: RoutineLogsProps) {
const refreshToken = useRefreshToken();
const logs = useRoutineLogs(id);
useEffect(() => {
const ms = refreshMs(refreshToken);
if (ms === undefined) return;
const timer = window.setInterval(() => void logs.refetch(), ms);
return () => window.clearInterval(timer);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [refreshToken, id]);
return (
<main className="logs-page">
<div className="page-hd">
<button type="button" className="btn btn-ghost btn-sm" onClick={onBack}>
← BACK
</button>
<div className="page-title">LOGS / {title}</div>
<RefreshFreshness updatedAtMs={logs.dataUpdatedAt} />
<button
type="button"
className="btn-refresh"
title="Refresh"
aria-label="Refresh"
onClick={() => void logs.refetch()}
>
↻
</button>
</div>
<LogViewer
content={logs.data}
loading={logs.isLoading}
err={logs.isError ? logs.error.message : undefined}
/>
</main>
);
}