Skip to content

Commit 0e46284

Browse files
committed
Make install modal global with state moved to zustand
1 parent 3ec21de commit 0e46284

File tree

8 files changed

+74
-79
lines changed

8 files changed

+74
-79
lines changed

src/frontend/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { ThemeProvider, createTheme } from '@mui/material/styles'
1919
import LogFileUploadDialog from './components/UI/LogFileUploadDialog'
2020
import UploadedLogFilesList from './screens/Settings/sections/LogSettings/components/UploadedLogFilesList'
2121
import { TourProvider } from './state/TourContext'
22+
import { InstallGameWrapper } from './screens/Library/components/InstallModal'
2223

2324
function Root() {
2425
const {
@@ -61,6 +62,7 @@ function Root() {
6162
type={isSettingsModalOpen.type}
6263
/>
6364
)}
65+
<InstallGameWrapper />
6466
<ExternalLinkDialog />
6567
<LogFileUploadDialog />
6668
<UploadedLogFilesList />

src/frontend/screens/Game/GamePage/index.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {
3939
import GamePicture from '../GamePicture'
4040
import TimeContainer from '../TimeContainer'
4141

42-
import { InstallModal } from 'frontend/screens/Library/components'
4342
import { install } from 'frontend/helpers/library'
4443
import { hasProgress } from 'frontend/hooks/hasProgress'
4544
import ErrorComponent from 'frontend/components/UI/ErrorComponent'
@@ -72,6 +71,7 @@ import { hasHelp } from 'frontend/hooks/hasHelp'
7271
import Genres from './components/Genres'
7372
import ReleaseDate from './components/ReleaseDate'
7473
import { hasKnownFixes } from 'frontend/hooks/hasKnownFixes'
74+
import { openInstallGameModal } from 'frontend/state/InstallGameModal'
7575

7676
export default React.memo(function GamePage(): JSX.Element | null {
7777
const { appName, runner } = useParams() as { appName: string; runner: Runner }
@@ -83,7 +83,6 @@ export default React.memo(function GamePage(): JSX.Element | null {
8383

8484
const { gameInfo: locationGameInfo } = location.state
8585

86-
const [showModal, setShowModal] = useState({ game: '', show: false })
8786
const [showUninstallModal, setShowUninstallModal] = useState(false)
8887
const [wikiInfo, setWikiInfo] = useState<WikiInfo | null>(null)
8988

@@ -263,7 +262,7 @@ export default React.memo(function GamePage(): JSX.Element | null {
263262
}
264263

265264
function handleModal() {
266-
setShowModal({ game: appName, show: true })
265+
openInstallGameModal({ appName, runner, gameInfo })
267266
}
268267

269268
let hasUpdate = false
@@ -374,14 +373,6 @@ export default React.memo(function GamePage(): JSX.Element | null {
374373
className="backgroundImage"
375374
/>
376375
)}
377-
{gameInfo.runner !== 'sideload' && showModal.show && (
378-
<InstallModal
379-
appName={showModal.game}
380-
runner={runner}
381-
backdropClick={() => setShowModal({ game: '', show: false })}
382-
gameInfo={gameInfo}
383-
/>
384-
)}
385376
{showUninstallModal && (
386377
<UninstallModal
387378
appName={appName}

src/frontend/screens/Game/GameSubMenu/index.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { useTranslation } from 'react-i18next'
99
import ContextProvider from 'frontend/state/ContextProvider'
1010
import { NavLink } from 'react-router-dom'
1111

12-
import { InstallModal } from 'frontend/screens/Library/components'
1312
import { CircularProgress } from '@mui/material'
1413
import UninstallModal from 'frontend/components/UI/UninstallModal'
1514
import GameContext from '../GameContext'
15+
import { openInstallGameModal } from 'frontend/state/InstallGameModal'
1616

1717
interface Props {
1818
appName: string
@@ -59,7 +59,6 @@ export default function GamesSubmenu({
5959
const [hasShortcuts, setHasShortcuts] = useState(false)
6060
const [eosOverlayEnabled, setEosOverlayEnabled] = useState<boolean>(false)
6161
const [eosOverlayRefresh, setEosOverlayRefresh] = useState<boolean>(false)
62-
const [showModal, setShowModal] = useState(false)
6362
const eosOverlayAppName = '98bc04bc842e4906993fd6d6644ffb8d'
6463
const [showUninstallModal, setShowUninstallModal] = useState(false)
6564
const [protonDBurl, setProtonDBurl] = useState(
@@ -147,7 +146,7 @@ export default function GamesSubmenu({
147146
}
148147

149148
function handleEdit() {
150-
setShowModal(true)
149+
openInstallGameModal({ appName, runner, gameInfo })
151150
}
152151

153152
async function handleEosOverlay() {
@@ -379,13 +378,6 @@ export default function GamesSubmenu({
379378
)}
380379
</div>
381380
</div>
382-
{showModal && (
383-
<InstallModal
384-
appName={appName}
385-
runner={runner}
386-
backdropClick={() => setShowModal(false)}
387-
/>
388-
)}
389381
</>
390382
)
391383
}

src/frontend/screens/Library/components/InstallModal/index.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ import WineSelector from './WineSelector'
2020
import { SelectField } from 'frontend/components/UI'
2121
import { useTranslation } from 'react-i18next'
2222
import ThirdPartyDialog from './ThirdPartyDialog'
23+
import {
24+
closeInstallGameModal,
25+
useInstallGameModal
26+
} from 'frontend/state/InstallGameModal'
2327

2428
type Props = {
2529
appName: string
26-
backdropClick: () => void
2730
runner: Runner
2831
gameInfo?: GameInfo | null
2932
}
@@ -35,12 +38,7 @@ export type AvailablePlatforms = {
3538
icon: IconDefinition
3639
}[]
3740

38-
export default React.memo(function InstallModal({
39-
appName,
40-
backdropClick,
41-
runner,
42-
gameInfo = null
43-
}: Props) {
41+
function InstallModal({ appName, runner, gameInfo = null }: Props) {
4442
const { platform } = useContext(ContextProvider)
4543
const { t } = useTranslation('gamepage')
4644

@@ -155,10 +153,12 @@ export default React.memo(function InstallModal({
155153
const showDownloadDialog = !isSideload && gameInfo
156154
const isThirdPartyManagedApp = gameInfo && !!gameInfo.thirdPartyManagedApp
157155

156+
const closeModal = () => closeInstallGameModal()
157+
158158
return (
159159
<div className="InstallModal">
160160
<Dialog
161-
onClose={backdropClick}
161+
onClose={closeModal}
162162
showCloseButton
163163
className="InstallModal__dialog"
164164
>
@@ -169,7 +169,7 @@ export default React.memo(function InstallModal({
169169
winePrefix={winePrefix}
170170
wineVersion={wineVersion}
171171
availablePlatforms={availablePlatforms}
172-
backdropClick={backdropClick}
172+
backdropClick={closeModal}
173173
platformToInstall={platformToInstall}
174174
gameInfo={gameInfo}
175175
crossoverBottle={crossoverBottle}
@@ -197,7 +197,7 @@ export default React.memo(function InstallModal({
197197
winePrefix={winePrefix}
198198
wineVersion={wineVersion}
199199
availablePlatforms={availablePlatforms}
200-
backdropClick={backdropClick}
200+
backdropClick={closeModal}
201201
platformToInstall={platformToInstall}
202202
gameInfo={gameInfo}
203203
crossoverBottle={crossoverBottle}
@@ -223,7 +223,7 @@ export default React.memo(function InstallModal({
223223
winePrefix={winePrefix}
224224
wineVersion={wineVersion}
225225
availablePlatforms={availablePlatforms}
226-
backdropClick={backdropClick}
226+
backdropClick={closeModal}
227227
platformToInstall={platformToInstall}
228228
appName={appName}
229229
crossoverBottle={crossoverBottle}
@@ -246,4 +246,20 @@ export default React.memo(function InstallModal({
246246
</Dialog>
247247
</div>
248248
)
249-
})
249+
}
250+
251+
export function InstallGameWrapper() {
252+
const installGameModalState = useInstallGameModal()
253+
254+
if (!installGameModalState.isOpen) {
255+
return <></>
256+
}
257+
258+
return (
259+
<InstallModal
260+
appName={installGameModalState.appName!}
261+
runner={installGameModalState.runner!}
262+
gameInfo={installGameModalState.gameInfo}
263+
/>
264+
)
265+
}

src/frontend/screens/Library/components/index.tsx

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/frontend/screens/Library/index.tsx

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,16 @@ import {
2727
sideloadedCategories
2828
} from 'frontend/helpers/library'
2929
import RecentlyPlayed from './components/RecentlyPlayed'
30-
import { InstallModal } from './components'
3130
import LibraryContext from './LibraryContext'
3231
import { Category, PlatformsFilters, StoresFilters } from 'frontend/types'
3332
import { hasHelp } from 'frontend/hooks/hasHelp'
3433
import EmptyLibraryMessage from './components/EmptyLibrary'
3534
import CategoriesManager from './components/CategoriesManager'
3635
import LibraryTour from './components/LibraryTour'
36+
import { openInstallGameModal } from 'frontend/state/InstallGameModal'
3737

3838
const storage = window.localStorage
3939

40-
type ModalState = {
41-
game: string
42-
show: boolean
43-
runner: Runner
44-
gameInfo: GameInfo | null
45-
}
46-
4740
export default React.memo(function Library(): JSX.Element {
4841
const { t } = useTranslation()
4942

@@ -181,12 +174,6 @@ export default React.memo(function Library(): JSX.Element {
181174

182175
const [showCategories, setShowCategories] = useState(false)
183176

184-
const [showModal, setShowModal] = useState<ModalState>({
185-
game: '',
186-
show: false,
187-
runner: 'legendary',
188-
gameInfo: null
189-
})
190177
const [sortDescending, setSortDescending] = useState(
191178
JSON.parse(storage?.getItem('sortDescending') || 'false')
192179
)
@@ -246,7 +233,7 @@ export default React.memo(function Library(): JSX.Element {
246233
runner: Runner,
247234
gameInfo: GameInfo | null
248235
) {
249-
setShowModal({ game: appName, show: true, runner, gameInfo })
236+
openInstallGameModal({ appName, runner, gameInfo })
250237
}
251238

252239
// cache list of games being installed
@@ -666,22 +653,6 @@ export default React.memo(function Library(): JSX.Element {
666653
<ArrowDropUp id="backToTopArrow" className="material-icons" />
667654
</button>
668655

669-
{showModal.show && (
670-
<InstallModal
671-
appName={showModal.game}
672-
runner={showModal.runner}
673-
gameInfo={showModal.gameInfo}
674-
backdropClick={() =>
675-
setShowModal({
676-
game: '',
677-
show: false,
678-
runner: 'legendary',
679-
gameInfo: null
680-
})
681-
}
682-
/>
683-
)}
684-
685656
{showCategories && <CategoriesManager />}
686657
</LibraryContext.Provider>
687658
)

src/frontend/state/GlobalState.tsx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { getGameInfo, getLegendaryConfig, notify } from '../helpers'
2323
import { i18n, t, TFunction } from 'i18next'
2424

2525
import ContextProvider from './ContextProvider'
26-
import { InstallModal } from 'frontend/screens/Library/components'
2726

2827
import {
2928
configStore,
@@ -964,7 +963,6 @@ class GlobalState extends PureComponent<Props> {
964963

965964
render() {
966965
const {
967-
showInstallModal,
968966
language,
969967
epic,
970968
gog,
@@ -1060,18 +1058,6 @@ class GlobalState extends PureComponent<Props> {
10601058
}}
10611059
>
10621060
{this.props.children}
1063-
{showInstallModal.show && (
1064-
<InstallModal
1065-
appName={showInstallModal.appName}
1066-
runner={showInstallModal.runner}
1067-
gameInfo={showInstallModal.gameInfo}
1068-
backdropClick={() =>
1069-
this.setState({
1070-
showInstallModal: { ...showInstallModal, show: false }
1071-
})
1072-
}
1073-
/>
1074-
)}
10751061
</ContextProvider.Provider>
10761062
)
10771063
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { GameInfo, Runner } from 'common/types'
2+
import { create } from 'zustand'
3+
4+
interface InstallGameModalState {
5+
isOpen: boolean
6+
appName?: string
7+
runner?: Runner
8+
gameInfo: GameInfo | null
9+
}
10+
11+
export const useInstallGameModal = create<InstallGameModalState>()(() => ({
12+
isOpen: false,
13+
gameInfo: null
14+
}))
15+
16+
interface OpenInstallGameModalParams {
17+
appName: string
18+
runner: Runner
19+
gameInfo: GameInfo | null
20+
}
21+
export const openInstallGameModal = ({
22+
appName,
23+
runner,
24+
gameInfo
25+
}: OpenInstallGameModalParams) => {
26+
useInstallGameModal.setState({
27+
isOpen: true,
28+
appName,
29+
runner,
30+
gameInfo
31+
})
32+
}
33+
34+
export const closeInstallGameModal = () => {
35+
useInstallGameModal.setState({
36+
isOpen: false
37+
})
38+
}

0 commit comments

Comments
 (0)