Skip to content

Commit 0c8a5c1

Browse files
authored
[General] Open game store page in webview and refactor (#1149)
* Open game store page in webview and refactor * Highlight correct sidebar store when showing game store page * Remove debug code
1 parent b6ae736 commit 0c8a5c1

File tree

7 files changed

+56
-48
lines changed

7 files changed

+56
-48
lines changed

electron/legendary/library.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import {
1818
} from '../types'
1919
import { LegendaryGame } from './games'
2020
import { LegendaryUser } from './user'
21-
import { getLegendaryBin, isEpicServiceOffline, isOnline } from '../utils'
21+
import {
22+
formatEpicStoreUrl,
23+
getLegendaryBin,
24+
isEpicServiceOffline,
25+
isOnline
26+
} from '../utils'
2227
import {
2328
fallBackImage,
2429
installed,
@@ -483,7 +488,8 @@ export class LegendaryLibrary {
483488
title,
484489
canRunOffline,
485490
is_linux_native: false,
486-
runner: 'legendary'
491+
runner: 'legendary',
492+
store_url: formatEpicStoreUrl(title)
487493
} as GameInfo)
488494

489495
return app_name

electron/utils.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,21 @@ function getGOGdlBin(): { dir: string; bin: string } {
405405
)
406406
}
407407

