Skip to content

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

4.9 KB
Raw Blame
import './SteamMoveModal.css'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import {
  Modal,
  ModalContent,
  ModalFooter,
  ModalHeader
} from 'frontend/components/UI/Modal'
import Button from 'frontend/components/UI/Button'
import Checkbox from 'frontend/components/UI/Checkbox'
import { Alert, UpdateComponent } from 'frontend/components/UI'
import SteamLibrarySelector from 'frontend/screens/Library/components/InstallModal/DownloadDialog/SteamLibrarySelector'
import { SteamInstallLibrary } from 'common/types/steam'

interface SteamMoveModalProps {
  /** `<library>/steamapps/common/<dir>` of the installed game. */
  installPath?: string
  /** Starts the move; the modal closes itself. */
  onMove: (destination: string) => void
  onClose: () => void
}

const unifySeparators = (path: string) =>
  path.replaceAll('\\', '/').replace(/\/+$/, '')

/** Whether the game at `installPath` lives in `library`. */
function isInLibrary(installPath: string, library: string): boolean {
  return unifySeparators(installPath)
    .toLowerCase()
    .startsWith(`${unifySeparators(library).toLowerCase()}/steamapps/`)
}

/**
 * Moves a Steam game to another Steam library folder. Steam only knows games
 * inside its libraries, so the destination is picked from those instead of a
 * free-form folder browser.
 */
export default function SteamMoveModal({
  installPath,
  onMove,
  onClose
}: SteamMoveModalProps) {
  const { t } = useTranslation('gamepage')
  const [libraries, setLibraries] = useState<SteamInstallLibrary[] | null>(null)
  const [destination, setDestination] = useState('')
  // Steam rewrites its library files on exit, so the user must agree to close it.
  const [closeSteam, setCloseSteam] = useState(false)
  // The library the game is in now; the install folder until that is known.
  const [source, setSource] = useState(installPath ?? '')

  useEffect(() => {
    let cancelled = false
    void window.api.getSteamInstallLibraries().then((all) => {
      if (cancelled) return
      const others = installPath
        ? all.filter((lib) => !isInLibrary(installPath, lib.path))
        : all
      setLibraries(others)
      const current = all.find(
        (lib) => installPath && isInLibrary(installPath, lib.path)
      )
      if (current) setSource(current.path)
      setDestination(others[0]?.path ?? '')
    })
    return () => {
      cancelled = true
    }
  }, [installPath])

  const confirm = () => {
    if (!destination || !closeSteam) return
    onMove(destination)
    onClose()
  }

  return (
    <Modal
      onClose={onClose}
      showCloseButton
      size="md"
      className="SteamMoveModal"
    >
      <ModalHeader>{t('box.move.title', 'Move Game Installation')}</ModalHeader>
      <ModalContent>
        {libraries === null ? (
          <UpdateComponent inline />
        ) : libraries.length === 0 ? (
          <p className="SteamMoveModal__text">
            {t(
              'box.move.steam-no-destination',
              'There is no other Steam library folder to move this game to. Add a drive in Steam first.'
            )}
          </p>
        ) : (
          <>
            <Alert variant="warning" className="SteamMoveModal__warning">
              {t(
                'box.move.steam-warning',
                'Be aware that this can take a long time. Steam must be closed during the move.'
              )}
            </Alert>
            {source && (
              <div className="SteamMoveModal__source">
                <span className="SteamMoveModal__sourceLabel">
                  {t('box.move.steam-source', 'Current location')}
                </span>
                <span className="SteamMoveModal__sourcePath" title={source}>
                  {source}
                </span>
              </div>
            )}
            <SteamLibrarySelector
              libraries={libraries}
              selectedPath={destination}
              onChange={setDestination}
              label={t(
                'box.move.steam-destination',
                'Destination (Steam library)'
              )}
            />
            <label className="SteamMoveModal__closeSteam">
              <Checkbox
                size="small"
                checked={closeSteam}
                onChange={(event) => setCloseSteam(event.target.checked)}
              />
              <span>
                {t(
                  'box.move.steam-close',
                  'Close Steam if it is running. It only starts again if it was running.'
                )}
              </span>
            </label>
          </>
        )}
      </ModalContent>
      <ModalFooter>
        <Button variant="secondary" onClick={onClose}>
          {t('box.cancel', { ns: 'translation', defaultValue: 'Cancel' })}
        </Button>
        <Button onClick={confirm} disabled={!destination || !closeSteam}>
          {t('box.move.steam-confirm', 'Move')}
        </Button>
      </ModalFooter>
    </Modal>
  )
}