Skip to content

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

2.6 KB
Raw Blame
import './index.css'

import { useEffect, useRef, useState } from 'react'
import { Modal, ModalContent, ModalHeader } from 'frontend/components/UI/Modal'
import { LinearProgress } from '@mui/material'
import classNames from 'classnames'
import { useTranslation } from 'react-i18next'

export function ProgressDialog(props: {
  title: string
  progress: string[]
  showCloseButton: boolean
  onClose: () => void
  children?: JSX.Element
  className?: string
  hideProgress?: boolean
}) {
  const { t } = useTranslation()
  const winetricksOutputBottomRef = useRef<HTMLDivElement>(null)
  const logRef = useRef<HTMLDivElement>(null)
  const [autoScroll, setAutoScroll] = useState(true)

  const scrollToBottom = () => {
    winetricksOutputBottomRef.current?.scrollIntoView({ behavior: 'auto' })
  }

  useEffect(() => {
    if (autoScroll) {
      scrollToBottom()
    }
  }, [props.progress, autoScroll])

  const onLogScroll = (ev: Event) => {
    const target = ev.target as HTMLDivElement

    const atTheBottom =
      target.scrollTop + target.getBoundingClientRect().height >=
      target.scrollHeight

    setAutoScroll(atTheBottom)
  }

  useEffect(() => {
    if (logRef.current) {
      logRef.current.addEventListener('scroll', onLogScroll)
    }
  }, [logRef.current])

  return (
    <>
      <Modal
        showCloseButton
        onClose={props.onClose}
        className={classNames('progressDialog', props.className)}
      >
        <ModalHeader>{props.title}</ModalHeader>
        {props.children}
        <ModalContent>
          <div className="progressDialog header">
            {t('progress', 'Progress')}:
          </div>
          <div className="progressDialog log-box" ref={logRef}>
            {props.progress.map((line, key) => {
              if (line.toLowerCase().includes(' err')) {
                return (
                  <p key={key} className="progressDialog log-error">
                    {line}
                  </p>
                )
              } else if (line.toLowerCase().includes(' warn')) {
                return (
                  <p key={key} className="progressDialog log-warning">
                    {line}
                  </p>
                )
              } else {
                return (
                  <p key={key} className="progressDialog log-info">
                    {line}
                  </p>
                )
              }
            })}
            <div ref={winetricksOutputBottomRef} />
          </div>
          {!props.hideProgress && (
            <LinearProgress className="progressDialog linearProgress" />
          )}
        </ModalContent>
      </Modal>
    </>
  )
}