Skip to content

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

7.6 KB
Raw Blame
import './index.scss'
import React, { useContext, useEffect, useState } from 'react'
import {
  Modal,
  ModalContent,
  ModalFooter,
  ModalHeader
} from 'frontend/components/UI/Modal'
import Button from 'frontend/components/UI/Button'
import { useTranslation } from 'react-i18next'
import { Runner } from 'common/types'
import ToggleSwitch from '../ToggleSwitch'
import { useNavigate, useLocation } from 'react-router-dom'
import ContextProvider from 'frontend/state/ContextProvider'

interface UninstallModalProps {
  appName: string
  runner: Runner
  onClose: () => void
  isDlc: boolean
}

const UninstallModal: React.FC<UninstallModalProps> = function ({
  appName,
  runner,
  onClose,
  isDlc
}) {
  const [isNative, setIsNative] = useState(true)
  const [winePrefix, setWinePrefix] = useState('')
  const [deletePrefixChecked, setDeletePrefixChecked] = useState(false)
  const [deleteSettingsChecked, setDeleteSettingsChecked] = useState(false)
  const [disableDeleteWine, setDisableDeleteWine] = useState(false)
  const { t } = useTranslation('gamepage')
  const [showUninstallModal, setShowUninstallModal] = useState(false)
  const navigate = useNavigate()
  const location = useLocation()
  const { installingEpicGame, libraryStatus } = useContext(ContextProvider)
  const [gameTitle, setGameTitle] = useState('')
  const [isScannedGame, setIsScannedGame] = useState(false)

  const isGameRunning = libraryStatus.find(
    (st) =>
      st.appName === appName && st.runner === runner && st.status === 'playing'
  )

  const checkIfIsNative = async () => {
    // This assumes native games are installed should be changed in the future
    // if we add option to install windows games even if native is available

    setShowUninstallModal(true)

    const gameInfo = await window.api.getGameInfo(appName, runner)

    const isNative = await window.api.isNative({
      runner,
      appName
    })
    setIsNative(isNative)

    if (isDlc) {
      return
    }

    if (!gameInfo) {
      return
    }

    setGameTitle(gameInfo.overrides?.title || gameInfo.title)
    setIsScannedGame(Boolean(gameInfo.sourcePresetId))

    const { install } = gameInfo
    if (install.platform?.toLowerCase() !== 'windows') {
      return
    }

    const gameSettings = await window.api.getGameSettings(appName, runner)
    if (!gameSettings) {
      return
    }

    const defaultSettings = await window.api.requestGameSettings('default')

    setWinePrefix(gameSettings.winePrefix)
    setDisableDeleteWine(
      gameSettings.winePrefix === defaultSettings.winePrefix ||
        Boolean(gameInfo.sourcePresetId)
    )
  }

  useEffect(() => {
    checkIfIsNative()
  }, [])

  const storage: Storage = window.localStorage
  const uninstallGame = async () => {
    onClose()

    await window.api.uninstall(
      appName,
      runner,
      deletePrefixChecked,
      deleteSettingsChecked
    )
    if (runner === 'sideload' && location.pathname.match(/gamepage/)) {
      navigate('/#library')
    }
    storage.removeItem(appName)
  }

  const showWineCheckbox = !isNative && !isDlc

  // disallow uninstalling epic games if an epic game is being installed
  if (installingEpicGame && runner === 'legendary') {
    return (
      <>
        {showUninstallModal && (
          <Modal
            onClose={onClose}
            showCloseButton
            size="sm"
            className="uninstall-modal"
          >
            <ModalHeader>{t('gamepage:box.uninstall.title')}</ModalHeader>
            <ModalContent>
              {t(
                'gamepage:box.uninstall.cannotUninstallEpic',
                'Epic games cannot be uninstalled while another Epic game is being installed.'
              )}
            </ModalContent>
            <ModalFooter>
              <Button onClick={onClose}>{t('box.close', 'Close')}</Button>
            </ModalFooter>
          </Modal>
        )}
      </>
    )
  }

  if (isGameRunning) {
    return (
      <>
        {showUninstallModal && (
          <Modal
            onClose={onClose}
            showCloseButton
            size="sm"
            className="uninstall-modal"
          >
            <ModalHeader>{t('gamepage:box.uninstall.title')}</ModalHeader>
            <ModalContent>
              {t('gamepage:box.uninstall.gameIsRunning', {
                defaultValue:
                  '{{title}} is running. Close the game to uninstall it.',
                title: gameTitle
              })}
            </ModalContent>
            <ModalFooter>
              <Button onClick={onClose}>{t('box.close', 'Close')}</Button>
            </ModalFooter>
          </Modal>
        )}
      </>
    )
  }

  // normal dialog to uninstall a game
  return (
    <>
      {showUninstallModal && (
        <Modal
          onClose={onClose}
          showCloseButton
          size="sm"
          tone="destructive"
          className="uninstall-modal"
        >
          <ModalHeader>{t('gamepage:box.uninstall.title')}</ModalHeader>
          <ModalContent>
            <div className="uninstallModalMessage">
              {isDlc
                ? t('gamepage:box.uninstall.dlc', {
                    defaultValue: 'Do you want to uninstall "{{title}}" (DLC)?',
                    title: gameTitle
                  })
                : t('gamepage:box.uninstall.message', {
                    defaultValue: 'Do you want to uninstall "{{title}}"?',
                    title: gameTitle
                  })}
            </div>
            {showWineCheckbox && (
              <ToggleSwitch
                htmlId="uninstallCheckbox"
                value={deletePrefixChecked}
                title={t('gamepage:box.uninstall.checkbox', {
                  defaultValue:
                    "Remove prefix: {{prefix}}{{newLine}}Note: This can't be undone and will also remove not backed up save files.",
                  prefix: winePrefix,
                  newLine: '\n'
                })}
                disabled={disableDeleteWine}
                handleChange={() => {
                  setDeletePrefixChecked(!deletePrefixChecked)
                }}
              />
            )}
            {disableDeleteWine && (
              <p className="default-wine-warning">
                {isScannedGame
                  ? t(
                      'gamepage:box.uninstall.shared_prefix_warning',
                      'This game uses the launcher prefix, which may be shared by other scanned games. Remove the launcher itself to delete that prefix.'
                    )
                  : t(
                      'gamepage:box.uninstall.prefix_warning',
                      'The Wine prefix for this game is the default prefix. If you really want to delete it, you have to do it manually.'
                    )}
              </p>
            )}
            {!isDlc && (
              <ToggleSwitch
                htmlId="uninstallsettingCheckbox"
                value={deleteSettingsChecked}
                title={t('gamepage:box.uninstall.settingcheckbox', {
                  defaultValue:
                    "Erase settings and remove log{{newLine}}Note: This can't be undone. Any modified settings will be forgotten and log will be deleted.",
                  newLine: '\n'
                })}
                handleChange={() => {
                  setDeleteSettingsChecked(!deleteSettingsChecked)
                }}
              />
            )}
          </ModalContent>
          <ModalFooter>
            <Button variant="dangerSubtle" onClick={uninstallGame}>
              {t('box.yes')}
            </Button>
            <Button variant="secondary" onClick={onClose}>
              {t('box.no')}
            </Button>
          </ModalFooter>
        </Modal>
      )}
    </>
  )
}

export default UninstallModal