teqo-backup/src/lib/ai/markdownLinks.ts
Francisco Solla aa8fe582c0 feat(B198): drawer mobile do Sollinha fecha ao navegar por link interno
O renderer de links do chat decide interno vs externo (B187/B188); a decisão
pura de fechamento mora no owner src/lib/ai/markdownLinks.ts
(shouldCloseDrawerOnLinkClick: interno && mobile && clique simples) e o
markdownComponents estatico virou useMemo no corpo do CampaignAIChat para
alcancar o contexto. Desktop inalterado; links externos/modos de nova aba
mantem o drawer da aba de origem; sessao e restore intactos (close persiste
open:false).

- unit: 12 testes da decisao pura
- e2e (modo CI, E2E_PROD=1): interno fecha + navega sem load + reload nao
  reabre; externo abre popup e mantem o drawer
- debito B199/#672 (depends #660): race de persistencia ao fechar mid-stream
2026-08-11 14:29:29 -03:00

57 lines
2 KiB
TypeScript

export type ExternalLinkTarget = {
target: '_blank'
rel: 'noopener noreferrer'
}
/**
* App-internal destinations the assistant emits — the B162 link catalog only
* ever produces `/campanha…` paths (B187/B188).
*/
const CAMPAIGN_INTERNAL_LINK = /^\/campanha(?:\/|$)/
export const isCampaignInternalLink = (href: string): boolean => CAMPAIGN_INTERNAL_LINK.test(href)
/**
* Decides whether a markdown href rendered by the Sollinha chat should open in
* a new tab. Only absolute http(s) URLs (and protocol-relative ones) qualify —
* everything the assistant produces today (`/campanha/...` paths) stays as a
* same-tab anchor, and non-web protocols (`mailto:`, `tel:`, `#anchor`) render
* untouched.
*/
export const externalLinkTarget = (href: string): ExternalLinkTarget | null => {
if (/^https?:\/\//i.test(href) || href.startsWith('//')) {
return { target: '_blank', rel: 'noopener noreferrer' }
}
return null
}
/** The click metadata that decides same-tab vs new-tab navigation (B198). */
export type LinkClickMeta = {
button: number
altKey: boolean
ctrlKey: boolean
metaKey: boolean
shiftKey: boolean
defaultPrevented: boolean
}
/**
* B198 — a mobile link click on the mobile drawer closes it only for the
* navigation that replaces the page in the SAME tab: an internal link touched
* with a plain left click. Modified clicks (cmd/ctrl/shift/alt), middle clicks
* and already-prevented clicks open the destination in a new tab (or nowhere)
* — the origin tab keeps the drawer so the conversation is where it was. The
* desktop panel never closes (the accept keeps it open while navigating).
*/
export const shouldCloseDrawerOnLinkClick = (
href: string,
isMobile: boolean,
event: LinkClickMeta,
): boolean => {
if (!isMobile) return false
if (!isCampaignInternalLink(href)) return false
if (event.defaultPrevented) return false
if (event.button !== 0) return false
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return false
return true
}