Skip to content

Commit d3c313e

Browse files
authored
Update parent url when page changes (#26)
* update parent URL on each new page * remove Link component as we send an update on each page load, we can use <a> elements or cahnge window.location directly.
1 parent b831fc3 commit d3c313e

File tree

5 files changed

+14
-34
lines changed

5 files changed

+14
-34
lines changed

src/components/App/App.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ export default function App() {
7878
return <div>Could not load a data source. You have to pass a valid source in the url, eg: <a href={defaultUrl}>{defaultUrl}</a>.</div>
7979
}
8080

81+
/* Send a message to the parent window to synchronize the query string
82+
*
83+
* Hugging Face Space impose some restrictions to the static apps that are hosted on their platform,
84+
* with respect to the URLs. Only hash and query strings can be changed, and doing so requires
85+
* some custom code:
86+
* https://huggingface.co/docs/hub/spaces-handle-url-parameters
87+
*
88+
* Note that the iframe has no access to the parent window's location, so
89+
* it might already by in sync, we just don't know.
90+
*/
91+
window.parent.postMessage({ queryString: window.location.search }, 'https://huggingface.co')
92+
8193
return <ConfigProvider value={config}>
8294
<Page source={source} navigation={{ row, col }} />
8395
</ConfigProvider>

src/components/Home/Home.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { OAuthResult } from '@huggingface/hub'
22
import { FormEvent, useEffect } from 'react'
33
import HFLoginIcon from '../../assets/sign-in-with-huggingface-lg.svg'
44
import { login, logout } from '../../lib/auth.js'
5-
import { changeQueryString } from '../../lib/huggingfaceSource.js'
65
import Search from '../Search/Search.js'
76
import styles from './Home.module.css'
87

@@ -17,7 +16,7 @@ export default function Home({ auth }: { auth: OAuthResult | undefined }) {
1716
function onUrlSubmit(event: FormEvent<HTMLFormElement>) {
1817
event.preventDefault()
1918
const url = new FormData(event.currentTarget).get('url') as string
20-
changeQueryString(`?url=${url}`)
19+
window.location.href = `/?url=${url}`
2120
}
2221

2322
// pre-dismiss the welcome popup since user landed on the home page

src/components/Link/Link.tsx

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/components/Search/Search.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { DatasetEntry, listDatasets } from '@huggingface/hub'
22
import { ChangeEvent, useEffect, useState } from 'react'
33
import { baseUrl } from '../../lib/huggingfaceSource.js'
4-
import Link from '../Link/Link.js'
54
import styles from './Search.module.css'
65

76
export default function Search({ accessToken } : { accessToken?: string }) {
@@ -38,7 +37,7 @@ export default function Search({ accessToken } : { accessToken?: string }) {
3837
<ul className={styles.refList}>
3938
{datasets.map((dataset) =>
4039
<li key={dataset.name}>
41-
<Link url={`${baseUrl}/${dataset.name}`}>{dataset.name}</Link>
40+
<a href={`/?url=${baseUrl}/${dataset.name}`}>{dataset.name}</a>
4241
</li>,
4342
)}
4443
</ul>

src/lib/huggingfaceSource.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ import { getFileName } from 'hyperparam'
44

55
export const baseUrl = 'https://huggingface.co/datasets'
66

7-
// Hugging Face Space impose some restrictions to the static apps that are hosted on their platform,
8-
// with respect to the URLs. Only hash and query strings can be changed, and doing so requires
9-
// some custom code:
10-
// https://huggingface.co/docs/hub/spaces-handle-url-parameters
11-
export function changeQueryString(queryString: string) {
12-
const url = new URL(window.location.href)
13-
url.search = queryString
14-
window.parent.postMessage({ queryString }, 'https://huggingface.co')
15-
window.location.assign(url)
16-
}
17-
187
function getSourceParts(url: HFUrl): SourcePart[] {
198
const sourceParts: SourcePart[] = [{
209
sourceId: `${baseUrl}/${url.repo}/tree/${url.branch}/`,

0 commit comments

Comments
 (0)