mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
* feat(web): setting to open external links in a new tab Left-clicking an external http(s) link renders it with no explicit target, defaulting to _self. In an installed/standalone PWA this gets intercepted by the browser and shown as a captive in-app overlay instead of leaving the app; in a normal tab it navigates away from HAPI entirely. Right-click already opens in a new tab, but that's not a fair ask for every link. Add an opt-in Settings > Display > Links toggle that forces target="_blank" rel="noopener noreferrer" on genuine external http(s) links only (relative/SPA hrefs and other schemes like mailto are left untouched). Defaults to off, preserving today's behaviour, since the overlay may be intentional/liked by some users. Vibe Coding disclosure: implemented with Claude Sonnet 5 (Claude Code). * fix(web): simplify link-toggle setting description per review The description mentioned the target attribute (an implementation detail, not user-facing) and asserted the in-app overlay is what every installed PWA shows, which was only actually confirmed on Chromium/ Edge. Describe the user-visible behaviour only, without promising a specific rendering across all installed-app environments. * fix(web): stop promising specific behaviour when installed as an app Per follow-up review: describing the setting as "always" opening links in a new tab "even when installed as an app" still guarantees a specific outcome across every browser's PWA handling, which isn't something this change can verify or promise. State only what the setting does (open in a separate tab), not how every installed-app environment will render that. --------- Co-authored-by: Ian Stevenson <is@lockhouse.systems>
100 lines
3.0 KiB
TypeScript
100 lines
3.0 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react'
|
|
|
|
const OPEN_EXTERNAL_LINKS_IN_NEW_TAB_STORAGE_KEY = 'hapi-open-external-links-in-new-tab'
|
|
export const DEFAULT_OPEN_EXTERNAL_LINKS_IN_NEW_TAB = false
|
|
|
|
// Same-tab sync: `storage` events only fire in OTHER tabs/windows, but several
|
|
// <UriConfirmProvider> instances can coexist in one window (e.g. one per
|
|
// chat message), each with its own useState. Without this, toggling the
|
|
// setting in Settings would only affect providers mounted after the change.
|
|
const sameTabListeners = new Set<(value: boolean) => void>()
|
|
|
|
function isBrowser(): boolean {
|
|
return typeof window !== 'undefined' && typeof document !== 'undefined'
|
|
}
|
|
|
|
function safeGetItem(key: string): string | null {
|
|
if (!isBrowser()) {
|
|
return null
|
|
}
|
|
try {
|
|
return localStorage.getItem(key)
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function safeSetItem(key: string, value: string): void {
|
|
if (!isBrowser()) {
|
|
return
|
|
}
|
|
try {
|
|
localStorage.setItem(key, value)
|
|
} catch {
|
|
// Ignore storage errors
|
|
}
|
|
}
|
|
|
|
function safeRemoveItem(key: string): void {
|
|
if (!isBrowser()) {
|
|
return
|
|
}
|
|
try {
|
|
localStorage.removeItem(key)
|
|
} catch {
|
|
// Ignore storage errors
|
|
}
|
|
}
|
|
|
|
function parseOpenExternalLinksInNewTab(raw: string | null): boolean {
|
|
if (raw === 'true') {
|
|
return true
|
|
}
|
|
return DEFAULT_OPEN_EXTERNAL_LINKS_IN_NEW_TAB
|
|
}
|
|
|
|
export function getInitialOpenExternalLinksInNewTab(): boolean {
|
|
return parseOpenExternalLinksInNewTab(safeGetItem(OPEN_EXTERNAL_LINKS_IN_NEW_TAB_STORAGE_KEY))
|
|
}
|
|
|
|
export function useOpenExternalLinksInNewTab(): {
|
|
openExternalLinksInNewTab: boolean
|
|
setOpenExternalLinksInNewTab: (value: boolean) => void
|
|
} {
|
|
const [openExternalLinksInNewTab, setOpenExternalLinksInNewTabState] = useState<boolean>(getInitialOpenExternalLinksInNewTab)
|
|
|
|
useEffect(() => {
|
|
if (!isBrowser()) {
|
|
return
|
|
}
|
|
|
|
const onStorage = (event: StorageEvent) => {
|
|
if (event.key !== OPEN_EXTERNAL_LINKS_IN_NEW_TAB_STORAGE_KEY) {
|
|
return
|
|
}
|
|
setOpenExternalLinksInNewTabState(parseOpenExternalLinksInNewTab(event.newValue))
|
|
}
|
|
|
|
sameTabListeners.add(setOpenExternalLinksInNewTabState)
|
|
window.addEventListener('storage', onStorage)
|
|
return () => {
|
|
sameTabListeners.delete(setOpenExternalLinksInNewTabState)
|
|
window.removeEventListener('storage', onStorage)
|
|
}
|
|
}, [])
|
|
|
|
const setOpenExternalLinksInNewTab = useCallback((value: boolean) => {
|
|
for (const listener of sameTabListeners) {
|
|
listener(value)
|
|
}
|
|
|
|
if (value === DEFAULT_OPEN_EXTERNAL_LINKS_IN_NEW_TAB) {
|
|
safeRemoveItem(OPEN_EXTERNAL_LINKS_IN_NEW_TAB_STORAGE_KEY)
|
|
} else {
|
|
safeSetItem(OPEN_EXTERNAL_LINKS_IN_NEW_TAB_STORAGE_KEY, String(value))
|
|
}
|
|
}, [])
|
|
|
|
return { openExternalLinksInNewTab, setOpenExternalLinksInNewTab }
|
|
}
|