Skip to content

Commit

Permalink
Migrate refresh, refreshLibrary, epic, gog, amazon, and `si…
Browse files Browse the repository at this point in the history
…deloadedLibrary`

Quite a big commit, but splitting this up further isn't possible
  • Loading branch information
CommandMC committed Jun 23, 2024
1 parent 50e7602 commit 514b3e7
Show file tree
Hide file tree
Showing 25 changed files with 436 additions and 664 deletions.
7 changes: 0 additions & 7 deletions src/common/types.ts
Original file line number Diff line number Diff line change
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
7 changes: 4 additions & 3 deletions src/frontend/components/UI/ActionIcons/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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'
Expand All @@ -20,8 +19,10 @@ import { useShallowGlobalState } from 'frontend/state/GlobalStateV2'

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

const {
handleLayout,
Expand Down
9 changes: 5 additions & 4 deletions src/frontend/components/UI/ErrorComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import React, { useContext } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import { faHeartCrack, faSyncAlt } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'

import { CleaningServicesOutlined, DeleteOutline } from '@mui/icons-material'
import './index.css'
import ContextProvider from 'frontend/state/ContextProvider'
import { useShallowGlobalState } from 'frontend/state/GlobalStateV2'

export default function ErrorComponent({ message }: { message: string }) {
const { t } = useTranslation()
const { refreshLibrary } = useContext(ContextProvider)
const { showResetDialog } = useShallowGlobalState('showResetDialog')
const { showResetDialog, refreshLibrary } = useShallowGlobalState(
'showResetDialog',
'refreshLibrary'
)

return (
<div className="errorComponent">
Expand Down
14 changes: 9 additions & 5 deletions src/frontend/components/UI/LibraryFilters/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ToggleSwitch from '../ToggleSwitch'
import { useTranslation } from 'react-i18next'
import LibraryContext from 'frontend/screens/Library/LibraryContext'
import { Category } from 'frontend/types'
import ContextProvider from 'frontend/state/ContextProvider'
import { useShallowGlobalState } from 'frontend/state/GlobalStateV2'
import './index.css'

const RunnerToStore = {
Expand All @@ -15,7 +15,11 @@ const RunnerToStore = {

export default function LibraryFilters() {
const { t } = useTranslation()
const { epic, gog, amazon } = useContext(ContextProvider)
const { epicUsername, gogUsername, amazonUserId } = useShallowGlobalState(
'epicUsername',
'gogUsername',
'amazonUserId'
)
const {
setShowFavourites,
setShowHidden,
Expand Down Expand Up @@ -163,9 +167,9 @@ export default function LibraryFilters() {
<div className="libraryFilters">
<button className="selectStyle">{t('header.filters', 'Filters')}</button>
<div className="dropdown">
{epic.username && storeToggle('legendary')}
{gog.username && storeToggle('gog')}
{amazon.user_id && storeToggle('nile')}
{epicUsername && storeToggle('legendary')}
{gogUsername && storeToggle('gog')}
{amazonUserId && storeToggle('nile')}
{storeToggle('sideload')}

<hr />
Expand Down
20 changes: 13 additions & 7 deletions src/frontend/components/UI/LibrarySearchBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useContext, useMemo } from 'react'
import { useNavigate } from 'react-router-dom'
import ContextProvider from 'frontend/state/ContextProvider'
import { GameInfo } from '../../../../common/types'
import SearchBar from '../SearchBar'
import { useTranslation } from 'react-i18next'
import LibraryContext from 'frontend/screens/Library/LibraryContext'
import { useShallowGlobalState } from 'frontend/state/GlobalStateV2'

function fixFilter(text: string) {
const regex = new RegExp(/([?\\|*|+|(|)|[|]|])+/, 'g')
Expand All @@ -18,17 +18,23 @@ const RUNNER_TO_STORE = {
}

export default function LibrarySearchBar() {
const { epic, gog, sideloadedLibrary, amazon } = useContext(ContextProvider)
const { epicLibrary, gogLibrary, amazonLibrary, sideloadedLibrary } =
useShallowGlobalState(
'epicLibrary',
'gogLibrary',
'amazonLibrary',
'sideloadedLibrary'
)
const { handleSearch, filterText } = useContext(LibraryContext)
const navigate = useNavigate()
const { t } = useTranslation()

const list = useMemo(() => {
return [
...(epic.library ?? []),
...(gog.library ?? []),
...(sideloadedLibrary ?? []),
...(amazon.library ?? [])
...epicLibrary,
...Object.values(gogLibrary),
...amazonLibrary,
...sideloadedLibrary
]
.filter(Boolean)
.filter((el) => {
Expand All @@ -38,7 +44,7 @@ export default function LibrarySearchBar() {
)
})
.sort((g1, g2) => (g1.title < g2.title ? -1 : 1))
}, [amazon.library, epic.library, gog.library, filterText])
}, [epicLibrary, gogLibrary, amazonLibrary, sideloadedLibrary, filterText])

const handleClick = (game: GameInfo) => {
handleSearch('')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ import {
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { NavLink, useLocation } from 'react-router-dom'
import classNames from 'classnames'
import React, { useContext } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import { faDiscord, faPatreon } from '@fortawesome/free-brands-svg-icons'
import { openDiscordLink } from 'frontend/helpers'

import ContextProvider from 'frontend/state/ContextProvider'
import { Runner } from 'common/types'
import './index.css'
import QuitButton from '../QuitButton'
import { LocationState } from 'frontend/types'
import { SHOW_EXTERNAL_LINK_DIALOG_STORAGE_KEY } from 'frontend/components/UI/ExternalLinkDialog'
import { useGlobalState } from 'frontend/state/GlobalStateV2'
import {
useGlobalState,
useShallowGlobalState
} from 'frontend/state/GlobalStateV2'

type PathSplit = [
a: undefined,
Expand All @@ -40,7 +42,23 @@ export default function SidebarLinks() {
const location = useLocation() as { pathname: string }
const [, , runner, appName, type] = location.pathname.split('/') as PathSplit

const { amazon, epic, gog, refreshLibrary } = useContext(ContextProvider)
const {
refreshLibrary,
epicLibrary,
epicUsername,
gogLibrary,
gogUsername,
amazonLibrary,
amazonUserId
} = useShallowGlobalState(
'refreshLibrary',
'epicLibrary',
'epicUsername',
'gogLibrary',
'gogUsername',
'amazonLibrary',
'amazonUserId'
)

const inWebviewScreen =
location.pathname.includes('store') ||
Expand All @@ -50,15 +68,15 @@ export default function SidebarLinks() {

const settingsPath = '/settings/app/default/general'

const loggedIn = epic.username || gog.username || amazon.user_id
const loggedIn = epicUsername || gogUsername || amazonUserId

async function handleRefresh() {
localStorage.setItem('scrollPosition', '0')

const shouldRefresh =
(epic.username && !epic.library.length) ||
(gog.username && !gog.library.length) ||
(amazon.user_id && !amazon.library.length)
(epicUsername && !epicLibrary.length) ||
(gogUsername && !Object.keys(gogLibrary).length) ||
(amazonUserId && !amazonLibrary.length)
if (shouldRefresh) {
return refreshLibrary({ runInBackground: true })
}
Expand All @@ -80,10 +98,10 @@ export default function SidebarLinks() {

// By default, open Epic Store
let defaultStore = 'epic'
if (!epic.username && !gog.username && amazon.user_id) {
if (!epicUsername && !gogUsername && amazonUserId) {
// If only logged in to Amazon Games, open Amazon Gaming
defaultStore = 'amazon'
} else if (!epic.username && gog.username) {
} else if (!epicUsername && gogUsername) {
// Otherwise, if not logged in to Epic Games, open GOG Store
defaultStore = 'gog'
}
Expand Down
18 changes: 0 additions & 18 deletions src/frontend/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
GameInfo,
InstallProgress,
Runner,
GameSettings,
Expand All @@ -9,7 +8,6 @@ import {

import { install, launch, repair, updateGame } from './library'
import * as fileSize from 'filesize'
const readFile = window.api.readConfig

const writeConfig = window.api.writeConfig

Expand Down Expand Up @@ -40,21 +38,6 @@ const syncSaves = async (
return response
}

const getLegendaryConfig = async (): Promise<{
library: GameInfo[]
user: string
}> => {
// TODO: I'd say we should refactor this to be two different IPC calls, makes type annotations easier
const library: GameInfo[] = (await readFile('library')) as GameInfo[]
const user: string = (await readFile('user')) as string

if (!user) {
return { library: [], user: '' }
}

return { library, user }
}

const getGameInfo = async (appName: string, runner: Runner) => {
return window.api.getGameInfo(appName, runner)
}
Expand Down Expand Up @@ -157,7 +140,6 @@ export {
getGameInfo,
getGameSettings,
getInstallInfo,
getLegendaryConfig,
getProgress,
handleQuit,
install,
Expand Down
13 changes: 2 additions & 11 deletions src/frontend/hooks/hasStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useContext, useEffect, useState } from 'react'
import ContextProvider from 'frontend/state/ContextProvider'
import { useEffect, useState } from 'react'
import { GameInfo, Status } from 'common/types'
import { hasProgress } from './hasProgress'
import { useTranslation } from 'react-i18next'
Expand All @@ -12,7 +11,6 @@ export function hasStatus(
gameInfo: GameInfo,
gameSize?: string
) {
const { epic, gog } = useContext(ContextProvider)
const thisStatus = useGlobalState(
useShallow((state) => state.libraryStatus[`${appName}_${gameInfo.runner}`])
)
Expand Down Expand Up @@ -89,14 +87,7 @@ export function hasStatus(
return setGameStatus({ status: 'notInstalled', label, statusContext })
}
checkGameStatus()
}, [
thisStatus,
appName,
epic.library,
gog.library,
is_installed,
progress.percent
])
}, [thisStatus, appName, is_installed, progress.percent])

return gameStatus
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './index.css'

import React, { useContext, useEffect, useState } from 'react'
import React, { useEffect, useState } from 'react'

import { DMQueueElement, DownloadManagerState } from 'common/types'
import StopIcon from 'frontend/assets/stop-icon.svg?react'
Expand All @@ -9,7 +9,6 @@ import { handleStopInstallation } from 'frontend/helpers/library'
import { getGameInfo, getStoreName } from 'frontend/helpers'
import { useTranslation } from 'react-i18next'
import { hasProgress } from 'frontend/hooks/hasProgress'
import ContextProvider from 'frontend/state/ContextProvider'
import { useNavigate } from 'react-router-dom'
import PlayIcon from 'frontend/assets/play-icon.svg?react'
import PauseIcon from 'frontend/assets/pause-icon.svg?react'
Expand Down Expand Up @@ -42,7 +41,6 @@ const DownloadManagerItem = ({
state,
handleClearItem
}: Props) => {
const { amazon, epic, gog } = useContext(ContextProvider)
const { t } = useTranslation('gamepage')
const { t: t2 } = useTranslation('translation')
const isPaused = state && ['idle', 'paused'].includes(state)
Expand All @@ -57,8 +55,6 @@ const DownloadManagerItem = ({
)
}

const library = [...epic.library, ...gog.library, ...amazon.library]

const { params, addToQueueTime, endTime, type, startTime } = element
const {
appName,
Expand All @@ -84,7 +80,8 @@ const DownloadManagerItem = ({
const {
art_cover,
art_square,
install: { is_dlc }
install: { is_dlc },
title
} = gameInfo || {}

const [progress] = hasProgress(appName)
Expand Down Expand Up @@ -200,16 +197,6 @@ const DownloadManagerItem = ({

return current ? 'var(--text-default)' : 'var(--accent)'
}

const currentApp = library.find(
(val) => val.app_name === appName && val.runner === runner
)

if (!currentApp) {
return null
}

const { title } = currentApp
const cover = art_cover || art_square

const translatedTypes = {
Expand Down
Loading

0 comments on commit 514b3e7

Please sign in to comment.