|
3 | 3 | the iFrameRef is not really compatiple with ts,
|
4 | 4 | */
|
5 | 5 |
|
6 |
| -import React, { useEffect, useRef, useState } from 'react' |
| 6 | +import React, { useEffect, useRef, useMemo, useState } from 'react' |
7 | 7 | import { useLocation, useParams, useSearchParams } from 'react-router-dom'
|
8 | 8 | import DocumentControlButtons from '../components/DocumentControlButtons'
|
9 | 9 | import ProjectDetails from '../models/ProjectDetails'
|
@@ -36,22 +36,34 @@ export default function Docs (): JSX.Element {
|
36 | 36 | const location = useLocation()
|
37 | 37 | const iFrameRef = useRef<HTMLIFrameElement>(null)
|
38 | 38 |
|
| 39 | + const iFrame = useMemo(() => { |
| 40 | + return (<iframe |
| 41 | + ref={iFrameRef} |
| 42 | + key={uniqueId()} |
| 43 | + src={ProjectRepository.getProjectDocsURL(project, version, page, hash)} |
| 44 | + title="docs" |
| 45 | + className={styles['docs-iframe']} |
| 46 | + onLoad={() => { |
| 47 | + if (iFrameRef.current == null) { |
| 48 | + console.error('iFrameRef is null') |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + // @ts-expect-error ts can't find contentWindow |
| 53 | + onIFrameLocationChanged(iFrameRef.current.contentWindow.location.href) |
| 54 | + }} |
| 55 | + /> |
| 56 | + ) |
| 57 | + }, [project, version]) |
| 58 | + |
39 | 59 | document.title = `${project} | docat`
|
40 | 60 |
|
41 | 61 | if (projectParam === '') {
|
42 | 62 | setLoadingFailed(true)
|
43 | 63 | }
|
44 | 64 |
|
45 | 65 | const updateURL = (newProject: string, newVersion: string, newPage: string, newHash: string, newHideUi: boolean): void => {
|
46 |
| - let url = `#/${newProject}/${newVersion}/${newPage}` |
47 |
| - |
48 |
| - if (newHash.length > 0) { |
49 |
| - url += newHash |
50 |
| - } |
51 |
| - |
52 |
| - if (newHideUi) { |
53 |
| - url += '?hide-ui=true' |
54 |
| - } |
| 66 | + const url = `#/${newProject}/${newVersion}/${newPage}${newHash}${newHideUi ? '?hide-ui=true' : ''}` |
55 | 67 |
|
56 | 68 | if (project === newProject && version === newVersion && page === newPage && hash === newHash && hideUi === newHideUi) {
|
57 | 69 | // no change
|
@@ -217,18 +229,7 @@ export default function Docs (): JSX.Element {
|
217 | 229 |
|
218 | 230 | return (
|
219 | 231 | <>
|
220 |
| - <iframe |
221 |
| - ref={iFrameRef} |
222 |
| - key={uniqueId()} |
223 |
| - src={ProjectRepository.getProjectDocsURL(project, version, page, hash)} |
224 |
| - title="docs" |
225 |
| - className={styles['docs-iframe']} |
226 |
| - onLoad={() => { |
227 |
| - // @ts-expect-error ts can't find contentWindow |
228 |
| - onIFrameLocationChanged(iFrameRef.current?.contentWindow.location.href) |
229 |
| - }} |
230 |
| - /> |
231 |
| - |
| 232 | + {iFrame} |
232 | 233 | {!hideUi && (
|
233 | 234 | <DocumentControlButtons
|
234 | 235 | version={version}
|
|
0 commit comments