import React, { MouseEvent, ReactNode, useState } from 'react'; import FocusTrap from 'focus-trap-react'; import { Box, Button, Dialog, Header, Icon, IconButton, Icons, Overlay, OverlayBackdrop, OverlayCenter, Text, color, config, } from 'folds'; import { stopPropagation } from '../../utils/keyboard'; import { LinkSafety } from '../../utils/linkSafety'; type SuspiciousLinkProps = { href: string; safety: LinkSafety; anchorProps: Record; children: ReactNode; }; /** * [Gitea #122] A link whose visible text names a different site than its * destination (or whose destination is a punycode host) opens a small * confirm instead of navigating straight away. Honest links never see this. */ export function SuspiciousLink({ href, safety, anchorProps, children }: SuspiciousLinkProps) { const [open, setOpen] = useState(false); const handleClick = (evt: MouseEvent) => { evt.preventDefault(); evt.stopPropagation(); setOpen(true); }; const proceed = () => { setOpen(false); window.open(href, '_blank', 'noopener,noreferrer'); }; return ( <> {children} {open && ( }> setOpen(false), clickOutsideDeactivates: true, escapeDeactivates: stopPropagation, }} >
This link doesn't go where it says setOpen(false)} radii="300" aria-label="Close" >
{safety.shownHost ? ( <> It shows {safety.shownHost} but goes to{' '} {safety.realHost}. ) : ( <> It goes to {safety.realHost}. )} {safety.punycode && ' The destination uses look-alike (internationalised) characters in its name.'} {href}
)} ); }