Skip to content

Commit

Permalink
Fix: Docs blinking when navigating between hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
reglim committed Aug 29, 2023
1 parent 452246a commit 4bdddbd
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions web/src/pages/Docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
the iFrameRef is not really compatiple with ts,
*/

import React, { useEffect, useRef, useState } from 'react'
import React, { useEffect, useRef, useMemo, useState } from 'react'
import { useLocation, useParams, useSearchParams } from 'react-router-dom'
import DocumentControlButtons from '../components/DocumentControlButtons'
import ProjectDetails from '../models/ProjectDetails'
Expand Down Expand Up @@ -36,22 +36,29 @@ export default function Docs (): JSX.Element {
const location = useLocation()
const iFrameRef = useRef<HTMLIFrameElement>(null)

const iFrame = useMemo(() => {
return (<iframe
ref={iFrameRef}
key={uniqueId()}
src={ProjectRepository.getProjectDocsURL(project, version, page, hash)}
title="docs"
className={styles['docs-iframe']}
onLoad={() => {
// @ts-expect-error ts can't find contentWindow
onIFrameLocationChanged(iFrameRef.current?.contentWindow.location.href)
}}
/>
)
}, [project, version, page])

document.title = `${project} | docat`

if (projectParam === '') {
setLoadingFailed(true)
}

const updateURL = (newProject: string, newVersion: string, newPage: string, newHash: string, newHideUi: boolean): void => {
let url = `#/${newProject}/${newVersion}/${newPage}`

if (newHash.length > 0) {
url += newHash
}

if (newHideUi) {
url += '?hide-ui=true'
}
const url = `#/${newProject}/${newVersion}/${newPage}${newHash}${newHideUi ? '?hide-ui=true' : ''}`

if (project === newProject && version === newVersion && page === newPage && hash === newHash && hideUi === newHideUi) {
// no change
Expand Down Expand Up @@ -217,18 +224,7 @@ export default function Docs (): JSX.Element {

return (
<>
<iframe
ref={iFrameRef}
key={uniqueId()}
src={ProjectRepository.getProjectDocsURL(project, version, page, hash)}
title="docs"
className={styles['docs-iframe']}
onLoad={() => {
// @ts-expect-error ts can't find contentWindow
onIFrameLocationChanged(iFrameRef.current?.contentWindow.location.href)
}}
/>

{iFrame}
{!hideUi && (
<DocumentControlButtons
version={version}
Expand Down

0 comments on commit 4bdddbd

Please sign in to comment.