Skip to content

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

8.5 KB
Raw Blame
import { CircleCheck, MonitorX } from 'lucide-react'
import { InstallPlatform, WineInstallation } from 'common/types'
import { ParsedProtonShorctut } from 'common/types/proton_shorctuts'
import {
  Alert,
  CachedImage,
  EmptyState,
  Icon,
  PathSelectionBox,
  TextInputField,
  ToggleSwitch
} from 'frontend/components/UI'
import Button from 'frontend/components/UI/Button'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'

type Props = {
  setGameUrl: (val: string) => void
  gameUrl: string
  appPlatform: InstallPlatform
  setCustomUserAgent: (val: string) => void
  customUserAgent: string
  setLaunchFullScreen: (val: boolean) => void
  launchFullScreen: boolean
  winePrefix: string
  wineVersion?: WineInstallation
  setSelectedExe: (val: string) => void
  selectedExe: string
  fileFilters: (installPlatform: InstallPlatform) => Electron.FileFilter[]
  launcherMode?: boolean
  presetTitle?: string
}

export default function FinishStep({
  winePrefix,
  wineVersion,
  appPlatform,
  gameUrl,
  customUserAgent,
  launchFullScreen,
  selectedExe,
  setSelectedExe,
  setGameUrl,
  setCustomUserAgent,
  setLaunchFullScreen,
  fileFilters,
  launcherMode = false,
  presetTitle
}: Props) {
  const { t } = useTranslation('gamepage')

  const [protonShortcuts, setProtonShortcuts] = useState<
    ParsedProtonShorctut[]
  >([])

  const showSideloadExe = appPlatform !== 'Browser'
  function handleGameUrl(url: string) {
    if (!url.startsWith('https://')) {
      return setGameUrl(`https://${url}`)
    }

    setGameUrl(url)
  }

  async function loadProtonShortcuts() {
    if (window.platform !== 'linux' || wineVersion?.type !== 'proton') return
    const shortcuts = await window.api.getProtonShortcuts(winePrefix)
    setProtonShortcuts(shortcuts)
  }

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

  return (
    <div className="sideloadFinish">
      {showSideloadExe && (
        <>
          {launcherMode && (
            <Alert variant="warning" className="sideloadLauncherExeNotice">
              {t(
                'sideload.info.launcher-exe',
                'Heroic identified the executables in this prefix, but this entry must launch the launcher itself ({{title}}). Do not select a game executable here; installed games can be added later with Scan games on the launcher page.',
                { title: presetTitle ?? '' }
              )}
            </Alert>
          )}

          {window.platform === 'linux' && appPlatform === 'Windows' && (
            <ProtonShorcutsSection
              protonShortcuts={protonShortcuts}
              selectedExe={selectedExe}
              setSelectedExe={setSelectedExe}
              launcherMode={launcherMode}
              launcherTitle={presetTitle}
            />
          )}

          <PathSelectionBox
            type="file"
            onPathChange={setSelectedExe}
            path={selectedExe}
            placeholder={
              launcherMode
                ? t(
                    'sideload.info.launcher-exe-placeholder',
                    'Launcher executable'
                  )
                : t('sideload.info.exe', 'Select Executable')
            }
            pathDialogTitle={
              launcherMode
                ? t(
                    'box.sideload.launcher-exe',
                    'Select the launcher executable'
                  )
                : t('box.sideload.exe', 'Select Executable')
            }
            pathDialogDefaultPath={winePrefix}
            pathDialogFilters={fileFilters(appPlatform)}
            htmlId="sideload-exe"
            label={
              launcherMode
                ? t('sideload.info.launcher-exe-label', 'Launcher executable')
                : t('sideload.info.exe', 'Select Executable')
            }
            noDeleteButton
          />
        </>
      )}
      {!showSideloadExe && (
        <>
          <TextInputField
            label={t('sideload.info.broser', 'BrowserURL')}
            placeholder={t(
              'sideload.placeholder.url',
              'Paste the Game URL here'
            )}
            onChange={(newValue: string) => handleGameUrl(newValue)}
            htmlId="sideload-game-url"
            value={gameUrl}
          />
          <TextInputField
            label={t('sideload.info.useragent', 'Custom User Agent')}
            placeholder={t(
              'sideload.placeholder.useragent',
              'Write a custom user agent here to be used on this browser app/game'
            )}
            onChange={(newValue: string) => setCustomUserAgent(newValue)}
            htmlId="sideload-user-agent"
            value={customUserAgent}
          />
          <ToggleSwitch
            htmlId="launch-fullscreen"
            value={launchFullScreen}
            handleChange={() => setLaunchFullScreen(!launchFullScreen)}
            title={t(
              'sideload.info.fullscreen',
              'Launch Fullscreen (F11 to exit)'
            )}
          />
        </>
      )}
    </div>
  )
}

