Files
coder/site/src/modules/templates/useWatchVersionLogs.ts
T
d70e26d2e3 chore: remove react-helmet (#19963)
Since React 19 supports head tags natively, we no longer need to use
`react-helmet`.

---------

Co-authored-by: ケイラ <mckayla@hey.com>
Co-authored-by: Andrew Aquino <dawneraq@gmail.com>
2025-09-26 10:52:53 -03:00

38 lines
1.2 KiB
TypeScript

import { watchBuildLogsByTemplateVersionId } from "api/api";
import type { ProvisionerJobLog, TemplateVersion } from "api/typesGenerated";
import { useEffectEvent } from "hooks/hookPolyfills";
import { useEffect, useState } from "react";
export const useWatchVersionLogs = (
templateVersion: TemplateVersion | undefined,
options?: { onDone: () => Promise<unknown> },
) => {
const [logs, setLogs] = useState<ProvisionerJobLog[]>();
const templateVersionId = templateVersion?.id;
const [cachedVersionId, setCachedVersionId] = useState(templateVersionId);
if (cachedVersionId !== templateVersionId) {
setCachedVersionId(templateVersionId);
setLogs([]);
}
const stableOnDone = useEffectEvent(() => options?.onDone());
const status = templateVersion?.job.status;
const canWatch = status === "running" || status === "pending";
useEffect(() => {
if (!templateVersionId || !canWatch) {
return;
}
const socket = watchBuildLogsByTemplateVersionId(templateVersionId, {
onError: (error) => console.error(error),
onDone: stableOnDone,
onMessage: (newLog) => {
setLogs((current) => [...(current ?? []), newLog]);
},
});
return () => socket.close();
}, [stableOnDone, canWatch, templateVersionId]);
return logs;
};