|
| 1 | +import { useRef } from 'react'; |
| 2 | +import { ProhibitIcon, WarningOctagonIcon, XIcon } from '@phosphor-icons/react'; |
| 3 | +import type { DisplayMatchVerdict, ExternalUriDecision } from '../lib/external-links'; |
| 4 | +import { |
| 5 | + ModalOverlay, |
| 6 | + ModalSurface, |
| 7 | + modalActionButton, |
| 8 | + modalIconButton, |
| 9 | + useModalFocusTrap, |
| 10 | +} from './design'; |
| 11 | + |
| 12 | +export interface ExternalLinkDialogRequest { |
| 13 | + uri: string; |
| 14 | + displayText: string; |
| 15 | + verdict: DisplayMatchVerdict; |
| 16 | + decision: ExternalUriDecision; |
| 17 | +} |
| 18 | + |
| 19 | +// "Open ___" button label suffix. The title is uniformly "Confirm open" and |
| 20 | +// doesn't vary with scheme; the button is the only place the scheme noun |
| 21 | +// appears, so the user sees what they're committing to next to their cursor. |
| 22 | +function pickOpenButtonNoun(scheme: string, uri: string): React.ReactNode { |
| 23 | + switch (scheme) { |
| 24 | + case 'http': |
| 25 | + case 'https': |
| 26 | + return 'URL'; |
| 27 | + case 'file': |
| 28 | + return 'file'; |
| 29 | + case 'mailto': |
| 30 | + return 'email'; |
| 31 | + case 'tel': |
| 32 | + return 'phone app'; |
| 33 | + case 'sms': |
| 34 | + return 'SMS app'; |
| 35 | + default: |
| 36 | + return <code className="font-mono">{schemePrefix(scheme, uri)}</code>; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function schemePrefix(scheme: string, uri: string): string { |
| 41 | + return uri.slice(scheme.length + 1).startsWith('//') ? `${scheme}://` : `${scheme}:`; |
| 42 | +} |
| 43 | + |
| 44 | +export function ExternalLinkDialog({ |
| 45 | + request, |
| 46 | + onCancel, |
| 47 | + onConfirm, |
| 48 | +}: { |
| 49 | + request: ExternalLinkDialogRequest; |
| 50 | + onCancel: () => void; |
| 51 | + onConfirm: () => void; |
| 52 | +}) { |
| 53 | + const dialogRef = useRef<HTMLDivElement>(null); |
| 54 | + const primaryButtonRef = useRef<HTMLButtonElement>(null); |
| 55 | + const secondaryButtonRef = useRef<HTMLButtonElement>(null); |
| 56 | + |
| 57 | + const openableDecision = request.decision.status === 'openable' ? request.decision : null; |
| 58 | + const blockedDecision = request.decision.status === 'blocked' ? request.decision : null; |
| 59 | + const displayUri = request.decision.displayUri || request.uri; |
| 60 | + const verdict = request.verdict; |
| 61 | + const isDeceptive = verdict === 'deceptive'; |
| 62 | + const buttonNoun = openableDecision |
| 63 | + ? pickOpenButtonNoun(openableDecision.scheme, openableDecision.uri) |
| 64 | + : 'URL'; |
| 65 | + |
| 66 | + useModalFocusTrap(dialogRef, { |
| 67 | + // Deceptive case: focus the copy action so a default Enter doesn't dismiss |
| 68 | + // silently. Everywhere else: focus the safe affordance (Cancel/Close). |
| 69 | + initialFocusRef: isDeceptive ? primaryButtonRef : secondaryButtonRef, |
| 70 | + onEscape: onCancel, |
| 71 | + }); |
| 72 | + |
| 73 | + const handleCopy = () => { |
| 74 | + void navigator.clipboard.writeText(request.uri); |
| 75 | + onCancel(); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <ModalOverlay zIndex={9999} backdrop="strong" className="px-4 py-6"> |
| 80 | + <ModalSurface |
| 81 | + ref={dialogRef} |
| 82 | + role="dialog" |
| 83 | + aria-modal="true" |
| 84 | + aria-labelledby="external-link-dialog-title" |
| 85 | + elevation="modal" |
| 86 | + className="w-full max-w-[34rem]" |
| 87 | + > |
| 88 | + <div className="flex items-start gap-3"> |
| 89 | + <h2 |
| 90 | + id="external-link-dialog-title" |
| 91 | + className="min-w-0 flex-1 text-sm leading-5 text-foreground" |
| 92 | + > |
| 93 | + {isDeceptive ? ( |
| 94 | + <DeceptiveTitle displayText={request.displayText} /> |
| 95 | + ) : blockedDecision ? ( |
| 96 | + <BlockedTitle reason={blockedDecision.reason} /> |
| 97 | + ) : ( |
| 98 | + <OpenTitle verdict={verdict} displayText={request.displayText} /> |
| 99 | + )} |
| 100 | + </h2> |
| 101 | + <button |
| 102 | + type="button" |
| 103 | + aria-label="Close" |
| 104 | + className={modalIconButton()} |
| 105 | + onClick={onCancel} |
| 106 | + > |
| 107 | + <XIcon size={13} weight="bold" /> |
| 108 | + </button> |
| 109 | + </div> |
| 110 | + |
| 111 | + {/* Bordered nested box: explicit exception to the bg-only chrome rule |
| 112 | + in DESIGN.md. The URL is the literal artifact the user is being |
| 113 | + asked to scrutinize, and a framed box reads better than a bare |
| 114 | + bg-shift in this high-stakes context. */} |
| 115 | + <div className="mt-3 max-h-40 overflow-auto whitespace-pre-wrap break-all rounded border border-border bg-app-bg px-2.5 py-2 text-sm leading-relaxed text-foreground"> |
| 116 | + {displayUri} |
| 117 | + </div> |
| 118 | + |
| 119 | + <div className="mt-4 flex justify-end gap-2 text-xs"> |
| 120 | + {isDeceptive ? ( |
| 121 | + <> |
| 122 | + <button |
| 123 | + ref={secondaryButtonRef} |
| 124 | + type="button" |
| 125 | + onClick={onCancel} |
| 126 | + className={`${modalActionButton({ tone: 'secondary' })} min-w-[5rem]`} |
| 127 | + > |
| 128 | + Close |
| 129 | + </button> |
| 130 | + <button |
| 131 | + ref={primaryButtonRef} |
| 132 | + type="button" |
| 133 | + onClick={handleCopy} |
| 134 | + className={modalActionButton({ tone: 'primary' })} |
| 135 | + > |
| 136 | + Copy deceptive URL to clipboard |
| 137 | + </button> |
| 138 | + </> |
| 139 | + ) : openableDecision ? ( |
| 140 | + <> |
| 141 | + <button |
| 142 | + ref={secondaryButtonRef} |
| 143 | + type="button" |
| 144 | + onClick={onCancel} |
| 145 | + className={`${modalActionButton({ tone: 'secondary' })} min-w-[5rem]`} |
| 146 | + > |
| 147 | + Cancel |
| 148 | + </button> |
| 149 | + <button |
| 150 | + ref={primaryButtonRef} |
| 151 | + type="button" |
| 152 | + onClick={onConfirm} |
| 153 | + className={`${modalActionButton({ tone: 'primary' })} min-w-[5rem]`} |
| 154 | + > |
| 155 | + {'Open '}{buttonNoun} |
| 156 | + </button> |
| 157 | + </> |
| 158 | + ) : ( |
| 159 | + <button |
| 160 | + ref={secondaryButtonRef} |
| 161 | + type="button" |
| 162 | + onClick={onCancel} |
| 163 | + className={`${modalActionButton({ tone: 'primary' })} min-w-[6rem]`} |
| 164 | + > |
| 165 | + Close |
| 166 | + </button> |
| 167 | + )} |
| 168 | + </div> |
| 169 | + </ModalSurface> |
| 170 | + </ModalOverlay> |
| 171 | + ); |
| 172 | +} |
| 173 | + |
| 174 | +function OpenTitle({ |
| 175 | + verdict, |
| 176 | + displayText, |
| 177 | +}: { |
| 178 | + verdict: DisplayMatchVerdict; |
| 179 | + displayText: string; |
| 180 | +}) { |
| 181 | + if (verdict === 'plain' && displayText.trim()) { |
| 182 | + return ( |
| 183 | + <> |
| 184 | + Confirm open: <span className="font-semibold">{displayText.trim()}</span> |
| 185 | + </> |
| 186 | + ); |
| 187 | + } |
| 188 | + return <>Confirm open</>; |
| 189 | +} |
| 190 | + |
| 191 | +function DeceptiveTitle({ displayText }: { displayText: string }) { |
| 192 | + return ( |
| 193 | + <span className="flex items-start gap-1.5"> |
| 194 | + <WarningOctagonIcon |
| 195 | + size={14} |
| 196 | + weight="fill" |
| 197 | + className="mt-px shrink-0 text-error" |
| 198 | + aria-hidden |
| 199 | + /> |
| 200 | + <span className="leading-snug"> |
| 201 | + Deceptive link text was{' '} |
| 202 | + <span className="font-semibold">{displayText.trim()}</span>, URL was: |
| 203 | + </span> |
| 204 | + </span> |
| 205 | + ); |
| 206 | +} |
| 207 | + |
| 208 | +function BlockedTitle({ reason }: { reason: string }) { |
| 209 | + return ( |
| 210 | + <span className="flex items-start gap-1.5"> |
| 211 | + <ProhibitIcon size={14} weight="bold" className="mt-px shrink-0 text-error" aria-hidden /> |
| 212 | + <span className="leading-snug"> |
| 213 | + Blocked.{' '} |
| 214 | + <span className="text-muted">{reason}</span> |
| 215 | + </span> |
| 216 | + </span> |
| 217 | + ); |
| 218 | +} |
0 commit comments