Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

981 B
Raw Blame
import { useEffect, useRef, useState } from 'react'

/**
 * Polls the install log of a sideloaded app. The backend writes installer
 * output to the app's install log, so this works even before the app itself
 * has been added to the library.
 */
export default function useInstallerLog(appName: string, active: boolean) {
  const [logContent, setLogContent] = useState('')
  const fetching = useRef(false)

  useEffect(() => {
    if (!active) {
      return
    }

    const fetchLog = () => {
      if (fetching.current) {
        return
      }
      fetching.current = true
      window.api
        .getLogContent({ appName, runner: 'sideload', type: 'install' })
        .then((content) => setLogContent(content || ''))
        .catch(() => {})
        .finally(() => {
          fetching.current = false
        })
    }

    fetchLog()
    const interval = setInterval(fetchLog, 1000)
    return () => clearInterval(interval)
  }, [active, appName])

  return logContent
}