Skip to content

Commit

Permalink
[UX] Remember last url opened in the webview, reopen there (#3149)
Browse files Browse the repository at this point in the history
  • Loading branch information
arielj authored Oct 27, 2023
1 parent 75f79d1 commit 41595f3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/frontend/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ function App() {
<Route path="/" element={<Navigate replace to="/library" />} />
<Route path="/library" element={<Library />} />
<Route path="login" element={<Login />} />
<Route path="epicstore" element={<WebView />} />
<Route path="gogstore" element={<WebView />} />
<Route path="amazonstore" element={<WebView />} />
<Route path="epicstore" element={<WebView store="epic" />} />
<Route path="gogstore" element={<WebView store="gog" />} />
<Route path="amazonstore" element={<WebView store="amazon" />} />
<Route path="wiki" element={<WebView />} />
<Route path="/gamepage">
<Route path=":runner">
<Route path=":appName" element={<GamePage />} />
</Route>
</Route>
<Route path="/store-page" element={<WebView />} />
<Route path="/last-url" element={<WebView />} />
<Route path="loginweb">
<Route path=":runner" element={<WebView />} />
</Route>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export default function SidebarLinks() {
handleExternalLinkDialog
} = useContext(ContextProvider)

const isStore = location.pathname.includes('store')
const inWebviewScreen =
location.pathname.includes('store') ||
location.pathname.includes('last-url')
const isSettings = location.pathname.includes('settings')
const isWin = platform === 'win32'

Expand Down Expand Up @@ -90,6 +92,12 @@ export default function SidebarLinks() {
defaultStore = '/gogstore'
}

// if we have a stored last-url, default to the `/last-url` route
const lastStore = localStorage.getItem('last-store')
if (lastStore) {
defaultStore = lastStore
}

return (
<div className="SidebarLinks Sidebar__section">
{!loggedIn && (
Expand Down Expand Up @@ -142,7 +150,7 @@ export default function SidebarLinks() {
<span>{t('stores', 'Stores')}</span>
</>
</NavLink>
{isStore && (
{inWebviewScreen && (
<div className="SidebarSubmenu">
<NavLink
data-testid="store"
Expand Down
35 changes: 34 additions & 1 deletion src/frontend/screens/WebView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import './index.css'
import LoginWarning from '../Login/components/LoginWarning'
import { NileLoginData } from 'common/types/nile'

export default function WebView() {
interface Props {
store?: 'epic' | 'gog' | 'amazon'
}

export default function WebView({ store }: Props) {
const { i18n } = useTranslation()
const { pathname, search } = useLocation()
const { t } = useTranslation()
Expand Down Expand Up @@ -66,6 +70,14 @@ export default function WebView() {
}
let startUrl = urls[pathname]

if (store) {
localStorage.setItem('last-store', `/${store}store`)
const lastUrl = localStorage.getItem(`last-url-${store}`)
if (lastUrl) {
startUrl = lastUrl
}
}

if (pathname.match(/store-page/)) {
const searchParams = new URLSearchParams(search)
const queryParam = searchParams.get('store-url')
Expand Down Expand Up @@ -212,6 +224,27 @@ export default function WebView() {
return
}, [webviewRef.current, preloadPath, amazonLoginData])

useEffect(() => {
const webview = webviewRef.current
if (webview && store) {
const onNavigate = () => {
localStorage.setItem(`last-url-${store}`, webview.getURL())
}

// this one is needed for gog/amazon
webview.addEventListener('did-navigate', onNavigate)
// this one is needed for epic
webview.addEventListener('did-navigate-in-page', onNavigate)

return () => {
webview.removeEventListener('did-navigate', onNavigate)
webview.removeEventListener('did-navigate-in-page', onNavigate)
}
}

return
}, [webviewRef.current, store])

const [showLoginWarningFor, setShowLoginWarningFor] = useState<
null | 'epic' | 'gog' | 'amazon'
>(null)
Expand Down

0 comments on commit 41595f3

Please sign in to comment.