mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
fix: handle redirect to GitHub pull request file
This commit is contained in:
parent
420e1e288e
commit
6ac043a13a
3 changed files with 42 additions and 1 deletions
37
src/components/FileExplorer/hooks/useHandleClickFileLink.ts
Normal file
37
src/components/FileExplorer/hooks/useHandleClickFileLink.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { useCallback, useRef } from 'react'
|
||||
import { useEvent } from 'react-use'
|
||||
import { useAfterRedirect } from 'utils/hooks/useFastRedirect'
|
||||
|
||||
export function useHandleClickFileLink(ref: React.MutableRefObject<HTMLElement | null>) {
|
||||
const toGoRef = useRef<string | null>(null)
|
||||
const onClickCaptureSaveHrefWithHash = useCallback(
|
||||
(e: Event) => {
|
||||
const target = e.target
|
||||
if (target instanceof HTMLElement && ref.current?.contains(target)) {
|
||||
let e: HTMLElement | null = target
|
||||
while (e && e !== ref.current) {
|
||||
if (e instanceof HTMLAnchorElement) {
|
||||
const hash = e.href.split('#')[1]
|
||||
if (hash) {
|
||||
toGoRef.current = e.href
|
||||
}
|
||||
}
|
||||
e = e.parentElement
|
||||
}
|
||||
}
|
||||
},
|
||||
[ref],
|
||||
)
|
||||
useEvent('click', onClickCaptureSaveHrefWithHash, document, true)
|
||||
|
||||
const redirectToSavedHrefWithHash = useCallback(() => {
|
||||
const toGo = toGoRef.current
|
||||
if (toGo) {
|
||||
toGoRef.current = null
|
||||
if (toGo.startsWith(location.href)) {
|
||||
window.location.replace(toGo)
|
||||
}
|
||||
}
|
||||
}, [])
|
||||
useAfterRedirect(redirectToSavedHrefWithHash)
|
||||
}
|
||||
|
|
@ -41,7 +41,8 @@ export function useHandleNodeClick(
|
|||
|
||||
focusNode(node)
|
||||
if (node.url) {
|
||||
const isHashLink = node.url.includes('#')
|
||||
const isHashLink =
|
||||
node.url.includes('#') && node.url.split('#')[0] === location.pathname
|
||||
if (!isHashLink) {
|
||||
event.preventDefault()
|
||||
loadWithFastRedirect(node.url, event.currentTarget)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import { useOnLocationChange } from 'utils/hooks/useOnLocationChange'
|
|||
import { VisibleNodes, VisibleNodesGenerator } from 'utils/VisibleNodesGenerator'
|
||||
import { SideBarStateContext } from '../../containers/SideBarState'
|
||||
import { useGetCurrentPath } from './hooks/useGetCurrentPath'
|
||||
import { useHandleClickFileLink } from './hooks/useHandleClickFileLink'
|
||||
import { useHandleKeyDown } from './hooks/useHandleKeyDown'
|
||||
import {
|
||||
NodeRenderer,
|
||||
|
|
@ -199,6 +200,8 @@ function LoadedFileExplorer({
|
|||
useCallback(() => ref.current?.focus(), []),
|
||||
)
|
||||
|
||||
useHandleClickFileLink(ref)
|
||||
|
||||
return (
|
||||
<div ref={ref} className={`file-explorer`} tabIndex={-1} onKeyDown={handleKeyDown}>
|
||||
{visibleNodesGenerator?.defer && (
|
||||
|
|
|
|||
Loading…
Reference in a new issue