Skip to content

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

6.4 KB
Raw Blame
import { useTranslation } from 'react-i18next'
import { ChevronDown } from 'lucide-react'
import { ZoomDownloadFile } from 'common/types/zoom'
import {
  selectedZoomFileGroups,
  ZoomFileGroups,
  zoomFilesBytes
} from 'common/zoomFiles'
import { formatSize } from 'common/format'
import { useDisplayUnits } from 'frontend/state/DisplayUnits'
import Alert from 'frontend/components/UI/Alert'
import Button from 'frontend/components/UI/Button'
import Icon from 'frontend/components/UI/Icon'
import SettingsCard from 'frontend/components/UI/SettingsCard'

export type ZoomCollectionsStatus = 'loading' | 'error' | 'empty' | 'ready'

interface Props {
  files: ZoomFileGroups
  selectedIds: string[]
  status: ZoomCollectionsStatus
  error?: string | null
  onToggle: (fileId: string) => void
  onRetry: () => void
  canChangePlatform: boolean
  onChangePlatform: () => void
  onClose: () => void
}

function FileRow({
  file,
  checked,
  onToggle
}: {
  file: ZoomDownloadFile
  checked: boolean
  onToggle: (fileId: string) => void
}) {
  const units = useDisplayUnits((state) => state.units)
  const inputId = `zoom-file-${file.id}`

  return (
    <label
      className="ZoomCollections__row"
      htmlFor={inputId}
      title={file.filename}
    >
      <input
        checked={checked}
        className="ZoomCollections__checkbox"
        id={inputId}
        onChange={() => onToggle(file.id)}
        type="checkbox"
      />
      <span className="ZoomCollections__name">{file.name}</span>
      <span className="ZoomCollections__size">
        {formatSize(file.total_size, units)}
      </span>
    </label>
  )
}

export default function ZoomCollections({
  files,
  selectedIds,
  status,
  error,
  onToggle,
  onRetry,
  canChangePlatform,
  onChangePlatform,
  onClose
}: Props) {
  const { t } = useTranslation()
  const units = useDisplayUnits((state) => state.units)
  const title = t('zoom.collections.title', 'Select files to install')
  const description = t(
    'zoom.collections.description',
    'This product includes more than one file. Pick what you want to install.'
  )

  if (status === 'loading') {
    return (
      <SettingsCard className="ZoomCollections" title={title}>
        <p className="ZoomCollections__loading" role="status">
          {t('zoom.collections.loading', 'Loading files…')}
        </p>
      </SettingsCard>
    )
  }

  if (status === 'error') {
    return (
      <SettingsCard className="ZoomCollections" title={title}>
        <Alert variant="error">
          {error ||
            t('zoom.collections.loadError', 'Could not load the files.')}
        </Alert>
        <div className="ZoomCollections__stateActions">
          <Button onClick={onRetry}>
            {t('zoom.collections.retry', 'Try again')}
          </Button>
          <Button variant="secondary" onClick={onClose}>
            {t('zoom.collections.close', 'Close')}
          </Button>
        </div>
      </SettingsCard>
    )
  }

  if (status === 'empty' || (!files.game.length && !files.extras.length)) {
    return (
      <SettingsCard className="ZoomCollections" title={title}>
        <p className="ZoomCollections__empty">
          {t(
            'zoom.collections.noFiles',
            'This product has no files to install.'
          )}
        </p>
        <Button variant="secondary" onClick={onClose}>
          {t('zoom.collections.close', 'Close')}
        </Button>
      </SettingsCard>
    )
  }

  const selected = selectedZoomFileGroups(files, selectedIds)
  const selectedBytes = zoomFilesBytes([...selected.game, ...selected.extras])
  const extrasBytes = zoomFilesBytes(files.extras)

  return (
    <SettingsCard
      className="ZoomCollections"
      title={title}
      description={description}
    >
      <fieldset className="ZoomCollections__group">
        <legend>{t('zoom.collections.gameGroup', 'Game files')}</legend>

        {files.game.length === 0 ? (
          <div className="ZoomCollections__noBuild">
            <p className="ZoomCollections__empty">
              {t(
                'zoom.collections.noBuild',
                'No game files are available for this platform.'
              )}
            </p>
            {canChangePlatform ? (
              <>
                <p className="ZoomCollections__hint">
                  {t(
                    'zoom.collections.noBuildHint',
                    'Choose another platform to see its files.'
                  )}
                </p>
                <Button variant="secondary" onClick={onChangePlatform}>
                  {t('zoom.collections.changePlatform', 'Change platform')}
                </Button>
              </>
            ) : (
              <Button variant="secondary" onClick={onClose}>
                {t('zoom.collections.close', 'Close')}
              </Button>
            )}
          </div>
        ) : (
          files.game.map((file) => (
            <FileRow
              key={file.id}
              checked={selectedIds.includes(file.id)}
              file={file}
              onToggle={onToggle}
            />
          ))
        )}
      </fieldset>

      {files.extras.length > 0 && (
        <details className="ZoomCollections__extras">
          <summary className="ZoomCollections__summaryRow">
            <Icon
              glyph={ChevronDown}
              size="sm"
              className="ZoomCollections__chevron"
            />
            <span className="ZoomCollections__summaryLabel">
              {t('zoom.collections.extrasGroup', 'Extras')}
            </span>
            <span className="ZoomCollections__summary">
              {t(
                'zoom.collections.extrasSummary',
                '{{count}} files · {{size}}',
                {
                  count: files.extras.length,
                  size: formatSize(extrasBytes, units)
                }
              )}
            </span>
          </summary>

          <div className="ZoomCollections__extrasList">
            {files.extras.map((file) => (
              <FileRow
                key={file.id}
                checked={selectedIds.includes(file.id)}
                file={file}
                onToggle={onToggle}
              />
            ))}
          </div>
        </details>
      )}

      <p className="ZoomCollections__selectedTotal">
        {t('zoom.collections.selectedTotal', 'Selected: {{size}}', {
          size: formatSize(selectedBytes, units)
        })}
      </p>
    </SettingsCard>
  )
}