( // buildId is optional because sometimes the build is not loaded yet buildId: string | undefined, enabled = true, )
| 4 | import type { ProvisionerJobLog } from "#/api/typesGenerated"; |
| 5 | |
| 6 | export const useWorkspaceBuildLogs = ( |
| 7 | // buildId is optional because sometimes the build is not loaded yet |
| 8 | buildId: string | undefined, |
| 9 | enabled = true, |
| 10 | ) => { |
| 11 | const [logs, setLogs] = useState<ProvisionerJobLog[]>(); |
| 12 | const socket = useRef<WebSocket>(undefined); |
| 13 | |
| 14 | useEffect(() => { |
| 15 | if (!buildId || !enabled) { |
| 16 | socket.current?.close(); |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | // Every time this hook is called reset the values |
| 21 | setLogs(undefined); |
| 22 | |
| 23 | socket.current = watchBuildLogsByBuildId(buildId, { |
| 24 | // Retrieve all the logs |
| 25 | after: -1, |
| 26 | onMessage: (log) => { |
| 27 | setLogs((previousLogs) => { |
| 28 | if (!previousLogs) { |
| 29 | return [log]; |
| 30 | } |
| 31 | return [...previousLogs, log]; |
| 32 | }); |
| 33 | }, |
| 34 | onError: () => { |
| 35 | toast.error(`Error on getting "${buildId}" build logs.`); |
| 36 | }, |
| 37 | }); |
| 38 | |
| 39 | return () => { |
| 40 | socket.current?.close(); |
| 41 | }; |
| 42 | }, [buildId, enabled]); |
| 43 | |
| 44 | return logs; |
| 45 | }; |
no test coverage detected