408+
const specialCharactersRegex =
409+
/('\w)|(\\(\w|\d){5})|(\\"(\\.|[^"])*")|[^((0-9)|(a-z)|(A-Z)|\s)]/g // addeed regex for capturings "'s" + unicodes + remove subtitles in quotes
410+
const cleanTitle = (title: string) =>
411+
title
412+
.replaceAll(specialCharactersRegex, '')
413+
.replaceAll(' ', '-')
414+
.replaceAll('®', '')
415+
.toLowerCase()
416+
.split('--definitive')[0]
417+
418+
const formatEpicStoreUrl = (title: string) => {
419+
const storeUrl = `https://www.epicgames.com/store/product/`
420+
return `${storeUrl}${cleanTitle(title)}`
421+
}
422+
408423
export {
409424
checkForUpdates,
410425
errorHandler,
@@ -421,5 +436,6 @@ export {
421436
clearCache,
422437
resetHeroic,
423438
getLegendaryBin,
424-
getGOGdlBin
439+
getGOGdlBin,
440+
formatEpicStoreUrl
425441
}

src/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function App() {
6161
<Route exact path="/gogstore" component={WebView} />
6262
<Route exact path="/wiki" component={WebView} />
6363
<Route exact path="/gameconfig/:appName" component={GamePage} />
64+
<Route path="/store-page" component={WebView} />
6465
<Route path="/login/:runner" component={WebView} />
6566
<Route path="/settings/:appName/:type" component={Settings} />
6667
<Route path="/wine-manager" component={WineManager} />

src/components/UI/Sidebar/components/SidebarLinks/index.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ export default function SidebarLinks() {
150150
data-testid="store"
151151
className="Sidebar__item SidebarLinks__subItem"
152152
isActive={(match, location) =>
153-
location.pathname.includes('epicstore')
153+
location.pathname.includes('epicstore') ||
154+
(location.pathname === '/store-page' &&
155+
location.search.includes('epicgames.com/store'))
154156
}
155157
to={{ pathname: '/epicstore' }}
156158
>
@@ -160,7 +162,9 @@ export default function SidebarLinks() {
160162
data-testid="store"
161163
className="Sidebar__item SidebarLinks__subItem"
162164
isActive={(match, location) =>
163-
location.pathname.includes('gogstore')
165+
location.pathname.includes('gogstore') ||
166+
(location.pathname === '/store-page' &&
167+
location.search.includes('gog.com/en/game'))
164168
}
165169
to={{ pathname: '/gogstore' }}
166170
>

src/helpers/index.ts

-16
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ const getLegendaryConfig = async (): Promise<{
7474
return { library, user }
7575
}
7676

77-
const specialCharactersRegex =
78-
/('\w)|(\\(\w|\d){5})|(\\"(\\.|[^"])*")|[^((0-9)|(a-z)|(A-Z)|\s)]/g // addeed regex for capturings "'s" + unicodes + remove subtitles in quotes
79-
const cleanTitle = (title: string) =>
80-
title
81-
.replaceAll(specialCharactersRegex, '')
82-
.replaceAll(' ', '-')
83-
.replaceAll('®', '')
84-
.toLowerCase()
85-
.split('--definitive')[0]
86-
8777
const getGameInfo = async (
8878
appName: string,
8979
runner: Runner = 'legendary'
@@ -114,11 +104,6 @@ const handleSavePath = async (game: string) => {
114104
const createNewWindow = (url: string) =>
115105
ipcRenderer.send('createNewWindow', url)
116106

117-
const formatStoreUrl = (title: string, lang: string) => {
118-
const storeUrl = `https://www.epicgames.com/store/${lang}/product/`
119-
return `${storeUrl}${cleanTitle(title)}`
120-
}
121-
122107
function getProgress(progress: InstallProgress): number {
123108
if (progress && progress.percent) {
124109
return Number(progress.percent.replace('%', ''))
@@ -226,7 +211,6 @@ function getAppSettings(): Promise<AppSettings> {
226211
export {
227212
createNewWindow,
228213
fixSaveFolder,
229-
formatStoreUrl,
230214
getGameInfo,
231215
getGameSettings,
232216
getInstallInfo,

src/screens/Game/GameSubMenu/index.tsx

+6-16
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import React, { useContext, useEffect, useState } from 'react'
55
import { AppSettings, Runner } from 'src/types'
66
import { IpcRenderer } from 'electron'
77
import { SmallInfo } from 'src/components/UI'
8-
import {
9-
createNewWindow,
10-
formatStoreUrl,
11-
getGameInfo,
12-
repair
13-
} from 'src/helpers'
8+
import { createNewWindow, getGameInfo, repair } from 'src/helpers'
149
import { useTranslation } from 'react-i18next'
1510
import ContextProvider from 'src/state/ContextProvider'
1611
import { uninstall } from 'src/helpers/library'
@@ -46,11 +41,7 @@ export default function GamesSubmenu({
4641
const isLinux = platform === 'linux'
4742
const [info, setInfo] = useState({ prefix: '', wine: '' } as otherInfo)
4843
const [isNative, setIsNative] = useState(false)
49-
const { t, i18n } = useTranslation('gamepage')
50-
let lang = i18n.language
51-
if (i18n.language === 'pt') {
52-
lang = 'pt-BR'
53-
}
44+
const { t } = useTranslation('gamepage')
5445

5546
const protonDBurl = `https://www.protondb.com/search?q=${title}`
5647

@@ -193,14 +184,13 @@ export default function GamesSubmenu({
193184
)}
194185
</>
195186
)}
196-
<button
197-
onClick={() =>
198-
createNewWindow(storeUrl || formatStoreUrl(title, lang))
199-
}
187+
<NavLink
200188
className="link button is-text is-link"
189+
exact
190+
to={`/store-page?store-url=${storeUrl}`}
201191
>
202192
{t('submenu.store')}
203-
</button>
193+
</NavLink>
204194
{!isWin && (
205195
<button
206196
onClick={() => createNewWindow(protonDBurl)}

src/screens/WebView/index.tsx

+18-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type SID = {
1616

1717
export default function WebView() {
1818
const { i18n } = useTranslation()
19-
const { pathname } = useLocation()
19+
const { pathname, search } = useLocation()
2020
const { t } = useTranslation()
2121
const { handleFilter, handleCategory } = useContext(ContextProvider)
2222
const [loading, setLoading] = useState<{
@@ -34,7 +34,7 @@ export default function WebView() {
3434
lang = 'pt-BR'
3535
}
3636

37-
const loginUrl =
37+
const epicLoginUrl =
3838
'https://www.epicgames.com/id/login?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Fid%2Fapi%2Fredirect'
3939
const epicStore = `https://www.epicgames.com/store/${lang}/`
4040
const gogStore = `https://gog.com`
@@ -46,17 +46,24 @@ export default function WebView() {
4646

4747
const trueAsStr = 'true' as unknown as boolean | undefined
4848
const { runner } = useParams() as { runner: Runner }
49-
const startUrl = runner
50-
? runner == 'legendary'
51-
? '/loginEpic'
52-
: '/loginGOG'
53-
: pathname
49+
5450
const urls = {
5551
'/epicstore': epicStore,
5652
'/gogstore': gogStore,
5753
'/wiki': wikiURL,
58-
'/loginEpic': loginUrl,
59-
'/loginGOG': gogLoginUrl
54+
'/loginEpic': epicLoginUrl,
55+
'/loginGOG': gogLoginUrl,
56+
'/login/legandary': epicLoginUrl,
57+
'/login/gog': gogLoginUrl
58+
}
59+
let startUrl = urls[pathname]
60+
61+
if (pathname.match(/store-page/)) {
62+
const searchParams = new URLSearchParams(search)
63+
const queryParam = searchParams.get('store-url')
64+
if (queryParam) {
65+
startUrl = queryParam
66+
}
6067
}
6168

6269
useLayoutEffect(() => {
@@ -131,15 +138,15 @@ export default function WebView() {
131138
<div className="WebView">
132139
<WebviewControls
133140
webview={webviewRef.current}
134-
initURL={urls[startUrl]}
141+
initURL={startUrl}
135142
openInBrowser={!startUrl.startsWith('/login')}
136143
/>
137144
{loading.refresh && <UpdateComponent message={loading.message} />}
138145
<webview
139146
ref={webviewRef}
140147
className="WebView__webview"
141148
partition="persist:epicstore"
142-
src={urls[startUrl]}
149+
src={startUrl}
143150
allowpopups={trueAsStr}
144151
/>
145152
</div>

0 commit comments

Comments
 (0)