Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tech] Remove the Global State #3827

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1a02994
Remove unused `error` property
CommandMC Jun 15, 2024
2bd5d68
Remove the `platform` property
CommandMC Jun 15, 2024
8f412ca
Migrate our first property (isFullscreen) to a Zustand store
CommandMC Jun 15, 2024
99b98ee
Migrate `isFrameless` to the Zustand store
CommandMC Jun 15, 2024
7e854cd
Add a helper function to make selecting properties from the global st…
CommandMC Jun 15, 2024
cf0edcd
Migrate `language`, `isRTL` and `setLanguage` to the Zustand store
CommandMC Jun 15, 2024
0ec1db5
Migrate `gameUpdates` and a part of `refresh` to the Zustand store
CommandMC Jun 15, 2024
22f3f58
Make the `language` and `gameUpdates` keys persist
CommandMC Jun 16, 2024
5da2287
Remove unused `wineVersions` property
CommandMC Jun 16, 2024
263601b
Migrate the `help` property
CommandMC Jun 16, 2024
5895401
Migrate the `isSettingsModalOpen` and `setIsSettingsModalOpen` proper…
CommandMC Jun 17, 2024
407c18a
Migrate `libraryStatus` and make it a Record
CommandMC Jun 18, 2024
c6c8cf9
Migrate `experimentalFeatures` and `handleExperimentalFeatures`
CommandMC Jun 18, 2024
6c4e96c
Remove unused `sidebarCollapsed` and `setSideBarCollapsed` properties
CommandMC Jun 18, 2024
572c93f
Migrate `externalLinkDialogOptions`
CommandMC Jun 18, 2024
a540355
Migrate `libraryTopSection`
CommandMC Jun 18, 2024
0294218
Migrate `hiddenGames` and `favouriteGames`
CommandMC Jun 18, 2024
6d8f647
Migrate `theme` and remove `window.setTheme`
CommandMC Jun 18, 2024
e393fd1
Migrate `connectivity`
CommandMC Jun 18, 2024
6ed21d2
Migrate `dialogModalOptions` and `showResetDialog`
CommandMC Jun 18, 2024
a693d2f
Migrate `primaryFontFamily` and `secondaryFontFamily`
CommandMC Jun 18, 2024
9b5f735
Migrate `disableDialogBackdropClose`
CommandMC Jun 18, 2024
581b502
Migrate `activeController`
CommandMC Jun 19, 2024
3980b8f
Migrate `installModalOptions`
CommandMC Jun 19, 2024
d45b4e8
Migrate `zoomPercent`
CommandMC Jun 19, 2024
a81f600
Migrate `allTilesInColor` and `titlesAlwaysVisible`
CommandMC Jun 19, 2024
940afac
Migrate `hideChangelogsOnStartup` and `lastChangelogShown`
CommandMC Jun 19, 2024
406d982
Migrate `customCategories` and `setCurrentCustomCategories`
CommandMC Jun 20, 2024
1608754
Remove `refreshWineVersionInfo`
CommandMC Jun 22, 2024
efbfd50
Migrate `refreshingInTheBackground`
CommandMC Jun 22, 2024
8e628bf
Migrate `handleLaunchGame` handler
CommandMC Jun 22, 2024
b3c23a4
Migrate `handleInstallGame` handler
CommandMC Jun 22, 2024
50e7602
Migrate `refreshing`
CommandMC Jun 22, 2024
514b3e7
Migrate `refresh`, `refreshLibrary`, `epic`, `gog`, `amazon`, and `si…
CommandMC Jun 23, 2024
556962a
Remove the old global state
CommandMC Jun 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/backend/__tests__/progress_bar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ describe('progress_bar', () => {
it('does nothing', () => {
sendGameStatusUpdate({
appName: 'Test',
status: 'queued'
status: 'queued',
runner: 'legendary'
})

expect(window.setProgressBar).not.toBeCalled()
Expand All @@ -43,7 +44,8 @@ describe('progress_bar', () => {
it('sets progress bar to indeterminate', () => {
sendGameStatusUpdate({
appName: 'Test',
status: 'installing'
status: 'installing',
runner: 'legendary'
})

expect(window.setProgressBar).toBeCalledWith(2)
Expand All @@ -54,7 +56,8 @@ describe('progress_bar', () => {

sendGameStatusUpdate({
appName: 'Test',
status: 'installing'
status: 'installing',
runner: 'legendary'
})

expect(backendEvents.on).toBeCalledWith(
Expand All @@ -69,7 +72,8 @@ describe('progress_bar', () => {
sendProgressUpdate({
appName: 'Test',
status: 'installing',
progress: { percent: 42, bytes: '', eta: '' }
progress: { percent: 42, bytes: '', eta: '' },
runner: 'legendary'
})

expect(window.setProgressBar).toBeCalledWith(0.42)
Expand All @@ -80,7 +84,8 @@ describe('progress_bar', () => {
it('removes the progress bar', () => {
sendGameStatusUpdate({
appName: 'Test',
status: 'done'
status: 'done',
runner: 'legendary'
})

expect(window.setProgressBar).toBeCalledWith(-1)
Expand All @@ -91,7 +96,8 @@ describe('progress_bar', () => {

sendGameStatusUpdate({
appName: 'Test',
status: 'done'
status: 'done',
runner: 'legendary'
})

expect(backendEvents.off).toBeCalledWith(
Expand Down
13 changes: 9 additions & 4 deletions src/backend/api/downloadmanager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { DownloadManagerState } from './../../common/types'
import {
DownloadManagerState,
Runner,
DMQueueElement,
InstallParams,
UpdateParams
} from 'common/types'
import { ipcRenderer } from 'electron'
import { DMQueueElement, InstallParams, UpdateParams } from 'common/types'

export const install = async (args: InstallParams) => {
const dmQueueElement: DMQueueElement = {
Expand Down Expand Up @@ -57,8 +62,8 @@ export const updateGame = (args: UpdateParams) => {
export const getDMQueueInformation = async () =>
ipcRenderer.invoke('getDMQueueInformation')

export const removeFromDMQueue = (appName: string) =>
ipcRenderer.send('removeFromDMQueue', appName)
export const removeFromDMQueue = (appName: string, runner: Runner) =>
ipcRenderer.send('removeFromDMQueue', appName, runner)

export const handleDMQueueInformation = (
onChange: (
Expand Down
16 changes: 11 additions & 5 deletions src/backend/downloadmanager/downloadqueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { gameManagerMap, libraryManagerMap } from 'backend/storeManagers'
import { TypeCheckedStoreBackend } from './../electron_store'
import { logError, logInfo, LogPrefix, logWarning } from '../logger/logger'
import { getFileSize, removeFolder, sendGameStatusUpdate } from '../utils'
import { DMQueueElement, DMStatus, DownloadManagerState } from 'common/types'
import {
DMQueueElement,
DMStatus,
DownloadManagerState,
Runner
} from 'common/types'
import { installQueueElement, updateQueueElement } from './utils'
import { sendFrontendMessage } from '../main_window'
import { callAbortController } from 'backend/utils/aborthandler/aborthandler'
Expand Down Expand Up @@ -89,7 +94,7 @@ async function initQueue() {

if (!isPaused()) {
addToFinished(element, status)
removeFromQueue(element.params.appName)
removeFromQueue(element.params.appName, element.params.runner)
element = getFirstQueueElement()
} else {
element = null
Expand Down Expand Up @@ -178,7 +183,7 @@ async function addToQueue(element: DMQueueElement) {
}
}

function removeFromQueue(appName: string) {
function removeFromQueue(appName: string, runner: Runner) {
if (appName && downloadManager.has('queue')) {
const elements = downloadManager.get('queue', [])
const index = elements.findIndex(
Expand All @@ -192,6 +197,7 @@ function removeFromQueue(appName: string) {

sendGameStatusUpdate({
appName,
runner,
status: 'done'
})

Expand All @@ -216,13 +222,13 @@ function cancelCurrentDownload({ removeDownloaded = false }) {
if (Array.isArray(currentElement.params.installDlcs)) {
const dlcsToRemove = currentElement.params.installDlcs
for (const dlc of dlcsToRemove) {
removeFromQueue(dlc)
removeFromQueue(dlc, currentElement.params.runner)
}
}
if (isRunning()) {
stopCurrentDownload()
}
removeFromQueue(currentElement.params.appName)
removeFromQueue(currentElement.params.appName, currentElement.params.runner)

if (removeDownloaded) {
const { appName, runner } = currentElement!.params
Expand Down
4 changes: 2 additions & 2 deletions src/backend/downloadmanager/ipc_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ ipcMain.handle('addToDMQueue', async (e, element) => {
await addToQueue(element)
})

ipcMain.on('removeFromDMQueue', (e, appName) => {
removeFromQueue(appName)
ipcMain.on('removeFromDMQueue', (e, appName, runner) => {
removeFromQueue(appName, runner)
})

ipcMain.on('resumeCurrentDownload', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/common/typedefs/ipcBridge.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ interface SyncIPCFunctions {
showLogFileInFolder: (appNameOrRunner: string) => void
addShortcut: (appName: string, runner: Runner, fromMenu: boolean) => void
removeShortcut: (appName: string, runner: Runner) => void
removeFromDMQueue: (appName: string) => void
removeFromDMQueue: (appName: string, runner: Runner) => void
clearDMFinished: () => void
abort: (id: string) => void
'connectivity-changed': (newStatus: ConnectivityStatus) => void
Expand Down
11 changes: 2 additions & 9 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ export type Status =

export interface GameStatus {
appName: string
runner: Runner
status: Status
progress?: InstallProgress
folder?: string
context?: string // Additional context e.g current step
runner?: Runner
status: Status
}

export type GlobalConfigVersion = 'auto' | 'v0'
Expand Down Expand Up @@ -484,13 +484,6 @@ export type HiddenGame = RecentGame

export type FavouriteGame = HiddenGame

export type RefreshOptions = {
checkForUpdates?: boolean
fullRefresh?: boolean
library?: Runner | 'all'
runInBackground?: boolean
}

export interface WineVersionInfo extends VersionInfo {
isInstalled: boolean
hasUpdate: boolean
Expand Down
34 changes: 16 additions & 18 deletions src/frontend/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from 'react'
import React, { useEffect } from 'react'

import './App.css'
import {
Expand All @@ -8,24 +8,26 @@ import {
RouterProvider
} from 'react-router-dom'
import Sidebar from './components/UI/Sidebar'
import ContextProvider from './state/ContextProvider'
import { ControllerHints, Help, OfflineMessage } from './components/UI'
import DialogHandler from './components/UI/DialogHandler'
import SettingsModal from './screens/Settings/components/SettingsModal'
import ExternalLinkDialog from './components/UI/ExternalLinkDialog'
import WindowControls from './components/UI/WindowControls'
import classNames from 'classnames'
import { ThemeProvider, createTheme } from '@mui/material/styles'
import { useShallowGlobalState } from './state/GlobalStateV2'
import InstallModal from './screens/Library/components/InstallModal'

function Root() {
const {
isSettingsModalOpen,
isRTL,
isFullscreen,
isFrameless,
experimentalFeatures,
help
} = useContext(ContextProvider)
const { isFullscreen, isFrameless, isRTL, enableNewDesign } =
useShallowGlobalState(
'isFullscreen',
'isFrameless',
'isRTL',
'enableNewDesign'
)

useEffect(window.api.frontendReady, [])

const hasNativeOverlayControls = navigator['windowControlsOverlay']?.visible
const showOverlayControls = isFrameless && !hasNativeOverlayControls
Expand All @@ -41,7 +43,7 @@ function Root() {
isRTL,
frameless: isFrameless,
fullscreen: isFullscreen,
oldDesign: !experimentalFeatures.enableNewDesign
oldDesign: !enableNewDesign
})}
// disable dragging for all elements by default
onDragStart={(e) => e.preventDefault()}
Expand All @@ -51,12 +53,8 @@ function Root() {
<Sidebar />
<main className="content">
<DialogHandler />
{isSettingsModalOpen.gameInfo && (
<SettingsModal
gameInfo={isSettingsModalOpen.gameInfo}
type={isSettingsModalOpen.type}
/>
)}
<InstallModal />
<SettingsModal />
<ExternalLinkDialog />
<Outlet />
</main>
Expand All @@ -65,7 +63,7 @@ function Root() {
<div className="simple-keyboard"></div>
</div>
{showOverlayControls && <WindowControls />}
{experimentalFeatures.enableHelp && <Help items={help.items} />}
{<Help />}
</ThemeProvider>
</div>
)
Expand Down
7 changes: 5 additions & 2 deletions src/frontend/components/UI/ActionIcons/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import { faHardDrive as hardDriveLight } from '@fortawesome/free-regular-svg-ico
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import React, { useContext } from 'react'
import { useTranslation } from 'react-i18next'
import ContextProvider from 'frontend/state/ContextProvider'
import FormControl from '../FormControl'
import './index.css'
import classNames from 'classnames'
import LibraryContext from 'frontend/screens/Library/LibraryContext'
import { useShallowGlobalState } from 'frontend/state/GlobalStateV2'

export default React.memo(function ActionIcons() {
const { t } = useTranslation()
const { refreshLibrary, refreshing } = useContext(ContextProvider)
const { refreshing, refreshLibrary } = useShallowGlobalState(
'refreshing',
'refreshLibrary'
)

const {
handleLayout,
Expand Down
4 changes: 1 addition & 3 deletions src/frontend/components/UI/Anticheat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { MouseEvent, useContext } from 'react'
import React, { MouseEvent } from 'react'
import { AntiCheatInfo } from 'common/types'
import { createNewWindow } from 'frontend/helpers'

Expand All @@ -8,7 +8,6 @@ import AllowedIcon from 'frontend/assets/rounded_checkmark_icon.svg?react'

import './index.scss'
import { useTranslation } from 'react-i18next'
import ContextProvider from 'frontend/state/ContextProvider'

type Props = {
anticheatInfo: AntiCheatInfo | null
Expand All @@ -18,7 +17,6 @@ const awacyUrl = 'https://areweanticheatyet.com/'

export default function Anticheat({ anticheatInfo }: Props) {
const { t } = useTranslation()
const { platform } = useContext(ContextProvider)

if (!anticheatInfo) {
return null
Expand Down
36 changes: 19 additions & 17 deletions src/frontend/components/UI/CategoryFilter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
import React, { useContext } from 'react'
import ContextProvider from 'frontend/state/ContextProvider'
import { useTranslation } from 'react-i18next'
import ToggleSwitch from '../ToggleSwitch'
import LibraryContext from 'frontend/screens/Library/LibraryContext'
import {
useGlobalState,
useShallowGlobalState
} from 'frontend/state/GlobalStateV2'
import { useShallow } from 'zustand/react/shallow'

export default function CategoryFilter() {
const {
customCategories,
currentCustomCategories,
setCurrentCustomCategories
} = useContext(ContextProvider)
const { currentCustomCategories } = useShallowGlobalState(
'currentCustomCategories'
)
const categoriesList = useGlobalState(
useShallow((state) => Object.keys(state.customCategories))
)
const { setShowCategories } = useContext(LibraryContext)
const { t } = useTranslation()

const toggleCategory = (category: string) => {
let newCategories: string[]
if (currentCustomCategories.includes(category)) {
const newCategories = currentCustomCategories.filter(
(cat) => cat !== category
)
setCurrentCustomCategories(newCategories)
newCategories = currentCustomCategories.filter((cat) => cat !== category)
} else {
setCurrentCustomCategories([...currentCustomCategories, category])
newCategories = [...currentCustomCategories, category]
}
useGlobalState.setState({ currentCustomCategories: newCategories })
}

const setCategoryOnly = (category: string) => {
setCurrentCustomCategories([category])
useGlobalState.setState({ currentCustomCategories: [category] })
}

const selectAll = () => {
setCurrentCustomCategories(
['preset_uncategorized'].concat(customCategories.listCategories())
)
useGlobalState.setState({
currentCustomCategories: ['preset_uncategorized'].concat(categoriesList)
})
}

const toggleWithOnly = (
Expand Down Expand Up @@ -66,8 +70,6 @@ export default function CategoryFilter() {
return toggleWithOnly(toggle, onOnlyClick, categoryValue || categoryName)
}

const categoriesList = customCategories.listCategories()

return (
<div className="categoriesFilter">
<button className="selectStyle">
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/components/UI/ControllerHints/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useContext, useEffect, useState } from 'react'
import React, { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useLocation } from 'react-router-dom'
import ContextProvider from 'frontend/state/ContextProvider'
import { useShallowGlobalState } from 'frontend/state/GlobalStateV2'

import './index.css'

export default function ControllerHints() {
const { activeController } = useContext(ContextProvider)
const { activeController } = useShallowGlobalState('activeController')
const [layout, setLayout] = useState('steam-deck') // default to steam deck icons
const [mainActionHint, setMainActionHint] = useState('') // A / Cross
const [altActionHint, setAltActionHint] = useState('') // X / Square
Expand Down
Loading
Loading