interface ProtonShorcutsSectionProps {
  protonShortcuts: ParsedProtonShorctut[]
  selectedExe: string
  setSelectedExe: (val: string) => void
  launcherMode?: boolean
  launcherTitle?: string
}

function ProtonShorcutsSection({
  protonShortcuts,
  selectedExe,
  setSelectedExe,
  launcherMode = false,
  launcherTitle
}: ProtonShorcutsSectionProps) {
  const { t } = useTranslation('gamepage')

  return (
    <>
      {!!protonShortcuts.length && (
        <h5 className="protonShortcutsTitle">
          {launcherMode
            ? t(
                'sideload.info.launcher-shortcuts-title',
                'Detected software — choose the launcher'
              )
            : t('sideload.info.proton-shortcuts', 'Detected software')}
        </h5>
      )}
      {launcherMode && (
        <p className="protonShortcutsHint">
          {t(
            'sideload.info.launcher-shortcuts-hint',
            'Heroic found the executables below. Keep the launcher ({{title}}) selected; game executables are added later with Scan games.',
            { title: launcherTitle ?? '' }
          )}
        </p>
      )}
      <div className="proton-shortcuts">
        {!protonShortcuts.length && !selectedExe && (
          <EmptyState
            glyph={MonitorX}
            title={t(
              'sideload.info.no-proton-shortcuts.title',
              'No shortcuts found'
            )}
            description={t(
              'sideload.info.no-proton-shortcuts.subtitle',
              "Heroic didn't detect any installed software. Please select the executable manually instead."
            )}
          />
        )}
        {protonShortcuts.map((shortcut) => {
          const isSelected = selectedExe === shortcut.executable
          const isLauncherShortcut =
            !launcherMode ||
            shortcut.name.localeCompare(launcherTitle ?? '', undefined, {
              sensitivity: 'base'
            }) === 0
          const hasLauncherTile =
            !launcherMode ||
            protonShortcuts.some(
              (candidate) =>
                candidate.name.localeCompare(launcherTitle ?? '', undefined, {
                  sensitivity: 'base'
                }) === 0
            )
          const isGameExecutable =
            launcherMode && hasLauncherTile && !isLauncherShortcut

          return (
            <Button
              key={shortcut.name}
              variant="ghost"
              className={[
                'shortcutTile',
                isSelected ? 'shortcutTile--selected' : '',
                isGameExecutable ? 'shortcutTile--disabled' : ''
              ]
                .filter(Boolean)
                .join(' ')}
              disabled={isGameExecutable}
              onClick={() => {
                if (!isGameExecutable) {
                  setSelectedExe(shortcut.executable)
                }
              }}
            >
              {shortcut.icon && (
                <CachedImage
                  className="shortcutImage"
                  src={`file://${shortcut.icon}`}
                />
              )}
              <span className="shortcutName">{shortcut.name}</span>
              {isGameExecutable && (
                <span className="shortcutTag">
                  {t('sideload.info.game-executable', 'Game executable')}
                </span>
              )}
              {isSelected && (
                <Icon
                  glyph={CircleCheck}
                  size="md"
                  className="shortcutSelectedIcon"
                />
              )}
            </Button>
          )
        })}
      </div>
    </>
  )
}