Files
remanso/src/hooks/useATProtoLinks.hook.ts
Julien Calixte 836b480ea6 fix(navigation): resolve clicked anchor when target is a nested element
A click on a child of an <a> (e.g. nested <strong>, <em>, <code>, icon)
made event.target a non-anchor, so getAttribute('href') returned null
and the handler bailed without preventDefault. The browser then
performed the native navigation, which for relative links like
'../note.md' resolved against the current /:user/:repo URL and the SPA
re-routed treating the destination as a new repo.
2026-04-26 13:58:48 +02:00

117 lines
2.7 KiB
TypeScript

import { ComputedRef, onUnmounted, Ref, toValue } from "vue"
import { useRouteQueryStackedNotes } from "@/hooks/useRouteQueryStackedNotes.hook"
import { parseAtUri } from "@/modules/atproto/parseAtUri"
import { toShortDid } from "@/modules/atproto/shortDid"
import { router } from "@/router/router"
import { isExternalLink } from "@/utils/link"
export const useATProtoLinks = (
className: ComputedRef<string> | string,
options: {
currentAtUri?: Ref<string> | string | ComputedRef<string>
mainNoteId: Ref<string> | string | ComputedRef<string>
}
) => {
const { addStackedNote, scrollToFocusedNote } = useRouteQueryStackedNotes()
const { currentAtUri, mainNoteId } = options
const linkNote = (event: Event) => {
const anchor = (event.target as HTMLElement).closest("a")
const href = anchor?.getAttribute("href")
if (!href) {
return
}
if (href.startsWith("#")) {
return
}
event.preventDefault()
event.stopPropagation()
if (isExternalLink(href)) {
window.open(href, "_blank")
return
}
if (href.startsWith(window.location.origin)) {
const { params } = router.resolve(
href.replace(window.location.origin, "")
)
if (!params.shortDid || !params.rkey) {
return
}
const noteId = params.slug
? `${params.shortDid}-${params.rkey}-${params.slug}`
: `${params.shortDid}-${params.rkey}`
if (noteId === toValue(mainNoteId)) {
scrollToFocusedNote()
return
}
addStackedNote(
toValue(currentAtUri) ?? "",
noteId,
`${params.shortDid}-${params.rkey}`
)
return
}
if (href.startsWith("at://")) {
const { did, rkey } = parseAtUri(href)
const noteId = `${toShortDid(did)}-${rkey}`
if (noteId === toValue(mainNoteId)) {
scrollToFocusedNote()
return
}
addStackedNote(toValue(currentAtUri) ?? "", noteId)
}
}
const LINK_SELECTOR = `.${toValue(className)} a`
const removeListeners = () => {
const elements = document.querySelectorAll(LINK_SELECTOR)
elements.forEach((element) => {
element.removeEventListener("click", linkNote)
})
}
const listenToClick = () => {
removeListeners()
const elements = document.querySelectorAll(LINK_SELECTOR)
elements.forEach((element) => {
const href = element.getAttribute("href")
if (!href) {
return
}
if (isExternalLink(href)) {
element.classList.add("external-link")
}
})
elements.forEach((element) => {
element.addEventListener("click", linkNote)
})
}
onUnmounted(() => {
removeListeners()
})
return {
listenToClick
}
}