A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.
TypeScript amazon-primeelectronepic-games-launcherepic-games-storeepic-storegaminggoggog-galaxyheroic-games-launcherlinuxplay-gamesprotonsteamwine
import React, { PureComponent } from 'react'
import {
ConnectivityStatus,
FavouriteGame,
GameInfo,
GameStatus,
HiddenGame,
RefreshOptions,
Runner,
WineVersionInfo,
LibraryTopSectionOptions,
ExperimentalFeatures,
Status
} from 'common/types'
import {
DialogModalOptions,
ExternalLinkDialogOptions,
HelpItem
} from 'frontend/types'
import { withTranslation } from 'react-i18next'
import { getGameInfo, getLegendaryConfig } from '../helpers'
import { i18n, t, TFunction } from 'i18next'
import ContextProvider from './ContextProvider'
import {
configStore,
gogConfigStore,
gogInstalledGamesStore,
gogLibraryStore,
libraryStore,
nileConfigStore,
nileLibraryStore,
itchioConfigStore,
itchioLibraryStore,
wineDownloaderInfoStore,
sideloadLibrary,
zoomConfigStore,
zoomInstalledGamesStore,
zoomLibraryStore,
steamConfigStore,
steamInstalledGamesStore,
steamLibraryStore
} from '../helpers/electronStores'
import { DEFAULT_THEME, isBuiltinTheme, migrateTheme } from '../helpers/themes'
import { NileRegisterData } from 'common/types/nile'
import { ItchioOAuthCodeData, ItchioRegisterData } from 'common/types/itchio'
import { SteamAccount, SteamLoginData } from 'common/types/steam'
import useGlobalState from './GlobalStateV2'
const storage: Storage = window.localStorage
const globalSettings = configStore.get_nodefault('settings')
const RTL_LANGUAGES = ['fa', 'ar']
type T = TFunction<'gamepage'> & TFunction<'translations'>
interface Props {
children: React.ReactNode
i18n: i18n
t: T
}
interface StateProps {
epic: {
library: GameInfo[]
username?: string
}
gog: {
library: GameInfo[]
username?: string
}
amazon: {
library: GameInfo[]
user_id?: string
username?: string
}
zoom: {
library: GameInfo[]
username?: string
enabled: boolean
}
itchio: {
library: GameInfo[]
username?: string
}
steam: {
library: GameInfo[]
username?: string
enabled: boolean
users: SteamAccount[]
}
wineVersions: WineVersionInfo[]
error: boolean
gameUpdates: string[]
language: string
libraryStatus: GameStatus[]
libraryTopSection: string
platform: NodeJS.Platform
isIntelMac: boolean
isLinuxArm: boolean
refreshing: boolean
refreshingInTheBackground: boolean
hiddenGames: HiddenGame[]
favouriteGames: FavouriteGame[]
customCategories: Record<string, string[]>
currentCustomCategories: string[]
theme: string
isFullscreen: boolean
isFrameless: boolean
zoomPercent: number
primaryFontFamily: string
secondaryFontFamily: string
allTilesInColor: boolean
titlesAlwaysVisible: boolean
sidebarCollapsed: boolean
activeController: string
connectivity: { status: ConnectivityStatus; retryIn: number }
dialogModalOptions: DialogModalOptions
externalLinkDialogOptions: ExternalLinkDialogOptions
sideloadedLibrary: GameInfo[]
hideChangelogsOnStartup: boolean
disableDiscounts: boolean
disableGOGAffiliate: boolean
lastChangelogShown: string | null
showInstallModal: {
show: boolean
gameInfo: GameInfo | null
appName: string
runner: Runner
}
helpItems: { [key: string]: HelpItem }
experimentalFeatures: ExperimentalFeatures
disableDialogBackdropClose: boolean
disableAnimations: boolean
}
// function to load the new key or fallback to the old one
const loadCurrentCategories = () => {
const currentCategories = storage.getItem('current_custom_categories') || null
if (!currentCategories) {
const currentCategory = storage.getItem('current_custom_category') || null
if (!currentCategory) {
return []
} else {
return [currentCategory]
}
} else {
return JSON.parse(currentCategories) as string[]
}
}
import { attachGameOverrides } from '../helpers/gameOverrides'
type GameOverride = NonNullable<GameInfo['overrides']>
const currentOverrides = () => useGlobalState.getState().gameOverrides
const applyGameOverrides = (
lib: GameInfo[],
overrides: Record<string, GameOverride> = currentOverrides()
) => attachGameOverrides(lib, overrides)
class GlobalState extends PureComponent<Props> {
loadLegendaryLibrary = (
overrides: Record<string, GameOverride> = currentOverrides()
): Array<GameInfo> => {
const games = libraryStore.get('library', [])
return applyGameOverrides(games, overrides)
}
loadGOGLibrary = (
overrides: Record<string, GameOverride> = currentOverrides()
): Array<GameInfo> => {
const games = gogLibraryStore.get('games', [])
const installedGames = gogInstalledGamesStore.get('installed', [])
for (const game of games) {
for (const installedGame of installedGames) {
if (installedGame.appName === game.app_name) {
game.install = installedGame
game.is_installed = true
}
}
}
return applyGameOverrides(games, overrides)
}
loadAmazonLibrary = (
overrides: Record<string, GameOverride> = currentOverrides()
): Array<GameInfo> => {
const games = nileLibraryStore.get('library', [])
return applyGameOverrides(games, overrides)
}
loadItchioLibrary = (
overrides: Record<string, GameOverride> = currentOverrides()
): Array<GameInfo> => {
const games = itchioLibraryStore.get('library', [])
return applyGameOverrides(games, overrides)
}
loadZoomLibrary = (
overrides: Record<string, GameOverride> = currentOverrides()
): Array<GameInfo> => {
const games = zoomLibraryStore.get('games', [])
const installedGames = zoomInstalledGamesStore.get('installed', [])
for (const game of games) {
const igame = installedGames.find(
(igame) => igame.appName === game.app_name
)
if (igame) {
game.install = igame
game.is_installed = true
}
}
return applyGameOverrides(games, overrides)
}
loadSteamLibrary = (
overrides: Record<string, GameOverride> = currentOverrides()
): Array<GameInfo> => {
const games = steamLibraryStore.get('games', [])
const installedGames = steamInstalledGamesStore.get('installed', [])
for (const game of games) {
const igame = installedGames.find(
(igame) => igame.appName === game.app_name
)
if (igame) {
game.install = igame
game.is_installed = true
}
}
return applyGameOverrides(games, overrides)
}
state: StateProps = {
epic: {
library: this.loadLegendaryLibrary(),
username: configStore.get_nodefault('userInfo.displayName')
},
gog: {
library: this.loadGOGLibrary(),
username: gogConfigStore.get_nodefault('userData.username')
},
amazon: {
library: this.loadAmazonLibrary(),
user_id: nileConfigStore.get_nodefault('userData.user_id'),
username: nileConfigStore.get_nodefault('userData.name')
},
zoom: {
library: this.loadZoomLibrary(),
username: zoomConfigStore.get_nodefault('username'),
// Zoom is a regular store now; the login card and filters key on username
enabled: true
},
itchio: {
library: this.loadItchioLibrary(),
username: itchioConfigStore.get_nodefault('userData.username')
},
steam: {
// Logged out: no list or search
library: steamConfigStore.get_nodefault('username')
? this.loadSteamLibrary()
: [],
username: steamConfigStore.get_nodefault('username'),
// On macOS the backend confirms that the Aurelia helper exists.
enabled: !window.isFlatpak && window.platform !== 'darwin',
users: []
},
wineVersions: wineDownloaderInfoStore.get('wine-releases', []),
error: false,
gameUpdates: [],
language: this.props.i18n.language,
libraryStatus: [],
libraryTopSection: globalSettings?.libraryTopSection || 'disabled',
platform: window.platform,
isIntelMac: false,
isLinuxArm: false,
refreshing: false,
refreshingInTheBackground: true,
hiddenGames: configStore.get('games.hidden', []),
currentCustomCategories: loadCurrentCategories(),
sidebarCollapsed: JSON.parse(
storage.getItem('sidebar_collapsed') || 'false'
),
favouriteGames: configStore.get('games.favourites', []),
customCategories: configStore.get('games.customCategories', {}),
theme: migrateTheme(configStore.get('theme', DEFAULT_THEME)),
isFullscreen: false,
isFrameless: false,
zoomPercent: configStore.get('zoomPercent', 100),
secondaryFontFamily:
configStore.get_nodefault('contentFontFamily') ||
getComputedStyle(document.documentElement).getPropertyValue(
'--default-secondary-font-family'
),
primaryFontFamily:
configStore.get_nodefault('actionsFontFamily') ||
getComputedStyle(document.documentElement).getPropertyValue(
'--default-primary-font-family'
),
allTilesInColor: configStore.get('allTilesInColor', false),
titlesAlwaysVisible: configStore.get('titlesAlwaysVisible', false),
activeController: '',
connectivity: { status: 'offline', retryIn: 0 },
showInstallModal: {
show: false,
appName: '',
runner: 'legendary',
gameInfo: null
},
sideloadedLibrary: applyGameOverrides(sideloadLibrary.get('games', [])),
dialogModalOptions: { showDialog: false },
externalLinkDialogOptions: { showDialog: false },
hideChangelogsOnStartup: globalSettings?.hideChangelogsOnStartup || false,
disableDiscounts: globalSettings?.disableDiscounts || false,
disableGOGAffiliate: globalSettings?.enableGOGAffiliate !== true,
lastChangelogShown: JSON.parse(storage.getItem('last_changelog') || 'null'),
helpItems: {},
experimentalFeatures: {
enableHelp: false,
cometSupport: true,
...(globalSettings?.experimentalFeatures || {})
},
disableDialogBackdropClose: configStore.get(
'disableDialogBackdropClose',
false
),
disableAnimations: configStore.get('disableAnimations', false)
}
setCurrentCustomCategories = (newCustomCategories: string[]) => {
storage.setItem(
'current_custom_categories',
JSON.stringify(newCustomCategories)
)
this.setState({ currentCustomCategories: newCustomCategories })
}
setLanguage = (newLanguage: string) => {
this.setState({ language: newLanguage })
document.querySelector('html')?.setAttribute('lang', newLanguage)
}
setTheme = (newThemeName: string) => {
configStore.set('theme', newThemeName)
this.setState({ theme: newThemeName })
void window.setTheme(newThemeName).then((theme) => {
if (theme !== newThemeName) {
configStore.set('theme', theme)
this.setState({ theme })
}
})
}
zoomTimer: NodeJS.Timeout | undefined = undefined
setZoomPercent = (newZoomPercent: number) => {
if (this.zoomTimer) clearTimeout(this.zoomTimer)
configStore.set('zoomPercent', newZoomPercent)
this.setState({ zoomPercent: newZoomPercent })
this.zoomTimer = setTimeout(() => {
window.api.setZoomFactor((newZoomPercent / 100).toString())
}, 500)
}
setPrimaryFontFamily = (newFontFamily: string, saveToFile = true) => {
if (saveToFile) configStore.set('actionsFontFamily', newFontFamily)
document.documentElement.style.setProperty(
'--primary-font-family',
newFontFamily
)
}
setSecondaryFontFamily = (newFontFamily: string, saveToFile = true) => {
if (saveToFile) configStore.set('contentFontFamily', newFontFamily)
document.documentElement.style.setProperty(
'--secondary-font-family',
newFontFamily
)
}
setAllTilesInColor = (value: boolean) => {
configStore.set('allTilesInColor', value)
this.setState({ allTilesInColor: value })
}
setTitlesAlwaysVisible = (value: boolean) => {
configStore.set('titlesAlwaysVisible', value)
this.setState({ titlesAlwaysVisible: value })
}
setDisableDialogBackdropClose = (value: boolean) => {
configStore.set('disableDialogBackdropClose', value)
this.setState({ disableDialogBackdropClose: value })
}
setDisableAnimations = (value: boolean) => {
configStore.set('disableAnimations', value)
this.setState({ disableAnimations: value })
}
setSideBarCollapsed = (value: boolean) => {
this.setState({ sidebarCollapsed: value })
}
setHideChangelogsOnStartup = (value: boolean) => {
this.setState({ hideChangelogsOnStartup: value })
}
setDisableDiscounts = (value: boolean) => {
this.setState({ disableDiscounts: value })
}
setDisableGOGAffiliate = (value: boolean) => {
sessionStorage.removeItem('last-url-gog')
this.setState({ disableGOGAffiliate: value })
}
setLastChangelogShown = (value: string) => {
this.setState({ lastChangelogShown: value })
}
hideGame = (appNameToHide: string, appTitle: string) => {
const newHiddenGames = [
...this.state.hiddenGames,
{ appName: appNameToHide, title: appTitle }
]
this.setState({
hiddenGames: newHiddenGames
})
configStore.set('games.hidden', newHiddenGames)
}
unhideGame = (appNameToUnhide: string) => {
const newHiddenGames = this.state.hiddenGames.filter(
({ appName }) => appName !== appNameToUnhide
)
this.setState({
hiddenGames: newHiddenGames
})
configStore.set('games.hidden', newHiddenGames)
}
addGameToFavourites = (appNameToAdd: string, appTitle: string) => {
const newFavouriteGames = [
...this.state.favouriteGames.filter(
(fav) => fav.appName !== appNameToAdd
),
{ appName: appNameToAdd, title: appTitle }
]
this.setState({
favouriteGames: newFavouriteGames
})
configStore.set('games.favourites', newFavouriteGames)
}
removeGameFromFavourites = (appNameToRemove: string) => {
const newFavouriteGames = this.state.favouriteGames.filter(
({ appName }) => appName !== appNameToRemove
)
this.setState({
favouriteGames: newFavouriteGames
})
configStore.set('games.favourites', newFavouriteGames)
}
getCustomCategories = () =>
Array.from(new Set(Object.keys(this.state.customCategories))).sort()
getCurrentCustomCategories = () =>
Array.from(new Set(this.state.currentCustomCategories)).sort()
setCustomCategory = (newCategory: string) => {
const newCustomCategories = this.state.customCategories
newCustomCategories[newCategory] = []
// when adding a new category, if there are categories selected, select the new
// one too so the game doesn't disappear form the library
let newCurrentCustomCategories = this.state.currentCustomCategories
if (this.state.currentCustomCategories.length > 0) {
newCurrentCustomCategories = [...newCurrentCustomCategories, newCategory]
}
this.setState({
customCategories: newCustomCategories,
currentCustomCategories: newCurrentCustomCategories
})
configStore.set('games.customCategories', newCustomCategories)
}
removeCustomCategory = (category: string) => {
if (!this.state.customCategories[category]) return
const newCustomCategories = this.state.customCategories
delete newCustomCategories[category]
this.setState({ customCategories: { ...newCustomCategories } })
const updatedCategories = this.getCurrentCustomCategories().filter(
(cat) => cat !== category
)
this.setCurrentCustomCategories(updatedCategories)
const numberOfCategories = updatedCategories.length
if (numberOfCategories < 1) {
this.setCurrentCustomCategories(['preset_uncategorized'])
}
configStore.set('games.customCategories', newCustomCategories)
}
renameCustomCategory = (oldName: string, newName: string) => {
if (!this.state.customCategories[oldName]) return
const newCustomCategories = this.state.customCategories
newCustomCategories[newName] = newCustomCategories[oldName]
delete newCustomCategories[oldName]
this.setState({ customCategories: { ...newCustomCategories } })
configStore.set('games.customCategories', newCustomCategories)
const newCurrentCustomCategories = this.state.currentCustomCategories.map(
(cat) => (cat === oldName ? newName : cat)
)
this.setCurrentCustomCategories(newCurrentCustomCategories)
}
addGameToCustomCategory = (category: string, appName: string) => {
const gamesInCategory = this.state.customCategories[category] || []
gamesInCategory.push(appName)
const newCustomCategories = {
...this.state.customCategories,
[category]: gamesInCategory
}
this.setState({
customCategories: newCustomCategories
})
configStore.set('games.customCategories', newCustomCategories)
}
removeGameFromCustomCategory = (category: string, appName: string) => {
if (!this.state.customCategories[category]) return
const newCustomCategories: Record<string, string[]> = {}
for (const [key, games] of Object.entries(this.state.customCategories)) {
if (key === category)
newCustomCategories[key] = games.filter((game) => game !== appName)
else newCustomCategories[key] = this.state.customCategories[key]
}
this.setState({
customCategories: newCustomCategories
})
configStore.set('games.customCategories', newCustomCategories)
}
handleShowDialogModal = ({
showDialog = true,
...options
}: DialogModalOptions) => {
this.setState({
dialogModalOptions: { showDialog, ...options }
})
}
showResetDialog = (() => {
this.handleShowDialogModal({
title: t('box.reset-heroic.question.title', 'Reset Heroic'),
message: t(
'box.reset-heroic.question.message',
"Are you sure you want to reset Heroic? This will remove all settings and caching but won't remove your installed games or your Epic credentials. Portable versions (AppImage, WinPortable, ...) of Heroic needs to be restarted manually afterwards."
),
buttons: [
{
text: t('box.yes'),
variant: 'dangerSubtle',
onClick: window.api.resetHeroic
},
{ text: t('box.no') }
]
})
}).bind(this)
handleExternalLinkDialog = (value: ExternalLinkDialogOptions) => {
this.setState({ externalLinkDialogOptions: value })
}
handleLibraryTopSection = (value: LibraryTopSectionOptions) => {
this.setState({ libraryTopSection: value })
}
handleExperimentalFeatures = (value: ExperimentalFeatures) => {
this.setState({
experimentalFeatures: value,
zoom: { ...this.state.zoom, enabled: value }
})
}
handleSuccessfulLogin = (runner: Runner) => {
storage.setItem('category', 'all')
this.refreshLibrary({
runInBackground: false,
library: runner
})
}
epicLogin = async (sid: string) => {
console.log('logging epic')
const response = await window.api.login(sid)
if (response.status === 'done') {
this.setState({
epic: {
library: [],
username: response.data?.displayName
}
})
this.handleSuccessfulLogin('legendary')
}
return response.status
}
epicLogout = async () => {
this.setState({ refreshing: true })
await window.api.logoutLegendary().finally(() => {
this.setState({
epic: {
library: [],
username: null
}
})
})
console.log('Logging out from epic')
this.setState({ refreshing: false })
window.location.reload()
}
gogLogin = async (token: string) => {
console.log('logging gog')
const response = await window.api.authGOG(token)
if (response.status === 'done') {
this.setState({
gog: {
library: [],
username: response.data?.username
}
})
this.handleSuccessfulLogin('gog')
}
return response.status
}
gogLogout = async () => {
window.api.logoutGOG()
this.setState({
gog: {
library: [],
username: null
}
})
console.log('Logging out from gog')
window.location.reload()
}
amazonLogin = async (data: NileRegisterData) => {
console.log('logging amazon')
const response = await window.api.authAmazon(data)
if (response.status === 'done') {
this.setState({
amazon: {
library: [],
user_id: response.user?.user_id,
username: response.user?.name
}
})
this.handleSuccessfulLogin('nile')
}
return response.status
}
amazonLogout = async () => {
await window.api.logoutAmazon()
this.setState({
amazon: {
library: [],
user_id: null,
username: null
}
})
console.log('Logging out from amazon')
window.location.reload()
}
getAmazonLoginData = async () => window.api.getAmazonLoginData()
itchioLogin = async (data: ItchioRegisterData) => {
console.log('logging itch.io')
const response = await window.api.authItchio(data)
if (response.status === 'done') {
this.setState({
itchio: {
library: [],
username: response.user?.username
}
})
this.handleSuccessfulLogin('itchio')
}
return response.status
}
itchioLogout = async () => {
await window.api.logoutItchio()
this.setState({
itchio: {
library: [],
username: null
}
})
console.log('Logging out from itch.io')
window.location.reload()
}
getItchioLoginData = async () => window.api.getItchioLoginData()
getItchioOAuthUrl = async () => window.api.getItchioOAuthUrl()
itchioOAuthLogin = async (data: ItchioOAuthCodeData) => {
console.log('logging itch.io with OAuth')
const response = await window.api.authItchioOAuth(data)
if (response.status === 'done') {
this.setState({
itchio: {
library: [],
username: response.user?.username
}
})
this.handleSuccessfulLogin('itchio')
}
return response.status
}
zoomLogin = async (url: string) => {
console.log('logging zoom')
const response = await window.api.authZoom(url)
if (response.status === 'done') {
const userInfo = await window.api.getZoomUserInfo()
this.setState({
zoom: {
library: [],
username: userInfo?.username,
enabled: true
}
})
this.handleSuccessfulLogin('zoom')
}
return response.status
}
zoomLogout = async () => {
window.api.logoutZoom()
this.setState({
zoom: {
library: [],
username: null,
enabled: true
}
})
console.log('Logging out from zoom')
window.location.reload()
}
// Shared post-login handling for both the credentials and QR Steam flows.
private finishSteamLogin = async (response: {
status: 'done' | 'error'
error?: string
}) => {
if (response.status === 'done') {
const userInfo = await window.api.getSteamUserInfo()
this.setState({
steam: {
...this.state.steam,
library: [],
username: userInfo?.username,
enabled: true
}
})
await this.loadSteamUsers()
storage.setItem('category', 'all')
// The startup refresh may still be running; refresh Steam directly so the
// new session's library is not dropped by refreshLibrary's guard.
try {
await window.api.refreshLibrary('steam')
} catch (error) {
window.api.logError(`Steam library refresh failed: ${String(error)}`)
}
this.setState((prev) => ({
steam: {
...(prev as StateProps).steam,
library: this.loadSteamLibrary()
}
}))
}
return response
}
steamLogin = async (credentials: SteamLoginData) => {
console.log('logging steam')
const response = await window.api.loginSteam(credentials)
return this.finishSteamLogin(response)
}
steamLoginQr = async () => {
console.log('logging steam (QR)')
const response = await window.api.loginSteamQr()
return this.finishSteamLogin(response)
}
steamLogout = async () => {
window.api.logoutSteam()
this.setState({
steam: {
...this.state.steam,
library: [],
username: null,
enabled: true
}
})
console.log('Logging out from steam')
window.location.reload()
}
loadSteamUsers = async () => {
const users = await window.api.getSteamUsers()
// Updater: batched with username refresh
this.setState((prev) => ({
steam: { ...(prev as StateProps).steam, users }
}))
}
logoutSteamUser = (steamId: string) => {
window.api.logoutSteamAccount(steamId)
this.setState((prev) => {
const { steam } = prev as StateProps
const users = steam.users.filter((user) => user.steamId !== steamId)
return { steam: { ...steam, users } }
})
void this.refreshLibrary({ runInBackground: true })
}
updateGameOverrides = (overrides: Record<string, GameOverride>) => {
useGlobalState.getState().setGameOverrides(overrides)
this.setState({
epic: {
...this.state.epic,
library: this.loadLegendaryLibrary(overrides)
},
gog: {
...this.state.gog,
library: this.loadGOGLibrary(overrides)
},
zoom: {
...this.state.zoom,
library: this.loadZoomLibrary(overrides)
},
amazon: {
...this.state.amazon,
library: this.loadAmazonLibrary(overrides)
},
sideloadedLibrary: applyGameOverrides(
sideloadLibrary.get('games', []),
overrides
)
})
}
refresh = async (
library?: Runner | 'all',
checkUpdates = false,
overridesArg?: Record<string, GameOverride>
): Promise<void> => {
console.log('refreshing')
const { epic, gog, amazon, zoom, itchio, steam, gameUpdates } = this.state
const overrides = overridesArg || currentOverrides()
let updates = gameUpdates
if (checkUpdates) {
try {
updates = await window.api.checkGameUpdates()
} catch (error) {
window.api.logError(`Game update check failed: ${String(error)}`)
}
}
const currentLibraryLength = epic.library?.length
let epicLibrary = libraryStore.get('library', [])
if (epic.username && (!epicLibrary.length || !epic.library.length)) {
window.api.logInfo('No cache found, getting data from legendary...')
const { library: legendaryLibrary } = await getLegendaryConfig()
epicLibrary = legendaryLibrary
}
let gogLibrary = this.loadGOGLibrary(overrides)
if (gog.username && (!gogLibrary.length || !gog.library.length)) {
window.api.logInfo('No cache found, getting data from gog...')
await window.api.refreshLibrary('gog')
gogLibrary = this.loadGOGLibrary(overrides)
}
let zoomLibrary: GameInfo[] = []
if (zoom.enabled) {
zoomLibrary = this.loadZoomLibrary(overrides)
if (zoom.username && (!zoomLibrary.length || !zoom.library.length)) {
window.api.logInfo('No cache found, getting data from zoom...')
await window.api.refreshLibrary('zoom')
zoomLibrary = this.loadZoomLibrary(overrides)
}
}
let steamLibrary: GameInfo[] = []
const steamUsername = steam.username ?? this.state.steam.username
if (steam.enabled && steamUsername) {
steamLibrary = this.loadSteamLibrary(overrides)
if (!steamLibrary.length || !steam.library.length) {
window.api.logInfo('No cache found, getting data from steam...')
await window.api.refreshLibrary('steam')
steamLibrary = this.loadSteamLibrary(overrides)
}
}
let amazonLibrary = nileLibraryStore.get('library', [])
if (amazon.user_id && (!amazonLibrary.length || !amazon.library.length)) {
window.api.logInfo('No cache found, getting data from nile...')
await window.api.refreshLibrary('nile')
amazonLibrary = this.loadAmazonLibrary(overrides)
}
let itchioLibrary = this.loadItchioLibrary(overrides)
if (itchio.username && (!itchioLibrary.length || !itchio.library.length)) {
window.api.logInfo('No cache found, getting data from itch.io...')
await window.api.refreshLibrary('itchio')
itchioLibrary = this.loadItchioLibrary(overrides)
}
const updatedSideload = sideloadLibrary.get('games', [])
this.setState((prev) => ({
epic: {
library: applyGameOverrides(epicLibrary, overrides),
username: epic.username
},
gog: {
library: gogLibrary,
username: gog.username
},
zoom: {
library: zoomLibrary,
username: zoom.username,
enabled: zoom.enabled
},
steam: {
// A Steam login can arrive while this refresh is running
library: this.state.steam.username
? this.loadSteamLibrary(overrides)
: [],
// Live values; loaded during refresh
username: (prev as StateProps).steam.username,
enabled: steam.enabled,
users: (prev as StateProps).steam.users
},
amazon: {
library: amazonLibrary,
user_id: amazon.user_id,
username: amazon.username
},
itchio: {
library: itchioLibrary,
username: itchio.username
},
gameUpdates: updates,
refreshing: false,
refreshingInTheBackground: true,
sideloadedLibrary: applyGameOverrides(updatedSideload, overrides)
}))
if (currentLibraryLength !== epicLibrary.length) {
window.api.logInfo('Force Update')
this.forceUpdate()
}
}
refreshLibrary = async ({
checkForUpdates,
runInBackground = true,
library = undefined
}: RefreshOptions): Promise<void> => {
if (this.state.refreshing) return
this.setState({
refreshing: true,
refreshingInTheBackground: runInBackground
})
window.api.logInfo(`Refreshing ${library} Library`)
try {
await window.api.refreshLibrary(library)
return await this.refresh(library, checkForUpdates)
} catch (error) {
window.api.logError(`Library refresh failed: ${String(error)}`)
}
}
handleGameStatus = async ({
appName,
status,
folder,
context,
progress,
runner
}: GameStatus) => {
const { libraryStatus, gameUpdates } = this.state
const currentApp = libraryStatus.find((game) => game.appName === appName)
// add app to libraryStatus if it was not present
if (!currentApp) {
return this.setState({
libraryStatus: [
...libraryStatus,
{ appName, status, folder, context, progress, runner }
]
})
}
// if the app's status didn't change, do nothing
if (currentApp.status === status && currentApp.context === context) {
return
}
// if the app's status did change, remove it from the current list and then handle the new status
const newLibraryStatus = libraryStatus.filter(
(game) => game.appName !== appName
)
// in these cases we just add the new status
if (
[
'installing',
'updating',
'playing',
'extracting',
'launching',
'winetricks',
'redist',
'queued'
].includes(status)
) {
newLibraryStatus.push({
appName,
status,
folder,
context,
progress,
runner
})
this.setState({ libraryStatus: newLibraryStatus })
}
// when error or done we remove it from the status info
if (['error', 'done'].includes(status)) {
// also remove from updates if it was updating; GOG updates end with a redist setup step
if (['updating', 'redist'].includes(currentApp.status)) {
const updatedGamesUpdates = gameUpdates.filter(
(game) => game !== appName
)
// This avoids calling legendary again before the previous process is killed when canceling
this.refreshLibrary({
checkForUpdates: true,
runInBackground: true,
library: runner
})
storage.setItem('updates', JSON.stringify(updatedGamesUpdates))
return this.setState({
gameUpdates: updatedGamesUpdates,
libraryStatus: newLibraryStatus
})
}
this.refreshLibrary({ runInBackground: true, library: runner })
this.setState({ libraryStatus: newLibraryStatus })
}
}
async componentDidMount() {
const {
epic,
gog,
amazon,
zoom,
steam,
gameUpdates = [],
libraryStatus,
platform
} = this.state
// Re-apply the theme with the same fallback used on startup, so a stale
// theme value ends up as the default theme in the selector as well.
if (!isBuiltinTheme(this.state.theme)) {
const appliedTheme = await window.setTheme(this.state.theme)
if (appliedTheme !== this.state.theme) {
configStore.set('theme', appliedTheme)
this.setState({ theme: appliedTheme })
}
}
// A failing support check must not abort the rest of this refresh.
const steamSupported = await window.api
.isSteamSupported()
.catch(() => false)
if (steamSupported !== this.state.steam.enabled) {
this.setState((prev) => {
const state = prev as StateProps
return { steam: { ...state.steam, enabled: steamSupported } }
})
}
if (steamSupported) {
void this.loadSteamUsers()
}
window.api.handleInstallGame(async (e, appName, runner) => {
const currentApp = libraryStatus.filter(
(game) => game.appName === appName
)[0]
if (!currentApp || (currentApp && currentApp.status !== 'installing')) {
const gameInfo = await getGameInfo(appName, runner)
if (!gameInfo || gameInfo.runner === 'sideload') {
return
}
return this.setState({
showInstallModal: {
show: true,
appName,
runner,
gameInfo
}
})
}
})
window.api.handleGameStatus((e, args) => {
this.handleGameStatus({ ...args })
})
window.api.handleRefreshLibrary((e, runner) => {
this.refreshLibrary({
checkForUpdates: false,
runInBackground: true,
library: runner
})
})
window.api.handleMetadataChanged((e, overrides) => {
this.updateGameOverrides(overrides)
})
window.api.handleGamePush((e, args) => {
if (!args.app_name) return
// Use the functional setState form so a burst of pushes (e.g. hundreds of
// games streamed in during a library refresh) can't lose updates: each
// push must read the latest library, not a stale `this.state` snapshot
// from before the previous setState was applied.
const upsert = (library: GameInfo[]): GameInfo[] => {
const next = [...library]
const index = next.findIndex((game) => game.app_name === args.app_name)
if (index !== -1) {
next[index] = args
} else {
next.push(args)
}
return next
}
// `prevState` is typed via the component's (empty) State generic, while
// the real state shape is declared as `state: StateProps`; cast to read it.
if (args.runner === 'gog') {
this.setState((prev) => {
const prevState = prev as StateProps
return {
gog: { ...prevState.gog, library: upsert(prevState.gog.library) }
}
})
} else if (args.runner === 'zoom') {
this.setState((prev) => {
const prevState = prev as StateProps
return {
zoom: {
...prevState.zoom,
library: upsert(prevState.zoom.library),
enabled: true
}
}
})
} else if (args.runner === 'steam') {
this.setState((prev) => {
const prevState = prev as StateProps
return {
steam: {
...prevState.steam,
library: upsert(prevState.steam.library),
enabled: true
}
}
})
} else if (args.runner === 'itchio') {
const library = [...this.state.itchio.library]
const index = library.findIndex(
(game) => game.app_name === args.app_name
)
if (index !== -1) {
library[index] = args
} else {
library.push(args)
}
this.setState({
itchio: {
...this.state.itchio,
library
}
})
}
})
if (platform === 'darwin') {
this.setState({ isIntelMac: await window.api.isIntelMac() })
}
if (platform === 'linux') {
this.setState({ isLinuxArm: await window.api.isLinuxArm() })
}
this.setState({
isFullscreen: await window.api.isFullscreen(),
isFrameless: await window.api.isFrameless()
})
window.api.handleFullscreen((e, isFullscreen) => {
this.setState({ isFullscreen })
})
const legendaryUser = configStore.has('userInfo')
const gogUser = gogConfigStore.has('userData')
const amazonUser = nileConfigStore.has('userData')
const zoomUser = zoomConfigStore.has('isLoggedIn')
const itchioUser = itchioConfigStore.has('userData')
const steamUser = steamConfigStore.has('isLoggedIn')
if (legendaryUser) {
await window.api.getUserInfo()
}
if (amazonUser) {
await window.api.getAmazonUserInfo()
}
if (zoom.enabled && zoomUser) {
await window.api.getZoomUserInfo()
}
if (itchioUser) {
// Re-establish butlerd session and refresh cached profile data
await window.api.getItchioUserInfo()
}
if (steam.enabled && steamUser) {
// Refresh name from live session
const steamInfo = await window.api.getSteamUserInfo()
this.setState((prev) => ({
steam: {
...(prev as StateProps).steam,
username: steamInfo?.username
}
}))
}
if (!gameUpdates.length) {
const storedGameUpdates = JSON.parse(storage.getItem('updates') || '[]')
this.setState({ gameUpdates: storedGameUpdates })
}
if (
legendaryUser ||
gogUser ||
amazonUser ||
(zoom.enabled && zoomUser) ||
itchioUser ||
(steam.enabled && steamUser)
) {
this.refreshLibrary({
checkForUpdates: true,
runInBackground:
epic.library.length !== 0 ||
gog.library.length !== 0 ||
amazon.library.length !== 0 ||
((this.state.zoom.enabled && zoom.library) || []).length !== 0 ||
((this.state.steam.enabled && steam.library) || []).length !== 0
})
}
window.addEventListener(
'controller-changed',
(e: CustomEvent<{ controllerId: string }>) => {
document.body.classList.toggle(
'controllerLayout',
e.detail.controllerId !== ''
)
this.setState({ activeController: e.detail.controllerId })
}
)
// listen to custom connectivity-changed event to update state
window.api.onConnectivityChanged((_, connectivity) => {
this.setState({ connectivity })
})
// get the current status
window.api
.getConnectivityStatus()
.then((connectivity) => this.setState({ connectivity }))
this.setPrimaryFontFamily(this.state.primaryFontFamily, false)
this.setSecondaryFontFamily(this.state.secondaryFontFamily, false)
window.api.frontendReady()
}
componentDidUpdate() {
const {
gameUpdates,
libraryStatus,
sidebarCollapsed,
hideChangelogsOnStartup,
lastChangelogShown,
language
} = this.state
const isRTL = RTL_LANGUAGES.includes(language)
document.body.classList.toggle('isRTL', isRTL)
storage.setItem('updates', JSON.stringify(gameUpdates))
storage.setItem('sidebar_collapsed', JSON.stringify(sidebarCollapsed))
storage.setItem('hide_changelogs', JSON.stringify(hideChangelogsOnStartup))
storage.setItem('last_changelog', JSON.stringify(lastChangelogShown))
const allowedPendingOps: Status[] = [
'installing',
'updating',
'launching',
'playing',
'redist',
'winetricks',
'extracting',
'repairing',
'moving',
'syncing-saves',
'uninstalling'
]
const pendingOps = libraryStatus.filter((game) =>
allowedPendingOps.includes(game.status)
).length
const playing =
libraryStatus.filter((game) => game.status === 'playing').length > 0
if (pendingOps) {
window.api.lock(playing)
} else {
window.api.unlock()
}
}
addHelpItem = (helpItemId: string, helpItem: HelpItem) => {
this.setState((previous: StateProps) => {
const newItems = { ...previous.helpItems }
newItems[helpItemId] = helpItem
return { helpItems: newItems }
})
}
removeHelpItem = (helpItemId: string) => {
this.setState((previous: StateProps) => {
delete previous.helpItems[helpItemId]
return { helpItems: { ...previous.helpItems } }
})
}
render() {
const {
language,
epic,
gog,
amazon,
zoom,
itchio,
steam,
favouriteGames,
customCategories,
hiddenGames,
hideChangelogsOnStartup,
lastChangelogShown,
libraryStatus
} = this.state
const isRTL = RTL_LANGUAGES.includes(language)
const installingEpicGame = libraryStatus.some(
(game) => game.status === 'installing' && game.runner === 'legendary'
)
return (
<ContextProvider.Provider
value={{
...this.state,
epic: {
library: epic.library,
username: epic.username,
login: this.epicLogin,
logout: this.epicLogout
},
gog: {
library: gog.library,
username: gog.username,
login: this.gogLogin,
logout: this.gogLogout
},
amazon: {
library: amazon.library,
user_id: amazon.user_id,
username: amazon.username,
getLoginData: this.getAmazonLoginData,
login: this.amazonLogin,
logout: this.amazonLogout
},
zoom: {
library: this.state.zoom.enabled ? zoom.library : [],
username: this.state.zoom.enabled ? zoom.username : undefined,
login: this.zoomLogin,
logout: this.zoomLogout,
enabled: this.state.zoom.enabled
},
itchio: {
library: itchio.library,
username: itchio.username,
getLoginData: this.getItchioLoginData,
getOAuthUrl: this.getItchioOAuthUrl,
login: this.itchioLogin,
loginWithOAuthCode: this.itchioOAuthLogin,
logout: this.itchioLogout
},
steam: {
library: this.state.steam.enabled ? steam.library : [],
username: this.state.steam.enabled ? steam.username : undefined,
login: this.steamLogin,
loginQr: this.steamLoginQr,
logout: this.steamLogout,
enabled: this.state.steam.enabled,
users: this.state.steam.users,
logoutUser: this.logoutSteamUser
},
installingEpicGame,
setLanguage: this.setLanguage,
isRTL,
refresh: this.refresh,
refreshLibrary: this.refreshLibrary,
hiddenGames: {
list: hiddenGames,
add: this.hideGame,
remove: this.unhideGame
},
favouriteGames: {
list: favouriteGames,
add: this.addGameToFavourites,
remove: this.removeGameFromFavourites
},
customCategories: {
list: customCategories,
listCategories: this.getCustomCategories,
addToGame: this.addGameToCustomCategory,
removeFromGame: this.removeGameFromCustomCategory,
addCategory: this.setCustomCategory,
removeCategory: this.removeCustomCategory,
renameCategory: this.renameCustomCategory
},
handleLibraryTopSection: this.handleLibraryTopSection,
handleExperimentalFeatures: this.handleExperimentalFeatures,
setTheme: this.setTheme,
setZoomPercent: this.setZoomPercent,
setAllTilesInColor: this.setAllTilesInColor,
setTitlesAlwaysVisible: this.setTitlesAlwaysVisible,
setSideBarCollapsed: this.setSideBarCollapsed,
setPrimaryFontFamily: this.setPrimaryFontFamily,
setSecondaryFontFamily: this.setSecondaryFontFamily,
showDialogModal: this.handleShowDialogModal,
showResetDialog: this.showResetDialog,
handleExternalLinkDialog: this.handleExternalLinkDialog,
hideChangelogsOnStartup: hideChangelogsOnStartup,
setHideChangelogsOnStartup: this.setHideChangelogsOnStartup,
setDisableDiscounts: this.setDisableDiscounts,
setDisableGOGAffiliate: this.setDisableGOGAffiliate,
lastChangelogShown: lastChangelogShown,
setLastChangelogShown: this.setLastChangelogShown,
setCurrentCustomCategories: this.setCurrentCustomCategories,
help: {
items: this.state.helpItems,
addHelpItem: this.addHelpItem,
removeHelpItem: this.removeHelpItem
},
setDisableDialogBackdropClose: this.setDisableDialogBackdropClose,
disableAnimations: this.state.disableAnimations,
setDisableAnimations: this.setDisableAnimations
}}
>
{this.props.children}
</ContextProvider.Provider>
)
}
}
export default withTranslation()(GlobalState)