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 { create } from 'zustand'
import { useShallow } from 'zustand/react/shallow'
import type { GetLogFileArgs } from 'backend/logger/paths'
import type { GameInfo } from 'common/types'
import type { GameSettingsModalType } from '../screens/Settings/components/SettingsModal'
import { notify } from 'frontend/helpers'
import { gameOverridesStore } from 'frontend/helpers/electronStores'
import { t } from 'i18next'
type GameOverride = NonNullable<GameInfo['overrides']>
interface GlobalStateV2 {
uploadLogFileProps:
| false
| {
logFileArgs: GetLogFileArgs
name: string
}
setUploadLogFileProps: (props: GlobalStateV2['uploadLogFileProps']) => void
settingsModalProps:
| { isOpen: false }
| {
isOpen: true
type: GameSettingsModalType
gameInfo: GameInfo
// Tab to open on
tab?: string
}
openGameSettingsModal: (gameInfo: GameInfo, tab?: string) => void
openGameLogsModal: (gameInfo: GameInfo) => void
openGameCategoriesModal: (gameInfo: GameInfo) => void
closeSettingsModal: () => void
showUploadedLogFileList: boolean
refreshingWineVersions: boolean
refreshWineVersions: (fetch: boolean) => void
gameOverrides: Record<string, GameOverride>
setGameOverrides: (overrides: Record<string, GameOverride>) => void
}
const useGlobalStateRaw = create<GlobalStateV2>()((set) => ({
uploadLogFileProps: false,
setUploadLogFileProps: (uploadLogFileProps) => {
set({ uploadLogFileProps })
},
settingsModalProps: { isOpen: false },
openGameSettingsModal: (gameInfo, tab) => {
set({
settingsModalProps: {
isOpen: true,
type: 'settings',
gameInfo,
tab
}
})
},
openGameLogsModal: (gameInfo) => {
set({
settingsModalProps: {
isOpen: true,
type: 'log',
gameInfo
}
})
},
openGameCategoriesModal: (gameInfo) => {
set({
settingsModalProps: {
isOpen: true,
type: 'category',
gameInfo
}
})
},
closeSettingsModal: () => {
set({ settingsModalProps: { isOpen: false } })
},
showUploadedLogFileList: false,
gameOverrides: gameOverridesStore.get('overrides', {}),
setGameOverrides: (gameOverrides) => set({ gameOverrides }),
refreshingWineVersions: false,
refreshWineVersions: (fetch) => {
window.api.logInfo('Refreshing wine downloader releases')
set({ refreshingWineVersions: true })
window.api
.refreshWineVersionInfo(fetch)
.catch(() => {
window.api.logError('Sync with upstream releases failed')
notify({
title: 'Wine-Manager',
body: t(
'notify.refresh.error',
"Couldn't fetch releases from upstream, maybe because of Github API restrictions! Try again later."
)
})
})
.finally(() => set({ refreshingWineVersions: false }))
}
}))
/**
* Wrapper around {@link useGlobalStateRaw} to only re-render if
* the return value of `selector` changes
* @param selector A function picking state out of {@link GlobalStateV2}. If the
* return value of this function changes, the component is
* re-rendered
*/
const useGlobalState = <T>(
selector: Parameters<typeof useShallow<GlobalStateV2, T>>[0]
) => useGlobalStateRaw(useShallow(selector))
/**
* Shorthand to select properties from {@link GlobalStateV2}
* @example
* ```ts
* const { foo, bar } = useGlobalStateKeys('foo', 'bar')
* ```
* is equivalent to
* ```ts
* const { foo, bar } = useGlobalState(({ foo, bar }) => ({ foo, bar }))
* ```
* @param keys The keys to return. Properties of {@link GlobalStateV2}
*/
const useGlobalStateKeys = <Keys extends (keyof GlobalStateV2)[]>(
...keys: Keys
): Pick<GlobalStateV2, Keys[number]> =>
useGlobalState(
(state) =>
Object.fromEntries(keys.map((key) => [key, state[key]])) as {
[key in Keys[number]]: GlobalStateV2[key]
}
)
export default {
...useGlobalState,
keys: useGlobalStateKeys,
setState: useGlobalStateRaw.setState,
getState: useGlobalStateRaw.getState
}