Skip to content

Commit

Permalink
Migrate libraryTopSection
Browse files Browse the repository at this point in the history
  • Loading branch information
CommandMC committed Jun 18, 2024
1 parent 572c93f commit a540355
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 25 deletions.
8 changes: 5 additions & 3 deletions src/frontend/screens/Library/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import { Category, PlatformsFilters, StoresFilters } from 'frontend/types'
import { hasHelp } from 'frontend/hooks/hasHelp'
import EmptyLibraryMessage from './components/EmptyLibrary'
import CategoriesManager from './components/CategoriesManager'
import { useGlobalState } from '../../state/GlobalStateV2'
import {
useGlobalState,
useShallowGlobalState
} from 'frontend/state/GlobalStateV2'
import { useShallow } from 'zustand/react/shallow'

const storage = window.localStorage
Expand All @@ -56,12 +59,11 @@ export default React.memo(function Library(): JSX.Element {
amazon,
sideloadedLibrary,
favouriteGames,
libraryTopSection,

currentCustomCategories,
customCategories,
hiddenGames
} = useContext(ContextProvider)
const { libraryTopSection } = useShallowGlobalState('libraryTopSection')

hasHelp(
'library',
Expand Down
12 changes: 5 additions & 7 deletions src/frontend/screens/Settings/components/LibraryTopSection.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import React, { ChangeEvent, useContext } from 'react'
import React, { ChangeEvent } from 'react'
import { useTranslation } from 'react-i18next'
import { SelectField } from 'frontend/components/UI'
import useSetting from 'frontend/hooks/useSetting'
import ContextProvider from 'frontend/state/ContextProvider'
import { LibraryTopSectionOptions } from 'common/types'
import { useGlobalState } from 'frontend/state/GlobalStateV2'

const LibraryTopSection = () => {
const { t } = useTranslation()
const { handleLibraryTopSection } = useContext(ContextProvider)
const [libraryTopSection, setLibraryTopSection] = useSetting(
'libraryTopSection',
'disabled'
)

const onSectionChange = (event: ChangeEvent) => {
const newValue = (event.target as HTMLSelectElement)
.value as LibraryTopSectionOptions
handleLibraryTopSection(newValue)
const onSectionChange = (event: ChangeEvent<HTMLSelectElement>) => {
const newValue = event.target.value as LibraryTopSectionOptions
useGlobalState.setState({ libraryTopSection: newValue })
setLibraryTopSection(newValue)
}

Expand Down
2 changes: 0 additions & 2 deletions src/frontend/state/ContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const initialContext: ContextType = {
logout: async () => Promise.resolve()
},
sideloadedLibrary: [],
libraryTopSection: 'disabled',
handleLibraryTopSection: () => null,
refresh: async () => Promise.resolve(),
refreshLibrary: async () => Promise.resolve(),
refreshWineVersionInfo: async () => Promise.resolve(),
Expand Down
10 changes: 1 addition & 9 deletions src/frontend/state/GlobalState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
GameInfo,
HiddenGame,
RefreshOptions,
Runner,
LibraryTopSectionOptions
Runner
} from 'common/types'
import { DialogModalOptions } from 'frontend/types'
import { withTranslation } from 'react-i18next'
Expand Down Expand Up @@ -56,7 +55,6 @@ interface StateProps {
user_id?: string
username?: string
}
libraryTopSection: string
refreshing: boolean
refreshingInTheBackground: boolean
hiddenGames: HiddenGame[]
Expand Down Expand Up @@ -134,7 +132,6 @@ class GlobalState extends PureComponent<Props> {
user_id: nileConfigStore.get_nodefault('userData.user_id'),
username: nileConfigStore.get_nodefault('userData.name')
},
libraryTopSection: globalSettings?.libraryTopSection || 'disabled',
refreshing: false,
refreshingInTheBackground: true,
hiddenGames: configStore.get('games.hidden', []),
Expand Down Expand Up @@ -384,10 +381,6 @@ class GlobalState extends PureComponent<Props> {
})
}).bind(this)

handleLibraryTopSection = (value: LibraryTopSectionOptions) => {
this.setState({ libraryTopSection: value })
}

handleSuccessfulLogin = (runner: Runner) => {
storage.setItem('category', 'all')
this.refreshLibrary({
Expand Down Expand Up @@ -787,7 +780,6 @@ class GlobalState extends PureComponent<Props> {
removeCategory: this.removeCustomCategory,
renameCategory: this.renameCustomCategory
},
handleLibraryTopSection: this.handleLibraryTopSection,
setTheme: this.setTheme,
setZoomPercent: this.setZoomPercent,
setAllTilesInColor: this.setAllTilesInColor,
Expand Down
10 changes: 9 additions & 1 deletion src/frontend/state/GlobalStateV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
ExperimentalFeatures,
GameInfo,
GameStatus,
LibraryTopSectionOptions,
Runner
} from 'common/types'

Expand Down Expand Up @@ -43,6 +44,8 @@ interface GlobalStateV2 extends ExperimentalFeatures {
libraryStatus: Record<`${string}_${Runner}`, GameStatus>

externalLinkDialogOptions: ExternalLinkDialogOptions

libraryTopSection: LibraryTopSectionOptions
}

const useGlobalState = create<GlobalStateV2>()(
Expand Down Expand Up @@ -103,7 +106,9 @@ const useGlobalState = create<GlobalStateV2>()(
enableHelp: false,
automaticWinetricksFixes: true,

externalLinkDialogOptions: { showDialog: false }
externalLinkDialogOptions: { showDialog: false },

libraryTopSection: 'disabled'
}),
{
name: 'globalState',
Expand Down Expand Up @@ -196,6 +201,9 @@ window.api.requestAppSettings().then((settings) => {
useGlobalState.setState({
...settings.experimentalFeatures
})

if (settings.libraryTopSection)
useGlobalState.setState({ libraryTopSection: settings.libraryTopSection })
})

export { useGlobalState, useShallowGlobalState }
3 changes: 0 additions & 3 deletions src/frontend/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ConnectivityStatus,
DialogType,
ButtonOptions,
LibraryTopSectionOptions,
DMQueueElement,
DownloadManagerState,
GameSettings,
Expand All @@ -19,8 +18,6 @@ import { NileLoginData, NileRegisterData } from 'common/types/nile'
export type Category = 'all' | 'legendary' | 'gog' | 'sideload' | 'nile'

export interface ContextType {
libraryTopSection: string
handleLibraryTopSection: (value: LibraryTopSectionOptions) => void
refresh: (library: Runner, checkUpdates?: boolean) => Promise<void>
refreshLibrary: (options: RefreshOptions) => Promise<void>
refreshWineVersionInfo: (fetch: boolean) => void
Expand Down

0 comments on commit a540355

Please sign in to comment.