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 { useCallback, useEffect, useRef, useState } from 'react'
import type { GameInfo } from 'common/types'
import type { SteamLaunchTarget } from 'common/types/steam'
export interface SteamLaunchTargetsState {
targets: SteamLaunchTarget[]
// Chosen id, empty for automatic
selected: string
select: (targetId: string) => Promise<void>
refresh: () => Promise<void>
// Chosen target, else the default
effective?: SteamLaunchTarget
// Launch platform, known offline too
platform?: string
// Missing picks download on their own
pinsPlatform: boolean
}
/**
* The game versions Aurelia can launch a Steam game with, and the user's pick.
*/
export function useSteamLaunchTargets(
appName: string,
gameInfo: GameInfo | null | undefined,
enabled: boolean
): SteamLaunchTargetsState {
const [targets, setTargets] = useState<SteamLaunchTarget[]>([])
const [selected, setSelected] = useState('')
const [pinsPlatform, setPinsPlatform] = useState(false)
// Pick when settings opened
const initial = useRef<string>()
const latest = useRef<string>()
const active =
enabled && gameInfo?.runner === 'steam' && !!gameInfo.is_installed
const installPath = gameInfo?.install?.install_path
const refresh = useCallback(async () => {
if (!active) {
setTargets([])
return
}
const result = await window.api.getSteamLaunchTargets(appName)
setTargets(result.targets)
setSelected(result.selected ?? '')
initial.current ??= result.selected ?? ''
latest.current ??= result.selected ?? ''
setPinsPlatform(result.pinsPlatform ?? false)
}, [appName, active])
useEffect(() => {
void refresh()
}, [refresh, installPath])
// Changed pick applies on settings close
useEffect(() => {
initial.current = undefined
latest.current = undefined
return () => {
if (latest.current !== initial.current) {
void window.api.applySteamLaunchTarget(appName)
}
}
}, [appName])
const select = useCallback(
async (targetId: string) => {
setSelected(targetId)
latest.current = targetId
await window.api.setSteamLaunchTarget(appName, targetId)
// Launch options follow the platform
window.dispatchEvent(
new CustomEvent('steam-launch-target-changed', { detail: { appName } })
)
},
[appName]
)
const effective =
targets.find((target) => target.id === selected) ??
targets.find((target) => target.isDefault)
// Aurelia lists targets only online
const platform = active
? (effective?.platform ??
targetIdPlatform(selected) ??
String(gameInfo?.install?.platform ?? '').toLowerCase())
: undefined
return {
targets,
selected,
select,
refresh,
effective,
platform,
pinsPlatform
}
}
/** Platform encoded in a target id. */
function targetIdPlatform(targetId: string): string | undefined {
if (targetId.startsWith('linux')) return 'linux'
if (targetId.startsWith('macos')) return 'macos'
if (targetId.startsWith('windows')) return 'windows'
return undefined
}